Feeling mixed up about syntax of switch statements in JS


#1


https://www.codecademy.com/en/courses/learn-javascript/lessons/control-flow/exercises/switch

It told me to do these things. I am utterly stuck:
moonPhase will become the condition of the switch statement. Then, each moon phase will become each case that the switch statement checks for.

Start by writing a switch statement with moonPhase as its condition.

Then, write each else if condition as a case.

If moonPhase is 'full', then use console.log to print 'Howwwwlll!!'.

If moonPhase is 'mostly full', then use console.log to print 'Arms and legs are getting hairier'.

If moonPhase is 'mostly new', then use console.log to print 'Back on two feet'.

Remember to add a break after each console.log, like in the example in the instructions.

Now, add a default at the end of the switch that uses console.log to print 'Invalid moon phase', in the case that moonPhase does not equal one of our cases.


var moonPhase = 'full';
switch (moonPhase)
{
    ...
    default:
    // run this code if no case is true
    break;
    
}

if (moonPhase === 'full' || foggyNight) {
  console.log('Howwwlll!!');
} case (moonPhase === 'mostly full') {
  console.log('Arms and legs are getting hairier.');
} case (moonPhase === 'mostly new') {
  console.log('Back on two feet');
} else {
  console.log('Invalid moon phase');
}


#2

Consider an if..else if..else statement:

if (a) { }
else if (b) {}
else if (c) {}
else if (d) {}
else {}

In the above, a, b, c & d are conditions or states. When these start to get numerous, this statement can become quite messy and difficult to read. That is where the switch statement comes in.

switch (state) {
case a: break;
case b: break;
case c: break;
case d: break;
default: //
}

These are simplistic examples only, just to demonstrate the difference in their structure, while also showing the similarities.

Notice that the if statement does not have case as part of the structure? Just as switch does not have else or else if as part of its syntax.

Now let's set this up for moonphase...

switch (moonPhase) {    // only the variable, not a condition
case "full": break;
case "mostly full": break;

default: console.log('Invalid moon phase');
}

Add in the remaining cases. Write the action code after the colon and before the break;. Be sure to separate the action statement from break with a semi-colon.

The default case is always last, and never needs a break.
}


#3

This was awesome help, but I seem to be having trouble with the formatting. I also need to put in console.logs. Could you please give an example with those in it?
var moonPhase = 'full';
switch (moonPhase)
{
case (moonPhase === 'full' || foggyNight)
console.log('Howwwlll!!');
break;
case (moonPhase === 'mostly full')
console.log('Arms and legs are getting hairier.');
break;
case (moonPhase === 'mostly new');
console.log('Back on two feet');
break;
default:
console.log('Invalid moon phase');
break;
}
}

On that first console log there are two errors saying Expected ':' and instead saw console. And Missing ':' on a case claus.


#4

The case comparison is of value only, corresponding to the switch parameter.

var a = "full";

switch (a) {
    case "full":
        console.log("Full Moon Phase");
    case "new":
        console.log("New Moon Phase");
    default:
        console.log("Moon is showing some of its face");
}

This is like asking,

if (a === "full") {

}

and so on...


#5

Sort of off topic, but inspired by this topic...

n = 5;
b = 10;
a = -5;

switch (a) {
    case n + b: y = 1; break;
    case n - b: y = -1; break;
    default: y = 0;
}

In my effort to come up with a simple way to demonstrate the expression vs value and value vs expression relationship within the switch statement I seem to have stumbled upon some interesting discrete mathematics. Purely by accident, not intent, so pay it no mind and just learn from the example the simple things.

Run the sample, then change a to 15. Run it again. Now change a to anything but -5 or 15. Run. See the ramifications? There could be some math implications here. If you want to run with this, be my guest. I'd love to know where such matching would be useful.


#6

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