FAQ: Code Challenges: JavaScript Fundamentals - finalGrade()

I’m getting the correct output (A) but an additional “undefined” keeps appearing beneath it.
What am I doing wrong?

const finalGrade = (num1, num2, num3) => {
let average = Math.round((num1 + num2 + num3) / 3);
if ((num1 < 0 || num1 > 100) || (num2 < 0 || num2 > 100) || (num3 < 0 || num3 > 100)) {
console.log(‘You have entered an invalid grade.’);
} else if (average >= 0 && average <=59) {
console.log(‘F’);
} else if (average >= 60 && average <= 69) {
console.log(‘D’);
} else if (average >= 70 && average <= 79) {
console.log(‘C’);
} else if (average >= 80 && average <= 89) {
console.log(‘B’);
} else if (average >= 90 && average <= 100) {
console.log(‘A’);
}
};

Because your function is doing the logging, there is nothing to return. This results in undefined being returned and echoed as the last response from the program; If you really want to get rid of it, then a rethink will be necessary.


Aside

Look for my previous rants about betweenness. Do we really need that level of logic?

I got what you meant and removed the extra comparisons operators and console.log’s and it works perfectly. I was reading into the instructions too much, sometimes the answer is simpler than you think. THANK YOU!

1 Like

Hi everyone,

In the finalgrade code challenge (5/15) I get the right output (A) but I still get an error message:

'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.’1

I’ve just started with JS for a couple of weeks ago and would love to get an explanation of what I’ve missed.

We need to check the num values, not their average. That shouldn’t be computed until all the values have been validated.

1 Like

Great thanks! Took a little time to understand but got it to work now :slight_smile:

1 Like

Please i have been Struggling with LINE 3 and 4,

  • i am trying to move Line 3 and 4 to finalGrade function without rewriting the if else statement like i did in Line 11 and 12… ! Thanks

I’m not sure what I’m doing wrong here. I am calculating the grade average correctly however underneath the grade average it also says undefined. I’m not sure why a second result would even print to the console after the correct grade average has already been published to console, and furthermore have no idea why that second value is undefined.

Here is my code:
`finalGrade = (num1, num2, num3) => {
if ((num1 < 0 || num1 > 100) || (num2 < 0 || num2 > 100) || (num3 < 0 || num3 > 100)) {
console.log(‘You have entered an invalid grade’)
}
let average = ((num1 + num2 + num3) / 3);
if (average < 60) {
console.log(‘F’)
} else if (average < 70) {
console.log(‘D’)
} else if (average < 80) {
console.log(‘C’)
} else if (average < 90) {
console.log(‘B’)
} else if (average > 90) {
console.log(‘A’)
}
}

console.log(finalGrade(99, 92, 95))`

The output will be different if instead of logging the letter grade it is returned.

 return 'A'

As it stands right now, your function returns undefined by default. That is what gets logged in the end.

Hi, programmers.
I keep having this problem

function finalGrade (first, second, third) {
  if ((first < 0 || first > 100) || (second < 0 || second > 100) || (third < 0 || third > 100)) {
    return 'You have entered an invaild grade.'
  } else {average = 
          Math.round((first + second + third) / 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'
  }
}

and Codecademy tells me I should still set for numbers less than 0 and more than 100.
I don’t see the problem here.

Word of advice - copy and paste is your friend for these lessons. I have just been scratching my head for 15 minutes as my code worked fine but I kept getting an error saying if the arguments inputted are invalid return the string “You have entered an invalid grade.”

I missed “.” and that was it! So yeah, copy and paste ha!

Here is my solution - but originally I was trying to pass my gradeArray with a .forEach but kept failing the <0 || >100 test, and I couldn’t figure out why. I tried googling returning from nested functions to see if that’s where my issue lay.

Was I just failing the tests because I was returning something for each grade, rather than just a single return (which .some solved)? Or should my .forEach have worked, but I’m just missing something silly?

   gradeArray.forEach(grade => {
        if (grade < 0 || grade > 100) {
          return 'You have entered an invalid grade.'}
      })
// Write your function here:

const finalGrade = (grade1, grade2, grade3) => {

    let gradeArray = [grade1, grade2, grade3];

    

    if (gradeArray.some(grade => (grade < 0 || grade > 100))) {

            return 'You have entered an invalid grade.'

    }

  

   sum = gradeArray.reduce((acc, current) => {

        return acc+current

    })/gradeArray.length;

    return (sum <= 59) ? 'F' :

    (sum <= 69) ? 'D' :

    (sum <= 79) ? 'C' :

    (sum <= 89) ? 'B' : 'A';

  }

The .forEach() approach might not have worked because we cannot break iteration. return is a form of break, one might suppose from this experience.

Code is working but still getting error?

// Write your function here:
let finalGrade = (homework, midterm, final) =>{
  let numbers = (homework + midterm + final)/3
if (numbers < 0 || numbers > 100) {
  return `You have entered an invalid grade.`
} else {
  switch (true){
case (numbers < 60):
return 'F'
break;
case (numbers < 70):
return 'D'
break;
case (numbers < 80):
return 'C'
break;
case (numbers < 90):
return 'B'
break;
case (numbers < 101):
return 'A'
break;
  }
 }
}
console.log(finalGrade(-1,-1,-1)); //returns `You have entered an invalid grade`
console.log(finalGrade(100,101,100)); //returns `You have entered an invalid grade`
console.log(finalGrade(99,92,95)); //returns A




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

// We encourage you to add more function calls of your own to test your code!

It will not let me move on to next exercise…

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.’1

Problem Solved

- I had to switch numbers with average (This may not have been the issue but I did it because the exercise asked for 'averages' and the word 'averages' was highlighted)

- I also had to check each parameter individually (homework, midterm, final)

Original Attempt:

let numbers = (homework + midterm + final)/3
if (numbers < 0 || numbers > 100)

Second Attempt:

let average = (homework + midterm + final)/3
if ((midterm < 0 || midterm > 100) || (final < 0 || final > 100) || (homework < 0 || homework > 100))

New Code:

// Write your function here:
let finalGrade = (homework, midterm, final) =>{
  let average = (homework + midterm + final)/3
if ((midterm < 0 || midterm > 100) || (final < 0 || final > 100) || (homework < 0 || homework > 100)) {
  return `You have entered an invalid grade.`
} else {
  switch (true){
case (average < 60):
return 'F'
break;
case (average < 70):
return 'D'
break;
case (average < 80):
return 'C'
break;
case (average < 90):
return 'B'
break;
case (average < 101):
return 'A'
break;
  }
 }
}
console.log(finalGrade(-1,-1,-1)); //returns `You have entered an invalid grade`
console.log(finalGrade(100,101,100)); //returns `You have entered an invalid grade`
console.log(finalGrade(99,92,95)); //returns A




// Uncomment the line below when you're ready to try out your function
// console.log(finalGrade(99, 92, 95)) // Should print 'A'
// We encourage you to add more function calls of your own to test your code!```

Hope this helps someone else that tried to do a switch statement instead of an if statment.

-Cheers-