FAQ: Code Challenges: JavaScript Fundamentals - finalGrade()

Good that you found your error.

Hey Guys,
I don’t know if I do this right. But I just don’t find the mistake in my coding. Any Chance somebody sees it ? It’s probably something really small… I always get the Syntax Error message

function finalGrade(grade1, grade2, grade3) {
if((grade1 > 100 || grade 1 < 0) || (grade2 >100 || grade2 < 0) || (grade3 > 100 || grade3 < 0)) {
return ‘You have entered an invalid grade.’
} let average = (grade1 + grade2 + grade3) / 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’
}
}

Can you please post the error message?

This would be the error I get:

/home/ccuser/workspace/js-challenge-final-grade/main.js:3
if((grade1 > 100 || grade 1 < 0) || (grade2 >100 || grade2 < 0) || (grade3 > 100 || grade3 < 0)) {
^
SyntaxError: Unexpected number
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)

Hi guys,
I used this code, because I liked to use switch.
Why was it not approved by the course program? It is just as easy.
Otherwise, perhaps a remark like ‘only use if-clauses’ would save time.
Thank you for the answer!

// Write your function here:

function finalGrade(num1,num2,num3) {
  const averageGrade=Math.floor((num1 + num2 + num3)/30);
  if(num1<1||num2<1||num3<1||num1>100||num2>100||num3>100){
    return 'You have entered an invalid grade.'
  } else if (averageGrade<6){
    return 'F'
  } else {
      switch (averageGrade){
        case 6:
        return 'D';
        case 7:
        return 'C'
        case 8:
        return 'B'
        default:
        return 'A'
      }       
  }
}

Hello, @webd_johan_b.

Welcome to the forums! Your code was not rejected because it uses switch. You should recall from the instructions that 0 is a valid grade.

image

You might also consider checking the validity of the grades before determining the average. No need to bother calculating the average if one or more of the grades is not valid. :wink:

Also, please review this post: How do I format code in my posts?

I formatted your code in your post above.

Happy Coding!

I am still getting an output of “undefined” with the code below. Can someone explain to me why. Thank you.

const finalGrade = (midterm, final, homework) => {

if ( midterm > 100 || midterm < 0 || final > 100 || final < 0 || homework > 100 || homework < 0)

{ return ‘You entered an invalid grade’
}
let average = midterm + final + homework / 3
if (average < 59) {
return ‘F’
}
if (average < 69) {
return ‘D’
}
if (average < 79) {
return ‘C’

}
if (average < 89) {
return ‘B’

}
if (average < 100) {
return ‘A’
}
}

console.log(finalGrade(95, 96, 97)) // Should print ‘A’

Not necessarily the actual problem, but it is a problem, all the same. Due to order of operations, that above will divide homework by 3 and then add it to the other two operands. Grouping will be necessary to ensure the addition takes place first.

(a + b + c) / 3

As it turns out, that was the problem.

95 + 96 + 32  =>  223

223 is not less than any of the quantities in the conditionals.

1 Like

In addition to what @mtf has said, you may want to consider using <= rather than <, or changing the values. For example, what if the average is exactly 59? 59 should return F not D. What will be returned if a student has an average of 100?

Hi, I’m trying to use shorter function command but it turned out error. Wonder where did I go wrong?

I’ve tried using if, else if and it works.

So I tried the following:

console.log(finalGrade(99, 92, 105));
The code seems to work, but nonetheless I am presented with an error message that I am not checking for invalid grades. But as you can see from the grades entered I have entered an invalid grade and the correct output is printed to the console. Can anyone explain why I am getting this error message?

a, b, c

is a sequence. We cannot compare it to a number.

a < 0 || b < 0 || c < 0

and so on.

Ok… but then why does it still work? Apparently it is syntactically correct and it makes the code more concise. I was trying the same in Visual Studio Code and don’t get any error messages or indications of problems. I did find out that by playing with the parameters that I only get the result “You have entered an invalid grade” when grade3 is larger than 100. When change grade1 and grade2 to invalid grades(<0 or >100) it still calculates a result or returns ‘undefined’. I am really trying to understand the logic behind this.
Once again thanks for your feedback!

1 Like

Ok, learned an important lesson today. Even with the rewritten code as shown in previous posts I kept getting the error message. Turns out it all come down to a missing ‘.’ in the string for the error message. The Codecademy platform is apparently very strict in checking the string text as well. :face_with_monocle: Check, check and check again…

let a, b, c
console.log(a, b, c < 0)
// undefined undefined false

Now,

a = -1, b = 0, c = 1
console.log(a, b, c < 0)
// -1 0 false
console.log(a < 0, b < 0, c < 0)
// true false false
if (a, b, c < 0) console.log('Huh?')
//undefined
if (a, b, c < 0 || a, b, c > 0) console.log('Huh?')
// Huh?

Some part of that expression is true or it wouldn’t log.

It would be a mistake to assume that just because it appears to work that it is syntactically correct; it’s just not raising any errors. There is obviously a problem, though.

:+1: thanks again for your feedback and clarification!

1 Like

Thank you for pointing out the need for the ‘.’ in the string. I’ve just been tearing my hair out trying to figure out what my codes missing! now it finally works. Needed the full stop.

Getting ReferenceError: final is not defined.
Can anyone help?

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

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

The interpreter may not see the correct structure owing to missing curly braces. It looks like the function closes after the first if statement.

Thanks for the help buddy.Now I can proceed further without any backlogs.

1 Like