1

I am trying to return an array of each of the names in the array of objects that satisfy the condition in the if statement. Can you please help me understand why my code is only returning just the first name when the condition is met? My goal is to return an array of all object names that satisfy the condition in the if statement.

function getNamesOfLegalDrivers(people){
  for(let i =0; i < examplePeopleArray.length; i++){
    if(examplePeopleArray[i].age > 15) {
      return examplePeopleArray[i].name
    }
  }
}

const examplePeopleArray = [
  { name: 'John', age : 14},
  { name : 'Joey', age : 16},
  { name : 'Jane', age: 18}
];

console.log(getNamesOfLegalDrivers(examplePeopleArray))

3 Answers 3

4

When you start a line with return, it ends the function (similar to how break ends a loop).

function getNamesOfLegalDrivers(people){
  let returnArray = [];
  let numberOfDrivers = people.length;
  for(let i =0; i < numberOfDrivers; i++){
    if(people[i].age > 15) {
      returnArray.push(people[i].name)
    }
  }
   return returnArray;
}

const examplePeopleArray = [
  { name: 'John', age : 14},
  { name : 'Joey', age : 16},
  { name : 'Jane', age: 18}
];

console.log(getNamesOfLegalDrivers(examplePeopleArray))
Sign up to request clarification or add additional context in comments.

2 Comments

Just be aware that in this example, the function is changing a variable outside its scope. Rather than looping through the global variable examplePeopleArray, it should be looping through the array that was passed to the function (people).
You missed one ... the if statement :)
2

This happens because you return the first occurrence you encounter in this statement:

return examplePeopleArray[i].name

Assuming you want to keep that logic, you should store the desired result in an array and then return the final array outside of the for loop:

function getNamesOfLegalDrivers(people){
let names = [];
  for(let i =0; i < examplePeopleArray.length; i++){
    if(people[i].age > 15) {
      names.push(people[i].name);
    }
  }
  return names;
}

const examplePeopleArray = [
  { name: 'John', age : 14},
  { name : 'Joey', age : 16},
  { name : 'Jane', age: 18}
];

console.log(getNamesOfLegalDrivers(examplePeopleArray))

Also, a cleaner way to achieve the same result would be:

function getNamesOfLegalDrivers(people){
    return people.filter(person => person.age > 15).map(person => person.name);
}

const examplePeopleArray = [
  { name: 'John', age : 14},
  { name : 'Joey', age : 16},
  { name : 'Jane', age: 18}
];

console.log(getNamesOfLegalDrivers(examplePeopleArray))

First, you filter ages higher than 15 and return the result to the map operator which will make sure to return just their names to an array.

1 Comment

Just be aware that in this example, the function is changing a variable outside its scope. Rather than looping through the global variable examplePeopleArray, it should be looping through the array that was passed to the function (people).
2

It is because you are exiting your function after the first time that the if statement is true. return stops execution of the function and returns the value.

Instead, you need to return all of the values after going through the entire for loop.

function getNamesOfLegalDrivers(people){
  let drivers = [];
  for(let i=0; i < people.length; i++){
    if(people[i].age > 15) {
      drivers.push(people[i]['name']);
    }
  }
    return drivers;

}
const examplePeopleArray = [
  { name: 'John', age : 14},
  { name : 'Joey', age : 16},
  { name : 'Jane', age: 18}
];

console.log(getNamesOfLegalDrivers(examplePeopleArray));

Also, inside your function you are using the global variable examplePeopleArray -- you should be using the people variable (the information passed to the function). Otherwise, if you were to change what array you passed to the function (e.g. a second list of drivers), you would still be looking at the examplePeopleArray.

2 Comments

Please note that you return the filtered objects and not the names
Corrected. Thanks. (and actually, I ommitted the return statement -- so I didn't return anything!)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.