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.
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.
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.
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!
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.