Adding a Default Option

Why does the script change here? In one exercise I learn to use switch statements like this:

case 1:
return “blabla”;
break;
case 2:

And so on…

The next exercise, I followed the same pattern but it didn’t work. When I checked the answer, the format is different.

It’s become

case ‘a’: answer = ‘apple’;
break;

I don’t understand why this method is being used when adding a default option, and when I should switch between the scripting formats.

Can someone help me?


function switchOfStuff(val) {
  var answer = "";
  // Only change code below this line
  switch(val) {
    case a:
    return "apple";
    break;
    case b:
    return "bird";
    break;
    case c:
    return "cat";
    break;
    default:
    return "stuff";
    break;
  }
  
  
  // Only change code above this line  
  return answer;  
}

// Change this value to test
switchOfStuff(1);

You have a variable answer. You assign to a variable using the = operator.

So if you assign: answer starts as an empty string, then if a case matches in the switch statement, then something is assigned to answer. Then once that’s been checked, answer gets returned. It’s possible nothing matches, in which case answer just stays an empty string.

If you use return in the switch statement, if anything matches, then you return a value immediately, exiting the function straightaway. If nothing matches, answer gets returned, and it’s an empty string.

In this particular example there is no difference. Assigning a value to answer then returning that variable later on, or just returning that value straightaway. Both produce the same result.

But it’s not normally as simple as “either way works fine”: you do whatever is appropriate in the circumstance. If you want to use switch to decide which value to assign to a variable, you do that. If you want switch to decide what value to return from a function, do that.

Edit: just to clarify — you don’t need the answer variable here, it isn’t used, because you have the default. The code can’t ever hit return answer, because if the case goes through to default, that’s what gets returned.

Also, you don’t need break. break is used to break out of the switch statement. But you’re returning, the code never reaches any of the breaks

1 Like

You sound like you’re making a lot of sense but I’m just not getting it. I think I’ll continue the course and eventually this will all make sense to me. Thank you for your help though!

A switch statement can execute anything

So the basic syntax is

switch(val) {
   case 1: // if val is 1
   {execute this}
   break;
   case 2: 
   {execute this}
   break;
   ...
   default: // if val is nothing of the previous ones
   {execute this}
   break;
}

In the two cases you have two different things were I have written {execute this}, one case you are assigning a value to a variable, in the other case you are returning something.

The default case is optional, you can use it or not. It is not related to what you write instead of {execute this}

If you are returning something, (example return "Avalon") you don’t need to add break because it was already done by the return keyword

1 Like

Thank you so much, that was a really helpful explanation :grin:

1 Like