JS finalGrade() project


#1

https://www.codecademy.com/paths/web-development/tracks/getting-started-with-javascript/modules/code-challenge-javascript-fundamentals/lessons/javascript-fundamentals-code-challenge/exercises/final-grade

My code seems to work, but I’m getting an error message stating '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.’

function finalGrade(test, quiz, final) {
const ave = (test + quiz + final)/3;
if ((test < 0 || test > 100) || (quiz < 0 || quiz > 100) || (final < 0 || final > 100)) {
console.log(‘You have entered an invalid grade.’);
} else if(ave >= 0 && ave < 60) {
return ‘F’;
} else if(ave > 59 && ave < 70) {
return ‘D’;
} else if(ave > 69 && ave < 80) {
return ‘C’;
} else if(ave > 79 && ave < 90) {
return ‘B’;
} else if(ave > 89 && ave <= 100) {
return ‘A’;
}
}

// Uncomment the line below when you’re ready to try out your function
console.log(finalGrade(101, 92, 95)) // Should print ‘A’

// We encourage you to add more function calls of your own to test your code!


#2

One idea would be to do the validation first, and not calculate the average until you know the inputs are valid. Be sure to return the message so you exit the function.

const avg = ...
if (avg < 60) 

We don’t need interval testing if you go up in steps of 10.

< 60
< 70
< 80
< 90
else {
  return 'A';
}

#3

Thank you! Turns out that the console.log statement within my function should have been a return statement. I changed that and it worked! Thanks for the feedback and idea.