0

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;
}
}

3 Answers 3

2

The prompt() function returns the value entered by the user in the textbox, not the value of the button clicked by the user. Thus, the value of play will never ever be 'false', unless the user entered 'false' in the textbox.

Try this instead:

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 === "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;

}       
else 
{
    document.getElementById("pick").innerHTML="Sorry, maybe next time.";
}

Also, since this is more a yes-or-no type situation, you might want to use confirm() instead of prompt().

Sign up to request clarification or add additional context in comments.

Comments

1

Minor tweak will do

if (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;
} else {
    document.getElementById("pick").innerHTML="Sorry, maybe next time.";
}

Demo: Fiddle

Comments

1
else (play= "YES");

is an assignment (not a comparison), in brackets, followed by the line delimiter. The block after it will always be executed. Your script currently works as if it was written

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;

You seem to want an else-if-statement:

if (play == "false") {
    document.getElementById("pick").innerHTML = "Sorry, maybe next time.";
} else if (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;
} /* else {
    alert("neither 'false' nor 'YES' were entered");
} */

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.