代码停止循环

时间:2016-09-25 14:01:08

标签: javascript

我的代码无法正常运行。它没有继续循环。它只是表明你的价值低或高,并在那里停止。为什么它不继续循环?



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()
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

当值更高或更低时,您正在使用return false;。将其替换为continue;

相关问题