5. Logical Operators - getting "ReferenceError: invalid assignment left-hand side"


#1



I'm not sure why but I am getting getting "ReferenceError: invalid assignment left-hand side" when I run this code. Is there something wrong with my logical expressions?


var user = prompt("You encounter a troll. What do you want to do?(FIGHT, RUN, PAY)").toUpperCase();
var userStats = prompt("Are you STRONG, FAST, or WEALTHY?").toUpperCase();
var userStats2 = prompt("What items do you have? (SWORD, CAR, MONEY)").toUpperCase();

switch(user) {
    case 'FIGHT':
        console.log("You decide to fight!");
        if (userStats = "STRONG" && userStats2 = "SWORD") {
            console.log("You beat the troll!");
        }
        else {
            console.log("The troll defeats you!");
        }
        break;
    case 'RUN':
        console.log("You decide to run!");
        if (userStats = "FAST" && userStats2 = "CAR") {
            console.log("You run away from the troll!");
        }
        else {
            console.log("The troll catches and defeats you!");
        }
        break;
    case 'PAY':
        console.log("You decide to bribe the troll!");
        if (userStats = "WEALTHY" && userStats2 = "MONEY") {
            console.log("You succesfully bribe the troll!");
        }
        else {
            console.log("You don't have enough money to bribe the troll!");
        }
        break;
    default:
        console.log("Command not recognized!");
}


#2

a = b in Javascript assigns a value to a variable, a === b compares two different variables.


#3

That did it! Also, a great explanation that helped me to better understand assignment and comparison. Thank you!


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.