Fight case does not work


#1



I was coding using the troll idea that Codecademy used as an example (with my own variations) but the fight case is not working. Everything works fine except for the fight case. For some reason it always displays the victory message, "You defeated the troll and claimed the treasure across the bridge!". Someone please help me! Thank you in advance to anyone who takes the time to help.


var user = prompt("You run into a troll on a bridge, do you fight, pay or run?").toUpperCase();
switch(user) {
    case'FIGHT':
        var userStrength = prompt("Are you strong?").toUpperCase();
        var userIntelligence = prompt("Are you smart?").toUpperCase();
        if(userStrength === "YES"){
            userStrength = true;
            }else{
                userStrength = false;
                }
                if(userIntelligence = "YES"){
                    userIntelligence = true;
                    }else{
                        userIntelligence = false;
                        }
                        if(userStrength || userIntelligence){
                            console.log("You defeated the troll and claimed the treasure across the bridge!");
                            }else{
                                console.log("The troll knocked you out!");
                                }
        break;
    case'PAY':
        var userMoney = prompt("Do you have money?").toUpperCase();
        if(userMoney === "YES"){
            userMoney = true
            }else{
                userMoney = false
                }
                if(userMoney){
        var userSpending = prompt("Do you want to spend that money to cross the bridge?").toUpperCase();
                if(userSpending === "YES"){
                    userSpending = true
                    }else{
                        userSpending = false
                        }
                        if(userSpending){
                            console.log("You pay the goblin and he lets you cross the bridge. You then claim your treasure.")
                            }else{
                                console.log("You refuse to pay the goblin but he steals your money and does not let you cross the bridge anyways!")
                                }
                        }else{
                            console.log("You cannot afford to pay the goblin so you head back home.")
                            }
        break; 
    case'RUN':
        console.log("You ran all the way home!")
        break;
    default:
    console.log("Please type in only options fight, pay, and run.")
    }


#2

this line

if(userIntelligence = "YES")

you are checking the variable so you have to use == or ===
we use = for assignment.

and next this line

if(userStrength || userIntelligence)

use && operator instead of ||

check this Link


#3

Thank you so much. I didn't see that I was using only 1 =. I fixed it and now the code works. Btw on this line

if(userStrength || userIntelligence)

I meant to use the OR operator.


#4

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