Why do I put movie in switch instead of getReview in I have to celebrate you Baby?


#1


Hey, so when we learned about switch before we wrote it something like this.
var thing = prompt("Question")
switch(thing)

But in this exercise they're telling me to put the function into the switch instead of the var. I'd love an explanation as I have no idea why.


var getReview = function(movie) {
    
    switch(movie)


Hope you guys understood what I'm trying to say.


#2

well, movie will contain the movie by supplying an argument at function call:

var getReview = function(movie) {
    
    switch(movie){
          // different cases
    }
}
getReview("Toy Story 2")

so now movie will contain toy story 2, if you call switch on the function (getReview) nothing will happen


#3

prompt() and switch/case are unrelated

the code you posted doesn't use the function itself as a argument for switch. It just passes the argument that is given to getReview() on to the switch statement

the switch statement evaluates a given variable (first example: thing, second example: movie)...

In your first example the prompt() function is used to set a value for the variable thing which is later evaluated in the switch statement.
The variable will have the value whatever the user writes in the prompt

In the second example the value for the variable movie is set through calling the function getReview

so whatever you pass as an argument to the getReview function is evaluated in the switch statement.

var getReview = function(movie) {
    switch(movie){
          case "Toy Story 2":
                 return "Great story. Mean prospector.";
    }
}
getReview("Toy Story 2")

in this call the argument value is "Toy Story 2" so the variable movie inside the getReview function will have the value "Toy Story 2" which will make the function return "Great story. Mean prospector."


#4

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