4. I have to celebrate you baby: my code runs correctly, but it won't pass me


#1


Sorry to clog this up with another one about #4.

Every time I run this, with the prompt inside or before the getReview function, I get the correct output, but also the error message "Oops, try again. Better use a switch statement."

To my knowledge, I do not have any typos in what I'm returning or in my switch statement's cases, and I get the same result with or without the break; blocks.

I'm getting frustrated, as I accidentally took a few weeks' break in my studying and can't remember why the function needs to have (movie) and why (title) breaks it.


var getReview = function(movie) {
    
    var title = prompt("Movie?");
    
    switch(title) {
        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();


#2

@askrahn,
The code-checker tests:
-1 is getReview a function ??
-2 is a switch used with its parameter-expression being =movie=
-3 it then tests if you use the correct text's
You will have to use the EXACT text's as mentioned in the Instructions...

— "Toy Story 2" - "Great story. Mean prospector."  (they ask)
  "Toy Story 2"   "Great Story. Mean Prospector."  <<==you use.....!!!!
— "Finding Nemo" - "Cool animation, and funny turtles."  (they ask)
  "Finding Nemo"   "Cool animation, and funny turtles."
— "The Lion King" - "Great songs."  (they ask)
  "The Lion King"   "Great Songs." <==you use !!!!

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

If you use a return statement, you will not need to use a break statement
as the return statement will cause an exit of the switch AND the function.....


#3

thanks for catching the capitalization errors (the pitfall of monospaced fonts)

I'm still getting the same result without the break;s though, which I said above.

var getReview = function() {

var title = prompt("Movie?");

switch(title) {
    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 don't know!";
}

};

getReview();


#4

in the hint they asked you to preform the switch on the 'movie' variable , but here you have used the variable 'title'. Try initializing 'title' outside the function and passing it to the the function while calling it
like this
var getReview = function(movie) {

switch(movie) {
    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 don't know!";
}

};
var title = prompt("Movie?");

getReview(title);


#5

@askrahn I've fiddled around with your program and it seems to run fine as is. However, it seems like the only way to avoid the error is to pass the argument movie into the function and also into the switch.

Maybe the checker is getting thrown by the prompt being between your function declaration and the switch. I've tried replacing your title variable with the movie variable to see if that will circumvent the error, but at least on my console it results in multiple prompts before giving the answer to whatever I put in the first time no matter how many times I refresh (I don't have this issue on another online JS console).

So if you really want to keep the prompt, I'd recommend trying that and seeing if it works for you, or alternatively placing the prompt outside of your function, which should work.

frisby


#6

Hi @alricj,

I've thought movie = prompt("Movie?"); would be more D.R.Y.; does it circumvent the point of having written function to do it this way?

frisby


#7

yeah, I ran it essentially every way but what @alricj put, so I'll try something like that.

making sure I'm understanding this:

  • It runs through the switch with no input
  • it prompts for a user input stored as title
  • title becomes movie when getReview is called at the end
  • switch uses the movie variable to determine what to return

Is that right?

I'm trying to make sure I understand what I was doing wrong, if I was doing something wrong, or if it was just that the specific checker was looking for a certain variable name.


#8

To your points:

  • Sort of; yes in that the instructions don't specify a prompt and putting one there appears to throw the lesson checker off. No in that I think the function will not check the cases without something to match against, therefore it does not run without the input (which would not run without the function call).
  • Yes, but see above point
  • It can, but unless I'm wrong I believe that is not D.R.Y. (see my response to @alricj for alternative). Is also unnecessary provided you don't prompt.
  • Yes. Let's use your original code as an example to avoid confusion and do a walkthrough. Here's what happens:

  1. getReview(); at the bottom tells your program to run function getReview.
  2. getReview prompts for atitle. User input (let's say "The Notebook") is assigned to variabletitle.
  3. case "Toy Story 2" checks whether the string title is holding, "The Notebook", matches "Toy Story 2". It does not. Move to next case.
  4. case "Finding Nemo" checks whether the string title is holding, "The Notebook", matches "Toy Story 2". It does not. Move to next case.
  5. case "The Lion King" checks whether the string title is holding, "The Notebook", matches "Toy Story 2". It does not. Move to final case.
  6. Final case is default. Program returns "I don't know!"

Does this help clarify the issue you are trying to understand?

frisby