Logical operators && and ||?


#1



Im not sure why but I get this message. Oops, try again. Make sure to use at least one logical 'and' (&&)!
Am I not using the && and || right?

var user = prompt("Do you like potatos?").toLowerCase();
    switch(user){
        case 'no':
          if(user === 'no'){
            console.log("I don't like them either!");
          }else{
            console.log("why?");   
          }
            break;
        case 'yes':
          if(user === 'Yes' || user === 'yea'){
            console.log("Who are you?");
          }
            break;
        case 'sometimes':
            console.log("I like them in certain things too");
            break;
        default:
            console.log("What???");
            break;
    }


#2

Well, it doesn't look like you have used && at all? You have used || . So maybe your problem is you just need to include && somewhere


#3

This has been confusing to me. How come the && would work and not the ||. Do they not go in the same spot as the && would go, or set up the same way?


#4

Well let me give you an example from my code. I think see it used will give you an idea of how it works and what it works with.

switch(user) {
    var BRAD = BRAD
    case'BRAD':
        if(Brad&&Stew){
        console.log("That is my name too!");
        } else {
        console.log("Your name is close to my name!");}
        if (Brad||Braden){
        console.log("That is my name too!");
        } else {
        console.log("Our names are very close!");}
        break;

So basically, && is used as to say if the person types in brad and stew in the console they will get back that is my name too! but if not then you get another sentence. In the second if statement, if the person puts in brad or braden then you get that is my name too! else you get something else. As you can see you sortof use it like and and or in sentences @blogninja19450


#5

So I don't need to add the === after my var, to check if it's equal?


#6

I don't believe so because the user data doesn't have to equal that. It is just when it does equal that it needs to be noticed and thus get another sentence. Hopefully that is decent enough in explaining it... If not, I can get another help desker, super user or mod to help explain it


#7

Ok thanks. Also in this lesson do I need to add another var asking the user another question?


#8

No what you have is fine! I just went about it a slightly different way than you. :smiley: In fact, I needed to get rid of that var Brad = 'Brad' statement so don't go down that path haha


#9

Alright! But I managed to put another one in and it seemed to work anyways! Thanks!
it works both ways!


#10

Good! Glad you figured it out and hopefully the rest of your coding is less confusing! :smiley:


#11

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