Javascript if语句仅返回true

时间:2018-03-29 16:57:02

标签: javascript function if-statement

我正在写一个石头剪刀游戏。

播放器的console.log显示所选值。

计算机的console.log显示所选值。

检查值是否匹配的if语句始终返回true,即使控制台日志显示它们不同。

我尝试过几种不同的方法,但总是遇到这个问题。

// Player Choice Selection 
var player = function() {
  const playerSelects = "rock";
  return playerSelects;
};
console.log(player());

// Computer Choice Selection
var computer = function() {
  const computerSelects = ['rock', 'paper', 'scissors'];
  let randomSelection = Math.floor(Math.random() * 3);
  return computerSelects[randomSelection];
};
console.log(computer());

// Game 
var game = function(player, computer) {
  if (player == computer) {
    const draw = "its a draw";
    return draw;
  }
};
console.log(game());

谁能告诉我这里哪里出错了?

由于

3 个答案:

答案 0 :(得分:1)

游戏是一个有两个参数的函数:

 var game = function(player, computer){
    if(player == computer){

因此,如果您将其称为

 game()

... playercomputer都是undefined,因为您没有传入任何值。可能会这样做:

 game(computer(), player())

...但是你真的不应该影响这些变量名......

答案 1 :(得分:1)

您没有向game()功能提供任何内容。

我认为这就是你想要的。

如上所述,player == computer由于命名也不直观。人们不会指望玩家永远等于电脑。像playerValue == computerValue这样的东西会更好。

// Player Choice Selection 
var player = function() {
  const playerSelects = "rock";
  return playerSelects;
};

// Computer Choice Selection
var computer = function() {
  const computerSelects = ['rock', 'paper', 'scissors'];
  let randomSelection = Math.floor(Math.random() * 3);
  return computerSelects[randomSelection];
};

// Game 
var game = function(playerValue, computerValue) {
  if (playerValue === computerValue) {
    return "its a draw";
  } else {
    return "It's not a draw";
  }
};

var playerValue = player();
var computerValue = computer();

console.log(playerValue);
console.log(computerValue);
console.log(game(playerValue, computerValue));

如果您不需要函数表达式,最好使用常规函数,如下所示:

// Player Choice Selection 
function getPlayerChoice() {
  const playerSelects = "rock";
  return playerSelects;
};

// Computer Choice Selection
function getComputerChoice() {
  const computerSelects = ['rock', 'paper', 'scissors'];
  let randomSelection = Math.floor(Math.random() * 3);
  return computerSelects[randomSelection];
};

// Game 
function playGame(playerChoice, computerChoice) {
  if (playerChoice === computerChoice) {
    return "its a draw";
  } else {
    return "It's not a draw";
  }
};

var playerChoice = getPlayerChoice();
var computerChoice = getComputerChoice();

console.log(playerChoice);
console.log(computerChoice);
console.log(playGame(playerChoice, computerChoice));

答案 2 :(得分:0)

你有一些错误。其他一些人已经部分修复,请允许我解释一下:

// Player Choice Selection 
var player = function() {
  const playerSelects = "rock";
  return playerSelects;
};
// Don't log the result here, because it will change in the game instance.

// Computer Choice Selection
var computer = function() {
  const computerSelects = ['rock', 'paper', 'scissors'];
  let randomSelection = Math.floor(Math.random() * 3);
  return computerSelects[randomSelection];
};
// Don't log the result here, because it will change in the game instance.

// Game
// Don't pass the player and computer functions in, because they're in global scope already.
var game = function() {
  // Call an instance of the player, and computer functions.
  // We do this so that each game we play, has new results.
  var playerResult = player();
  var computerResult = computer();
  // Log each result
  console.log(playerResult, computerResult)

  if (playerResult == computerResult) {
    const draw = "its a draw";
    return draw;
  }
};
console.log(game());
相关问题