FAQ: Code Challenges: JavaScript Fundamentals - finalGrade()

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

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.

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.

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

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

1 Like

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.

1 Like

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
 > 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?