Defining variables within an if/else block

Hi, I get an error if I use const average, but it is ok if I use var average. I don’t get it. Please help


please fix your “if else if block”

if (condition1)
  statement1
else if (condition2)
  statement2
else if (condition3)
  statement3
...
else
  statementN

i.e. make it one block of “if else if” instead of two?

you block looks like this

if
else
if
else if
else if
else if
else if

try to change it to

if (condition1)
  statement1
else if (condition2)
  statement2
else if (condition3)
  statement3
...
else
1 Like

Ok, clear. Thanks a lot!

1 Like

The problem is that you have your average variable being assigned inside an else block. You do not need the else block at all. Your first if statement is checking whether or not the grades are valid. If they aren’t, you return the ‘You have entered an invalid grade.’ message. That’s all you need for the first if statement. After that, assign you your const average variable. Then your if..else if..else block will function as is. You can change your last else if to just else without checking a condition since return A is the only option left at that point.

1 Like

Thanks for the clear explanation. Makes sense that the variable is inside an useless else block