Code Your Adventure 2: Logical Operators


#1

This is my code which was deemed correct:

var user = prompt("Is your favorite band 'the beatles,' 'the who,' or 'Nirvana?'").toLowerCase();

switch(user) {
case "the beatles":
var pennylane = prompt("is your favorite song 'Penny Lane' (yes or no)?").toLowerCase();
var walrus = prompt("is your favorite song 'I am the Walrus' (yes or no)?").toLowerCase();
if(pennylane === "yes" && walrus === "yes") {console.log("Great choices. You have excellent tastes in songs."); }
else if(pennylane === "yes" || walrus === "yes") {console.log("All you need is one!"); }
else { console.log("What? You don't like either of these? BOOOOOOO to you."); }
break;
case "the who":
console.log("They rock!");
break;
case "nirvana":
console.log("This group saved the world from the pop crap of the 1980s"); }
break;
default:
console.log("The answer you have chosen is not recognized.");
}

Question 1: instead of stating "yes" twice in the && section or "yes" twice in the || section, why couldn't I just say "if((pennylane && walrus) === "yes"), for example? I tried that however, and it didn't work out right.

Question 2: we were taught when using variables that the first word should be lowercase and the second should be uppercase (this is a common practice rather than a required rule, obviously). However, when using .toLowerCase(); , it turns the second word of the variable (which I originally wanted to be "pennyLane"--note the capital "L") to lower case as well. That is why I named that variable "pennylane," instead. Is there any way around that? Obviously, it's not a huge deal, but it threw me for a loop (JavaScript pun, sorry) initially when I had the "L" capitalized in the variable as I wasn't sure what I was doing wrong.


#2

It's 2 am for me, so gonna be blunt hehe, but I'll answer what i can.

toLowerCase does not affect a variable name. Variables are a different kind of data treated by js. For example there is a function that coments a string to the raw text data that a variable exists as. toLowerCase works on the returned data, for example it won't lowercase the string in a prompt, just the user return. Variables exist in different states depending on how you use them. You can really consider thme an object, which store a value, and have methods, which are natively processed by JS. Calling a variable let will just return a value.

var variable = function (val) {
    return val;
}

However it's also an array if it holds a string, like if you call length on a string, this visual makes sense.

var variable = ["H","i","!"];

This is to just understand what raw text is and how it's handled by the browser.

Yes, it's important to adhere to Web lang practices, though don't only do what you're told, just be consistent. I learnt recently that case pattern is known as pascal case. Extra tid bit of info there.

Earlier I stated the function wouldn't redefine the variables name, only the returned string. To fix your concern, thereally are other functions beside toLowerCase which handle strings differently. Www.w3schools has a Javascript string function reference list.

To understand why grouping a logic, which makes sense to us, doesnt work in js, we gotta understand what its actually doing, not what it means. I can't say what a string returns but grouping integers will return the result of whatever you are doing inside it before moving on to the condition. Remember PEMDAS. It could be simply an empty condition is ce you're basically just saying this and this with no comparison. You could be comparing true/1 Inoted of a string.


#3

Hey guys, thanks for your help I was a bit stuck on what they want it from me to do it :smiley:
Both posts helped me a lot.


#4

i have tried your code ,it didn't work


Uknown Error?
#5

I have no idea what I am supposed to do


#6

This is my code:

var user = prompt("What's your name?").toUpperCase();

switch(user) {
case 'Buster':
console.log("Hey, brother!");
break;
case 'GOB':
console.log("I've made a huge mistake.");
break;
case 'Steve':
console.log("Steve Holt!");
break;
default:
console.log("I don't know you!");}


#7

what lesson are you on?


#8

you wrote the names in upper case so you have to make them in an upper case in the prompt