I am making a simple javascript number game for a school assignment using math.random to generate a random number (var = randomDigit) then ask the user to think of a number greater than 1. Basically, they double their number and JS doubles my random number and outputs it. They add the numbers together, then divide by two and subtract their original number leaving me with (var = randomDigit)
Here's the part where I am stuck. I have to make it so if the user doesn't want to play, it will not keep going... and i can only use one "if" statement. I opted to use a prompt() method... and it works if you type in YES, like the dialogue says and if you press cancel it doesn't work (good news). But if I type in "NO" like i don't want to play the game, it still works.
I want it so the program only works with a "YES" typed into the prompt dialogue.
Here is my JS Fiddle : http://jsfiddle.net/JrYAq/
function dialog(){
var x;
var randomDigit = Math.floor((Math.random()*20)+1);
var doubleDigit = +randomDigit * 2;
var play = prompt("Would you like to play a game? If not, press cancel below.\n\nThink of a number greater than 1. Confirm you thought of a number by typing the word: YES","Type Here");
if (play==='false')
{
document.getElementById("pick").innerHTML="Sorry, maybe next time.";
}
else (play= "YES");
{
x="1. Now double the number you thought of. I'm going to think of a number too:" + ' ' + doubleDigit + '<br><br>' + "2. Now add my number and your doubled number together." + '<br><br>' + "Now divide the added numbers by 2, then subtract your original number. Did you get" + ' ' + randomDigit + "?" + '<br><br>' + "Pretty cool, right?" ;
document.getElementById("pick").innerHTML=x;
}
}