简体   繁体   English

JavaScript初学者变量混乱

[英]JavaScript beginner variable confusion

Learning JS from a book, the exercise question was this: 从书中学习JS,练习题是:

Modify the code of Question 1 to request the times table to be displayed from the user; 修改问题1的代码,要求用户显示时间表。 the code should continue to request and display times tables until the user enters ‐1. 该代码应继续请求并显示时间表,直到用户输入-1。 Additionally, do a check to make sure that the user is entering a valid number; 另外,进行检查以确保用户输入的有效号码; if the number is not valid, ask the user to re‐enter it. 如果该号码无效,请要求用户重新输入。

This is the proposed solution: 这是建议的解决方案:

 function writeTimesTable(timesTable, timesByStart, timesByEnd) { for (; timesByStart <= timesByEnd; timesByStart++) { document.write(timesTable + " * " + timesByStart + " = " + timesByStart * timesTable + "<br />"); } } var timesTable; while ((timesTable = prompt("Enter the times table", -1)) != -1) { while (isNaN(timesTable) == true) { timesTable = prompt(timesTable + " is not a " + "valid number, please retry", -1); } if (timesTable == -1) { break; } document.write("<br />The " + timesTable + " times table<br />"); writeTimesTable(timesTable, 1, 12); } 
 <!DOCTYPE html> <html lang="en"> <head> <title>Chapter 4: Question 2</title> </head> <body> <script> </script> </body> </html> 

This is my code, which also runs with the same result, without != -1 : 这是我的代码,它也以相同的结果运行,没有!= -1

 function writeTimesTable(timesTable, timesByStart, timesByEnd) { for (; timesByStart <= timesByEnd; timesByStart++) { document.write(timesTable + " * " + timesByStart + " = " + timesByStart * timesTable + "<br />"); } } var timesTable; while (timesTable = prompt("Enter the times table", -1)) { while (isNaN(timesTable) == true) { timesTable = prompt(timesTable + " is not a " + "valid number, please retry", -1); } if (timesTable == -1) { break; } document.write("<br />The " + timesTable + " times table<br />"); writeTimesTable(timesTable, 1, 15); } 
 <!DOCTYPE html> <html lang="en"> <head> <title>Chapter 4: Question 2</title> </head> <body> <script> </script> </body> </html> 

Why do I need != -1 parameter in the first while statement, since my code runs perfectly fine? 由于我的代码运行得很好,为什么在第一个while语句中需要!= -1参数? Why is it there, what is it for? 为什么在那儿,它有什么用?

The check for -1 is almost but not quite superfluous. -1的检查几乎但不是完全多余。 It catches the conditions 'user canceled prompt' and 'user entered an empty string' which evaluates to false. 它捕获条件“用户取消提示”和“用户输入了空字符串”,其结果为false。 In your version, this terminates the loop but the requirement is to terminate at user input '-1'. 在您的版本中,这终止了循环,但要求是在用户输入“ -1”处终止。

If a while loop doesn't return anything, it will return as -1 (or false ). 如果while循环不返回任何内容,它将返回-1 (或false )。 In the case of the original example, I assume that the != -1 condition is there for example purposes only so it makes more sense to a beginner. 在原始示例的情况下,我假设仅在示例中使用!= -1条件,因此对于初学者而言更有意义。

Let's say you were only wanting to terminate the while loop when the user entered -2 . 假设您只想在用户输入-2时终止while循环。 To do that, you would need to specify the != -2 condition in the loop, but -1 would still terminate the loop. 为此,您需要在循环中指定!= -2条件,但是-1仍会终止循环。

You're telling the browser/compiler to keep executing the code in the while loop until the user enters -1. 您要告诉浏览器/编译器在while循环中继续执行代码,直到用户输入-1。 When timesTable gets the value "-1" - that is, when the user enters "-1" - the while loop stops running. 当timesTable获得值“ -1”时-也就是说,当用户输入“ -1”时-while循环将停止运行。

// timesTable gets what the user enters in the prompt
// while timesTable is not equal to -1, execute the code in brackets
while ((timesTable = prompt("Enter the times table", -1)) != -1) {
  while (isNaN(timesTable) == true) {
    timesTable = prompt(timesTable + " is not a " +
      "valid number, please retry", -1);

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

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