FAQ: Code Challenges: JavaScript Fundamentals - finalGrade()

This community-built FAQ covers the “finalGrade()” exercise from the lesson “Code Challenges: JavaScript Fundamentals”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

FAQs on the exercise finalGrade()

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

2 posts were split to a new topic: Is there logic behind this?

2 posts were split to a new topic: The “helper” tells me that there is an error

3 posts were split to a new topic: Where does my validation need to be?

9 posts were merged into an existing topic: Where does my validation need to be?

6 posts were split to a new topic: Error? Missing close bracket

A post was split to a new topic: Why doesn’t this code work? (check strings)

3 posts were merged into an existing topic: How to format switch/case?

4 posts were merged into an existing topic: Why doesn’t this code work? (check strings)

11 posts were split to a new topic: Missing brace? And troubleshooting function syntax?

3 posts were split to a new topic: Can I use an if statement with a false condition?

7 posts were split to a new topic: Defining variables within an if/else block

Hi! i state that my english level is pretty low. so sorry for this.
well, i’m stuck with the check instruction. my code that you can read below i think is correct, and it’s work, but it isn’t accept by the control step. i’m asking why.
thank you all

const finalGrade= (n1, n2, n3) => {
	const av= (n1+n2+n3)/3
  if (av>=0 && av<=59)
    return 'F';
  else if (av>=60 && av<=69)
    return 'D';
  else if (av>=70 && av<=79)
    return 'C';
  else if (av>=80 && av<=89)
    return 'B';
  else if (av>=90 && av<=100)
    return 'A';
  else if ((n1<0 || n1>100) || (n2<0 || n2>100) || (n3<0 || n3>100))
  return 'You have entered an invalid grade';
};

2 Likes

That needs to be checked at the top so you know you have valid data going forward. It should either kick out of the function (return) or let JS throw an error.

3 Likes

yes, i see the solution but i don’t understand why.

you mean that is not the same thing check data before or inside a function? sorry but i don’t understand the logic behind.

Analogy: If you were going to build a house, would you prefer to test the integrity of your building materials before you started, or after you had finished building?

If one or more of the inputs is invalid, there is no need to continue.

4 Likes

It is important to know that the values are, a) numbers, and, b) within range. Otherwise exit.

1 Like

Thank you both. now it’s clear. it was easy to understand in the end. sorry I’m at the beginning

3 Likes

Hey guys,

I stuck a bit with my function though.
It prints perfectly the grades, but it does not print if an invalid number has been entered.
Not sure though what I’m doin wrong and why.
I’m looking forward to some advise :slight_smile:

const finalGrade = (n1, n2, n3) => {
var average = ((n1 + n2 + n3) / 3);
if ((n1 < 0 || n1 > 100), (n2 < 0 || n2 > 100), (n3 < 0 || n3 > 100)) {
return “You have entered an invalid grade.”
} else if (average >= 0 && average < 60) {
return “F”
} else if (average > 59 && average < 70) {
return “D”
} else if (average > 69 && average < 80) {
return “C”
} else if (average > 79 && average < 90) {
return “B”
} else if (average > 89 && average < 101) {
return “A”
};
}

Replace the commas with || so it is one, complete expression. The additional parens are not needed.

4 Likes