1

This nested for adds up until i and j are both 3 currently. How can I reset the variables once they've capped so I can continue the loop?

Or is that not the correct thought process?

for (i=1; i<=3; i=i+1)
  {
   for (j=1; j<=3; j=j+1)
     {
      ans=i + j;
      document.write(i + "+" + j + "=" + "<br />");
      var user_input = prompt("What is your answer?",0);
      while (user_input != ans) {
      if (ans == user_input)
        {
         document.write("Yes, the answer is " + ans + "<br />");
        }
      else
        {
         document.write("No, the answer is " + ans + "<br />");
        } 
      }
    }  
  }

I've tried a while loop, but it's consistently crashing. I'm just a bit iffy on the logic of the problem.

What I'm trying to do:

If the user answers correctly at any point, the loop ends. But I don't want the math to add above 6. Once it hits 6 I'd preferably like it to reset back to for both i and j.

5
  • Sorry, still trying to understand. So if user answers correctly 9 times, you want to restart the whole process? Commented May 10, 2017 at 19:47
  • No if the user answers correctly at any point, the loop ends. But I don't want the math to add above 6. Once it hits 6 I'd preferably like it to reset back to for both i and j. Commented May 10, 2017 at 19:51
  • it's crashing because inside your while loop no variables are changing. If the answer was wrong, then it's going berzerk doing nothing else than if/else/if/else a zillion times per second Commented May 10, 2017 at 20:04
  • What is the while loop for? What if user answers wrong, would they get stuck at same question or go to next iteration? Commented May 10, 2017 at 20:14
  • The while-loop starts to run the code if the condition is true and keeps looping as long as the condition is true, in this case, if user_input != ans. If user answers wrong they get stuck inside that loop and with an overheating computer.. nothing else! Commented May 10, 2017 at 20:20

2 Answers 2

1

I think you don't need the while loop. Assuming if user answer wrong, they get new question and if they answer right, the whole process stops, please try this

var finish = false;
for (i=1; i<=3; i=i+1)
  {
   for (j=1; j<=3; j=j+1)
     {
      ans=i + j;
      console.log(i + "+" + j + "=" + "<br />");
      var user_input = prompt("What is your answer?",0);
      if (ans == user_input)
        {
            finish = true;
            console.log("Yes, the answer is " + ans + "<br />");
          break;
        }
      else
        {
            console.log("No, the answer is " + ans + "<br />");
        }
    }  
    if(finish){break;}
    if(i==3 && j==4){i =0; j=0}
  }
Sign up to request clarification or add additional context in comments.

1 Comment

This is almost exactly what I'm looking for, except it still cuts out once ans=6. How can I reset the variables once they've hit the 3 each? Reinitialize them somewhere else in the loop?
0

I would use a recursive function to do this. Using the nested loops is just scary, harder to read, and easier to get into an infinite loop condition:

var ans = 6;
function getInput() {
    var user_input = prompt("What is your answer?",0);
    if (ans == user_input) {
        document.write("Yes, the answer is " + ans + "<br />");
    }
    else {
        getInput();
    } 
}

getInput();

If you wanted the ans to change every time you go through the loop, you could add random number logic to set the answer:

var ans = Math.floor(Math.random() * 6) + 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.