Switch - how to make it work?

Switch - how to make it work?
0

#1

Tell us what’s happening:
I’m having a hard time distinguishing which variable I’m working with once I pass a parameter (val) into my function. I’m confused with whether I’m working with “val” or “answer”.

How do I edit this code to make it work?

Directions:
case values are tested with strict equality (===). The break tells JavaScript to stop executing statements. If the break is omitted, the next statement will be executed.

Write a switch statement which tests val and sets answer for the following conditions:
1 - “alpha”
2 - “beta”
3 - “gamma”
4 - “delta”

Your code so far


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

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

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/selecting-from-many-options-with-switch-statements


#2

Hi @gchan,

In this exercise you have to check if the provided value i.e., val is equal to 1 or 2 or 3 or 4. So you have to work with val.

switch(val) 

Also remove quotes from ‘1’, ‘2’, ‘3’, ‘4’


#3

val is the number that is passed in (i.e. 1,2,3, or 4). answer is a variable that represents the answer you will be returning. So the lesson is basically wanting the answer to be “alpha” when the val passed in is 1, and so on.

Aside from that, a quick thing I noticed from your code, case should be lowercase.

Hope this helps.


#4

function caseInSwitch(val) {
  var answer = "";
  // Only change code below this line
  switch(answer){  // testing answer? answer = ""   should be testing something else
  Case '1':  // 1 and '1' are not same thing
    answer = answer +"alpha";
    break;
  Case '2':  // case and Case are not same thing
    answer = answer + "beta";
    break;
  Case '3':
    answer = answer + "gamma";
    break;
  Case '4':
    answer = answer + "delta";
    break;
}
  // Only change code above this line  
  return answer;  
}

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

#5

Thank you, Sunit and Katie! So, val is passed to the function, and it is then passed to the switch, correct? But, the var answer result will depend on the val (whether it is 1,2,3 or 4) - is that the correct way to think of this?


#6

You switch on val, answer is a variable you’ve created in the function to store the answer. It’s like

if (val == '1') {
  answer = answer +"alpha";
} else if (val == '2') {
  ...

Switch takes val, and checks if the value of val matches whatever is specified for each case in turn: if it does, then it executes whatever is in that branch, and the switch statement ends.

Also case, not Case


#7

@gchan: Yes, that is correct