Replacing If Else Chains with Switch

Hi,

I wonder what is wrong with this code. There is no if else statements any more but it still warns that it’s wrong.

function chainToSwitch(val) {
var answer = “”;
// Only change code below this line
switch (val) {
case “bob”:
answer=“Marley”;
break;
case “42”:
answer=“The Answer”;
break;
case “1”:
answer=“There is no #1”;
break;
case “99”:
answer=“Missed me by this much!”;
break;
case “7”:
answer=“Ate nine”;
break;
default:
answer=“default”;
break;

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

// Change this value to test
chainToSwitch(100);

Edit: I realized what is wrong. The numbers should be without " ". And nine should be written with big N.

3 Likes

Hi, switches use strict type checking. This means that “42” is not equal to 42, because “42” is a string and 42 is a number.

Edit: didn’t see your edit :smile:

2 Likes

It’s ok :slight_smile: Good answer anyway.

2 Likes

You could make a map.

2 Likes

Thank for sharing this on forum. It helped me better understand the program.
Thank you again.

1 Like

The solution for this problem:

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

switch (val) {
case “bob”:
return “Marley”;
break;
case 42:
return “The Answer”;
break;
case 1:
return “There is no #1”;
break;
case 99:
return “Missed me by this much!”;
break;
case 7:
return “Ate Nine”;
default:
}

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

// Change this value to test
chainToSwitch(“bob”);

1 Like

so why is this answer not working?

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

switch (val) {
case "bob":
answer = "Marley";
break;
case 42:
answer = "The Answer";
    break;
case 1:
answer = "There is no #1";
    break;
case 99:
answer = "Missed me by this much!";
break;
case 7:
answer = "Ate Nine";
    break;
  default:

}

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

// Change this value to test
chainToSwitch(bob);

1 Like

“bob”, not bob (call your function with string not variable, because you don’t even have variable bob)

I’m having problem with Replacing If/Else Chain with Switch because I’m keep having an error about chainToSwitch(7). I got print, “Ate Nine”, but it won’t allow me to move to next challenge.

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

switch(val){

 case "bob":      
answer = "Marley";
break;  

case 42: 
answer = "The Answer";
break; 
  
case 1:    
answer = "There is no #1";
break;

case 99:
answer = "Missed me by this much!";
break;
   
 case 7:  
 answer = "Ate nine"; 
 break; 
   
 default: 
  answer = ""; 

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

// Change this value to test
chainToSwitch(99);

It is because you have ‘nine’, not ‘Nine’. These challenges often require that you have the exact answer it is looking for – including case. So just make the ‘n’ in nine uppercase, and it should work,

Try to replace answer by return and remove =.

Example:
switch (val){
case “bob”:
return “Marley”;
break;

If you use return there is no need for break, since return will immediately exit not only the switch but the function itself.

Both returning a value directly and setting a value and breaking are valid styles, but don’t mix them.

(Edit: sorry, didn’t realize this was a necro.)

Nice catch. Thanks!

Final version example:
switch (val){
case “bob”:
return “Marley”;

But why are not writing the empty string cases. there are 2 empty string cases.

Hi Everyone.
The error is with the curly bracket “{}”.
You all included a curly bracket before the final return value.
Take the first closing curly bracket between your final break and return, then place it after chainToSwitch(100);.
It worked for me

27

change the location of the closing curly bracket.