Review JavaScript Fundamentals by Building a Gradebook App - Step 2

Tell us what’s happening:

Why is this code not working?I have followed all instructions to create a conversion to letter grades template and have double checked via AI.

Your code so far

function getAverage(scores) {
  let sum = 0;

  for (const score of scores) {
    sum += score;
  }

  return sum / scores.length;
}

// User Editable Region

function getGrade(score) {
if (score===100){return "A++"};
if (score>=90){return "A"};
if(80>=score && score< 90){return "B"};
if(70>=score && score< 80){return "C"};
if(60>=score && score< 70){return "D"};
if(0>=score && score< 60 ){return "F"};
}

console.log(getGrade(96));
console.log(getGrade(82));
console.log(getGrade(56));

// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0

Challenge Information:

Review JavaScript Fundamentals by Building a Gradebook App - Step 2

1 Like

Hello,
Simply because the conditions are reversed,
Look here, 80>=score && score< 90 this will return true when score is less than 90 and score is less than 80 at the same time, which is not what is required in the instructions

It’s still not working:

  let sum = 0;

  for (const score of scores) {
    sum += score;
  }

  return sum / scores.length;
}
function getGrade(score) {
if (score===100){return "A++"};
if (score < 90){return "A"};
if(80<=score && score>90){return "B"};
if(70<=score && score>80){return "C"};
if(60<=score && score>70){return "D"};
if(0<=score && score>60 ){return "F"};
}

console.log(getGrade(96));
console.log(getGrade(82));
console.log(getGrade(56));

Hi @zm17jaga

If you are a beginner I would advise against using AI for the moment. Learning to debug is a part of learning to code. You gain insights, skills, and build valuable knowledge. This process cannot occur is you obtain answers without thorough analysis and understanding.

  • Looking at getGrade(96), your code will skip the first if condition as 96 is not the same as 100. It will also skip the second if condition as 96 is not less than 90.

The third if statement is satisfied as 80 is less than or equal to 96 and 96 is greater than 90. This will return the string "B"

Since string "A" is not returned, test 2 will fail.

  • getGrade(82) will return "A" as 82 is less than 90`, so test 2 will fail.

Happy coding

order of the comparison isn’t correct. Variable score should come before less than or equal operator. Same for the rest and follow the above post for debug your code.