how to return only the names of the people from an array born between certain ages with a function

This is the code I’m currently working with. For an example "findCodersBetween(coders, 1960, 1986)" would return "[{name: ‘Alice’, born: 1990}, {name: ‘Susan’, born: 1960}, {name: ‘Charlotte’, born: 1986}]". But what I want is for the function to only output the names "[‘Alice’, ‘Susan’, ‘Charlotte’]". I think the answer is simple I just can’t figure it out.

const allCoders = [
  {name: 'Ada', born: 1816},
  {name: 'Alice', born: 1990},
  {name: 'Susan', born: 1960},
  {name: 'Eileen', born: 1936},
  {name: 'Zoe', born: 1995},
  {name: 'Charlotte', born: 1986},
]

const findCodersBetween = (coders, startYear, endYear) => {
  return coders.filter((coder) => coder.born >= startYear && coder.born <= endYear);
};

>Solution :

You can use map and return only the name: .map(coder => coder.name)

const allCoders = [
  {name: 'Ada', born: 1816},
  {name: 'Alice', born: 1990},
  {name: 'Susan', born: 1960},
  {name: 'Eileen', born: 1936},
  {name: 'Zoe', born: 1995},
  {name: 'Charlotte', born: 1986},
]

const findCodersBetween = (coders, startYear, endYear) => {
  return coders.filter((coder) => coder.born >= startYear && coder.born <= endYear).map(coder => coder.name);
};

console.log(findCodersBetween(allCoders, 1960, 1986))

Leave a Reply