Help please! on curly bracket

Tell us what’s happening:

Your code so far


function testElseIf(val) {
if (val > 10) {
  return "Greater than 10";
}else if (val < 5)

if (val < 5) {
  return "Smaller than 5";
}else{
  
}

return "Between 5 and 10";
}

testElseIf(7);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36.

Challenge: Introducing Else If Statements

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/introducing-else-if-statementsPreformatted text

You should have closing and opening curly braces for each if else code block. - where???

Hi @ShamGTR35 your having a bit of code issues, and missing curly on the second else if statement. and the last if should be remove and have an else as the last return.

1 Like
Whice ones {}  or  () ?

like {else if} 
or
like (else of) 

always remember we have this syntax on our if statement. {} inside the brakets if the condition its true. wrapping your code.


if(condition){
/* if the condition its true do this
....code
*/
}
//for a if else statement
else if( condition){
/* if the condition its true do this
....code
*/
}
function testElseIf(val) {
if (val > 10) {
  return "Greater than 10";

{else if} (val < 5)
  return "Smaller than 5";

return "Between 5 and 10";
}

testElseIf(7);
``

your second else if should be like this.

else if( val < 5 ){
  return "Smaller than 5";
}

insdead of

`{ else if}`(val<5)
return "smaller than 5"
```//This wont compile on the web or console.
function testElseIf(val) {
else if (val > 10){
  return "Greater than 10";

else if (val < 5){
  return "Smaller than 5";
}

return "Between 5 and 10";

testElseIf(7);``



correct?

your almost there but remember you only start with if statement first.
remove the first else if. and add a else to the last return wrapping it with curly braces.
This will work on your challenge.

The challenge says at least two if’s and two else’s statements.

the first statement i have changed to just if  but don't understand the second point. is it like this?

{else} 
return "Between 5 and 10";

testElseIf(7);``

It helps to say it out loud as a sentence

if (this is true) {
  // then do this
} else if (this other thing is true) {
  // then do this second thing
} else {
  // do this third thing
}

@JeremyLT yes thats right! i just wanted him to learn from his mistakes its better to learn that way… than giving the code, but he was doing it right… and learning at the same time.

I don’t think I gave him the code any more so than the example in the challenge description gave him the code. He still has to translate that pesudo code into the problem statement. All I did was restate the example.

What im trying to said its learning the syntax structure thats all how a if statement its composed and if else that’s all, But I’m sure he did well :slight_smile:

We all learning here and that’s the best thing… good job guys.

function testElseIf(val) {
if (val > 10){
  return "Greater than 10";

} else if (val < 5){
  return "Smaller than 5";
} else {
return "Between 5 and 10";
}
testElseIf(7);``

i have 2 else statements but the code is still not working. i still have syntax error showing
Thanks Ivan , your a Star !

You forgot a last curly braces on your function

function testElseIf(val){

} // this one. closing function.

Oh don’t, I’m a beginner at this. Dont thank me i think we all help each other as well with the help of @JeremyLT and that you also put an effort as well. we all learn in here :slight_smile:

both else statements are working , correctly now!

1 Like
functiontestElseIf(val) {
  if (val > 10) {
    return "Greater than 10"; 

}else if (val < 5) {
    return "Smaller than 5";

}else{
  return "between 5 and 10";}

}

testElseIf(7);``



where am i going wrong with the curly brackets?

The code works perfect, just have a typo in this line.

functiontestElseIf(val) {

}

should be this: you had functiontestElseIf together should be separate first function then space and name of the function.

function testElseIf(val){

}
function testElseIf(val) {
  if (val > 10) {
    return "Greater than 10";

}else if (val < 5) {
    return "Smaller than 5";

}else{
  return "Between 5 and 10";
}

testElseIf(7);``


done.

i still get error on the brackets !!