4/6: Help! What's wrong with my code?


#1

Could someone please tell me what's wrong with my code??? It should work. I've gone through it over and over again. Please tell me!!!!

var user = prompt("What's your name?").toLowerCase();
var hole = prompt("You see a big hole in the ground. Someone calls '" + user + "! Help! Help!' What do you do? Do you RESCUE, ASK, or IGNORE?").toLowerCase();
switch(hole) {
    case 'idk':
    case 'i dont know':
    case 'i don\'t know'    
        console.log("YOU LITTLE...");
        break;
        case 'rescue':
        var rope = prompt("Do you have a rope or a ladder?", "Type 'Yes' or 'No'.").toLowerCase();
        if(rope = "yes"){
         console.log("You save him, and he gives you 1000 GOLD in thanks!!!! YES!!");   
        } else {
        console.log("While trying to save him, you fall into the hole and die. Sorry.");
    break;
    case 'ask':
    var ask = prompt("Do you ask someone else to save him, or do you ask what he needs help with?", "Type 'ask someone else' or 'ask what he needs help with'.").toLowerCase();
    if(ask = "ask someone else"){
     console.log("There is noone around, and when you go looking for someone, you forget where the hole is.");   
    } else {
     console.log("He says, 'Bacon'.");
     var bacon = prompt("Do you have any bacon?").toLowerCase();
     if(bacon = "yes"){
      console.log("You throw your bacon into the hole, he says 'thank you', and you walk away. Hm.");   
     } else {
      console.log("When you tell him you have no bacon, he screams extremely loud and then stops. He then says, 'Golden butter'. How strange.");
     }
    }
    break;
    case 'ignore':
    console.log("You walk away, hearing him yell after you, 'NO! NO! PLEASE! " + user.toUpperCase(); + "!!! COME BACK....!!' Pity.");
    break;
    
 
 default:
 console.log("I'm sorry, I don't know what that is. Do you RESCUE, ASK, or IGNORE?");
}

#2

You missing a colon on this case :


#3

This should be if : >>>>(rope === "yes"){

And as @zainabrawat said you are indeed missing the colon in that case

Because if you say just = you are setting that variable to be what you want to check, and not checking what you want to check.

Also something on line 16 is wrong but I can´t seem to figure it out, but it errors there after fixing those things.


#4

Thank you so much for the help!!


#5

But it still isn't working. Here is my code now:

var user = prompt("What's your name?").toLowerCase();
var hole = prompt("You see a big hole in the ground. Someone calls '" + user + "! Help! Help!' What do you do? Do you RESCUE, ASK, or IGNORE?").toLowerCase();
switch(hole) {
    case 'idk':
    case 'i dont know':
    case 'i don\'t know':    
        console.log("YOU LITTLE...");
        break;
        case 'rescue':
        var rope = prompt("Do you have a rope or a ladder?", "Type 'Yes' or 'No'.").toLowerCase();
        if(rope === "yes"){
         console.log("You save him, and he gives you 1000 GOLD in thanks!!!! YES!!");   
        } else {
        console.log("While trying to save him, you fall into the hole and die. Sorry.");
    break;
    case 'ask':
    var ask = prompt("Do you ask someone else to save him, or do you ask what he needs help with?", "Type 'ask someone else' or 'ask what he needs help with'.").toLowerCase();
    if(ask === "ask someone else"){
     console.log("There is noone around, and when you go looking for someone, you forget where the hole is.");   
    } else {
     console.log("He says, 'Bacon'.");
     var bacon = prompt("Do you have any bacon?").toLowerCase();
     if(bacon === "yes"){
      console.log("You throw your bacon into the hole, he says 'thank you', and you walk away. Hm.");   
     } else {
      console.log("When you tell him you have no bacon, he screams extremely loud and then stops. He then says, 'Golden butter'. How strange.");
     }
    }
    break;
    case 'ignore':
    console.log("You walk away, hearing him yell after you, 'NO! NO! PLEASE! " + user.toUpperCase(); + "!!! COME BACK....!!' Pity.");
    break;
    
 
 default:
 console.log("I'm sorry, I don't know what that is. Do you RESCUE, ASK, or IGNORE?");
}

#6

these two cases the format is incorrect

some of your if/else statements are incorrect

remember the format:

//case:
 case 'name':
        //do something
        break;

//if:
if(condition) {
//run something
}

//else
else{
//run something else
}

It would be best if you reset lesson and start again


#7

Hi this part

if(rope === "yes"){
         console.log("You save him, and he gives you 1000 GOLD in thanks!!!! YES!!");   
        } else {
        console.log("While trying to save him, you fall into the hole and die. Sorry.");
    break;

you didn't close the else statement }
then here

 case 'ignore':
    console.log("You walk away, hearing him yell after you, 'NO! NO! PLEASE! " + user.toUpperCase(); + "!!! COME BACK....!!' Pity.");
    break;

you should remove the ; after user.toUpperCase()


#8

Thank you!! And zainabrawat, you can do multiple case'name': and it will mean [if it is {case}, {case}, or {case}, do this]


#9

I'm sorry I don't understand you


#10

Like this:

case 'one':
case 'two':
case 'three':
    //This code will run if any case is matched.
    break;