Basic JavaScript - Selecting from Many Options with Switch Statements

Hello,

could you plz tell me what is wrong with my code:
here is the question:
Write a switch statement which tests val and sets answer for the following conditions:
1 - alpha
2 - beta
3 - gamma
4 - delta
and this is my code:
function caseInSwitch(val) {
let answer = “”;
// Only change code below this line
switch(answer){

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;
}
thank you so much.
Sara

   **Your code so far**
function caseInSwitch(val) {
 let answer = "";
 // Only change code below this line
switch(answer){
 
 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);
   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Selecting from Many Options with Switch Statements

Link to the challenge:

The instructions say to “set answer”. No matter what the input is to your current function, it always returns an empty string, which is what answer is initialized to.

Thank you so much for your reply. But the let answer = “”; statement is typed by the site not me and it says only change the code below it.
How should I change it?

Do you remember how to assign a value to a variable?

For example, if I wanted to assign the string “Honey1835” to the variable answer, I could write this:

answer = "Honey1835";

and pay closer attention to the switch syntax. Each case should be paired with an expected value to evaulate with. If i expect val to be equal 3, it would look like case 3:

Yes I do remember, but here whatever value I assign to answer, it throws an error.

Please post your latest code

ok, so case 1 for alpha and so on? or what I wrote is wrong?sorry I am completely confused here

function caseInSwitch(val) {
let 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;
}

You need to assign a value to answer, inside your switch statement.

oh I solved it finally.
function caseInSwitch(val) {
let 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;

1 Like

Thank you so much. :heart_eyes:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.