Code your own adventure 2, && doesnt run properly


#1


everything runs smootly except for the if && part line 8 which logs as else.When I input "chocolate" and "yes" it logs "very tasty" instead of "a classic". What is wrong with it?Thanks in advance

var user=prompt("what is your favorite candy?").toLowerCase();

    switch(user) {
        case "cake":
         var kind =prompt("chocolate? , vanilla? strawberry?").toLowerCase();
         var top=prompt("mousse yes or no?").toLowerCase();
                if(kind==="chocolate" && top==="yes"){
            console.log("a classic!");
                }
            else {
                console.log("very tasty");
            }
                 break;
            case "muffin":
         var type=prompt("blueberry? chocolate chip? caramel?").toLowerCase();
              if (type=== "blueberry" ||type==="caramel"){
                  
                console.log("yes a good one");
              }
              else {
                  console.log("I like chocolate chip too");
              }
                  
                break;
                case"mr. sark":
                    console.log("Mine,too!!!");
                    break;
                    
                case "ice cream":
                    var taste= prompt("soft? sandwich? gelato?").toLowerCase();
                    if(taste==="gelato" ||taste==="soft") {
                        
                    
                    console.log("of course");
                    }
                    else {
                        console.log("ok..");
                    }
                    break;
                    default:
                    console.log("except that");
    };


#2

top seems to be a reserved word in js. you can see this when you insert an alert:

var top=prompt("mousse yes or no?").toLowerCase();
alert(top);

the solution is simple, use topping as your variable name


#3

Yes! Thank you! Works fine now,I am relieved it was something irrelevant with my logic..
Thank you!


#4

your logic was fine, you just stumbled on a reserved keyword, you can find all keywords here

might be useful, just in case


#5

This has happened before with "this" if I remember correctly and I figured it out by accident.Thank you for the list definetely useful. I copy pasted your code at the end and run it so what does it do exactly? does it prompt after all other prompts?like universaly if that makes sense?


#6

what? alert?

alert() is just like prompt() except it just display information to the user (without the user having to enter something)


#7

ok, thank you for your help and prompt responses.


#8

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