Question about an exercise ( 1 part of it )


#1

So right now i finished this part but i still dont understand why its done like that. My question is probably stupid but i want to make sure it gets stuck in my head.

So after i got the first part of the question with this:

var moonPhase = 'full';

switch (moonPhase) {
case (moonPhase === 'full'):
console.log('Howwwlll!!');
break;
case (moonPhase === 'mostly full'):
console.log('Arms and leg are getting hairier');
break;
case (moonPhase === 'mostly new'):
console.log('Back on two feet');
break;
default:
console.log('Invalid moon phase');
break;
}

The 2nd part ( where i didnt understand ) where it asks this:

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'.

So i assumed it would be ( just showing a small part of the code):

var moonPhase = 'full';

switch (moonPhase) {
case ('full'):
console.log('Howwwlll!!');
break;

but it was wrong and it took me awhile and the only mistake was the brackets around 'full'. Now am asking what difference does it make ? As they said to write each else if condition as a case where the brackets are required ( as understood from previous exercises ).

thank you!


#2

We do not need to parenthesize the case expression.

case "full":

is ample, and easier to read.

I should add, it also doesn't make case look like a function.


#3

thanks! i thought it would be the same since they mentioned else if there. good to know :slight_smile:


#4

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