Exam Question, getDeansList() failed?

Hello. I don’t understand why this is failing?

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(let i = 0; i < studentList.length; i++) { 
    if(studentList[i].gpa >= 3.5){
      deansList.push(studentList[i].name)
    }
return deansList
    }
  }


console.log(getDeansList(students))

The question is " Complete the code below so that the getDeansList() function accepts a list of student objects, and returns an array of the student names who are on the Dean’s List based on their GPA of 3.5 or higher."

The code returns the only student who meets these requirements but the question comes back saying that its wrong.

2 Likes

Since it is an exam question, so I will try not to offer any solutions.

Is there only one student in the students array who meets the criteria? I can definitely see more than one student.

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(let i=0; i<students.length; i++) {
if (students[i].gpa > 3.5) {
deansList.push(students[i].name)
}
return deansList
}
}

console.log(getDeansList(students))

Fellas. I really need your help. I"ve been stuck for a couple of days. If I run this code I only get one student who has a gpa more than 3.5. I’m supposed to get two students…Please help.

1 Like

Just here to say I’m stuck on the same question, 20 minutes on one question, no hints, and no ability to skip without failing! Uhg! Googling until I find the way to solve!

---------SOLVED---------
ANSWER IS BELOW!
Please don’t peek until you’ve suffered a bit, I trust you all.

I began solving it by finding this Stack Overflow question

My initial reaction was to use a for loop, but after finding this answer it made sense enough to finish it on my own.
I don’t completely understand for..of , but I got enough to pull this off!

You can read about for..of here

-------ANSWER IS BELOW-------
-------NO PEEKING-------

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 (const {gpa, name} of studentList) {
    if(gpa >= 3.5){
      deansList.push(name)
    }
  }
  return deansList
}

console.log(getDeansList(students))

Thanks for your help but it didn’t work on me. Stil logging only one student with gpa of 4.0…I’ve tried with for …in loop as well but the result is the same.

Think about where your return statement should be placed.

Consider the three possibilities and decide which of them makes sense.

Should the return statement be part of the if statement’s body?
OR
Should the return statement be part of the for statement’s body?
OR
Should the return statement be part of the function’s body?

The results will vary significantly depending on which option you choose.

Here is a related post about the positioning of the return statement in Python. It talks about indentation, but in JavaScript we use curly braces (instead of indentation) to create blocks of code. However the underlying issue is similar to the mistake you are making.

3 Likes

Thank you. finally solved.

2 Likes