ReferenceError: Invalid left-hand side in assignment


ReferenceError: Invalid left-hand side in assignment

var user = prompt("You're walking down a forest path, with a bow on your back and coin purse hanging from your belt when a band of bandits stroll out from the bushes, surrounding you. Do you PAY, FIGHT, or RUN from the bandits?").toUpperCase();

switch(user) {
    case 'PAY':
        var money = prompt("Smart thinking, but do you actually have any money? (YES or NO)").toUpperCase();
        var dollars = prompt("Is it in the local currency, though?").toUpperCase();
            if(money === 'YES' && dollars === 'YES') {
                console.log("You throw your coin purse to the floor, the bandits spare your life, but they broke the bank on this one");
            } else {
                console.log("You get on your knees and throw whatever money you have at them, pleading for mercy, but it's not enough to stop the flurry of blades cutting across your body");
    case 'FIGHT':
        var strong = prompt("Are you strong? (YES or NO)").toUpperCase();
        var smart = prompt("Are you smart?").toUpperCase();
            if(strong === 'YES' || smart = 'YES') {
                console.log("You are a master archer, with one flick of the wrist you've already drawn and slain your first man, the second goes down before he hits the floor, and a third after that. The remaining bandits fear for their lives, running with death looming over them as your arrows fly killing every last one.");
            } else {
                console.log("You attempt to take on the bandits, but your arrows are weak and the bandits strong; they overcome you in a matter of seconds, laying on pound after pound, crushing your skull and tearing you apart.");
    case 'RUN':
        var fast = prompt("Are you fast? (YES or NO)").toUpperCase();
        var headstart = prompt("Did you get a headstart?").toUpperCase();
            if(fast === 'YES' || headstart === 'YES') {
                console.log("You're quick on your feet, and without a moments hesitation you bolt out of sight leaving behing nothing but their perplexed faces in the dust.");
            } else {
                console.log("You turn your tail and run, but failed to zig zag out of the path of a flying axe. You die.");
        console.log("Your inability to pick one of the three options tells me you failed at taking on the bandits and lost your head. Better luck next time!");


@freckals have a look at your above code. In your if statement first you're checking if the response string you received
var strong
is equal to YES , whereas in the second bit you're assigning a string 'YES' to
var smart
That's why you were getting an error as invalid assignment. You can't use an assignment operator and expect the value to be true as it just assigns the value to variable instead of a boolean result.

Not sure if it was a typo but in short you were missing an

Hope you understand what I mean.


Just some further information about the problem.

You can't use an assignment operator and expect the value to be true as
it just assigns the value to variable instead of a boolean result.

Unfortunately this works. And I say unfortunately because in most of the cases it is not intended and therefore leads to massive confusion about why something is happening this way. For example

variable = VALUE;

will as a statement have a value of VALUE as you can prove by using console.log(variable = VALUE). Now if you have a look at this:

you see that any value is treated as a boolean if used in a condition.

About the error:
If you have a look at the operator precedence:

You can see that === comes before || which comes before = so the order in which this is evaluated is:

((strong === 'YES') || smart) = 'YES'

So first the boolean value of strong === 'YES' is determined and than it is compared to smart. Which in the end leads to a boolean value of either true or false (probably true because smart is likely to be a non-empty string). Now it is tried to assign "YES" to this boolean value and that is where it ultimately fails because it is impossible to overwrite a value.

What to put as a condition

Thanks, you got it, appreciate the help and explanations


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