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[0]) ? arguments[0] : 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'

``````if (...) {