I cant seem to return the result of inside the function
我已经创建了一个基本的石头剪刀游戏,我无法在函数内返回结果。
我一直在尝试一切
链接控制台记录比较并将其放入变量..
console.log(compare)由于某种原因返回undefined。
请帮忙
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | var userChoice = prompt("Do you choose rock, paper or scissors?"); var computerChoice = Math.random(); var result; if (computerChoice < 0.34) { computerChoice ="rock"; } else if(computerChoice <= 0.67) { computerChoice ="paper"; } else { computerChoice ="scissors"; } console.log("Computer's choice:" + computerChoice); console.log("Your choice:" + userChoice); console.log(computerChoice +" vs" + userChoice); var compare = function(userChoice, computerChoice){ if (userChoice === computerChoice){ return "The result is a tie!"; } else if (userChoice ==="rock"){ if(computerChoice ==="scissors"){ return"rock wins!"; } else { return"paper wins!"; } } else if (userChoice ==="paper") { if(computerChoice ==="rock") { return"paper wins!"; } else { return"scissors wins!"; } } else if (userChoice ==="scissors") { if(computerChoice ==="rock") { return"rock wins!"; } else { return"scissors wins!"; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | var userChoice = prompt("Do you choose rock, paper or scissors?"); var computerChoice = Math.random(); var result; if (computerChoice < 0.34) { computerChoice ="rock"; } else if(computerChoice <= 0.67) { computerChoice ="paper"; } else { computerChoice ="scissors"; } console.log("Computer's choice:" + computerChoice); console.log("Your choice:" + userChoice); console.log(computerChoice +" vs" + userChoice); function compare(userChoice, computerChoice){ if (userChoice === computerChoice){ return "The result is a tie!"; } else if (userChoice ==="rock"){ if(computerChoice ==="scissors"){ return"rock wins!"; } else { return"paper wins!"; } } else if (userChoice ==="paper") { if(computerChoice ==="rock") { return"paper wins!"; } else { return"scissors wins!"; } } else if (userChoice ==="scissors") { if(computerChoice ==="rock") { return"rock wins!"; } else { return"scissors wins!"; } } } var Compare = compare(userChoice, computerChoice); console.log(Compare); |
这里的主要问题是你从未执行过你编写的匿名函数。 我比较了一个普通函数而不是匿名函数,只是为了让它更像其他语言。 这是一个很好的工作代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | var userChoice = prompt("Do you choose rock, paper or scissors?"); var computerChoice = Math.random(); var result; if (computerChoice < 0.34) { computerChoice ="rock"; } else if(computerChoice <= 0.67) { computerChoice ="paper"; } else { computerChoice ="scissors"; } console.log("Computer's choice:" + computerChoice); console.log("Your choice:" + userChoice); console.log(computerChoice +" vs" + userChoice); function compare(userChoice, computerChoice){ if (userChoice === computerChoice){ return "The result is a tie!"; } else if (userChoice ==="rock"){ if(computerChoice ==="scissors"){ return"rock wins!"; } else { return"paper wins!"; } } else if (userChoice ==="paper") { if(computerChoice ==="rock") { return"paper wins!"; } else { return"scissors wins!"; } } else if (userChoice ==="scissors") { if(computerChoice ==="rock") { return"rock wins!"; } else { return"scissors wins!"; } } } console.log(compare(userChoice, computerChoice)); |
正如Shinra tensei所提到的那样,第18行中的
这是一个更新的剪辑。 我还为比较功能添加了一个警报。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | var userChoice = prompt("Do you choose rock, paper or scissors?"); var computerChoice = Math.random(); var result; if (computerChoice < 0.34) { computerChoice ="rock"; } else if(computerChoice <= 0.67) { computerChoice ="paper"; } else { computerChoice ="scissors"; } console.log("Computer's choice:" + computerChoice); console.log("Your choice:" + userChoice); console.log(computerChoice +" vs" + userChoice); alert(compare(userChoice,computerChoice)); function compare (userChoice, computerChoice){ if (userChoice === computerChoice){ return "The result is a tie!"; } else if (userChoice ==="rock"){ if(computerChoice ==="scissors"){ return"rock wins!"; } else { return"paper wins!"; } } else if (userChoice ==="paper") { if(computerChoice ==="rock") { return"paper wins!"; } else { return"scissors wins!"; } } else if (userChoice ==="scissors") { if(computerChoice ==="rock") { return"rock wins!"; } else { return"scissors wins!"; } } } |
你在这里有语法错误......
1 | return = "The result is a tie!"; |
将此更改为:
1 | return"The result is a tie!"; |