Non capisco cosa cambia dall'esercizio precedente

mi sto chiedendo il perchè, non va bene mettere:
il return ma,
invece devo inserire answer.

perchè, invece di mettere:
case 1:
devo mettere:
case ‘a’:

non mi entra il concetto, sempre che uno ci sia, nel senso è solo per questo caso specifico, cioè l’esercizio lo vuole così. o perchè c’è un motivo particolare?

function switchOfStuff(val) {
var answer = "";
// Only change code below this line
switch(val){
case 1:
return 'apple';
break;

case 2:
return 'bird';
break;

case 3:
return 'cat';
break;

default:
return 'stuff';
break;
}


// Only change code above this line
return answer;
}

switchOfStuff(1);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36.

Challenge: Adding a Default Option in Switch Statements

Link to the challenge:

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

If I understand you, either works just fine.

This:

function greekify(letter) {
  var greekLetter;
  switch (letter) {
    case 'a':
      greekLetter = 'alpha';
      break;
    case 'b':
      greekLetter = 'beta';
      break;
    default:
      greekLetter = 'Don\'t know, only learned the first two';
  }
   return greekLetter;
}

and this:

function greekify(letter) {
  switch (letter) {
    case 'a':
      return 'alpha';
    case 'b':
      return 'beta';
    default:
      return 'Don\'t know, only learned the first two';
  }
}

do the same thing. One isn’t really better than the other. I tend to prefer the second one, as long as things aren’t too complicated, but if things get weird, I might choose the first one if it adds clarity.

Note that in the second one, we don’t need break statements because the returns prevent it from continuing on in the function.

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