lionde
July 11, 2022, 1:15pm
1
The lesson asked me to do this:
Combine the if statements into a single if/else statement.
I wrote this:
function testElse(val) {
let result = "";
// Only change code below this line
if (val > 5) {
result = "Bigger than 5";
}
else (val <= 5) {
result = "5 or Smaller";
}
// Only change code above this line
return result;
}
testElse(4);
and I got this error:
SyntaxError: unknown: Missing semicolon. (9:17)
7 | }
8 |
> 9 | else (val <= 5) {
| ^
10 | result = "5 or Smaller";
11 | }
12 |
If I put the semicolon where it is being asked, the first if statement is never executed, so I still can’t pass the lesson.
What can I do?
hey @lionde welcome to the forum
the form you are using is if-else with a typo.
there are two ways you could use if-else
1 . if (condition){
// condition is true execute these lines
} else{
// condition fails, execute following line
}
2. if(condition1){
} else if( condition2){
// ....
}
that’s why you are getting that error
lionde
July 11, 2022, 1:27pm
3
I tried the if-else-if but this lesson only accepts one “if” in the code, however the first method passed. Thanks!!!
if any number is not greater than 5
then obviously it’s either less than 5 or equal to
lionde:
else (val <= 5)
should be only
else {
//...
}
Yeah that’s true. the compiler will not throw the error any more.
and it doesn’t execute the code in the { }
// ERROR : this statement is not syntactically correct
else (val <= 5)
system
Closed
January 10, 2023, 2:58am
6
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.