5.Logical operator, please help


#1

var user = prompt("What's your favorite color?").toLowerCase()

switch(user) {
case 'yellow':
console.log("yellow");
break;
case 'red':
console.log("red");
break;
var yellow = true;
var red = true;

  if(yellow&&red) ;{
      console.log("yellow and red are my favorite color");
  }else {
      console.log("blue is my favorite color");
  }
  if(yellow||red);{
      console.log("yellow or red are my favorite color");
  } else {
      console.log("I like blue");
  }

  break;

case 'blue':
console.log("blue");
break;
default:
console.log("I dont have a favorite color");
};


#2

@pascac,
With a google search
javascript switch site:developer.mozilla.org
have a read
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch
AND
javascript logical operator site:developer.mozilla.org
have a read
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators


#3

What kind of error message are you getting? One thing is that you don't need a semicolon after the "if" condition. Here's what I mean (I'll only use the code of yours which is wrong):

if(yellow&&red) ;<-----YOU DO NOT NEED THIS SEMICOLON{
console.log("yellow and red are my favorite color");
}else {
console.log("blue is my favorite color");
}
if(yellow||red);<------ OR THIS ONE{
console.log("yellow or red are my favorite color");
} else {
console.log("I like blue");
}

break;

Try it out and let us know what happens! All you need to do is to remove those semicolons.


#4

@marleemaloy,
And he could remove the code

var yellow = true;
 var red = true;
  if(yellow&&red) ;{
      console.log("yellow and red are my favorite color");
  }else {
      console.log("blue is my favorite color");
  }
  if(yellow||red);{
      console.log("yellow or red are my favorite color");
  } else {
      console.log("I like blue");
  }

  break;

as this code will NEVER be executed
as it is prepended by the break-statement.....


#5

@leonhard.wettengmx.n

Hmm, this lesson is proving to be very confusing to me as well. Here is the syntax provided in the lesson:

switch(month) {

case 0:

    if(day == 1) {
        console.log("Happy New Year");
    }
    else {
        console.log("Happy January");
    }

    break;

/* ... */

}

There's a break statement at the bottom enclosed within the switch. From what I can tell, the break you pointed out is also enclosed within the switch. Could you explain? I haven't gotten past this lesson yet, I just noticed the semicolons out of place and spoke up!! :slightly_smiling:


#6

@marleemaloy,
Apart from his code being a total confusion

  • missing case clauses ????
  • usage of confused logical operators in the IF conditions
  • multiple break-statements in the same code-block

========================================

In
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch
[quote]

switch (parameter-expression) {
  case value1:
    //Statements executed 
    //      when the VALUE of expression matches value1
    [break;]
  case value2:
     //Statements executed 
    //      when the VALUE of expression matches value2
    [break;]
 ...
  case valueN:
    //Statements executed 
    //      when the VALUE of expression matches valueN
    [break;]
  default:
    //Statements executed 
    //      when none of the VALUE's match the VALUE of the expression
    [break;]
}

= = = = = = = = = = = = = = = = = = = = = = = = = = = =

The switch statement evaluates an parameter-expression,
matching the parameter-expression's VALUE to a case clause,
and executes statements associated with that case.
( and with the break or return-statement will exit the switch statement )
[end-quote]

= = = = = = = = = = = = = = = = = = = = = = = = = = = =

In your case clause you are using an expression
An expression is any valid unit of code that resolves to a VALUE.

Conceptually, there are two types of expressions:
-1 those that assign a value to a variable
and
2- those that simply resolve to a VALUE.

So in your case clause you are using the 2nd kind of expression
and you do that by
using the remainder operator on numbers[x]
which will result in a number VALUE

Thus your case clause VALUE
is matched against the VALUE of the parameter-expression

So if use a number VALUE of 0 as
being the parameter-expression's VALUE
would get the -result- we want:

With
var numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
you created a numbers Array.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

To get the 1st Array-Element of the numbers Array
you use
numbers[0]

and to get all Elements with a for-loop you will have to start
at Element-zero
like

for (var x = 0; x < numbers.length ; x++ ) {
   console.log( number[x] );
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for

for (var x=0; x < numbers.length; x++) { 
 switch(0) {
    case (numbers[x] % 3 && numbers % 5):
        console.log("FizzBuzz");
        break;
    case ( numbers[x] % 3):
        console.log("Fizz");
        break;
    case (numbers[x] % 5):
        console.log("Buzz");
        break;
    default:
       console.log( numbers[x] );
 } 
// console.log(numbers[x]);
 }

=OR=

fizzbuzz with a switch-statement
for(i=1;i<21;i++){
  switch( true ) {
    
    case (i%3===0 && i%5===0):
         console.log("FizzBuzz");
         break;   //<== exit from the swtich, return into for-loop
    case (i%3===0):
         console.log("Fizz");
         break;   //<== exit from the swtich, return into for-loop
    case (i%5===0):
         console.log("Buzz");
         break;   //<== exit from the swtich, return into for-loop
    default:
         console.log(i);
         //automatic    exit from the swtich, return into for-loop
  }
}