Hi,
my switch statement seems to have an error and I just can’t seem to work it out.
Every time I run a test, the error I’m faced with is;
// running tests
switchOfStuff(“a”)
should have a value of “apple”
switchOfStuff(“b”)
should have a value of “bird”
switchOfStuff(“c”)
should have a value of “cat”
switchOfStuff(“d”)
should have a value of “stuff”
switchOfStuff(4)
should have a value of “stuff” You should use a default
statement //
tests completed
// console output
ReferenceError: a is not defined
So if I input a,b,c, into the console.log(swithOfStuff(a)) this error appears however as soon as I put “a”, “b”, “c” this seems to work perfectly and when I run the test I’m told I’ve passed.
My 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";
break;
}
// Only change code above this line
return answer;
}
// Change this value to test
console.log(switchOfStuff(a));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.106 Safari/537.36
.
Challenge: Adding a Default Option in Switch Statements
Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/adding-a-default-option-in-switch-statements