Tell us what’s happening:
“Hello everyone! I’m working on a function that needs to determine whether a student has passed or failed based on their score. However, I’m encountering an issue with the logic and could use some guidance. Could anyone help me troubleshoot this?”
Your code so far
// Function to get the average of an array of numbers
function getAverage(numbers) {
let sum = 0;
for (let num of numbers) {
sum += num;
}
return Math.round((sum / numbers.length) * 10) / 10; // rounded to 1 decimal
}
// Function to return grade based on score
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) {
if (grade === "F") {
return false;
} else {
return true;
}
}
// Function to return the full message
function studentMsg(totalScores, studentScore) {
const average = getAverage(totalScores);
const grade = getGrade(studentScore);
const passed = hasPassingGrade(grade);
const message = passed ? "You passed the course." : "You failed the course.";
return `Class average: ${average}. Your grade: ${grade}. ${message}`;
}
//Sample Test Cases:
console.log(studentMsg([92, 88, 12, 77, 57, 100, 67, 38, 97, 89], 37));
//"Class average: 71.7. Your grade: F. You failed the course."
console.log(studentMsg([56, 23, 89, 42, 75, 11, 68, 34, 91, 19], 100));
//"Class average: 50.8. Your grade: A+. You passed the course."
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Challenge Information:
Build a Gradebook App - Build a Gradebook App