samedi 30 janvier 2016

How to have 3 conditions using both if and while Javascript

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.

Aucun commentaire:

Enregistrer un commentaire