Switch vs if else chains issue

Switch vs if else chains issue
0

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

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

Error Message: For such a solution, bob case is not defined. Not sure what is the error.

If I use the other solution I tried

function chainToSwitch(val) {
  var answer = "";
  // Only change code below this line
  
  switch (val) {
    case 1:
    return answer = "Marley";
    break;
    case 2:
    return answer ="The Answer"; 
    break;
    case 3: 
    return answer = "There is no #1";
    break;
    case 4: 
    return answer = "Missed me by this much!";
    break;
    case 5:
    return answer = "Ate Nine";
    break;
    case 6: 
    return answer = "John"
    break;
    case 7:
    return answer = "56"
    break;

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

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

Error Message: Bob is still not defined.

Can any JS developer explain what the problem is?


Replacing If Else Chains with Switch
#2

Please always start a new thread for you own challenge questions. I created this topic to help answer your questions.

Also, I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.


#3

You should not be adding additional return statements to solve this challenge. You assign the applicable value to answer based on each case. The existing return statement will take care of the rest.


#4

Noted with thanks for the hints. Thanks for sharing the tip of how to make the readability of source codes.


#5

Upon altering and changing the code, without return answer for each of the cases.

The error message shows maximum call stack.


#6

I would need to see your latest code attempt to help you.


#7
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;
 }

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

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

#8

Please use the 3 backticks on the line before and after your code as I explained earlier, so your code is formatted for the forum.


#9

When I run your code, your code passes all the tests except:

chainToSwitch(99)

Your function returns “Missed me by this much” instead of “Missed me by this much!”

Are you using Chrome for your browser?


#10

Yes I am using google chrome as my browser! I forgot the “!” which caused the code to change. I have checked my code and have updated accordingly prior to your checking and have passed the test! Many Thanks!