In Switch case -what is valid?

// Tell us what’s happening:
//This passes the test in freeCodecamp course but I get an error for the case if I enter a //number. ie. switchOfStuff(3);

//Your code so far

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

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/adding-a-default-option-in-switch-statements

I’m not receiving an error message when I copy and paste the code into the editor.

I do think you’re confused about how the logical OR operator (||) works. case "a" || 1 doesn’t mean that either “a” or 1 satisfy the case.

MDN web docs

Logical OR ( || ) expr1 || expr2 If expr1 can be converted to true , returns expr1 ; else, returns expr2 .

The value of "a" || 1 will always be “a” according to how || works in JavaScript.

you can’t do this, if you want to match or a or 1 you need to do

case "a":
case 1:
   return "apple";

(you also don’t need break as the return statement exit from the function directly - if you do not have a return statement then you need break)

1 Like

Details. Thanks for clearing this up.