Adding a Default Option in Switch Statements NOTHING IS WORKING!

Adding a Default Option in Switch Statements NOTHING IS WORKING!
0.0 0

#1

Tell us what’s happening:

OK I’m really really frustrated right now. I’ve looked at a half dozen forum threads trying to get this to work and nothing is working. It looks exactly like an answer I found on one of the threads but it only returns “a is not defined”. Help please.

Your code so far


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);

Your browser information:

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


#2

hmm, it passed for me when i copied your code. try copying your code, then refresh the page and paste it and try to submit again.


#3

Thank you for replying so quickly. I tried refreshing the page and resetting the code and pasting it but nothing works.


#4

Can you post a screenshot which shows the entire page after you click Run the Tests?


#5


#6

The problem is the above line. Why did you put it into your solution? The variable a is not defined anywhere in your code, which is why you are seeing the error “a is not defined”. Get rid of that line and you are good to go.


#7

OK thank you and that worked but I really want to understand this. Isn’t that line to test the function? Isn’t this function basically to return the answer of “apple” when someone were to input “a” for example? If we can’t put a val in the switchOfStuff( ) then what exactly is this function doing? Not trying to be rude or anything I’m just not understanding why that made the program not work.


#8

what you wanted to test was switchOfStuff("a"); notice the quotes around the a.

you should absolutely test your functions, but if you pass undeclared variables, everything goes to :poop:

hope that helps!


#9

Oh I gotcha. Ok I understand now thank you both for your help.