5. logical operators help!


#1


I'm not too sure what I've done wrong in my code. Can anyone help please? Been looking at this for a while now and I still don't know what I've missed!


var user = prompt("A ghost is chasing after you, do you run away, face the ghost or become friends?").toUpperCase();
switch(user) {
    case 'Run away':
        var run away = prompt("Are you a fast runner?" YES or NO)").toUpperCase();
        if (run away === 'YES') {
        console.log("I would run too!");
        } else {
            console.log("It's going to get you");
        };
        break;
    case 'Face the ghost':
        var face the ghost = prompt("Will you be brave or cry?" BRAVE or CRY)").toUpperCase();
        var brave = prompt("Scare the ghost back");
        var cry = prompt("Nobody likes a big baby");
        if (brave === 'YES' || cry === 'NO') {
        console.log("You're really brave");
        } else {
        console.log("At least you tried");
        break;
    case 'Become friends':
        var become friends = prompt("Are you sure?" YES or NO)").toUpperCase();
        if (becomes friends === 'YES' && sure === 'YES') {
        console.log("You can scare people together");
        } else {
            console.log("It wasn't meant to be");
        }
        break;
        default:
        console.log("I don't believe in ghosts");
}


#2

Take a look at this line:

var run away = prompt("Are you a fast runner?" YES or NO)").toUpperCase();

Variable names can not include the space. Use runAway or run_away. And when you use " around the string you should not use the same quotation marks inside the string, it will terminate the string. So it should be:

var runAway = prompt("Are you a fast runner?' YES or NO)").toUpperCase(););

But I don't really know why you want to use quotation mark in this place :slight_smile:


These problems occur in the later parts of the code, you have to fix them all.


else in the case 'Face the ghost': is not closed, you have to close it with }.


#3

I would have recommend you check What you'll be building. That said, there were many syntax errors and your naming conventions are wrong.

You don't name a variable as:

face the ghost for instance, you either do *face_the_ghost or something else that ,makes the computer see it as a single word.

Also, you had inconsistencies.

Please see below for corrected. :slight_smile:

var user = prompt("A ghost is chasing after you, do you run away, face the ghost or become friends?").toUpperCase();
switch(user) {
    case 'Run away':
        var run_away = prompt("Are you a fast runner? YES or NO").toUpperCase();
        if (run_away === 'YES') {
        console.log("I would run too!");
        } else {
            console.log("It's going to get you");
        }
        break;
    case 'Face the ghost':
        var face_the_ghost = prompt("Will you be brave or cry? BRAVE or CRY").toUpperCase();
        var brave = prompt("Scare the ghost back");
        var cry = prompt("Nobody likes a big baby");
        if (brave === 'YES' || cry === 'NO') {
        console.log("You're really brave");
        } else {
        console.log("At least you tried");
        }
        break;
    case 'Become_friends':
        var become_friends = prompt("Are you sure? YES or NO").toUpperCase();
        if (become_friends === 'YES' && sure === 'YES') {
        console.log("You can scare people together");
        } else {
            console.log("It wasn't meant to be");
        }
        break;
        default:
        console.log("I don't believe in ghosts");
}

#4

Just going to add my 2 cents here. You have the user prompt set toUpperCase(). You have to then capitalize all of cases inside your switch statement or none of your code will execute in response to the prompt.


#5

Thank you all so much! My code works now! :grin:

I'm always making silly errors in JS. I appreciate your help!


#6

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