Question About Switch Statements and Case Clauses

Hello
I eventually got the correct answer. But I have a question: why isn’t the first case clause 0? I initially got the task wrong because I put down case 0 instead of case 1 for the first clause. I appreciate any help you guys can offer.

Your code so far


function caseInSwitch(val) {
  var answer = "";
  // Only change code below this line
  switch(val) {
  case 1:
  return "alpha";
  break;
  case 2:
  return "beta";
  break;
  case 3:
  return "gamma";
  break;
  case 4:
  return "delta";
  break;
  }
  
  // Only change code above this line  
  return answer;  
}

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

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 11895.118.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.159 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/selecting-from-many-options-with-switch-statements

The Switch statement evaluate an expression and match it to a case.

It’s not really a matter of case “order”; so it’s not gonna me 0, 1, 2 ... N; but whatever cases you think your expression will be:

switch (expr) {
  case 'Oranges':
    console.log('Oranges are $0.59 a pound.');
    break;
  case 'Mangoes':
  case 'Papayas':
    console.log('Mangoes and papayas are $2.79 a pound.');
    // expected output: "Mangoes and papayas are $2.79 a pound."
    break;
  default:
    console.log('Sorry, we are out of ' + expr + '.');
}

For example cases can be strings :smile:

expr = 'Bananas'
// Sorry, we are out of Bananas

Hope this helps :+1:

Your explanation did help. Thanks, Marmiz!