A newbie here. I started to learn some coding like 1 weeks ago. And I’m having trouble with my code. I just want to print employers name who’s score is bigger than 4 .
Here is my code:
const employers = [
{ name: "Adam", score: 5 },
{ name: "Jane", score: 3 },
{ name: "Lucas", score: 3 },
{ name: "Francesca", score: 2 },
{ name: "Mason", score: 4 }
]
function getEmployer(employerList) {
let List = [employers];
for (let i = 0; i < List.length; i++) {
if(List['score'] > 4 ) {
return List[i].name;
}
}
}
console.log(getEmployer(employers))
Could you give me some details about it ?
3
Answers
Here is how I would do it, similar to what you did. See the comments next to the lines for explainations.
No need to use a
for
loop here when you can useforEach
– you can loop directly through your array that is passed as an argument to the function.Also, added an array to hold more than one array element where
score
is greater than4
.In this part of your code
let List = [employers];
you are putting your array argument inside an array that you do not need and you are not passing index inside if . You can fix your code in this way however this solution will print just the first matchYou can return an employers array to show all matches in this way