Exams question failed - please advice what is wrong

Hi there, just failed one thing in my exam, but have no idea what is wrong with the function. It suppose to add any student with gpa higher than 3.5. this is my solution.

const students = [{
        name: "Paisley Parker",
        gpa: 4.0
    },
    {
        name: "Lake Herr",
        gpa: 3.2
    },
    {
        name: "Promise Lansing",
        gpa: 3.9
    },
    {
        name: "Friar Park",
        gpa: 2.8
    },
    {
        name: "Mason Amitie",
        gpa: 3.49
    }
]

function getDeansList(studentList) {
    let deansList = [];
    for (i = 0; i < studentList.length; i++) {
        if (studentList.gpa > 3.5) {
            deansList.push(i);
        }
    }
    return deansList
}

console.log(getDeansList(students))

Hi, there!

If you run the program, you’ll see that it logs an empty list:
image

The reason for this is that in your if statement, you are not accessing each individual student with the i variable.

Should instead be:

if(studentList[i].gpa > 3.5)

But if you run this, you will then notice that getDeansList will now log:
image

While technically pushing the position of the student, it would make more sense to push the name.

This line should then be:

deansList.push(studentList[i].name)

Which will then log:
image

1 Like

Thank you, legend. I can rest now as it bothered me all day

1 Like