如何在其他函数中使用函数表达式值?

问题描述 投票:1回答:4

我正在学习Javascript,并制作了石头,剪刀,纸项目。我已经使它可以与示例代码一起使用,但是现在我想使用函数表达式在其他函数中使用其值。当我加载代码时,它给了我未定义的信息,我也不知道为什么。我分配所有值。这是我的代码。

const userChoice = userInput => {
  //userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'scissors' || userInput === 'paper' || userInput === 'bomb') {
    return userInput;
  } else {
    console.log('Error!');
  }
}

//console.log(getUserChoice('Fork'));

const computerChoice = function() {
  const randomNumber = Math.floor(Math.random() * 3);
  switch (randomNumber) {
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 2:
      return 'scissors'
  }
};
//console.log(getComputerChoice());

function determineWinner(userChoice, computerChoice) {
  console.log()
  if (userChoice === computerChoice) {
    return 'The game is a tie!'
  }
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'The computer won!';
    } else {
      return 'You won!'
    }
  }
  if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'The computer won!';
    } else {
      return 'You won!'
    }
  }
  if (userChoice === 'paper') {
    if (computerChoice === 'rock') {
      return 'The computer won!';
    } else {
      return 'You won!'
    }
  }
  if (userChoice === 'bomb') {
    return 'You won!';

  }
}

const playGame = () => {

  console.log('You threw: ' + userChoice('rock'));
  console.log('The computer threw: ' + computerChoice());
  console.log(determineWinner(userChoice, computerChoice));
}
playGame();

请帮助我。

javascript function function-expression
4个回答
0
投票

[您要将函数userChoice作为参数传递,应该存储调用函数userChoice的结果并将结果作为参数传递给函数determineWinner

const userChoice = userInput => {
  //userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'scissors' || userInput === 'paper' || userInput === 'bomb') {
    return userInput;
  } else {
    console.log('Error!');
  }
}

//console.log(getUserChoice('Fork'));

const computerChoice = function() {
  const randomNumber = Math.floor(Math.random() * 3);
  switch (randomNumber) {
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 2:
      return 'scissors'
  }
};
//console.log(getComputerChoice());

function determineWinner(userChoice, computerChoice) {
  if (userChoice === computerChoice) {
    return 'The game is a tie!'
  }
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'The computer won!';
    } else {
      return 'You won!'
    }
  }
  if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'The computer won!';
    } else {
      return 'You won!'
    }
  }
  if (userChoice === 'paper') {
    if (computerChoice === 'rock') {
      return 'The computer won!';
    } else {
      return 'You won!'
    }
  }
  if (userChoice === 'bomb') {
    return 'You won!';

  }
}

const playGame = () => {
  
  let uc = userChoice('rock');
  
  console.log('You threw: ' + uc);
  console.log('The computer threw: ' + computerChoice());
  console.log(determineWinner(uc, computerChoice));
}
playGame();

0
投票

您应将userChoicecomputerChoice的输出分配给变量,然后再将其传递给determineWinner

const userChoice = userInput => {
  //userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'scissors' || userInput === 'paper' || userInput === 'bomb') {
    return userInput;
  } else {
    console.log('Error!');
  }
}

//console.log(getUserChoice('Fork'));

const computerChoice = function() {
  const randomNumber = Math.floor(Math.random() * 3);
  switch (randomNumber) {
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 2:
      return 'scissors'
  }
};
//console.log(getComputerChoice());

function determineWinner(userChoice, computerChoice) {
  console.log()
  if (userChoice === computerChoice) {
    return 'The game is a tie!'
  }
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'The computer won!';
    } else {
      return 'You won!'
    }
  }
  if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'The computer won!';
    } else {
      return 'You won!'
    }
  }
  if (userChoice === 'paper') {
    if (computerChoice === 'rock') {
      return 'The computer won!';
    } else {
      return 'You won!'
    }
  }
  if (userChoice === 'bomb') {
    return 'You won!';

  }
}

const playGame = () => {
  const userChose = userChoice('rock')
  const computerChose = computerChoice()
  console.log('You threw: ' + userChose);
  console.log('The computer threw: ' + computerChose);
  console.log(determineWinner(userChose, computerChose));
}
playGame();

0
投票

尝试一下

const userChoice = userInput => {
  //userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'scissors' || userInput === 'paper' || userInput === 'bomb') {
    return userInput;
  } else {
    console.log('Error!');
  }
}

//console.log(getUserChoice('Fork'));

const computerChoice = function() {
  const randomNumber = Math.floor(Math.random() * 3);
  switch (randomNumber) {
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 2:
      return 'scissors'
  }
};
//console.log(getComputerChoice());

function determineWinner(userChoice, computerChoice) {
  console.log()
  if (userChoice === computerChoice) {
    return 'The game is a tie!'
  }
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'The computer won!';
    } else {
      return 'You won!'
    }
  }
  if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'The computer won!';
    } else {
      return 'You won!'
    }
  }
  if (userChoice === 'paper') {
    if (computerChoice === 'rock') {
      return 'The computer won!';
    } else {
      return 'You won!'
    }
  }
  if (userChoice === 'bomb') {
    return 'You won!';

  }
}

const playGame = () => {

  console.log('You threw: ' + userChoice('rock'));
  console.log('The computer threw: ' + computerChoice());
  console.log(determineWinner(userChoice('rock'), computerChoice()));
}
playGame();

0
投票

只是一些错误。您需要将值存储到变量中。

const userChoice = userInput => {
  //userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'scissors' || userInput === 'paper' || userInput === 'bomb') {
    return userInput;
  } else {
    console.log('Error!');
  }
}

//console.log(getUserChoice('Fork'));

const computerChoice = function() {
  const randomNumber = Math.floor(Math.random() * 3);
  switch (randomNumber) {
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 2:
      return 'scissors'
  }
};
//console.log(getComputerChoice());

function determineWinner(userChoice, computerChoice) {
  console.log()
  if (userChoice === computerChoice) {
    return 'The game is a tie!'
  }
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'The computer won!';
    } else {
      return 'You won!'
    }
  }
  if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'The computer won!';
    } else {
      return 'You won!'
    }
  }
  if (userChoice === 'paper') {
    if (computerChoice === 'rock') {
      return 'The computer won!';
    } else {
      return 'You won!'
    }
  }
  if (userChoice === 'bomb') {
    return 'You won!';

  }
}

const playGame = () => {
  const threw = {
      computer: null,
      user: null
  }
  threw.user = userChoice("rock")
  threw.computer = computerChoice()
  
  console.log('You threw: ' + threw.user);
  console.log('The computer threw: ' + threw.computer);
  console.log(determineWinner(userChoice(threw.user), threw.computer));
}
playGame();
© www.soinside.com 2019 - 2024. All rights reserved.