【问题标题】:How to use function expression values in other functions?如何在其他函数中使用函数表达式值?
【发布时间】:2020-05-30 21:50:06
【问题描述】:

我正在学习 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();

请帮帮我。

【问题讨论】:

  • 所以澄清一下,computerChoice = function() 与拥有类似函数 getComputerChoice() 的函数然后分配到类似 let computerChoice = getComputerChoice() 的变量不同?为什么? – Andres Pelaez 15 分钟前 删除

标签: javascript function function-expression


【解决方案1】:

你将函数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();

【讨论】:

    【解决方案2】:

    您应该将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();

    【讨论】:

    • 所以澄清一下,computerChoice = function() 与拥有类似函数 getComputerChoice() 的函数然后分配到类似 let computerChoice = getComputerChoice() 的变量不同?为什么?
    • computerChoice = function() {...} 是我们声明函数的部分。与拥有getComputerChoice之类的函数并声明变量let computerChoiceVariable = getComputerChoice()是不一样的
    【解决方案3】:

    试试这个

    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();

    【讨论】:

      【解决方案4】:

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

      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();

      【讨论】:

        猜你喜欢
        • 2023-01-09
        • 2019-10-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多