0

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?

2
  • Out of interest, what is the book? The side-effect condition in the while and the overriding of 'cancel' of the standard prompt dialog is kind of fugly. Commented Jul 10, 2015 at 0:35
  • Beginning JavaSript: link Commented Jul 10, 2015 at 1:09

3 Answers 3

1

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'.

Sign up to request clarification or add additional context in comments.

1 Comment

His version also terminates the loop if you use the Cancel button in the prompt.
0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.