Basic Javascript on Switch Statement

Hi!

I got stuck on the switch problem on Basic Javascript: Selecting from Many Options with Switch Statements. Here’s the link for the problem: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/selecting-from-many-options-with-switch-statements

My code isn’t working. It seems that I don’t understand how switch statement works. So, if you could have a brief explanation of how switch statement works and may be an example of how to solve this tutorial, it would really help me.

Here’s my code that didn’t pass.

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

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

caseInSwitch(1);

Thank you for your time!

Hia,

The sample switch looks like this

switch(lowercaseLetter) {
  case "a":
    console.log("A");
    break;
  case "b":
    console.log("B");
    break;
}

I don’t see the part in your code where your case statements are wrapped in a switch (varToMatch) { ... }

What do you mean by switch(varToMatch)?

You do not have switch anywhere in your code.

In the provided example

switch(lowercaseLetter) {
  case "a":
    console.log("A");
    break;
  case "b":
    console.log("B");
    break;
}

switch("b");

there is a switch so that the JavaScipt engine knows what the case statements are matching against.

1 Like

I see. So, it should be ‘switch’ instead of ‘function’. Function was already written as a set up. How could I work around that?

No, you need the function as given to you. Inside of your function you need to add a complete switch with cases, as shown in the example.

1 Like

Hi @Aimslee
In your code, you are using only the test conditions of switch statement. In order get outputs, your test conditions must be included inside switch statement. otherwise it does not make sense.

function caseInSwitch(val) {
  var answer = "";
  // step 1 - write switch statement
  // step 2 - write test conditions inside switch statement 

  return answer;
}

Note: the switch statement should check the value ( val ) that given as the parameter of the function.
Follow the steps and try to complete the challenge. :slightly_smiling_face:

1 Like

I followed the format and wrote the following code but it didn’t go through.

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

caseInSwitch(1);

Thank you!

This part of your original solution is good. You need to set the return value.

Good, it worked!

is answer same thing as return?

Also, why does the tutorial want me to use answer when console.log is used as an example?

The function has the variable answer already defined and returns it for you, so that’s what you needed to update with the switch.

For the example, they were just using the switch to print out some information to the console.

1 Like

@Aimslee you are missing the switch statement in your code

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";
  break;
  }
  // Only change code above this line
  return answer;
}
caseInSwitch(1);

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.