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

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

#1

Continuing the discussion from freeCodeCamp Challenge Guide: Adding a default option in Switch statements:

function switchOfStuff(val) {
  var answer = "";
  // Only change code below this line
  switch (answer) {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);

#2

I meant to say this is lesson 184!
Here is the copy of a correct answer (posted on the above link):

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

#4

I’m moving on, but I still was wondering what I did wrong in terms of formatting in my first post. Please let me know if you have a chance!


#5

You attempted to call your function with a variable called a which was not defined. I assume you meant to pass the string ‘a’ like:

switchOfStuff('a'); // yields 'apple'

Also, your function does not work as the challenge wants, because you pass in val to the function, but do not use it in your switch statement. You use answer which is a blank string, so your function will always return the default case of ‘stuff’.


#6

The challenges aren’t numbered. Those are your brownie points.


#7
<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;
      case 'd':
      answer = ("stuff");
      break;
      case 4:
      answer = ("stuff");
      break;
    default:
      answer = ("default");
      
  }
  
  
  // Only change code above this line  
  return answer;  
}

// Change this value to test
switchOfStuff(1);
</code>

I found that the number **4**
broke my code because it was surrounded by quotes. removed the quotes and tests pass 

#8

You do not need either of the above cases. The whole point of the challenge was to use the default case for returning “stuff” when val did not match any of the other cases.