简体   繁体   English

Javascript:while循环不起作用

[英]Javascript : problem with while loop that does not work

In the script below, I'm trying to get a function to find a random number chosen by the system.在下面的脚本中,我试图获得一个函数来查找系统选择的随机数。 To help me to find the number : When the number to find is smaller than what I enter on the interface: I get a message that the number to find is smaller When the number to find is bigger than the one I enter on the interface: I receive a message that the number to find is bigger When I find the number, I receive a message telling me that I have found the number in xxx tries.帮我找号码: 当要找的号码比我在界面上输入的要小: 我收到一条提示要找的号码更小 当要找的号码比我在界面上输入的要大时:我收到一条消息说要查找的号码更大 当我找到号码时,我收到一条消息,告诉我我在 xxx 次尝试中找到了号码。 When I find the number in one go, I want to change trial by trial in the message一口气查到号码的时候,想在留言里一试一试

When I rotate the code below I just have a box to ask me what is the number to guess.当我旋转下面的代码时,我只有一个框来问我要猜的数字是多少。 Then nothing happens.然后什么也没有发生。 Can you please help me to fix the code problems in my script below.您能帮我解决下面脚本中的代码问题吗? Could you please also indicate if my approach is correct to count the number of attempts in the code below.您能否还指出我的方法是否正确计算下面代码中的尝试次数。 How would you proceed ?你将如何进行?

 function askValue() { var answer = window.prompt( "Guess the number, enter a number between 1 and 10" ); // keep the answer to use it in the loop if (!answer || isNaN(answer)) { console.log("Please enter a valid number"); } else { return answer; } } function guessnumber() { var secret_number = Math.floor(Math.random() * 10) + 1; var guess = askValue(); var attempts; var i = 0; var resultMessage = "You won, you take"; while (win == false) { attempts++; if (guess < secret_number) { console.log("The secret number is bigger"); i++; } else if (guess > Secret_number) { console.log("The secret number is smaller"); i++; } else if (guess == secret_number) { win = true; } console.log(resultMessage); } } // call the function guessnumber();

I make your code works by fixing many mistake and bugs some of them:我通过修复许多错误和其中一些错误来使您的代码正常工作:

  • using var which is old and it's better use the keyword let to declare variable!使用旧的var ,最好使用关键字let来声明变量!
  • checking if the number between 1 & 10: if (+answer < 1 || +answer > 10)检查 1 和 10 之间的数字: if (+answer < 1 || +answer > 10)
  • prefix + , is just shorthand for parseInt() function to convert string to number, since prompt return string not number prefix + ,只是parseInt()函数将字符串转换为数字的简写,因为提示返回字符串而不是数字
  • many more... if you don't understand sth do a comment and I will explain to you!还有更多...如果您不明白某事,请发表评论,我会向您解释!
     function askValue() { let answer = window.prompt( "Guess the number, enter a number between 1 and 10" ); // keep the answer to use it in the loop if (!answer || isNaN(answer)) { alert("Please enter a valid number"); } else if (+answer < 1 || +answer > 10) { alert("Please enter a number between 1 and 10"); } else { return +answer; } } // Better using `let` than `var` function guessnumber() { let secret_number = Math.floor(Math.random() * 10) + 1; let guess = askValue(); let attempts = 0; //initialse attempts with zero let i = 0; let resultMessage = "You won, you take "; let win = false; //declare win while (win == false) { attempts++; if (guess < secret_number) { alert("The secret number is bigger"); i++; guess = askValue(); } else if (guess > secret_number) { //s lowercase not capital alert("The secret number is smaller"); i++; guess = askValue(); } else if (guess == secret_number) { win = true; resultMessage += attempts + " attempt" + (i != 1 ? "s" : ""); alert(resultMessage); } else { guess = askValue(); } } } // call the function guessnumber();

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

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