Javascript: Why is my prompt being requested twice?

Hi everyone. I’m going through Beginning Javascript 5th edition as I go through FreeCodeCamp, and I’ve encountered an issue. The exercise says to write a function which displays the times tables (1-12) requesting the times table from the user, checking if it’s a valid number, and stopping when the user enters -1. I couldn’t do this by myself without peeking at the answers, so I’m even more surprised by the fact that this happens. I’ve checked the answers, and with the exception of variable names, it’s pretty much identical. Everything seems to work fine except this is what happens when you run it:
- you get a promp window requesting a number between 1-12, which also has a placeholder value (-1). You type a valid number, say 5, and press enter.
**- Nothing happens and you get the same prompt window again, with the same placeholder value (-1). Here you have to press enter again in order for it to actually print the desired results to the html (in this case, the 5 times table). **
What seems to happen is that it only stops going through the loop and executing the document.write line after the user has entered -1. What it should do is request and display the results for each value, which it doesn’t until the user enters -1. i What the hell is going on? Bear in mind I’m a newbie so sorry if this is a very stupid question. Here’s my code:

  <script>

  function multiply (timesTable, startValue, endValue) {

    for (; startValue <= endValue; startValue++){
      document.write(timesTable * startValue + "<br />");
      }

    }

    var timesTable;

    while((timesTable = prompt("Enter a number from 1 to 12.", -1)) != -1) {
      while(isNaN(timesTable) == true) {
        timesTable = prompt("Enter a valid number", -1);
      }
    if (timesTable == -1){
      break;
    }
    document.write("The " + timesTable + " times table" + "<br />");
    multiply(timesTable, 1, 12);
  }
</script>

I tried it (I looked at it and made sense), but it just didn’t work at all. I know I could rewrite it in a different way, I just want to find out why this particular method is behaving like that.

I think people are not understanding the issue with the functionality. It’s supposed to keep asking for numbers and then when the user hits -1, exit, which is what it does. So maybe be more clear with your question. There’s a reason mods are not responding. Hence, why i withdrew my solution

I edited my question.
The problem is, it exits when user enters -1, but it doesn’t display any of the results for valid values until it leaves the loop (when -1 is entered). It should request values and display results each time, and only exit when -1 is entered.

i think using prompt prevents any console.log and/or document.write from being displayed until there are no more prompts. The other code after the prompts does execute in the proper order, but the prompts have higher priority. As soon as another number is entered (which is not -1), the prompt immediately takes priority causing a delay in updating the DOM or even console.

I could be mistaken, but I do not think there is a way to avoid this with your current use of prompt. In all practicality, you should be using a custom form (input fields) to get the inputs you want and then have complete control over when you display messages to the user for getting valid numbers.

1 Like

I thought as much. Thank you!