1

Javascript beginner here. I'm trying to compute a simple math function using a window prompt(What is 3+3=?). If the user gets the answer right I want it to report back (Correct!), but if they're one away from the correct answer (5 or 7) I want it to report back another window prompt saying (Very close please try again. What is 3+3?) But also, if they get the answer completely wrong(lets say they put in 2) I want it to report back (Incorrect please try again. What is 3+3=?) This third condition is what is giving me problems because I need 2 separate loops in the script and I'm not sure how to go about doing that.

This is my code so far, where I've got stuck is the third while loop

var answer = window.prompt("What is 3+3", "");
    answer=parseFloat(answer);

if(answer==6){
      document.write("Correct!");
}else while( answer==5 || answer==7){
      answer=window.prompt("Very Close Please Try again. What is 3+3", "");
}else while(answer!==6 && answer!==5 && answer!=7){
    answer=window.prompt("Incorrect. Please try again. What is 3+3", "");
}

also bonus points to anyone that can explain how I would use a random number and have answer to this number instead of the hard coded 3+3 and its answer.

7
  • Why not "while number != 6"? Or number-6==abs(1)? Commented Jan 30, 2016 at 18:51
  • Add while at the begining use if instead of while and in answer==6 use break. Commented Jan 30, 2016 at 18:53
  • Are you referring to the 3rd condition? I had while (answer!=6) in before and I couldn't get that to work either. It just loops continuously with Incorrect no matter what number I put in there. Commented Jan 30, 2016 at 18:53
  • 1
    Your program is syntactically incorrect. There's no such language construct as while { } else. You got away with it with first while, as it assumed that this while is within else block from if(answer==6){, but now you seem to do something like if with double else, which makes no sense and is in fact not parsing correctly. What you seem to e attempting, is to have single while loop on top level, and within that, do series of if (answer==6) { /* correct case */ } else if(answer==5 || answer==7) {/* very close case */} else {/* incorrect case */}. You'd also want to have prompt within loop. Commented Jan 30, 2016 at 18:57
  • 1
    No only one while loop that wrap the whole thing as in @gurvinder372 answer. Commented Jan 30, 2016 at 19:01

2 Answers 2

3

try this

var answer = parseInt( window.prompt("What is 3+3", "") );

while( answer !== 6 )
{
   if ( answer === 5 || answer === 7 )
   {
      answer = parseInt( window.prompt( "Very Close Please Try again. What is 3+3", "") );
   }
   else if( answer !== 6 )
   {
     answer = parseInt( window.prompt("Incorrect. Please try again. What is 3+3", "") );
   }
} 
document.body.innerHTML = "Correct!";
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you sir, now I understand...While loops must be top level and the if statements must set inside the while loop. Also I want to thank you for the parseInt(window.prompt(code here)); I did not know that was possible to do in one line. p.s And also document.body.innerHTML works a lot better than document.write.
1

This way it will be dynamic. .

<body></body>
<script type="text/javascript">
var firstval = 5;
var secondval = 3;

var sum = firstval + secondval;

var answer = parseInt( window.prompt("What is "+firstval+" + "+secondval, ""));

while( answer !== sum )
{
   if ( answer === sum - 1 || answer === sum + 1 )
   {
      answer = parseInt( window.prompt( "Very Close Please Try again. What is "+firstval+" + "+secondval+"", "") );
   }
   else if( answer !== sum )
   {
     answer = parseInt( window.prompt("Incorrect. Please try again. What is "+firstval+" + "+secondval+"", "") );
   }
} 
document.body.innerHTML = "Correct!";
</script>

4 Comments

for firstval and secondval could I use firstval = math.random(); secondval = math.random(); to create random numbers? that way I wont have to hard code set problems and will have a random sum?
yeahh , Obvously you can.. use Math.random(); but it will give you a floating number., you can convert it to Int and use my code .
Oh bear with me if I ask pretty obvious questions lol. I'm still learning all of this as I go. But..to convert the floating number to an integer it would be, for instance, (var firstval=math.random()*100;) right?
var x = Math.floor((Math.random() * 10) + 1); try this to get a random int number,.

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.