Need Help With "Selecting from many options with Switch Statements."

I think I might just be coding it wrong. Can someone give me a clue please? Here’s what I have:

function caseInSwitch(val) {
  var answer = "";
  // Only change code below this line
 case val("alpha"): 
   return answer "A";  
   break;
 case val ("beta"):
   return answer "B";
   break;
  case val ("gamma"):
  return answer "C";
  break;
  case val ("delta"):
  return answer "D";
  
  
  // Only change code above this line  
  return answer "N";  
}

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

Hi there,

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.

I also added spoiler tags as this pertains to a challenge questions.

The exercise wants you to make a function that takes numbers and spits out the corresponding phonetic alphabet. So, caseInSwitch(1) should return “alpha”. Right now, you’re taking the phonetic alphabet as input and returning a letter.

Thank you. Sorry about how I posted. I’m new and don’t understand much. I hope I’m doing it correctly now.
So I changed the code. This is what I got:

function caseInSwitch(val) {
  var answer = "";
  // Only change code below this line
 caseInSwitch(1): 
   return answer "alpha";  
   break;
 caseInSwitch(2):
   return answer "beta";
   break;
  caseInSwitch(3):
  return answer "gamma";
  break;
  caseInSwitch(4):
  return answer "delta";
  
  
  // Only change code above this line  
  return answer "N";  
}

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

What am I doing wrong?

You are missing an assignment operator.

1 Like

Whoops! The back tick key is to the left of the number 1 on your top row (on U.S. keyboards, at least).

This is what a switch should look like:

switch(val) {
   case "Something": // not a function!
      //do stuff here... like assign variables!
      break;
   case "Something else":
      //do stuff here
      break;
   default:
       //do stuff here
}
1 Like

do you mean the “=” sign on the return answer? i tried that and it didn’t work. i took them off because the example doesn’t use them. i’m getting a syntax error.

thanks. so, is “switch(val)” in your example equal to the “function caseInSwitch(val)” on the challenge? this is what confuses me. on other resources and examples it looks like what you just typed. but on the challenge it uses different wording and structure. i keep getting “missing semicolon” and "expected ‘}’ to match ‘{’ from line 2 and instead saw ‘:’."
also, am i supposed to use “return answer”? in the example “pseudocode” it doesn’t.

Oh man, I’m not rly good at explaining that kind of stuff.

Look at the top of your code. You have var answer = "" in there. By that, you create a variable called answer and assign "" (an empty string, don’t focus on this part for now).
A variable is kind of a storage box for a value. By assigning the value to the variable answer you kind of take that value and put it into a box labeled answer. Now, you can access that value using that particular label. For example, you can write return answer; at the end of your function. This way, your functions output will be a value assigned to answer.
So far so good?

Now, you can reassign the value of your variable pretty much at any time. You do it the same way you assign initially, using = operator. Thats what you are supposed to do in this exercise. You need to change the value assigned to answer, depending of what kind of value (1, 2, 3, 4) you were provided.

So, you have two bugs in your code. First, you are not using the assign operator where you need to.
Second, you put "N" at the end of your return statement, I’m not rly sure what for.

facepalm Of course, I try to be too fast.
caseInSwitch is just a name for your function. You could have actually named it iLikeBananas, would work the same way. What you need to do (along with what i have written up there) is tell Javascript that you want to use this special functionality that is switch. In order to do it, you need to use a template that @PortableStick gave you, and place it inside your function (which is currently named caseInSwitch).

2 Likes

A switch statement is something we can use inside of a function.

function myFunction(val) {
    var boop;
    switch(val) {  // say to yourself, "I'm going to switch based on what val is..."
      case "doop":  // "In the case that val is 'doop', I'm going to..."
        boop = "bleep blop"  // "... set boop to be 'bleep blop'"
        break
      case "hoop":
        boop = "meep mop";
        break;
      // ... etc
    } // end of switch
    return boop;
} // end of function

This is just another way of writing if statements.

function myFunction(val) {
    var boop;
    if(val == "doop") {
        boop = "bleep blop"
    } else if(val == "hoop") {
        boop = "meep mop";
    } else if(val == "something else") {
      // ... etc
    } // end of if's
    return boop;
} // end of function

Notice that I’m changing the value of the variable that gets returned after the switch/if. This isn’t the only way to do it, but I think it’s what the challenge has you do.

1 Like

So, I got this:

function caseInSwitch(val) {
  var answer = "";
  // Only change code below this line
 switch (val) {
  case "1": 
    answer = "alpha";  
   break;
 case "2":
   answer = "beta";
   break;
  case "3":
  answer = "gamma";
  break;
  case "4":
  answer = "delta";
  
 }
  // Only change code above this line  
 answer = "";  
}

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

Now I have no markups on the page but it still isn’t correct. =(

You are so close! I should have checked the challenge before making my post because I was wrong about when to return the answer. Here’s my modified example.

function myFunction(val) {
    // No more variable!
    switch(val) {  // say to yourself, "I'm going to switch based on what val is..."
      case "doop":  // "In the case that val is 'doop', I'm going to..."
       return "bleep blop"  // "return 'bleep blop'".  This will exit the switch and the function
       // break is not needed if we return a value
      case "hoop":
        return "meep mop";
    } // end of switch
    return "Default answer" // if none of the cases are used, we will exit the switch and return this value
} // end of function

Very sorry for the mixup. Hope this helps!

1 Like

I finally figured it out. Thank You!

Thank you! My code went thru. I guess my problem was that I didn’t understand how the top part (the part of the challenge where we’re not supposed to change code.) and the middle part (starting with “switch (val) {”) are supposed to work. I didn’t know that you were supposed to structure it that way. But I understood what the purpose was and what the answers should be, kinda.

Thanks for the question and replies here - I got stuck on this one as well, and y’all helped me get through it!

The best response I have seen on switches. Also, the challenge is quite straightforward.