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.

3 Likes

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

i try this variant: it’s work

1 Like

Hello.
Can you please have a look on my code?
It’s not working and I can’t fix it.
thank you in advance

Done! Finally working ))))

however, can please someone tell tell what was incorrect with
finalGrade() === average()

Correct me if I miss something here, but it seems to me that return A isn’t the only option, as one of the grades could be a string, or whatever other element which is Not a Number, and so there has to be a final else with ‘You have entered an invalid grade’ (or an else if like: (grade1 === NaN && grade2 === NaN… etc.)

Validation has already been completed. ‘A’ is the default when all other tests have been applied.

1 Like

Only for numbers out of range. But not for inputs other than numbers.
If you pass a test like this: console.log(finalGrade(39,'fifty-seven',null)), it will return A.
Though when you console.log(average) it appears to be NaN…

Aside for that, one question remained open:
Why did it pass with a var, while const and let threw an error?
I’ve reproduced the code so everyone can play with it:

`const finalGrade = (grade1, grade2, grade3) => {
if (grade1 < 0 || grade1 > 100 || grade2 < 0 || grade2 > 100 || grade3 < 0 || grade3 > 100){
return ‘You have entered an invalid grade.’
} else {
let /* var const */ average = (grade1 + grade2 + grade3)/3; //the line in question
console.log(average);
}
if (average >= 0 && average <= 59){
return ‘F’;
} else if (average >= 60 && average <= 69){
return ‘D’;
} else if (average >= 70 && average <= 79){
return ‘C’;
} else if (average >= 80 && average <= 89){
return ‘B’;
} else {
return ‘A’;
}
}

console.log(finalGrade(99, 92, 95))
console.log(finalGrade(48, null, ‘e’))`

They would raise an exception in the average calculation, if not before.

they do, but you can get a finalGrade with them nonetheless…

Exceptions are there for our benefit (the programmer). And if we give the programmer the benefit that only numbers are being passed to the function after some form of validation at that stage, then we’re in the clear.

There is only so much we should expect from any function. The precursors should have been gone through beforehand. It’s the general idea of functional programming to begin with. Use pure functions and don’t load up their tasks with things that should be done before they are called.


That is why I would write a utility (helper) function that determines whether a value is in range, or not.

const inRange = (x, lower, upper) => {
  return lower <= x && x <= upper
}

This ‘helps’ us to get around all that logic.

if (inRange(a) && inRange(b) && inRange(c)) {}
1 Like

Thanks! That was mind opening for me.

What about the var passing where const and let fail? ( @beta6154250935 's original question)

One need not give that much leave. The SCT is set to a specific pattern. Use either with the particular intention that matches what they promise. let is the safest bet in most cases. Arguments can be made for the other cases, so be it.

so can anyone check my code?
i think it works properly…
but since my code is a lot different than people here/
so i got curios whether i wrote right or not…

Hi I was reading what you said. I am having a similar problem but I have been stuck for 2 days straight. I was wondering if you can explain to me what I did wrong. Well give me a hand. I don’t want the answer right away. I want to see if I can figure it out by myself with a clue. Thank you and I hope you can comment back. Here is a link to what I am having trouble with: https://www.codecademy.com/paths/web-development/tracks/getting-started-with-javascript/modules/code-challenge-javascript-fundamentals/lessons/javascript-fundamentals-c%20e-challenge/exercises/final-grade