Hi guys!! I wanted to give you a resource that I found really useful when learning C++ and also works just the same is JS. Just for you to make more sense of these logical operators:
All of the above is valid, but we should always be looking for ways to make the logic as unconvoluted as possible. Be sure that precedence is well understood, as well. We should also be aware of short-circuiting.
At line 3 , tirednessLevel murst be equal to 6 in order for your program to work;
Also, line 2 changes the vaiable type to boolean from string and the === sign does not do type coercion .
The not operator ! simply toggles a boolean to it’s opposite.
console.log(!true); //prints false
console.log(!false); //prints true
//here we assign a variable to the value of an evaluated expression:
const myBool = 15 > 50; //15 is not greater than 50, so myBool will be assigned to false
console.log(myBool); //prints false
console.log(!myBool); //prints true
//we can apply the ! operator at the time of assignment as well:
const myNewBool = !(15 > 50); //the expression inside the parenthesis evaluates to false, and the ! operator toggles it to true
console.log(myNewBool); //prints true
console.log(!myNewBool); //prints false
Kind of, yes. The opposite of true is false. They are the only two values in the Boolean data type set. NOT is a boolean operator that negates, or toggles the value it is paired up with (operand). When paired with true, the result is false. When paired with false the result is true.
It doesn’t end there, though, just the ‘opposite’, it begins there. NOT can be paired up with any value or expression. This time it negates the boolean evaluation of the expression. To make this evaluation the truthiness is assessed.
value truthiness (truth value)
0 falsy
"" falsy
null falsy
undefined falsy
NaN falsy
All of these values can be negated, that is toggled to true.
! 0 => true
Note that the toggled outcome is always a boolean.
The NOT operator takes only one operand and has higher precedence that many operators so is often the first expression to be evaluated. That is why above we use grouping to allow the expression in brackets to be evaluated first, then NOT is applied to the outcome.
In the instructions for this exercise I am still getting a Red X and in the Main.js screen I am getting " Did you log 'time to sleep' ? Not a big deal I think because I can move forward anyway… just wondering why I can’t get the green check mark? I am getting the correct log to the console. Below is the code I have…
mft… you are very quick to reply… thanks for that. Just so I understand: I guess the instructors want us to type exactly what they have written?? In playing around with the exercise, I had originally typed “Go to bed!” instead of “Time for Sleep”… and “Not tired yet” for “Not time for bed”. I wasn’t really watching for the green check mark and my console was printing properly… depending on how I changed the values in the variables or which Logical Operator I used. It was only later that I noticed the red X and that I couldn’t move forward. In the end, text that is logged to the console is at the discretion of the programmer… correct???
Just trying a new tact, this is only my sixth day in codecademy-- or working with code for that matter. Thought I would share how I am trying to learn, incase others are having a hard time too-- hope it will help someone… or me with a helpful reply. lol
let mood = ‘sleepy’;
let tirednessLevel = 6;
if (mood === ‘sleepy’ || tirednessLevel > 8) {
console.log(‘time to sleep’)
}else {
console.log(‘not bed time yet’)
}
let thirtyRdMag = ‘check out’;
let california = 1;// (1)not appoved for high cap mags;
let texas = 1;// (1)not appoved for high cap mags;
let florida = 2;// (2)aproved state for high cap mags;
if (thirtyRdMag === ‘check out’ && california === 2){
console.log(‘proceed to shipping’)
}else {
console.log(‘does not ship to state!’)
};
if (thirtyRdMag === ‘check out’ && texas === 2){
console.log(‘proceed to shipping’)
}else {
console.log(‘does not ship to state!’)
};
if (thirtyRdMag === ‘check out’ && florida === 2){
console.log(‘proceed to shipping’)
}else {
console.log(‘does not ship to state!’)
};
time to sleep
does not ship to state!
does not ship to state!
proceed to shipping
For || and && is the left or right side evaluated first? And is the second condition even evaluated if the first one was already true for ||, or false for &&?