2016-09-25 110 views
0

我的代碼無法正常工作。它不是繼續循環。它只是表明你的價值低或高,並在那裏停止。爲什麼它不保持循環?代碼停止循環

var target; 
 
var count = 0; 
 
var play; 
 
var game; 
 

 
function do_this() { 
 
    var choose = (Math.floor(Math.random() * 100)); 
 
    target = choose + 1; 
 
    while (true) { 
 
    play = prompt("I am thinking of a no. between 1 and 100\n\n" + "enter the no."); 
 
    game = parseInt(play); 
 
    count = count + 1; 
 
    if (isNaN(game)) { 
 
     alert("Please enter integer value"); 
 
     return false; 
 
    } 
 
    if ((game < 1) || (game > 100)) { 
 
     alert("Please enter the value between 1 and 100"); 
 
     return false; 
 
    } 
 
    if (game < choose) { 
 
     alert("enter higher value"); 
 
     return false; 
 
    } 
 
    if (game > choose) { 
 
     alert("enter lower value"); 
 
     return false; 
 
    } 
 
    alert("You are correct\n\n" + "you took" + count + "to guess the game"); 
 
    return true; 
 
    } 
 
} 
 

 
do_this()

+0

請格式化爲可讀 –

+0

可能的重複[Does return stop a loop?](http://stackoverflow.com/questions/11714503/does-return-stop-a-loop) – showdev

回答

1

您正在使用return false;當值更高或更低。將其替換爲continue;

+0

thanx現在正在工作! – chunkydonuts21