# Typo in code and how to improve readability

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

``````

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

5 Likes

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?

I think the “correct” answer is a bad solution. Look at these results for finalGrade(68, 69, 71), then the formula written actually returns “undefined” because the average is 69.33333 which isn’t caught. It would actually be better to rely on the code to catch the values without doing the extra checks. By coding:
if (average >= 60 && average <= 69) {
return ‘D’;
} else if (average >= 70 && average <= 79) {
return ‘C’ ;
}
we don’t check for between 69 and 70. So we either have to round the average or just code the if/else to fall through the logic:

``````if (average < 60) { //evaluates for 0-59.999
return 'F';
} else if (average < 70) { //evaluates for 60-69.999
return 'D' ;
} else if (average < 80) { //evaluates for 70-79.999
return 'C' ;
} else if (average < 90) { //evaluates for 80-80.999
return 'B' ;
}  else { evaluates for >90
return 'A';
}

Rounding the average might still be a good idea, or else using half values as the conditions. someone with 59.99 will still fail the same as someone with a 59.49 average.``````

Is that meant to be a comment?

yes, sorry. coded on the screen and commented afterwards.

1 Like

function finalGrade(homework, midterm, final) {
if ((homework < 0 || homework > 100) || (midterm < 0 || midterm > 100) || (final < 0 || final > 100)) {
return ‘You have entered an invalid grade.’
}
let average = (homework + midterm + final)/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(99, 92, 95)) // Should print ‘A’