FAQ: Code Challenges: JavaScript Fundamentals - finalGrade()

Looks like a typo…

Have you come across any topics that discuss the float problem and the bulkiness of betweenness?

1 Like

if ((midterm < 0 || midterm > 100) || (final < 0 || final > 100) || (homwork < 0 || homework > 100))
^

ReferenceError: homwork is not defined
at finalGrade (/home/ccuser/workspace/js-challenge-final-grade/main.js:4:72)

Another typo, perhaps?

1 Like

lol thanks. I come across float only in HTML older version.

What happens if grade is a float (as in decimal number, not typographical feature) say 69.4?

it gets rounded down to 69?

We wish. Nothing happens, so it falls to the end of the function and returns, undefined.

1 Like

I’m going to lookup the float problem and the bulkiness of betweenness. will that help with complete finalGrade(). because I fix the typo. but the code is displaying my last else statement.

Let’s see your code again, after the corrections.

1 Like

const finalGrade = (midterm, final, homework) => {
const average = (midterm + final + homework)/3
if ((midterm < 0 || midterm > 100) || (final < 0 || final > 100) || (homework < 0 || homework > 100))
return ‘You have entered an invalid grade.’

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 if (average >= 90 && average === 100)
return ‘A’
else
return ‘something went wrong with the code’;
};

console.log(finalGrade(59.4, 69.6, 79.8))
something went wrong with the code

See how the result is the last clause of the if statement? Were that clause not there, the return would be, undefined. That’s the float problem.


Consider that you have already eliminated less than zero and greater than 100 so it is not necessary to test for zero. In fact your code will not accept 0, either so will return the same as above.

What if we write,

if (average < 60) return 'F'

and so on…

1 Like

const finalGrade = (midterm, final, homework) => {
const average = (midterm + final + homework)/3
if ((midterm < 0 || midterm > 100) || (final < 0 || final > 100) || (homework < 0 || homework > 100))
return ‘You have entered an invalid grade.’

if (average < 60)
return ‘F’
if (average < 69)
return ‘D’
if (average < 79)
return ‘C’
if (average < 89)
return ‘B’
if (average < 100)
return ‘A’
else
return ‘something went wrong with the code’;

};

thank you! thank you!
:joy:
the right code seems like it staring right at me. but for whatever reason my brain just doesn’t want to see it.

You’re catching on, so be patient with yourself. What did we change on the first one? 59 to 60. Follow through with the others and it should be a wrap.

Also there is no need to test for 100 since it is the final case and we can simply default to, return 'A', without a condition.