Adding a default option in Switch statements it gives me an error

Hello, I can not pass the test by more ways than I tried.
The following needs are not exceeded:
switchOfStuff (“a”) should have a value of “apple”
switchOfStuff (“b”) should have a value of “bird”
switchOfStuff (“c”) should have a value of “cat”
What am I wrong about?

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

switch (val){
case(“a”):
answer=“apple”;
break;

  case("b"):
  answer="brid";
  break;

  case("c"):
  answer="cat";
  break;

default:
answer=“stuff”;

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

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

Parentheses around switch cases are not needed. So , to take your apple switch case as an example, you can replace it with the following:

      case "a":
        answer = "apple";
        break;

Furthermore, you have a typo in your bird switch case (‘brid’).

One other thing that I noticed is that you use curly quotes instead of straight quotes in some places in your code (“ vs ").

Maybe you used a text editor that replaced the straight quotes with curly ones? Anyhow, try to replace those with straight quotes and see if that works.

1 Like

It still does not pass the three

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)

check which characters you are using.
your double quotes are flagged in VS Code as “invalid character”
visually, in my IDE i am seeing yours as being curled, whereas my double quotes are straight.

maybe you are using international keyboard layout. try switching to US keyboard layout

also, parentheses not needed.
also return answer; should be the last thin before the closing curly brace of the function.

tested with these changes and it works.

1 Like

In addition to JuggernOtt81’s answer: you still need quotation marks around your cases, so case “b” instead of case b. The return statement is part of your default case now, so your function only returns something in the default situation. If you move your return statement out of your switch, right after closing the switch and before closing the function it should work.

2 Likes

Effectively it worked. Thank you, that was the problem from the beginning, return statement out of your switch
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);

Effectively it worked. Thank you, that was the problem from the beginning,

Ahh that was my problem too! I had tried everything from single quotes to changing my keyboard settings but it was actually my return statement was still inside of my switch.