FAQ: Code Challenges: JavaScript Fundamentals - finalGrade()

#1

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!

split this topic #2

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

split this topic #3

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

#4

Hi everyone.
I am doing the finalGrade() exercise. Similary, I have encountered the same error as @megacoder73951: Where 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.’
Here are my codes:
// Write your function here:

const finalGrade = (num1, num2, num3) => {

const totalAmount = (num1 + num2 + num3) / 3;

if (totalAmount < 60) {

return ‘F’;

} else if (totalAmount >= 60 && totalAmount < 70) {

return ‘D’;

} else if (totalAmount >= 70 && totalAmount < 80) {

return ‘C’;

} else if (totalAmount >= 80 && totalAmount < 90) {

return ‘B’;

} else if (totalAmount >= 90 && totalAmount <= 100) {

return ‘A’;

} else {

return ‘You have entered an invalid grade.’;

}

}

console.log(finalGrade(90, 96, 92))

#5

Edit: There are some mistakes in my code - see comment in response to mine.

Hi John! I had the same issue at first, but then realised that the error message part shouldn’t be a part of the same if/then sequence as returning which ‘letter’ grade the averages lead to, but instead should be separate.

I’m not quite at the stage yet that it’s easy to explain what I’ve done and why - it makes sense in my head but once I try to put it into words I get confused - so I’ll just post my code block here and hope that helps.

const finalGrade = (num1, num2, num3) => {
average = (num1 + num2 + num3) / 3;
if ((num1 < 0 || num1 > 100) || (num2 < 0 || num2 > 100) || (num3 < 0 || num3 > 100)) {
return ‘You have entered an invalid grade.’
}
if (average <= 59) {
return ‘F’;
} else if (average <= 69) {
return ‘D’;
} else if (average <= 79) {
return ‘C’;
} else if (average <= 89) {
return ‘B’;
} else if (average <= 100) {
return ‘A’;
}
}

#6

As previously mentioned, we should not be doing any computation until after the validation has passed. Do the IF line, first, then compute the average when the inputs have been approved.

#7

Hi, thanks for the feedback! Hadn’t seen that mentioned previously, sorry about that.
So do you mean:

const finalGrade = (num1, num2, num3) => {
if ((num1 < 0 || num1 > 100) || (num2 < 0 || num2 > 100) || (num3 < 0 || num3 > 100)) {
return ‘You have entered an invalid grade.’
}
average = (num1 + num2 + num3) / 3;

Or should the average calculation be a part of the if/else statement? Would you mind explaining why this is important, that the computation should be after the validation? It may be obvious and a silly question, but still learning and hadn’t picked up on this until you mentioned it.

#8

It may have been a different thread, meant only as advice, not admonishment.

What you have now is the right idea.

Do you invite an intruder after letting them through the door? Or do you latch the door when you do not recognize them? Strange analogy, but makes the same general sense. It just makes things easier to comprehend going forward. Validation is a process that always comes first, especially when dealing with user inputs.

1 Like
#10

That’s a great analogy, thank you for taking the time to explain it to me! :slight_smile: Hope you are enjoying the holidays, and happy new year in advance.

1 Like
#11

Help please I’m stuck, I’m so bad at this…

// Write your function here:

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

let 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(60, 70, 75))

Shows an error saying number1 is not defined.

#12

Hint: Did you close your function body ahead of the critical code?

#13

I believe so…:sweat::sweat: can you help me please

#14

Locate the RBRACE (right curly brace) that closes your finalGrade function and move it to just above the last line where you log the result. That way all the critical code will be inside the function body, rather than out in space.

1 Like
#15

Oh my god… thank you

1 Like
#16

You’re welcome. It’s an easy mistake to make; and one to learn from.

Tip

Block out the code so all the bodies are visible…

const finalGrade () => {
  //
};

then,

const finalGrade () => {
  if () {
    return  // error
  }
  let average; // = ...
  if () {
    return  // letter
  }
  // remaining else if blocks
};

The code won’t run, of course, but we can fill in the blanks and be better assured that our syntax and structure is correct, or nearly so. I like to call it scaffolding or blocking where both terms are synonymous in this respect. We start with a sketch, then begin shading.

1 Like
#17

Please take a look at the following code :

const finalGrade = (grade1 , grade2 , grade3) =>
{ if (grade1 > 100 || grade1 < 0 || grade2 > 100 || grade2 < 0 || grade3 > 100 || grade3 < 0)
{return ‘You have entered one or more invalid grades’}
else {let avgGrade = Math.round((grade1 + grade2 + grade3)/3)

if (avgGrade < 60) {return ‘F’}
else if (avgGrade < 70) {return ‘D’}
else if (avgGrade < 80) {return ‘C’}
else if (avgGrade < 90) {return ‘B’}
else if (avgGrade < 101) {return ‘A’}
return avgGrade
}
}

the code above seems to be entirely correct. (tested with all the possible values for verification) unless I pasted it wrong which I don’t think would be the case.
Well,the code is quite similar to that of the system itself; the only small difference is the fact that I used an’if/else’ statement in another ‘else’ statement.
Yet I receive an error whenever I run it. I am well aware that the system can’t consider every possible method used by the users. This is merely an act towards awareness,and if possible , to improve (me) by telling me about the possible mistakes or errors. Thanks for reading,if you did.

#18

Is there a simpler way to check if multiple variables are within a certain numerical range? image

#19

There are other ways, but given the number of variables and conditions it’s doubtful any of them will be simpler. Take this bit of silliness, for example:

 > grade1 = -1, grade2 = 101, grade3 = -10
 > switch(true) {
     case 0 > grade1:
     case 0 > grade2:
     case 0 > grade3:
     case grade1 > 100:
     case grade2 > 100:
     case grade3 > 100:
       console.log('Invalid grade');
       break;
     default: console.log((grade1 + grade2 + grade3) / 3);
   }
<- Invalid grade
 > grade1 = 1, grade2 = 100, grade3 = 10
 > switch(true) {
     case 0 > grade1:
     case 0 > grade2:
     case 0 > grade3:
     case grade1 > 100:
     case grade2 > 100:
     case grade3 > 100:
       console.log('Invalid grade');
       break;
     default: console.log((grade1 + grade2 + grade3) / 3);
   }
<- 37
1 Like
#20

Thanks for that :+1:, I was trying with a switch statement before but then I realised that I was comparing a number (grade) to a conditional (0 > grade1) so it wouldn’t work.

1 Like
#21

Woah, I love this analogy! Thanks for that!

1 Like