石头剪刀布 JavaScript 代码不起作用。我把它作为我的第一个项目,我完全不知道为什么它不起作用

rock paper scissors javascript code doesnt work. I made this as my first project and i dont know at all why it doesnt work

提问人:eret 007 提问时间:10/14/2023 最后编辑:Scott Marcuseret 007 更新时间:10/14/2023 访问量:67

问:

function playRound(playerSelection, computerSelection) {
   
    if (playerSelection == computerSelection) {
        console.log("its a tie")

       } else if (playerSelection == "rock" && computerSelection == "paper") {
        return "you lose" 

     } else if (playerSelection == "rock" && computerSelection == "scissors") {
        
        return "you win"  

     }  else if (playerSelection == "paper" && computerSelection == "scissors") {
        return "you lose" 

     } else if (playerSelection == "paper" && computerSelection == "rock") {
        return "you win" 

     } else if (playerSelection == "scissors" && computerSelection == "rock") {
        return "you lose" 

     } else if (playerSelection == "scissors" && computerSelection == "paper") {
        return "you win" 
     }    
}
   
let playerSelection = parseInt(prompt("input rock paper or scissors"))
let computerSelection = Math.random();

if (computerSelection < 0.34){
  computerSelection = "rock"
} else if(computerSelection <=0.67){
  computerSelection = "paper"
} else {
  computerSelection = "scissors"
}

console.log(playRound(playerSelection, computerSelection))



  

我知道它可能不是最好的方法,但我认为它应该完成工作,任何帮助都是有用的,因为我不知道错误可能在哪里,谢谢。我查看了其他一些帖子,但找不到解决方案。这是我的第一篇文章,所以如果缺少一些信息或某些东西,请告诉我!

javascript 函数 if-statement undefined

评论

1赞 Scott Marcus 10/14/2023
parseInt用于从字符串中提取数值数据。由于您的输入将是单词,因此您没有使用它。
0赞 FreeSoftwareServers 10/14/2023
在处理这样的事情时,在使用“输入”变量之前尝试对变量进行硬编码。
2赞 jarmod 10/14/2023
您可以将所有“您丢失”的测试折叠到一个 .如果你没有打平或赢,你就输了。else

答:

1赞 Md Minhaz Ahamed 10/14/2023 #1

有几个问题,

  1. 当它被绑住时不回来。
  2. 将提示转换为 int。

请尝试以下操作

function playRound(playerSelection, computerSelection) {
  if (playerSelection === computerSelection) {
    return 'its a tie';
  } else if (playerSelection === 'rock' && computerSelection === 'paper') {
    return 'you lose';
  } else if (playerSelection === 'rock' && computerSelection === 'scissors') {
    return 'you win';
  } else if (playerSelection === 'paper' && computerSelection === 'scissors') {
    return 'you lose';
  } else if (playerSelection === 'paper' && computerSelection === 'rock') {
    return 'you win';
  } else if (playerSelection === 'scissors' && computerSelection === 'rock') {
    return 'you lose';
  } else if (playerSelection === 'scissors' && computerSelection === 'paper') {
    return 'you win';
  }
}

let playerSelection = prompt(`Type 'rock' or 'paper' or 'scissors'`)

let computerSelection = Math.random();
if (computerSelection < 0.34) {
  computerSelection = 'rock';
} else if (computerSelection <= 0.67) { 
  computerSelection = 'paper';
} else {
  computerSelection = 'scissors';
}

console.log(playRound(playerSelection, computerSelection));

评论

0赞 Roko C. Buljan 10/14/2023
附言...3.339* != 0.3333333333333333333333333333333333333333333333333333333333333333333333333
0赞 KooiInc 10/14/2023
仅供参考:中的每个条件都返回一个值:您可以删除所有条件。另请参阅playRoundelse
2赞 Scott Marcus 10/14/2023 #2

不应使用,因为用户将输入字符串数据,而不是数字。parseInt

你还需要稍微移动你的一些陈述,但除此之外,你就可以开始了!console.log()

// No parseInt when the input is strings that need to be strings
let playerSelection = prompt("input rock paper or scissors");
let computerSelection = Math.random();

if (computerSelection < 0.34) {
  computerSelection = "rock"
} else if(computerSelection <=0.67) {
  computerSelection = "paper"
} else {
  computerSelection = "scissors"
}

playRound(playerSelection, computerSelection)

function playRound(playerSelection, computerSelection) {
    // Instead of returning in each branch, just set the value of a string that you can
    // report at the end of the function.
    let result = "";
   
    if (playerSelection == computerSelection) {
       result = "its a tie"
     } else if (playerSelection == "rock" && computerSelection == "paper") {
        result = "you lose" 
     } else if (playerSelection == "rock" && computerSelection == "scissors") {
        result =  "you win"  
     }  else if (playerSelection == "paper" && computerSelection == "scissors") {
        result =  "you lose" 
     } else if (playerSelection == "paper" && computerSelection == "rock") {
        result =  "you win" 
     } else if (playerSelection == "scissors" && computerSelection == "rock") {
        result =  "you lose" 
     } else if (playerSelection == "scissors" && computerSelection == "paper") {
        reresult = "you win" 
     }
     
     console.log("Compupter picked: " + computerSelection, "You picked " + playerSelection, result);
}

0赞 KooiInc 10/14/2023 #3

通过使用具有获胜组合的小数组,将用户输入与计算机输入相结合并使用三元运算符来确定结果,而不是使用所有 's 和 ',这可能是一个想法。ifelse

这是这个想法的一个最小的可重复的例子

它用于此 stackblitz 项目。

document.addEventListener(`click`, handle);

function playRound(playerSelection) {
  const choices = [`rock`, `paper`, `scissors`];
  const wins = [`scissorspaper`,`rockscissors`,`paperrock`];
  const computerSelection = choices[Math.floor(Math.random() * 3)];
  const combined = playerSelection + computerSelection;
  const report = `you: ${playerSelection}, computer ${computerSelection}: `
  return wins.find(v => v === combined) 
    ? `${report} you win` : computerSelection === playerSelection
    ? `${report} tie` : `${report} you loose`;
}

function handle(evt) {
  if (evt.target.dataset.rps) {
    return console.log(playRound(evt.target.dataset.rps));
  }
  if (evt.target.dataset.clear) {
    return console.clear();
  }
}
span[data-rps] {
  cursor: pointer;
}
span[data-rps]:hover {
  background-color: #c0c0c0;
}
.as-console-wrapper {
    max-height: 70% !important;
}
<div>
  <span data-rps="rock"> rock</span>
  <span data-rps="paper"> paper</span>
  <span data-rps="scissors"> scissors</span>
  <div>
    <button data-clear="1">clear console</button>
  </div>
</div>