FAQ: Conditional Statements - Review

Ah ok!

let x = 0;

if (!x) {
  // Code Here
} else {
  // Code Here
}

In the example above, the code in the if statement would get executed, right? This is because !x = true because x is falsy

Could you please provide an example where you would use the NOT operator in a real project?

gameover = 0

while (! gameover) {
    // continue game
    // ...
    if (prompt('Quit? Y/N') === 'Y') {
        gameover = 1
    }
}
console.log('Bye')
1 Like

Thank you for your clarification!

1 Like

Hi there! I was playing around with switch statements, but the result was not what I expected. Could anyone help me out? Thanks!

let stoplight = ‘green’
let pedestrians = 1

switch(stoplight&&pedestrians)
{
case’red’&&1 :
console.log(‘Stop!’)
break
case’green’&&1 :
console.log(‘Pass!’)
break
default :
console.log(‘Out of Service!’)
break
}

I thought the output would be Pass!, but it turned out to be Stop!

So I copied and pasted your code and tested it. What happened is when you use && instead of ||, and the pedestrian is set at 1 it will just do whichever first, and in your code your Stop! code is first. So you need to use || because || checks both and if one or more is true the answer is true but if none are true the answer is false, and with && if one is false the answer is false even if there are 30 true’s along with the one single false. Hope this helps even though it’s 3 weeks later!

Hi all. Working my way through this lesson and while practicing conditional statements, I came across a TypeError when I tried creating the following if…else statement - when I use the = assignment operator. The error is corrected when I use === comparison operator.

const dogName = "K"; const dogLocation = "home"; if((dogName = "Spot") || (dogLocation = "home")) { console.log("Hi bff!"); } else { console.log("Hi pup!") };

I think I found the answer to my question when Googling these two operators - but I’m hoping someone can confirm (or deny) the accuracy of my understanding.

The = assignment operator is used to assign value, so using it within my if…else statement results in an error because I’ve already assigned the value to my variable. The === comparison operator is the correct operator for use within the if…else statement because I am attempting to compare the condition I’ve created with the value I previously assigned to the variable.

Is this correct? (Does this even make sense?)

(Edited for typo.)

Yes, this is correct.

= is the assignment operator, used to assign values to variables.

=== is the strict equality operator, used to compare if two operands are equal and of the same type.

Thank you for confirming that.

1 Like

Hi there, a quick question about using switch statements…

I’d like to use a switch statement to simplify the else if statements in this code. Any ideas? Thanks!

function Range(begin,end) { this.low = begin; this.hi = end; this.has = function(n) { return ( n >= this.low && n <= this.hi ); } } let lightWind = new Range(0,14); let beginnerWind = new Range(15,19); let awesomeWind = new Range(20,29); let pumpingWind = new Range(30,39); let windSpeedInKnots = 21; if (lightWind.has(windSpeedInKnots)) { console.log('The wind is too light for kitesurfing today'); } else if (beginnerWind.has(windSpeedInKnots)) { console.log('It\'s a perfect day for learning to kitesurf.'); } else if (awesomeWind.has(windSpeedInKnots)) { console.log('Awesome wind today! 9m kite weather!'); } else if (pumpingWind.has(windSpeedInKnots)) { console.log('It\'s pumping today!! Definitely 5 to 7m kite weather! And it\'s forecast to get stronger this afternoon!'); } else { console.log('It\'s absolutely nuking today!!! Probably best to give it a miss today!'); }

This might be a bit of a less common use of switch, but you can also match expressions - this would work:

switch (true) {
  case lightWind.has(windSpeedInKnots):
    console.log('The wind is too light for kitesurfing today');
    break;
  case beginnerWind.has(windSpeedInKnots):
    console.log('It\'s a perfect day for learning to kitesurf.');
    break;
  case awesomeWind.has(windSpeedInKnots):
    console.log('Awesome wind today! 9m kite weather!');
    break;
  case pumpingWind.has(windSpeedInKnots):
    console.log('It\'s pumping today!! Definitely 5 to 7m kite weather! And it\'s forecast to get stronger this afternoon!');
    break;
  default:
    console.log('It\'s absolutely nuking today!!! Probably best to give it a miss today!');
}