Code your own adventure


#1



I cant seem to get || and && working. can someone please advise me. Thank you


var user = prompt ("You are surrounded by walkers. Do you run, fight, or give up?").toUpperCase();
switch (user){
    case 'RUN':
        var fast = prompt("Coward!, are you fast?") .toUpperCase();
        if (fast == "YES") {
        console.log ("You got away!");
        } 
        else {
        console.log ("You got away but fell down and broke your arm and was eaten by walkers.").toUpperCase();
        }
        break;
    case 'FIGHT':        
        var weapons = prompt("do you have any wepons?").toUpperCase();
        var ammo = prompt("do you have ammo?").toUpperCase();
        if (weapons == "YES" || ammo == "YES") {
        console.log ("You beat the walkers!");
        }
        else if (weapons == "YES" && ammo == "NO"){
        console.log("sucks for you");
        }
        else {
        console.log ("You were bit");
        }
        break;
    case 'GIVE UP':
        console.log ("you got bit and are now a walker. You dog Angel is too.")
        break;
    default:
    console.log ("Nice try");
};


#2

Here is your code

case 'FIGHT':
var weapons = prompt("do you have any wepons?").toUpperCase();
var ammo = prompt("do you have ammo?").toUpperCase();
if (weapons == "YES" || ammo == "YES") {
console.log ("You beat the walkers!");
}
else if (weapons == "YES" && ammo == "NO"){
console.log("sucks for you");

now see your code you wrote

if (weapons == "YES" || ammo == "YES") {
console.log ("You beat the walkers!");
}

The OR operator (||) returns true if one or both expressions are true, otherwise it returns false
so weapons == "YES" means it always consoles You beat the walkers!
it doesn't matter if you write

else if (weapons == "YES" && ammo == "NO"){
console.log("sucks for you");

so if you want the result according to your expectation,then you should use && instead of ||
like this

if (weapons == "YES" && ammo == "YES") {
console.log ("You beat the walkers!");
}

hope this'll help you :slight_smile:


#3

I really appreciate the help! Makes total sense to me now. Thank you so much!