[英]Prompt Not working inside for loop in Javascript
alert("You will play 10 round and the highest scorer will win the game")
//let player1 = prompt("choose")
let element = ['snake', 'water', 'gun']
let player1_score=0
let player2_score=0
for (let i =0; i<10;i++)
{
let player1 = prompt("choose")
let player2 = element[Math.floor(Math.random() * (element.lenght -0) + 0)]
if ((player1== snake && player2 ==snake) || (player1== water && player2==water) || (player1== gun && player2 ==gun))
{
alert("Tie")
}
if ((player1== snake && player2 ==water) | (player1== gun && player2==snake) || (player1== water && player2 == gun))
{
alert("player1 wins")
player1_score++
}
if ((player1== snake && player2 ==gun) || (player1== gun && player2==water) || (player1== water && player2 ==snake))
{
alert("player2 wins")
player2_score++
}
}
alert("winner of Game is : ")
if (player1_score > player2_score )
alert("user wins with score : " + player1_score)
else if (player2_score > player1_score)
alert ("bot wins with score" + player2_score)
else
alert ("tie")
console.log("final score"+ "\n" + "user : " + player1_score + "\n" + "bot : " + player2_score )
The problem is with the prompt inside the for loop for player1.问题在于 player1 的 for 循环内的提示。 When I enter snake inside the prompt box it gives a error " snake is not defined ".当我在提示框中输入蛇时,它会给出错误“蛇未定义”。 But when I try outside the for loop it works fine.但是当我在 for 循环之外尝试时,它工作正常。 Please help me out I am still in learning phase.请帮助我,我仍处于学习阶段。
The problem is with the prompt inside the for loop for player1.问题在于 player1 的 for 循环内的提示。
No it isn't.不,不是。 The problem is where the code is trying to use a variable that isn't defined:问题是代码试图使用未定义的变量:
(player1 == snake && player2 == snake)
There is no variable called snake
.没有变量名为snake
。 There is, however, a string value 'snake'
:但是,有一个字符串值'snake'
:
let element = ['snake', 'water', 'gun']
Did you mean to compare it with a string?:您的意思是将其与字符串进行比较吗?:
(player1 == 'snake' && player2 == 'snake')
Or perhaps with the array element?:或者也许与数组元素?:
(player1 == element[0] && player2 == element[0])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.