JavaScript problem of beginner

Tell us what’s happening:

  **Your code so far**

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

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

Challenge: Selecting from Many Options with Switch Statements

Link to the challenge:

Why my code is wrong?

function caseInSwitch(val) {
  var answer = "";
  // Only change code below this line
switch(caseInSwitch){
  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);

you need to remember the difference between a variable and a string

I do really not know how to do it.

You should know how we declare a variable. var answer= "" where answer is the variable name and "" is the variable value, which in his case is an empty string. After you declare a variable, you can use it in your code. For example answer="alpha", which would set the value of answer to alpha.
Now you use some cases in your switch code, like answer = beta , which corresponds in the case where answer would be equal the variable beta (their values), but beta is not declared. You use it as variable named “beta”. If you mean the case where answer is equal the string "beta" you must use quotation marks like answer="beta". Or you can declare a variable named beta and assign to it, the value of "beta", before using it in your code, like you did with the variable answer

There are two issues with your code.

  1. You need to switch based on the value of the parameter val. caseInSwitch is the name of the function, not the parameter of the function.

  2. There are different types of data we manipulate in a program. We have numbers like 23, 45.0, and so forth. We also have strings, or text. Textual data are represented in programs by double quotes or single quotes, like

"hello"
"John Smith"
'What is your name?'

We can assign a value to a variable, for example,

var x = 24;
var name = "Jack";

We call them variables because they can “vary.” For example,

var today = "Monday";

today = "Tuesday";

Now, the variable today has a value, so we can write

console.log(today);

It is equivalent to writing (assuming, of course, the value of today is “Tuesday”)

console.log("Tuesday");

But what happens with somebody writes

console.log(Tuesday);

There are no quotes, so the system treats Tuesday is a variable.
The function must return a string value such as “alpha”, “beta”, and so forth.

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