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.
whileat the begining use if instead of while and inanswer==6use break.while { } else. You got away with it with first while, as it assumed that this while is within else block fromif(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 ofif (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.