Lesson 4


#1

I am getting the correct answers but when I run it, it tells me I should use a switch:

"Great story. Mean prospector."
Oops, try again. Better use a switch statement

My 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!";  
 }
    
}

getReview("Toy Story 2");

#2

After your return for the reviews take out the ;

; only after break

ex)

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


#3

Why?

Syntax for switch looks like this:

switch (grade)
            {
               case 'A': document.write("Good job<br />");
               break;
            
               case 'B': document.write("Pretty good<br />");
               break;
            
               case 'C': document.write("Passed<br />");
               break;
            
               case 'D': document.write("Not so good<br />");
               break;
            
               case 'F': document.write("Failed<br />");
               break;
            
               default:  document.write("Unknown grade<br />")
            }

source: http://www.tutorialspoint.com/javascript/javascript_switch_case.htm

As for return statements:

function myFunction(p1, p2) {
    return p1 * p2;              // The function returns the product of p1 and p2
}

source: http://www.w3schools.com/js/js_functions.asp


#4

I am getting the same error about Ooops you should use a switch and I cannot see what I have done wrong

var name = prompt("Which movie would you like to review").toUpperCase();
var getReview = function (movie) {
    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(name);

#5

Figured it out

var name = prompt("Which movie would you like to review");
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";
}
};

console.log(getReview(name));

#6

I've also hit a wall - it returns the correct answer every time, but won't pass the lesson. Any thoughts?

var ask = prompt("Which movie would you like to watch?").toLowerCase();

var getReview = function(movie) {
switch(ask) {
    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(ask);

#7

Do you know why both function and switch can't just have name in the brackets?


#8

Hi everyone,

The lesson's SCT functions in such a way that it allows the switch statement argument to be only the variable movie (function's argument) instead of name (global variable). And this makes sense. For example, if, at a later point of time in your code, you wanted to do this:

getReview("Toy Story 2");

then, your switch block should work using the string I passed (argument movie) instead of any other variable (name).

Although I agree the error message could have been more helpful.

Hope it helps! :slightly_smiling:


#9

It looks like everybody declared one variable before the code prompting the user for the name of the movie O.o I was to lazy for that and completed the exercise without it xD
But now I'm curious, was it importante to add the variable prompting the user for the name of the movie? O.o


#10

Hi @molock,

If you mean this line:

var ask = prompt("Which movie would you like to watch?").toLowerCase();

then yes it's important. How else would you then get to know what movie the user wants to watch :wink:


#11

Yes I know how it works. Variables will store data for later use and yes I know that it is a good practice to keep using variables to prompt the user for input but... I am lazy man :stuck_out_tongue: so I did like this:

I knew I could do this with the "var ask = prompt("Which movie would you like to watch?").toLowerCase(); getReview(ask)"

But with the:

"console.log(getReview("The Lion King"))"

wWas just faster xD But still... Better start declaring my variables every time :smiley:


#14

The exercises are case sensitive. You should write all strings exactly as it is in the instructions or even the right code won't pass;