Adding a Default Option in Switch Statements 2019

Tell us what’s happening:

I am very confused as to what is wrong with this code I wrote. secondly, I looked at the solution and I do not understand why switchOfStuff is used.

Your code so far


function switchOfStuff(val) {
  var answer = "";
  // Only change code below this line
switchOfStuff(val) {
  case "a": 'apple';  
  break;
  case "b": 'bird';
  break;
  case "c": 'cat';
  break;
    

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

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 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

The code in the bottom switchOfStuff(1); activates the code. By adding () after a functionname you’re activating (“invoking”) the function you want to run. Whatever value gets inputted in the () is called the parameter. In this case 1.

But the switch statement is written incorrectly. (see example code)

  1. First you need to say you want to use the switch method by replacing switchOfStuff() with switch()
  2. Tell system what value you want to run the switch on. Example uses num, but in the top of the code “function switchOfStuff(val)” num has a different name…
  3. The switch is used to assign values to the variable “answer”. = is used to assign values to variables. Example case “a”: answer = “apple”
  4. With default the system needs to know what to do if val isn’t in the switch. default: answer = “”

Hope this helps!

Why do I have to put answer =

Because that’s how you assign variables in JavaScript, a = 1, b = 2, answer = 'apple'. You have a variable answer. Depending on some input to the function, a value gets assigned to it, eg if the input it ‘a’ it gets assigned ‘apple’