FAQ: Code Challenges: JavaScript Fundamentals - finalGrade()

Man, thanks a bunch. This was helping me a lot. I fully missed that.

1 Like

You’re welcome. Happy coding!

Future Study

Something for down the road when you get to iterators…

const range = (a, b, c=1) => {
    y = []
    for (let x = a; x < b; x += c) {
    return y;

This will build an array with starting value, a included, ending value, b excluded, and a stride (or step) of c. We’ll use it below.

To start, let’s check the mechanics and return just the average from given arguments (or a single array of values)…

const finalGrade = function() {
    n = Array.isArray(arguments[0]) ? arguments[0] : Array.from(arguments);
    if (n.filter(x => range(0, 101).includes(x)).length < n.length) {
        return "You have entered an invalid grade."
    average = n.reduce((a, b) => a + b) / n.length;
    return average
 > finalGrade(50, 79, 99)
<- 76
 > finalGrade(50, 79, 99, 66)
<- 73.5
 > finalGrade([50, 79, 99, 66])
<- 73.5
 > finalGrade([50, 79, 99, -66])
<- "You have entered an invalid grade."

Note that the function is not defined with arrow syntax so it has the traditional objects of an ES5 function, namely, arguments.

The next step will be to evaluate the average and return a final letter grade. Your logic can be cleaned up considerably, but I’ll leave it for you to examine and refine when the subject of iterators comes around in your course. Don’t use this code in a solution, but bookmark it for future study.

How could you improve letter grade logic, ignoring the conditional testing for validity of the data points?

Thanks a bunch!
I definitely have to learn a bit more to make an better understanding of your example. :slight_smile:

You’re welcome. Forge ahead and stick to basics. There will be time to explore the above some time down the road when you have more tools in your kit.

The thing to bear in mind is keeping validation at the top of your code to eliminate invalid inputs before any computation using the supplied values.

 if ... => return "invalid input"
 compute average
 return letter grade
1 Like


Is there a way to make this more concise?

if ((midterm < 0 || midterm > 100) || (final < 0 || final > 100) || (homework < 0 || homework > 100))

for three variables it seems fine, but what if we were to check against a whole list?

Is there a way in JS to do something like the below (of course this isn’t JS, just to give an impression)?

if (num1,num2,num3 > 100 || num1,num2,num3 < 0 )


It would be nice if we could use that logic, but, alas, we cannot. Comparisons (relations) can only have two operands…

A > B

Unlike Python and one supposes some other languages the association ends there. Python let’s us use inequality relations

0 <= A <= 100

Above we used some rather elaborate code which is one possible solution, but not in the scope of the lessons leading up to this point, so foreign. The concepts there do come up, but later in the course.

We know there are three inputs, so can write for that scenario, starting with an array.

let array = [midterm, final, homework]

That makes it iterable so we can use a loop.

let average = 0;
for (let x of [midterm, final, homework]) {
    if (x < 0 || x > 100) {
        return "You have entered an invalid grade.";
    average += x;
average /= 3;

Which brings us to the letter selection phase.

Granted, the code is not any more concise, but it is another approach. Here’s a look at how we could use a switch…

function letterGrade(a, b, c) {
    switch (true) {
    case a < 0:
    case a > 100:
    case b < 0:
    case b > 100:
    case c < 0:
    case c > 100: return "You have entered an invalid grade."
    //default: return "Input validated"
    let average = (a + b + c) / 3
    return average;

Both examples above return the average but your solution will return a letter grade.

Let us know if you come up with another approach.

Grand, thanks for the detailed response. Iterating through it sounds like the most convenient way to do this.

1 Like

The advantage being the array can be any length, not just three elements.

Hello. If someone would be so kind as to help me. I am getting the “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.’” message, althought my code is giving the right responses when printing.
I have used the following:

// Write your function here:
const finalGrade = (grade1, grade2, grade3) => {
  const gradeAverage = Math.floor((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 if(gradeAverage < 60) {
    return 'You have scored an F';
  } else if(gradeAverage < 70) {
    return 'You have scored a D';
  } else if(gradeAverage < 80) {
    return 'You have scored a C';
  } else if(gradeAverage < 90) {
    return 'You have scored a B';
  } else {
    return 'You have scored an A';

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

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

Your code is fine but for one minor detail… It computes the average before validation of the inputs.

if (...) {
    return "Invalid..."
// compute average after validation
if (...) {

I tried this project and my code seems to output correctly and work fine. However, the website keeps telling me 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 logged multiple variations and always get the correct output. if < 0 invalid grade if >100 invalid grade. but still have this error. Is there something wrong with my syntax?..

const finalGrade = (numA,numB,numC) => {

if ((numA < 0 || numA > 100) || (numB < 0 || numB >100 ) || (numC < 0 || numC > 100)) {
return ‘invalid grade’

const average = (numA + numB + numC) / 3;
if (average < 60) {
return ‘F’;
} else if (average < 70) {
return ‘D’;
} else if (average <80) {
return ‘C’;
} else if (average <90) {
return ‘B’;
} else {
return ‘A’;

Nevermind. LOL i just found out that it was throwing an error because

I typed “invalid grade”

instead of…

“you have entered an invalid grade.”

Good that you found your error.

Hey Guys,
I don’t know if I do this right. But I just don’t find the mistake in my coding. Any Chance somebody sees it ? It’s probably something really small… I always get the Syntax Error message

function finalGrade(grade1, grade2, grade3) {
if((grade1 > 100 || grade 1 < 0) || (grade2 >100 || grade2 < 0) || (grade3 > 100 || grade3 < 0)) {
return ‘You have entered an invalid grade.’
} let average = (grade1 + grade2 + grade3) / 3;
if(average < 60) {
return ‘F’;
} else if(average < 70){
return ‘D’;
} else if(average < 80 ) {
return ‘C’
} else if(average < 90) {
return ‘B’
}else {
return ‘A’

Can you please post the error message?

This would be the error I get:

if((grade1 > 100 || grade 1 < 0) || (grade2 >100 || grade2 < 0) || (grade3 > 100 || grade3 < 0)) {
SyntaxError: Unexpected number
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)

Hi guys,
I used this code, because I liked to use switch.
Why was it not approved by the course program? It is just as easy.
Otherwise, perhaps a remark like ‘only use if-clauses’ would save time.
Thank you for the answer!

// Write your function here:

function finalGrade(num1,num2,num3) {
  const averageGrade=Math.floor((num1 + num2 + num3)/30);
    return 'You have entered an invalid grade.'
  } else if (averageGrade<6){
    return 'F'
  } else {
      switch (averageGrade){
        case 6:
        return 'D';
        case 7:
        return 'C'
        case 8:
        return 'B'
        return 'A'

Hello, @webd_johan_b.

Welcome to the forums! Your code was not rejected because it uses switch. You should recall from the instructions that 0 is a valid grade.


You might also consider checking the validity of the grades before determining the average. No need to bother calculating the average if one or more of the grades is not valid. :wink:

Also, please review this post: How do I format code in my posts?

I formatted your code in your post above.

Happy Coding!