Not sure what the issue is but it is driving me nuts!


#1

I have the following code:

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

I am getting the following error:

SyntaxError: Expected '('

Can you please point the error in my code?


#2

@sureshdev,

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 a string VALUE

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


#3

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

you forgot to put the movie inside ( )


#4

@leonhard.wettengmx.n ... thanks for the link. Using the details you have shared, I tried to create the switch statement with no luck!
@shekhtman shekhtman ... Thanks for the hint. I have put the movie in braces. However, I am getting new errors...
the output is

I don't know!
Great story. Mean prospector.

No matter what the movie is, the output is having this extra line -- which is attributed to Toy Story 2:

Great story. Mean prospector.

Please look into the code and debug the code.


#5

The answer is correct to the best of my knowledge. Check this out: http://www.w3schools.com/js/js_switch.asp
I noticed that when I was doing the lesson in Java, it asked to uncomment a piece of code and it still said there is an error. I clicked get code and it was identical to what I had and it worked. I also experienced the same problem in JavaScript. There is a glitch in their system. Try erasing everything. Log out. Restart your computer. Log back in. And rewrite the code or paste it from a saved notepad file. If it is still doesn't work, it is their fault, as the code is 100% correct, per the above link from w3schools. You are doing great. If enough people let them know they have glitches in their system, they will fix them. Let me know, please, if and when you get it working.


#6

@sureshdev,
The instructions.....
Write a function named getReview that takes in a movie name
and

returns

its review based on the information above. If given a movie name not found just return "I don't know!". Use a structure learned in an earlier lesson (NOT if/else statements) to write this function.

You are using the

console.log() Method

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

In some sections of this course, the course-checker Display's data
this you could then identify by placing to EXTRA code-line's:
console.log( "== My End ==" );
console.log( "== All that is displayed now, is the course-checker ==" );

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

function with 1 parameter using return-statement

var myFunction = function( param1 ) {
       //Begin of FUNCTION-BODY
       //myFunction =function= has 1 PARAMETER param1
       //this param1 PARAMETER is used as a -local- VARIABLE
       //throughout the FUNCTION-BODY

      return param1;

      //End of FUNCTION-BODY
      };

you have defined a myFunction function
which takes 1 parameter param1
this param1 parameter is used
as a variable throughout the FUNCTION-BODY.

If you want to call/execute this myFunction function
and this myFunction function was defined
as having 1 parameter param1
you will have to provide 1 argument
in our case a "number VALUE" 4
myFunction( 4 );

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**

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

As you are using the return-statement in your myFunction function
you will only get a return-value no-display.
You can however capture this return-value in a variable
and then use the console.log()-method to do a display.

var theResult = myFunction( 4 );
console.log( theResult );

OR directly

console.log( myFunction( 4 ) );

#7

@shekhtman Thanks for the encouragement and also, for pointing to the W3 school switch page. As per your suggestion, I cleared my system cache and restarted it, but no luck! The error message still appears!
I thank you for all the time and support you are providing me!
@leonhard.wettengmx.n Thanks for another detailed post! Now I understood and was able to complete this topic.
As instructed by you, I have written the following code, which worked like a charm!

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

#8

@sureshdev,
My suggestion was using

console.log( getReview("The Lion King") );
console.log( "== My End ==" );
console.log( "== All that is displayed now, is the course-checker ==" );

#9

I modified the code per your suggestion. I noticed with your suggestions, there is one less line of code.
Thanks for all your help!