There are mistake in 4. I have to celebrate you baby


#1


Replace this line with your code.

Here is my code:

var revText = '';

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

getReview("Raid");
console.log(revText); 

and result in console:
end of IDK
I don't know
end of TS 2
end of TS 2
end of FN
end of FN
end of TLK
end of TLK
As you see, my switch is try all cases, and don't break. 
In codepen.io it's work correctly - [codepen](http://codepen.io/tokamame/pen/NjbeeP)


#2

Please Post your code!:slight_smile:


#3

Just posted. I'm beginner on forum. And my English is weak))


#4

No problem!
This code lets me pass But you introduced many things that will create bugs even though Its working now.
the approach you used is bug prone.
1. Unnecessary creation of revText global variable.
Tho you have added few lines for testing the code,thats fine!

one more point...
Inside a function when you use return keyword , It works same as break keyword

return revText; //it already halted here you dont need break statement
            break;

#5

Thank you, I know, that return in function work same as break. I add it only because my switch try all cases)).
How to clear console without redownload the page?


#6

When you use return ,It will act same as break so even inside switch (when switch is used inside functions) you dont need break (if u already used return)

do reply if you get it or not?

How to clear console without redownload the page?

Pardon me, I did not get it,can you elaborate it?


#7

I mean that in codepen.io there are button "Clear", that clears all text in console.


#8

Finall version of code, whithout reduntant 'break' and global variables.
Here is my code:

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

console.log(getReview('Finding Nemo'));
console.log(getReview('Toy Story 2')); 
console.log(getReview('raid'));

and answer in console:

end of FN
Cool animation, and funny turtles.
end of TS 2
Great story. Mean prospector.
end of IDK
I don't know
end of TS 2
end of TS 2
end of FN
end of FN
end of TLK
end of TLK

Totally don't understand why loop print words (end of TS2, end of TS2, end of FN....) in console after 'I don't know'.


#9

Erase them then tell what're you getting... ok ?


#10

It's ok now. I don't know, how this magic works)) Because when I rewrite this code with if/else statement, it don't enter in another's condition and don't execute code from there (don't execute 'console.log' s from all cases, only from own case.
My code is below:

var getReview = function (movie) {
    if (movie === "Toy Story 2") { 
            console.log('end of TS 2');
            return 'Great story. Mean prospector.';
    } else if (movie ==="Finding Nemo") { 
            console.log('end of FN');
            return 'Cool animation, and funny turtles.';
    } else if (movie === "The Lion King") { 
            console.log('end of TLK');
            return 'Great songs.';
    } else {
            console.log('end of IDK');
            return "I don't know";
        }
};

console.log(getReview('Finding Nemo'));


and result in console:

end of FN
Cool animation, and funny turtles.

#11

Because you when the condition satisfies...

console.log(getReview('Finding Nemo'));

When it finds the 'Finding Name'
it enters inside the code...

{
    console.log('end of FN');
    return 'Cool animation, and funny turtles.';
}

It will console(print) every statements inside it.
You can see ,these two lines gets printed as well?
Do reply if it clears your doubt or not!


#12

When i delete 'console.log's it work good.

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

console.log(getReview('Finding Nemo'));

console text

Cool animation, and funny turtles.

#13

Can you read the instruction once again :slight_smile:

After reading this instruction can you tell me the need of adding those console.log inside the case blocks?


#14

When I begin to make this exercise, I don't see about using "return", and log reviews straight to console

case "Finding Nemo":
    console.log('Looks like good movie.');

Then I see, that script execute all cases. Then I see about using "return" and do it. But script continue to go in every condition and execute console.log's from there.

Looks like I don't know something about switch loop. I think that is works like this:

  1. ok what we are looking for? - Finding Nemo
  2. first is "Toy Story 2". "Toy Story 2" isn't "Finding Nemo" (false)
  3. Go next
  4. second is "Finding Nemo". (true) I'm find it. Go in and execute.
  5. Here is "return". Looks like my work is done.

But seems it works like this:

  1. ok what we are looking for? - Finding Nemo
  2. first is "Toy Story 2". "Toy Story 2" isn't "Finding Nemo" (false)
  3. But go in and see what are there. Wow, here is console.log. Let's do it.
  4. And here is return, but "Toy Story 2" isn't "Finding Nemo", so go to next
  5. second is "Finding Nemo". Finding Nemo is Finding Nemo (true)
  6. Lets execute code inside. Go next
  7. It's "The Lion King". It's not "Finding Nemo" (false)
  8. But go in and see what are there. Wow, here is console.log. Let's do it.
  9. And here is return, but "The Lion King" isn't "Finding Nemo", so go to next.....

#15

You know about switch (as you mentioned ) not like mentioned at last
My point was...
In instructions they only mentioned to return things not console.log something.


#16

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