在猜测游戏上做一会儿循环

时间:2014-06-22 17:48:30

标签: javascript html

我遇到了一些我似乎无法弄清楚的事情。我试图让它循环,所以当用户猜出正确的信息时,它会打破循环。但是,如果他们继续弄错了,它会说"想再玩一次"。这是我的代码:

while (userResponse="yes") {        

            var value = 15;
            var guess = prompt('Please enter a number greater than zero.');

                if (value == guess) {
                    alert ('The number you entered was correct. You won!');
                }else if (guess == null){
                    alert ('You didn\'t enter anything!');                  
                }else if ((guess > value + 0)){
                    alert ('You\'re way off!');
                }else if ((guess <= value + 7) && (guess >= value - 7)){
                    alert ('You\'re getting warm!');
                }else if ((guess > value + 7)  || (guess < value - 7)){
                    alert ('You\'re getting cold!');                
                } else {
                    alert ('Something went wrong.');
                }
                userResponse = prompt("Would you like to play again?");
        }

关于我做错了什么的任何想法?我想做的就是用户猜测&#34; 15&#34;它自动死亡。

1 个答案:

答案 0 :(得分:2)

在解决循环问题之前有一些问题:

  1. 替换您想要提问时调用的函数的无限循环。
  2. userResponse="yes"始终返回true,您可能需要userResponse=="yes"
  3. 要在用户猜到回复时退出循环,您只需break break on MDN)