简体   繁体   English

我是一个新学习者,无法弄清楚为什么我的代码不起作用

[英]im a new learner and cant figure out why my code is not working

I have just recently got into coding and have chosen to learn JavaScript as my first language.我最近刚开始编码,并选择学习 JavaScript 作为我的第一语言。 I have written up code for a Rock, Paper, Scissors game but the wrong outputs come out when I run it?我已经为 Rock, Paper, Scissors 游戏编写了代码,但是当我运行它时会出现错误的输出? for example I would put my answer as scissors and the computer would choose rock and the game will come out as a tie.例如,我会将我的答案设置为剪刀,而计算机会选择摇滚,游戏会以平局的形式出现。

 const getUserChoice = userInput => { if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') { return userInput } else { return 'Error!' } } var getComputerChoice = () => { const randomNumber = Math.floor(Math.random() * 3); switch (randomNumber) { case 0: return 'rock' break; case 1: return 'paper' break; case 2: return 'scissors' break; } }; const determineWinner = (userChoice, computerChoice) => { if (userChoice === computerChoice) { return 'its a tie!'; }; if (userChoice === 'rock') { if (computerChoice === 'paper') { return 'computer won'; } else { return 'user won'; } } if (userChoice === 'paper') { if (computerChoice === 'scissors') { return 'computer won'; } else { return 'user won' } } if (userChoice === 'scissors') { if (computerChoice === 'rock') { return 'computer won'; } else { return 'user won' } } }; const playGame = () => { console.log(`player chose ${getUserChoice('scissors')}`); console.log(`computer chose ${getComputerChoice()}`); console.log(determineWinner(getUserChoice("scissors"), getComputerChoice())); } playGame();<\/code><\/pre>

"

Maybe there are more issues, but here are some of them:也许还有更多问题,但这里有一些:

Each time you execute getComputerChoice you get a different value because a random value is picked inside:每次执行 getComputerChoice 时都会得到一个不同的值,因为在其中选取了一个随机值:

  console.log(`player chose ${getUserChoice('scissors')}`);
  console.log(`computer chose ${getComputerChoice()}`);
  console.log(determineWinner(getUserChoice("scissors"), getComputerChoice()));

Every time you call getComputerChoice<\/code> you'll get a different value, you could save<\/strong> the values in a variable<\/strong> with the const<\/em> keyword.每次调用getComputerChoice<\/code>都会得到不同的值,您可以使用const<\/em>关键字将值保存<\/strong>在变量<\/strong>中。

const playGame = () => {
  // It would be nice if you request this to the user, with prompt
  const userChoice = getUserChoice('scissors');

  // Save the random value
  const computerChoice = getComputerChoice();

  console.log(`player chose ${userChoice}`);
  console.log(`computer chose ${computerChoice}`);

  // This will work
  console.log(determineWinner(userChoice, computerChoice));
}

playGame();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我试图为我的训练营做一个测验,但我不知道代码有什么问题 - im trying to make a quiz for my bootcamp and i cant figure out what is wrong with the code 我不知道为什么我的导入/导出不起作用 - i cant figure out why my import/export is not working 无法弄清楚为什么我的div输出不起作用 - Cant figure out why my div output isn't working 我的捕获错误,我似乎无法弄清楚 - Im getting an error with my catch that i cant seem to figure out 我的代码不起作用,我不知道为什么(@sertynould) - my code is not working, im not know why(@sertynould) 我的代码有一个简单的问题,我无法弄清楚 - My code has a simple issue that i cant figure out 无法弄清楚jquery .show()和.hide(),即时消息很混乱 - cant figure out jquery .show() and .hide(), im a noob very confused 我的用于隐藏/显示div的jQuery页面导航代码不起作用,我无法弄清楚为什么 - My jQuery page-navigation code to hide/show divs doesnt work, and i cant figure out why exactly 我有编程经验,但是对Javascript还是陌生的,无法弄清楚为什么我的代码无法运行 - I'm experienced with programming but new to Javascript, can't figure out why my code won't run 编码新手,我不明白为什么我的 javascript 按钮无法正常工作? - New to coding,I can't figure out why my javascript buttons are not working properly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM