freeCodeCamp Challenge Guide: Adding a default option in Switch statements

freeCodeCamp Challenge Guide: Adding a default option in Switch statements
0

#1

For the life of me I cannot figure out why my code isn’t working.


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

// Change this value to test
switchOfStuff("a");


I get the green on the default statement that returns “stuff” but the first 3 cases are not working.

Any help would be appreciated.


#2

when others run across this -

make sure any alpha characters (a,b,c,d,…) used in your case are surrounded by " ".

even in the TEST -
switchOfStuff(“a,b,c,d,…”);

does not apply to NUMBERS


#3

Here’s a complete working code.

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

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

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


How does one fix "a is not defined" Reference Error?
#4

Whats wrong with this switch statement? please help…


#5

Looks like your code is a copy-paste??
why not follow the correct indentation for each line of codes.


#6

Thank you, wasn’t sure why my code wasn’t working!


#7

Actually, you didn’t add switch statement in your code.


#8

function switchOfStuff(val) {
var answer = “”;
// Only change code below this line

switch (val){
case ‘a’:
answer = “apple”;
break;
case ‘b’:
answer = “bird”;
break;
case ‘c’:
answer = “cat”;
break;
default:
answer = “stuff”;
}

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

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


#9

My error occurs for defining a instead of ‘a’ in the following line:

switchOfStuff(‘a’);