The “helper” tells me that there is an error


#1

finalGrade()

Hi everyone
I’m doing exercise finalGrade() and I think, I have finished it. However, the “helper” tells me that there is an error saying that 'If any of the grades passed in are less than 0 or greater than 100, the function should return ‘You have entered an invalid grade.’
I have called the function multiple times and it works fine with different arguments?
My code is following:

const finalGrade = (number1, number2, number3) =>

{ if ((number1 < 0 || number1 > 100) || (number2 < 0 || number2 > 100) || (number3 < 0 || number3 > 100)) {return 'You have entered an invalid grade'}

else {var average = (number1 + number2 + number3)/3}

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'
}
}

console.log(finalGrade(70, 70, 70));

FAQ: Code Challenges: JavaScript Fundamentals - finalGrade()
FAQ: Code Challenges: JavaScript Fundamentals - finalGrade()
#2

Add a period to the end of the ‘You have entered an invalid grade’ sentence. Then it should work.


#3

Extra Study

Now that we have function that works as expected, how might we simplify or improve its readability?

First tip, whitespace.

const funcName = (args) => {
    if (condition) {

    } else {

    }
};

Second, is there a way to simplify the logic with less logical operators?

In the case of the above, the answer is yes. Consider that the if statement has top down flow direction. What does this hint at?

Third, and rather minor, can we shorten some of our variable names to reduce horizontal scrolling when reading the code?

Fourth, and this goes along with whitespace, can we break up lines of code for the same reason as above?