function getAverage(arr) {
if (arr.length === 0) return 0;
let sum = 0;
for (let i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum / arr.length;
}
function getGrade(score) {
if (score === 100) return "A+";
if (score >= 90) return "A";
if (score >= 80) return "B";
if (score >= 70) return "C";
if (score >= 60) return "D";
return "F";
}
function hasPassingGrade(grade) {
return grade !== "F";
}
function studentMsg(scores, studentScore) {
const average = getAverage(scores);
const grade = getGrade(studentScore);
if (hasPassingGrade(grade)) {
return "Class average: " + average +
". Your grade: " + grade +
". You passed the course.";
} else {
return "Class average: " + average +
". Your grade: " + grade +
". You failed the course.";
}
}
It says :
" 1. The hasPassingGrade function should use the getGrade function to get the letter grade, and use it to determine if the grade is passing. A passing grade is anything different from "F"."
I wrote exactly what it told me to write:
function hasPassingGrade(grade) {
if (grade !== "F") {
return true;
} else {
return false;
}
}
The tests pass. But now 23, 24, 25, and 26 are failing
Tests 23 - 26:
studentMsg([56, 23, 89, 42, 75, 11, 68, 34, 91, 19], 100) should return the following message: "Class average: 50.8. Your grade: A+. You passed the course.".
Failed:24. studentMsg([12, 22, 32, 42, 52, 62, 72, 92], 85) should return the following message: "Class average: 48.25. Your grade: B. You passed the course.".
Failed:25. studentMsg([15, 25, 35, 45, 55, 60, 70, 60], 75) should return the following message: "Class average: 45.625. Your grade: C. You passed the course.".
Failed:26. Your studentMsg function should return the correct message based on the student’s score and the class average.
My code:
function studentMsg(scores, studentScore) {
const average = getAverage(scores);
const grade = getGrade(studentScore);
if (hasPassingGrade(grade)) {
return "Class average: " + average +
". Your grade: " + grade +
". You passed the course.";
} else {
return "Class average: " + average +
". Your grade: " + grade +
". You failed the course.";
}
}