pilgrim011
pilgrim011

Reputation: 57

JavaScript beginner variable confusion

Learning JS from a book, the exercise question was this:

Modify the code of Question 1 to request the times table to be displayed from the user; the code should continue to request and display times tables until the user enters ‐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:

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? Why is it there, what is it for?

Upvotes: 0

Views: 80

Answers (3)

Joseph
Joseph

Reputation: 339

You're telling the browser/compiler to keep executing the code in the while loop until the user enters -1. When timesTable gets the value "-1" - that is, when the user enters "-1" - the while loop stops running.

// 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);

Upvotes: 0

Wes Foster
Wes Foster

Reputation: 8900

If a while loop doesn't return anything, it will return as -1 (or 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.

Let's say you were only wanting to terminate the while loop when the user entered -2. To do that, you would need to specify the != -2 condition in the loop, but -1 would still terminate the loop.

Upvotes: 0

pvg
pvg

Reputation: 2729

The check for -1 is almost but not quite superfluous. It catches the conditions 'user canceled prompt' and 'user entered an empty string' which evaluates to false. In your version, this terminates the loop but the requirement is to terminate at user input '-1'.

Upvotes: 1

Related Questions