What is wrong with my code : 7. switch Statements


#1

var moonPhase = ‘mostly full’
switch(moonPhase){case’full’:console.log(‘Howwwwlll!!’);break; case’mostly full’:console.log(‘Arms and legs are getting hairier’);break; case’mostly new’:console.log(‘Back on two feet’);break: console.log(‘Invalid moon phase’);break}


#2

That needs to be the default case:

    default: console.log(‘Invalid moon phase’);
    break;
  }

#3

Let’s start with how you layout code. You want to make it as clear and readable as possible. Formatting matters, as it directly impacts how easy it is to read your code (for you and anyone else who reads it).

Let’s reformat your code as is. I added a few comments to point out the problem areas:

var moonPhase = 'mostly full' // <- 1. Missing the semicolon

switch (moonPhase) {
    case'full':
        console.log('Howwwwlll!!'); // <- 2. problem with string literal
        break;
    case'mostly full':
        console.log('Arms and legs are getting hairier');
        break;
    case'mostly new':
        console.log('Back on two feet');
        break // <- 1. Missing the semicolon
    : // 3. what's missing here?
        console.log('Invalid moon phase');
        break
}

Problems:

  1. Missing semicolons at the end of a couple of lines of code.
  2. Check instruction 2. Notice that when the moon phase is 'full', you should be printing out 'Howl! and not Howwwwlll!!.

If moonPhase is 'full', then use console.log() to print Howl!

  1. Look at the last switch level. You have just a colon and nothing else. What’s missing? Look at Instruction 3:

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.


#4

Thank you @mtf finally it worked


#5

Thank you @hellofromtonya you are heaven sent!


#6

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