简体   繁体   English

带有嵌套if语句的while循环不会循环javascript

[英]While loop with nested if statement won't loop javascript

I don't understand why, but my loop won't complete.我不明白为什么,但我的循环无法完成。 I am supposed to make a guessing game using javascript but the loop won't actually loop.我应该使用 javascript 制作一个猜谜游戏,但循环实际上不会循环。 It just takes an answer, displays the appropriate window and stops.它只需要一个答案,显示适当的窗口并停止。 Regardless of the answer.不管答案如何。 Here is the code :这是代码:

<!DOCTYPE >
<html>
<head>
<title>Welcome To The JS Guessing Game</title>
</head>
<script> 

//Initialize the variables that will be needed.
var target;
var target_index;
var guess_input;
var guesses=0;
var finished=false;
var guesses=0;
var colors = ["blue", "cyan", "gold", "green", "magenta", "orange", "red", "white", "yellow"];

function do_game() {
    var random_number = (Math.random()*colors.length);
    var random_number=Math.floor(random_number);
    target=colors[random_number];
    alert(target);
    while (!finished){
        guess_input=prompt("I am thinking of a color in the list below. Can you guess which color?"+"\n\n"+ colors.join() );
        guesses++;
        if (guess_input!=target){
            alert("no")
            return true;
        }
        alert("yes!!")
        return false;
    }
}
</script>
<body onload="do_game()">
</body>
</html>

Now I really don't understand what i am doing wrong, and the console flags up no error in any browser :/ I would really appreciate the help!现在我真的不明白我做错了什么,并且控制台在任何浏览器中都没有标记错误:/我真的很感激帮助!

Cheers,干杯,

David大卫

The issue is that you're breaking out of the while loop when an incorrect answer is entered and you run the code:问题在于,当输入错误答案并运行代码时,您会跳出while循环:

if (guess_input!=target){
    alert("no")
    return true;
}

Rather than returning a value, consider using a continue statement.与其返回值,不如考虑使用continue语句。

if (guess_input!=target){
    alert("no")
    continue;
}

This will jump to the next iteration of the loop, staying within the loop but not executing the alert("yes!!") .这将跳转到循环的下一次迭代,停留在循环内但不执行alert("yes!!") Here's a JSFiddle to demonstrate.这里有一个JSFiddle来演示。 Hope this helps!希望这可以帮助! Let me know if you have any questions.如果您有任何问题,请告诉我。

(And as per comments on your question, you don't really need the finished variable, just using while(true) should be sufficient.) (并且根据对您的问题的评论,您实际上并不需要finished变量,只需使用while(true)就足够了。)

Set finished to true (no need to set it to false as it will always be false until you change it).finished设置为true (无需将其设置为false因为在您更改它之前它将始终为false )。 If you need to return finished after the loop (which I don't think you do), I've added in the statement to that effect.如果您需要在循环后返回finished (我认为您不会这样做),我已在语句中添加了该效果。 You also need to have an else clause in there too or finished will be set to true in your code even if the condition was met.您还需要在那里有一个else子句, else即使满足条件,您的代码中也将finished设置为true

function do_game() {
    var random_number = (Math.random()*colors.length);
    var random_number=Math.floor(random_number);
    target=colors[random_number];
    while (!finished){
        guess_input=prompt("I am thinking of a color in the list below. Can you guess which color?"+"\n\n"+ colors.join() );
        guesses++;
        if (guess_input!=target){
            alert("no");
        } else {
          alert("yes!!");
          finished = true;
        }
    }
    return finished;
}

You were returning a boolean regardless of the condition.无论条件如何,您都返回一个布尔值。 Take a look a the code here: https://jsfiddle.net/zmdcL4h4/1/看看这里的代码: https : //jsfiddle.net/zmdcL4h4/1/

//Initialize the variables that will be needed.
var target;
var target_index;
var guess_input;
var guesses=0;
var finished=false;
var guesses=0;
var colors = ["blue", "cyan", "gold", "green", "magenta", "orange", "red", "white", "yellow"];

function do_game() {
    var random_number = (Math.random()*colors.length);
    var random_number=Math.floor(random_number);
    target=colors[random_number];
    alert(target);
    while (!finished){
        guess_input=prompt("I am thinking of a color in the list below. Can you guess which color?"+"\n\n"+ colors.join() );
        if(!guess_input){
            finished = true;
            return true;
        }
        guesses++;
        if (guess_input!=target){
            alert("no")
        }
        else
            return true;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM