Review JavaScript Fundamentals by Building a Gradebook App - Step 4

Tell us what’s happening:

Hello

Can someone tell me why I have “failed” printed in the second console.log?

I assume there is a bug in my code where the “if” statement is read as false, so the “else” statement is returned instead.

but I’m not sure what the issue is in my “if” statement? Did I type the wrong syntax?

Your code so far

function getAverage(scores) {
  let sum = 0;

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

  return sum / scores.length;
}

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

function hasPassingGrade(score) {
  return getGrade(score) !== "F";
}


// User Editable Region

function studentMsg(totalScores, studentScore) {
  totalScores = getAverage(totalScores);
 studentScore = getGrade(studentScore);
 if (studentScore === "A++", studentScore === "A", studentScore === "B", studentScore ==="C", studentScore === "D") {
   return "Class" + " " + "average:" + " " + totalScores + "." + " " + "Your" +" " + "grade:" +" " + studentScore + "." + " " + "You" + " " + "passed" + " " + "the" + " " + "course.";
 } else {
    return "Class" + " " + "average:" + " " + totalScores + "." + " " + "Your" +" " + "grade:" +" " + studentScore + "." + " " + "You" + " " + "failed" + " " + "the" + " " + "course.";
 }
}


console.log(studentMsg([92, 88, 12, 77, 57, 100, 67, 38, 97, 89], 37));
console.log(studentMsg([56, 23, 89, 42, 75, 11, 68, 34, 91, 19], 100));

// 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/133.0.0.0 Safari/537.36

Challenge Information:

Review JavaScript Fundamentals by Building a Gradebook App - Step 4

This is not a valid way to write a conditional statement:

EXAMPLE:

// using logical AND operator for multiple conditions
if (studentScore === "A++" && studentScore === "A" && studentScore... etc)

…but there’s a much logically simpler way to make this condition. If all of those are true what must not be true?

…but you also have a hasPassingGrade function. Why not use that instead?

1 Like

Thank you a lot!

by the way do you mind to explain what my previous “if” condition was reading? was it the last part where studentScore === “D”? or it was defaulted to false? I thought if the condition was written incorrectly it would give me “undefined” or some kind of error

I would not overwrite the inputs like this

a comma separated list does not work in the way you want, a comma separated list will give back the value of the last item in the list, so practically you are checking only for studentScore === "D"

you need to use a logic operator if you want to combine conditions like this, OR (||) or AND (&&)

1 Like

How would u do it? I did not like doing it, but I assumed it was the fastest way to do it

If you want to store the value returned by the getAverage and getGrade functions, I would choose different variable names than those already in use (e.g. average and grade).
Also, to determine whether the student has a pass or fail, use the hasPassingGrade function.

1 Like

I would make new variables. Overwriting variables isn’t especially ‘fast’ compared to making a new one.

1 Like