Logan Musselman Logan Musselman - 1 month ago 7
Javascript Question

I am trying to figure out how to make my functions run one after another... but it is not working

I am trying to create a very simple text based adventure game using java. I am very new and I feel this would be a great was to work with some of the functions, variables, and if-else statements. I have written some code to prompt the screen to ask for age, and give a result.

confirm("Ready to play?");
var age = prompt("How old are you?");
if (age >= 18) {
document.write(" ");
} else {
document.write("You're less than 18? You better just stay home.");
}

document.write("<button> Sounds like you're ready.</button>");

var gender = prompt("Male","Female");
if(gender = "Male") {
document.write("Sounds good!");
} else if(gender = "Female") {
document.write("Sounds good!");
} else if {
document.write("You're a damn liar");
};
};


If I run just the top half of the code, everything will run just fine. But if I try to run the gender prompt, alongside the original code, it will run nothing. Any suggestions?

Answer

This code should work:

confirm("Ready to play?");
var age = prompt("How old are you?");
if (age >= 18) {
    document.write(" "); 
} else {
    document.write("You're less than 18? You better just stay home.");
}

document.write("<button> Sounds like you're ready.</button>");

var gender = prompt("Are you Male or Female?");
if(gender == "Male") {
    document.write("Sounds good!");
} else if(gender == "Female") {
    document.write("Sounds good!");
} else {
    document.write("You lie...");
} 

Basically, you need to replace = with == or ===. And a prompt() only takes one argument.