I have to celebrate you baby, error


#1


I have to celebrate you baby


Oops, try again. Better use a switch statement

            function getReview(name){
    switch(name) {
        case "Toy Story 2":
            return "Great story. Mean prospector."

        case "Finding Nemo":
             return "Cool animation, and funny turtles." 

        case "The Lion King":
            return "Great songs."

        default: 
              return "I dont know"

        }
    };
    
    getReview("The Lion King")

Hi everyone. My code is executing fine and returns the desired string. But I get an error to use the switch statement, which I believe I am doing? So please direct me as to what the problem might be.

p.s I have tried including break AND return, also console.log but none of that seems to get rid of the error message.

Thanks


#2

All your return statements need semicolons at the end and you forgot your break statements.

When you call your function then it also needs a semicolon at the end.

Remember, a semicolon is essentially a period in Java Script and is often used. Their are only a few cases in which you don't use it.


#3

Ok so firstly, can you tell me why my function is still executing properly and returning the desired string even without the use of semicolons? I understand the importance of them, however in this case my issue is that I get an error message to "use a switch statement", but I am using one, and It IS WORKING. So why the error?

also, I made the changes you suggested:

       function getReview(name){
switch(name) {
    case "Toy Story 2":
        return "Great story. Mean prospector.";
        break;

    case "Finding Nemo":
         return "Cool animation, and funny turtles." ;
         break;

    case "The Lion King":
        return "Great songs.";
        break;

    default: 
          return "I dont know";
          break;

    }
};

getReview("The Lion King");

Still the same error...


#4

I would really appreciate if someone who has the solution to this can answer back. Thanks all.


#5

Remember that exercises won't let you pass if you misspell anything. Codecademy want's you to print out the exact strings that they give you, so you need to make sure that you have proper spelling and punctuation.

In your case then this is where you went wrong:

It should be "I don't know!". Remember that don't needs the apostrophe right before the t and your sentence needs an exclamation mark at the end so Codecademy is yelling at you.

There may be other little mistakes in the program, but just tell me if you have any more trouble :slight_smile:


#6

Hi. I still get the same error even with the correct spelling.

It may be that a bug exists in that level? Anyways, I have already moved past it because my code is executing. Also, it would be great if you or anyone can shed light on why the code executes without the use of semi colons just as well as if it were to have them?

Thanks.

Here is the code:

 function getReview(name){
switch(name) {
case "Toy Story 2":
    return "Great story. Mean prospector.";
    break;

case "Finding Nemo":
     return "Cool animation, and funny turtles." ;
     break;

case "The Lion King":
    return "Great songs.";
    break;

default: 
      return "I don't know!";
      break;

  }
};

 getReview("The Lion King");

#7

Writing code involves debugging and you should embrace it because making mistakes and correcting them helps you to become a better programmer.
This here is just an introduction into .js, the world outsides this sandbox is full of wonderful solutions and bugs :).

I don't see your prompt statement (which acquires the var that goes as an argument into switch) could you post it here.

and default doesn't need a break


#8

about semicolons, I hope this can help you:


#9

Could you please post a screen shot of the reference error and your code?


#10

The switch statement in this exercise doesn't need a prompt statement, but good spot on the break :slight_smile:


#11

Neither of you actually answered any part of the question ??

and I certainly do embrace debugging, hence why I am asking a question on this forum.
I would argue that debugging is a necessary condition for learning programming period. Not just becoming a good one.

Anyhow, I am already finished with the course. Thanks.


#13

Hi zzgooloo,
I realise you've finished the course but this article by a Codacademy moderator (Alex.J) is brilliant.
https://www.codecademy.com/blog/78

The other thing is that a string in your code requires a backslash to stop it from getting confused with your grammar (closing quotes).

So for your default statement, you should use the backslash like so:

default:
return "I don\'t know!";
break;

I stand to be corrected but I'm sure I've seen Codecademy use 'break' for their defaults?

In any event, good job and congrats on moving forward.


#14

This code works great, hope it will help you.

var getReview = function (movie) {
switch (movie) {
    case "Toy Story 2" :
        return "Great story. Mean prospector.";
        break;
    case "Finding Nemo" :
        return "Cool animation, and funny turtles.";
        break;
    case "The Lion King" :
        return "Great songs.";
        break;
    default :
        return "I don't know";
        break;
    }
};
getReview('Toy Story 2');

#15

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