I’m so confused, how could anyone be expected to get from the example to the answer?
I also changed the three to four… no go!
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/selecting-from-many-options-with-switch-statementshttps://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/selecting-from-many-options-with-switch-statements
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 3:
answer = "delta";
break;
}
// Only change code above this line
return answer;
caseInSwitch(1);
The 4 you have a 3 on the last case
You are missing an extra }
i changed the 4.
the extra } didnt help me
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;
}
}
What do the failing tests say?
SyntaxError: unknown: ‘return’ outside of function (22:2)
20 |
21 | // Only change code above this line
22 | return answer;
| ^
23 |
24 | caseInSwitch(1);
25 | Preformatted text
// running tests
caseInSwitch(1)
should have a value of “alpha”
caseInSwitch(2)
should have a value of “beta”
caseInSwitch(3)
should have a value of “gamma”
caseInSwitch(4)
should have a value of “delta” // tests comp
This is telling you exactly where the problem is. Your return
statement (which I don’t see in your most recent code) is outside of your function.
function caseInSwitch(val) {
var answer = "";
// Only change code below this line
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 says only change code above it.
I reset all the code and copy/pasted my answer back in and got this.
SyntaxError: unknown: Unexpected token (32:0)
30 |
31 | caseInSwitch(1);
32 |
| ^
And one of the changes you made was to add an extra }
. That ended the function before your return
statement.
I took it out.
… and I appreciate your help lol
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);`
[/quote]
type or paste code here
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);`
[/quote]
I got it! i have no idea what changed i just copied and pasted again and it passed. ??? Javascript sucks!!
You probably had a stray keystroke that occurred when you were making changes last time. I huge amount of errors are caused by typos.
Congratulations of figuring it out. Happy coding!
1 Like