Review JavaScript Fundamentals by Building a Gradebook App - Step 4

Tell us what’s happening:

ur studentMsg function should return the correct message based on the student’s score and the class average.
// tests completed
// console output
Class average: 71.7. Your grade: F. You failed the course.
Class average: 85.4. Your grade: F. You failed the course.
Class average: 85.4. Your grade: B. You passed the course.

Your code so far


// User Editable Region

function getAverage(scores) {
    if (scores.length === 0) {
        return 0;
    }
    
    let totalSum = 0;
    for (let i = 0; i < scores.length; i++) {
        totalSum += scores[i];
    }
    
    return totalSum / 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 if (score < 59) {
        return "F";
    }
}

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

function studentMsg(totalScores, studentScore) {
    let classAverage = getAverage(totalScores).toFixed(1); // Round to one decimal place for readability
    let studentGrade = getGrade(studentScore);
    let passed = hasPassingGrade(studentScore);
    
    let message = "Class average: " + classAverage + ". Your grade: " + studentGrade + ". ";
    if (passed) {
        message += "You passed the course.";
    } else {
        message += "You failed the course.";
    }
    
    return message;
}


// Example function calls to test the code
console.log(studentMsg([92, 88, 12, 77, 57, 100, 67, 38, 97, 89], 37));  // Expected: "Class average: 71.7. Your grade: F. You failed the course."
console.log(studentMsg([45, 87, 98, 100, 86, 94, 67, 88, 94, 95], 55));  // Expected: "Class average: 85.4. Your grade: F. You failed the course."
console.log(studentMsg([45, 87, 98, 100, 86, 94, 67, 88, 94, 95], 85));  // Expected: "Class average: 85.4. Your grade: B. You passed the course."


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

Challenge Information:

Review JavaScript Fundamentals by Building a Gradebook App - Step 4

hi and welcome to the forum!

You have some beautiful code here. Did you have a specific question though?
(I noticed you are doing some rounding as well, but I don’t recall the project needing that)

As said, don’t do anything you are not asked to do.

The Tests

assert.strictEqual(studentMsg([33, 44, 55, 66, 77, 88, 99, 100], 92), "Class average: 70.25. Your grade: A. You passed the course.");
assert.strictEqual(studentMsg([33, 44, 55, 66, 77, 88, 99, 100], 57), "Class average: 70.25. Your grade: F. You failed the course.");

The above method is not required for this challenge.
@humeshdeshmukh0