gpt4 book ai didi

javascript - 石头剪刀布 JavaScript 问题

转载 作者:行者123 更新时间:2023-11-30 16:19:30 25 4
gpt4 key购买 nike

作为类(class)的一部分,我正在为代码学院编写一个简单的剪刀石头布脚本;无论我输入什么,我得到的每一个答案都是接近尾声的警报(“摇滚胜利!”);我做错了什么?

var choice1 = prompt("Do you choose rock, paper or scissors?");
//-------------------------------------//
var choice2 = Math.random();

if (choice2 < 0.34) {
choice2 = "rock"
}

else if(choice2 <= 0.67) {
choice2 = "paper";
}

else {
choice2 = "scissors";
}

console.log(choice2)
//------ possible outcomes for the computer are above this comment-----------


compare = function(choice1,choice2){

//----------------------------------------
if(choice1 === "paper" & choice2 ==="paper"){
alert("The result is a tie!");}

if(choice1 === "rock" & choice2 ==="rock"){
alert("The result is a tie!");}

if(choice1 === "scissors" & choice2 ==="scissors"){
alert("The result is a tie!");}

//-possible outcomes for a tie are above this comment------------------------------


if(choice1 === "paper" & choice2 === "rock"){
alert("paper wins!");}


if(choice1 === "rock" & choice2 === "paper"){
alert("paper wins!");}

//-possible outcomes for paper & rock are above this comment------------------------


if(choice1 === "scissors" & choice2 === "rock"){
alert("rock wins!");}

if(choice1 === "rock" & choice2 === "scissors"){
alert("rock wins!");}

//-possible outcomes for a scissors & rock are above this comment--------------------


if(choice1 === "scissors" & choice2 === "paper"){
alert("scissors wins!");}

if(choice1 === "paper" & choice2 === "scissors"){
alert("scissors wins!");}

//-possible outcomes for a scissors & paper are above this comment--------------

}//<---End of compare function--//

最佳答案

使用 && 作为“和”,而不是 &

if (choice1 === "paper" && choice2 === "rock") {
...

关于javascript - 石头剪刀布 JavaScript 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34952221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com