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.

Hi I was wondering if someone could help me with my code. It seems to be working correctly in google dev tools. If the average is less than 0 or more than 100 it comes back ‘you have entered an invalid grade’ and it is also working with decimals when I tried but it still kicks it back when I run it on codecademy. Is it because I didnt define each grade like the solution? If so why would this be wrong?

function finalGrade (test, quiz, test2) {
let average=(test+quiz+test2)/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’
} else {
return ‘You have entered an invalid grade.’;
}
}

This will not validate individual inputs, and if any inputs are invalid it will not arrive at the correct average. Test the input individually before computing the average.

Can you think of a simpler way to write the if statement that does not use superfluous logic and does not let floats sneak by?

Okay I think I understand. I got a bit confused but I am understanding now after testing both functions that mine will still arrive at a letter grade if you enter a number > 100 or <0 and the codecademy code kicks back you have entered an invalid number. Is this what you are meaning by checking them individually?

Yes, we need all the grades to be valid (within the specified range). There is still the matter of the other questions we raised earlier.

I am not sure what’s wrong with my code. It would return undefined. I’m obviously missing something here.

// Write your function here:
function finalGrade(grade1, grade2, grade3) {
let average = (grade1 + grade2 + grade3)/3;
if ((grade1 < 0 || grade1 > 100) || (grade2 < 0 || grade2 > 100) || (grade3 < 0 || grade3 > 100)) {
return ‘You have entered an invalid grade’;
}
else {
switch (true) {
case (average => 90 && average <= 100):
return ‘A’;
break;
case (average => 80 && average <= 89):
return ‘B’;
break;
case (average => 70 && average <= 79):
return ‘C’;
break;
case (average => 60 && average <= 69):
return ‘D’;
break;
case (average => 0 && average <= 59):
return ‘F’;
break;
}
}
}

We could start by doing the validation first…

if (...) {
    return "You have entered an invalid grade."
}

It’s a return action so no else will be needed.

Next calculate the average, as you have done.

Now study the remaining code and see if it can be simplified.

switch (true) {
case average < 60: return 'F';
...
default: return 'A'
}

We’ve already excluded values less than zero or greater than 100 so evaluating as above is enough. Fill in the missing cases from lowest to highest up to < 90. The default case will catch any grades 90 and above.

When using return in case actions, do not include break since it is unreachable after return.

Be sure to pair up your braces. Your code above will have one less pair since there is no else clause.

Thanks for great tips! It works now with the cases from lowest to highest. That’s what was wrong with my code.

Why do I need to start from lowest to highest?

1 Like