Replacing If Else Chains with Switch

Replacing If Else Chains with Switch
0

#1

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.


#2

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:


#3

It’s ok :slight_smile: Good answer anyway.


#4

You could make a map.


#5

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


#6

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


#7

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


Can anyone tell me why compiler keeps saying chainToSwitch(1) should be "There is no #1" ?/
#8

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


#9

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


#10

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,


#11

Try to replace answer by return and remove =.

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


#12

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


#13

Nice catch. Thanks!

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


#14

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


#15

What exactly are you talking about?


#16

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


#17

27

change the location of the closing curly bracket.


#18

A post was split to a new topic: Switch vs if else chains issue


#19