-1

I'm pretty new with js, and I'm trying to filter out people born between certain ages with a function using .filter

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) => {
  allCoders.filter(coders = born > startYear && born > endYear)
  return coders
}
Claimaura
  • 5
  • 2
  • 1
    The `filter` function expects a function as it's argument, you are passing a boolean – mousetail Aug 22 '22 at 09:10
  • It would be useful if you stated your question clearly in the *body* of your "post". Otherwise we're left guessing, although the title does divulge the question pretty well. Nevertheless, just dumping some code and one sentence in the title don't make a good question, in my opinion. Have you read [documentation of `filter` at MDN](http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)? – Armen Michaeli Aug 22 '22 at 09:27
  • 1
    Does this answer your question? [How to filter object array based on attributes?](https://stackoverflow.com/questions/2722159/how-to-filter-object-array-based-on-attributes) – Armen Michaeli Aug 22 '22 at 09:31

1 Answers1

-1

Made small corrections to the return and function to filter

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

You can also shorten this to

const findCodersBetween = (coders, startYear, endYear) => coders.filter(({born}) => born > startYear && born < endYear);
Abito Prakash
  • 4,368
  • 2
  • 13
  • 26