Review JavaScript Fundamentals by Building a Gradebook App - Step 4

Tell us what’s happening:

What is the right code to this? I’m stuck on this one…

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) {

}

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";
}


console.log(studentMsg([92, 88, 12, 77, 57, 100, 67, 38, 97, 89], 37));

// 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/119.0.0.0 Safari/537.36 Edg/119.0.0.0

Challenge Information:

Review JavaScript Fundamentals by Building a Gradebook App - Step 4

What have you tried so far?

Do you understand what the function needs to output? You’ll need to return a string.

You can start by implementing one of the tips:

  • Use the getAverage function to get the class average.
  • Use the getGrade function to get the student’s grade.
  • Use string concatenation (+) to build the message.

This is my updated code, but I do not know the way forward from here.

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”;
}

function studentMsg(totalScores, studentScore) {
let average = getAverage(totalScores);
let grade = getGrade(studentScore);
let status = hasPassingGrade(studentScore) ? “passed” : “failed”;
return
}

console.log(studentMsg([92, 88, 12, 77, 57, 100, 67, 38, 97, 89], 37));

You have everything you need now to build the string that the studentMsg() function should return. Why don’t you try to build that string now based on the instructions. Is there something you don’t understand about that?

What kind of argument does the hasPassingGrade(score) function take?

Are you passing it the correct kind of argument? hasPassingGrade(studentScore)

Do you get any errors?

Use console.log() to print some of your new variables to the console and see if they are what you expect them to be.

This is my updated code blow, and it still says that studentMsg([92, 88, 12, 77, 57, 100, 67, 38, 97, 89], 37) should return the following message: "Class average: 71.7. Your grade: F. You failed the course." . What do i do from here?

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";
}

function studentMsg(totalScores, studentScore) {
  
    if (getGrade(studentScore) !== "F") {
    return "Class average: " + getAverage(totalScores) + "."+  "Your grade: " + getGrade(studentScore) + "."+ "You passed the course.";
  } else {
    return "Class average: " + getAverage(totalScores) + "."+  "Your grade: " + getGrade(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));

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Did you test that input? What does your function return?

You mean the studentmsg function?

Maybe I should just reset the code and start again with your directions.

What does your function return?

Did you compare the output?

Is it the same?

Do you think so?

It seems like you are almost done?

Thank you. The code has passed.

1 Like