 # 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) {
y.push(x);
}
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) ? arguments : Array.from(arguments);
console.log(n);
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
<- 73.5
<- 73.5
<- "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. 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
1 Like

Hi,

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 )
``````

Thanks,
Twan

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:
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'

``````

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)) {
}

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

“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

return ‘You have entered an invalid grade.’
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:

^
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.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.

``````function finalGrade(num1,num2,num3) {
const averageGrade=Math.floor((num1 + num2 + num3)/30);
if(num1<1||num2<1||num3<1||num1>100||num2>100||num3>100){
return 'You have entered an invalid grade.'
return 'F'
} else {
case 6:
return 'D';
case 7:
return 'C'
case 8:
return 'B'
default:
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. Also, please review this post: How do I format code in my posts?

Happy Coding!

I am still getting an output of “undefined” with the code below. Can someone explain to me why. Thank you.

const finalGrade = (midterm, final, homework) => {

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

{ return ‘You entered an invalid grade’
}
let average = midterm + final + homework / 3
if (average < 59) {
return ‘F’
}
if (average < 69) {
return ‘D’
}
if (average < 79) {
return ‘C’

}
if (average < 89) {
return ‘B’

}
if (average < 100) {
return ‘A’
}
}

console.log(finalGrade(95, 96, 97)) // Should print ‘A’

Not necessarily the actual problem, but it is a problem, all the same. Due to order of operations, that above will divide `homework` by `3` and then add it to the other two operands. Grouping will be necessary to ensure the addition takes place first.

``````(a + b + c) / 3
``````

As it turns out, that was the problem.

``````95 + 96 + 32  =>  223
``````

223 is not less than any of the quantities in the conditionals.

1 Like