Hey Everyone,
I was doing the CODE CHALLENGES: JAVASCRIPT FUNDAMENTALS

and it would not let me pass with my code. However, I thought my code to do it efficiently. Why might the training solution work out better if I am wrong here?

I am just trying to figure out why my code wouldn’t let me move on.

My solution:

``````// Write your function here:

const finalGrade = (num1, num2, num3) => {
const average = (num1 + num2 + num3) / 3;
// Find as string
if(average < 0 && average > 100) {
return 'You have entered an invalid grade.';
} 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';
} else {
return 'F';
};
};

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

``````

Training Solution

``````const finalGrade = (midterm, final, homework) => {
if ((midterm < 0 || midterm > 100) || (final < 0 || final > 100) || (homework < 0 || homework > 100)) {
return 'You have entered an invalid grade.'
}
let average = (midterm + final + homework) / 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'
}
}
``````

I remember doing the same mistake, it was exactly the same. This is what the Instructions say:

• return ‘You have entered an invalid grade.’ if any of the three grades are less than 0 or greater than 100

At the beginning of your `if` statement it just checks for the average number (if it is <0 or >100). But we need to check all of the 3 arguments. It should be an easy fix with the use of Logical Operators.

Funny that we just went for the shortcut At least we tried.

Cheers

1 Like

Ah, got it. That makes sense. Otherwise, input into the average could be wrong.

This makes sense. Thank you!

1 Like