Freecodecamp: Adding a default option in Switch statements not working

Created on 13 Feb 2016  路  6Comments  路  Source: freeCodeCamp/freeCodeCamp

Challenge Adding a default option in Switch statements has an issue.
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/601.4.4 (KHTML, like Gecko) Version/9.0.3 Safari/601.4.4.
Please describe how to reproduce this issue, and include links to screenshots if possible.

My code:
function myTest(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:
return "stuff";

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

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


Most helpful comment

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

All 6 comments

I also notice that several exercises ahead of this one is quite confusing.I spent hours on those,could you please make it clearer?

Default is structured just like the other switch cases so you might want to review your answer for that.
i.e. it does not return, but it should assign answer= something and then break.

Before posting a Github bug report, remember first to check in the JavaScript chat room.

Exactly what Jami said, that's how your default will work.

Please abstain from posting code questions in the issue board, as this only for verified system issues.
The instructions for bug reporting are very clear and easy to follow.

This is not a system issue, so I'm closing it.
Happy coding!

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

My Solution

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

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

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

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

Was this page helpful?
0 / 5 - 0 ratings