Code Your Own Adventure 2 Syntax Error


#1

Syntax error with Code Your Own Adventure 2

https://www.codecademy.com/en/courses/javascript-beginner-en-ZA2rb/0/5?curriculum_id=506324b3a7dffd00020bf661
I'm working on "Code Your Own Adventure 2," but I keep receiving this syntax error message:
"SyntaxError: Left hand side of operator '=' must be a reference."
What does this mean, and how can I fix it?

// initial prompt
var user = prompt("You, a successful entrepreneur, are sitting in one of your many chicken smoothie franchises. Suddenly, you hear screaming from the kitchen. The chickens have rebelled. Are you prepared to take them down and turn them into smoothies? (YES or NO)").toUpperCase();

// if user decides to kill chickens
if(user = "YES") {
        var areYouSure = prompt("The horrors you will face will be unspeakable. Do you still want to defeat the chickens? (YES or NO)").toUpperCase();
// if user decides not to kill chickens
 } else if(user = "NO") {
        console.log("The chickens kill everyone in the store. You barely manage to escape the restaurant before the city is put on lockdown and the chickens reign supreme.");
// if user responds improperly to initial prompt
 } else {
    console.log("I'm not sure what that means. Don't waste time fooling around. The chickens are gaining power.");
}

// if user still wants to kill chickens
if(user && areYouSure = "YES") {
    var howToKill = prompt("There are only 3 things you can grab: a blender, a samurai sword, and a potato. Do you BLEND the chickens, CHOP the chickens, or THROW the potato?").toUpperCase();
    // if user decides to chop the chickens or throw the potato
    if(howToKill = "CHOP" || "THROW") {
        console.log("Um, you should know better. You own a chicken SMOOTHIE store. The chickens kill everyone in the store. You barely manage to escape the restaurant before the city is put on lockdown and the chickens reign supreme.");
    // if user decides to blend the chickens
    } else if(howToKill = "BLEND") {
        var killBoss = prompt("Great job! You've blended all of the ... Wait, what's that? A giant BOSS CHICKEN rises out of the carnage in full NINJA CHICKEN GEAR. Your blender is jammed with the blood and guts of lesser chickens. How do you defeat this BOSS CHICKEN? Do you CHOP him with the samurai sword, use hand-to-wing COMBAT, or THROW the potato?").toUpperCase();
        switch(killBoss) {
            // if user decides to chop BOSS CHICKEN
            case "CHOP":
                console.log("You SLICE the BOSS CHICKEN's feet off. He bounces on his butt and tries to peck your face, but then you CHOP off his beak. Then you STAB him in his gizzard. You have defeated the BOSS CHICKEN! You receive a CHICKEN NINJA MEDAL, but then you go to jail for using chickens that were ninjatically-modified in your restaurant.");
                break;
            // if user decides to combat BOSS CHICKEN
            case "COMBAT":
                console.log("The BOSS CHICKEN grabs your arm, but then you PLUCK off his tailfeathers. He is stunned, and you PLUCK off all of his feathers. He is so embarrassed that he runs into the CHICKEN BOILER. You have defeated the BOSS CHICKEN! You receive a CHICKEN NINJA MEDAL, but then you go to jail for using chickens that were ninjatically-modified in your restaurant.");
                break;
            // if user decides to throw potato at BOSS CHICKEN
            case "THROW":
                console.log("The demon trapped inside the potato opens up and instills POWER into the BOSS CHICKEN. The BOSS CHICKEN consumes space and time and becomes the ALL-POWERFUL NINJA CHICKEN OVERLORD OF TIME AND SPACE. He puts you in a dimension of eternal torture and insanity: chickens peck our your eyes, but your eyes continually grow back and are pecked out again.");
                break;
            // if user responds improperly to killBoss
            default:
                console.log("You have acted too slowly. The BOSS CHICKEN kills you and rampages the whole world. Chaos abounds.");
                break;
        }
    // if user does not respond properly to howToKill
    } else {
        console.log("You've wasted enough time. The chickens kill everyone in the store. You barely manage to escape the restaurant before the city is put on lockdown and the chickens reign supreme.");
    }
// if on second thought, user decides not to kill chickens
} else if(areYouSure = "NO") {
    console.log("The chickens kill everyone in the store. You barely manage to escape the restaurant before the city is put on lockdown and the chickens reign supreme.");
// if user does not respond properly to areYouSure
} else {
    console.log("I'm not sure what that means. Don't waste time fooling around. The chickens are gaining power.");
}

Please excuse the crazy storyline haha. I'm sure there are more mistakes with this code, but that's the first one I've come across.

Thank you :slight_smile:


#2

You have many errors, but luckily, they're all the same :smiley:.
You are checking for equality in a wrong way. You should use == to check if two things are equal to each other, whereas you should use = to set a variable equal to something else.


#3

Thank you so much! :smiley:


#4

Another problem :hushed:

No matter what I put for howToKill, it always prints out the response for the answer of "chop" or "throw" (whether it's "blend" or another answer). What's the issue here?

Edit: And also, if I answer "NO" to the initial prompt, it brings up the prompt for if(user && areYouSure == "YES"). I think these two problems might be related?

Again, there's probably a lot more errors that I haven't spotted yet :stuck_out_tongue:


#5

Can you post the code you have currently?


#6

// initial prompt
var user = prompt("You, a successful entrepreneur, are sitting in one of your many chicken smoothie franchises. Suddenly, you hear screaming from the kitchen. The chickens have rebelled. Are you prepared to take them down and turn them into smoothies? (YES or NO)").toUpperCase();

// if user decides to kill chickens
if(user == "YES") {
        var areYouSure = prompt("The horrors you will face will be unspeakable. Do you still want to defeat the chickens? (YES or NO)").toUpperCase();
// if user decides not to kill chickens
 } else if(user == "NO") {
        console.log("The chickens kill everyone in the store. You barely manage to escape the restaurant before the city is put on lockdown and the chickens reign supreme.");
// if user responds improperly to initial prompt
 } else {
    console.log("I'm not sure what that means. Don't waste time fooling around. The chickens are gaining power.");
}

// if user still wants to kill chickens
if(user && areYouSure == "YES") {
    var howToKill = prompt("There are only 3 things you can grab: a blender, a samurai sword, and a potato. Do you BLEND the chickens, CHOP the chickens, or THROW the potato?").toUpperCase();
    // if user decides to chop the chickens or throw the potato
    if(howToKill == "CHOP" || "THROW") {
        console.log("Um, you should know better. You own a chicken SMOOTHIE store. The chickens kill everyone in the store. You barely manage to escape the restaurant before the city is put on lockdown and the chickens reign supreme.");
    // if user decides to blend the chickens
    } else if(howToKill == "BLEND") {
        var killBoss = prompt("Great job! You've blended all of the ... Wait, what's that? A giant BOSS CHICKEN rises out of the carnage in full NINJA CHICKEN GEAR. Your blender is jammed with the blood and guts of lesser chickens. How do you defeat this BOSS CHICKEN? Do you CHOP him with the samurai sword, use hand-to-wing COMBAT, or THROW the potato?").toUpperCase();
        switch(killBoss) {
            // if user decides to chop BOSS CHICKEN
            case "CHOP":
                console.log("You SLICE the BOSS CHICKEN's feet off. He bounces on his butt and tries to peck your face, but then you CHOP off his beak. Then you STAB him in his gizzard. You have defeated the BOSS CHICKEN! You receive a CHICKEN NINJA MEDAL, but then you go to jail for using chickens that were ninjatically-modified in your restaurant.");
                break;
            // if user decides to combat BOSS CHICKEN
            case "COMBAT":
                console.log("The BOSS CHICKEN grabs your arm, but then you PLUCK off his tailfeathers. He is stunned, and you PLUCK off all of his feathers. He is so embarrassed that he runs into the CHICKEN BOILER. You have defeated the BOSS CHICKEN! You receive a CHICKEN NINJA MEDAL, but then you go to jail for using chickens that were ninjatically-modified in your restaurant.");
                break;
            // if user decides to throw potato at BOSS CHICKEN
            case "THROW":
                console.log("The demon trapped inside the potato opens up and instills POWER into the BOSS CHICKEN. The BOSS CHICKEN consumes space and time and becomes the ALL-POWERFUL NINJA CHICKEN OVERLORD OF TIME AND SPACE. He puts you in a dimension of eternal torture and insanity: chickens peck our your eyes, but your eyes continually grow back and are pecked out again.");
                break;
            // if user responds improperly to killBoss
            default:
                console.log("You have acted too slowly. The BOSS CHICKEN kills you and rampages the whole world. Chaos abounds.");
                break;
        }
    // if user does not respond properly to howToKill
    } else {
        console.log("You've wasted enough time. The chickens kill everyone in the store. You barely manage to escape the restaurant before the city is put on lockdown and the chickens reign supreme.");
    }
// if on second thought, user decides not to kill chickens
} else if(areYouSure == "NO") {
    console.log("The chickens kill everyone in the store. You barely manage to escape the restaurant before the city is put on lockdown and the chickens reign supreme.");
// if user does not respond properly to areYouSure
} else {
    console.log("I'm not sure what that means. Don't waste time fooling around. The chickens are gaining power.");
}

#7

This doesn't work as you might expect. It might be clearer if you surround it with parentheses:

if((howToKill == "CHOP") || ("THROW"))

When you evaluate "THROW" as a boolean, it returns true (just a rule you have to know). So this if statement will always pass.


#8

Thank you! I rewrote it as if(howToKill == "CHOP" || howToKill == "THROW") and it worked :smiley: