Why doesn't this code work? (check strings)

Please take a look at the following code :

const finalGrade = (grade1 , grade2 , grade3) =>
{ if (grade1 > 100 || grade1 < 0 || grade2 > 100 || grade2 < 0 || grade3 > 100 || grade3 < 0)
{return ‘You have entered one or more invalid grades’}
else {let avgGrade = Math.round((grade1 + grade2 + grade3)/3)

if (avgGrade < 60) {return ‘F’}
else if (avgGrade < 70) {return ‘D’}
else if (avgGrade < 80) {return ‘C’}
else if (avgGrade < 90) {return ‘B’}
else if (avgGrade < 101) {return ‘A’}
return avgGrade

the code above seems to be entirely correct. (tested with all the possible values for verification) unless I pasted it wrong which I don’t think would be the case.
Well,the code is quite similar to that of the system itself; the only small difference is the fact that I used an’if/else’ statement in another ‘else’ statement.
Yet I receive an error whenever I run it. I am well aware that the system can’t consider every possible method used by the users. This is merely an act towards awareness,and if possible , to improve (me) by telling me about the possible mistakes or errors. Thanks for reading,if you did.

Why is this code invalid?
As far as i have tested it does what is required, yet I get the error “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.’”
But i have tested all instance and it does what is required…

const finalGrade = (numberOne, numberTwo, numberThree) => {

const average=((numberOne+ numberTwo + numberThree)/3);

if ((numberOne < 0 || numberOne > 100) || (numberTwo < 0 || numberTwo > 100) || (numberThree < 0 || numberThree > 100)){
return ‘You have entered and invalid grade.’;

else if(average <= 59){
return ‘F’;

else if(average > 59 && average <= 69){
return ‘D’;

else if(average > 69 && average <= 79){
return ‘C’;

else if(average > 79 && average <= 89){
return ‘B’;

else if(average > 89 && average <= 100){
return ‘A’;

else {
return ‘Error #001, please enter a valid number’;


console.log(finalGrade(100, 100, 100)) // Should print 'A

it was the typo in the return string…
I had written “… anD invalid grade.” not “…an invalid grade.”

I mean i get that syntax is important in coding, but it feels kind of besides the point to require the exact string. Besides in a real situation this would not give us an error, it would just include our typo in the return value at which point we would have to detect it ourselves anyways…

I understand the attempt to really make us triple check everything we type, but maybe this is holding our hands to much, considering in a real situations we wont have this “crutch” of telling us we mistyped a string…

At this stage the real world scenario is a ways off and the least of our concerns. This is a controlled environment, not the real world. Push the river and river pushes back. Go with the flow and everything becomes easier. Put your expectations aside and build your house one brick at a time.

The real world scenario starts tomorrow for me with a new job, so yeah there is that…

1 Like