Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
function that filter an array using if condition and cycles

I'm trying to learn something about arrays and I have an exercise. I need to write a function that filters an array and returns people that are 18 or older.

To do that I'm allowed to use only cycles and if statement.

function adultFilter(persons) {
    // return persons.filter(item => item.age >= 18);  - Not valid, i need to use if condition and cycles
   
}

const persons = [
    {name: 'Paul', age: 16},
    {name: 'George', age: 17},
    {name: 'Lucas', age: 21},
    {name: 'Marco', age: 32},
    {name: 'Peter', age: 18},
    {name: 'Carl', age: 13},
    {name: 'Simon', age: 24},
    {name: 'Mark', age: 15},
    {name: 'Sandra', age: 34},
    {name: 'Alice', age: 28}
];

const adults = adultFilter(persons);

console.log(persons);
console.log(adults);

can someone help me and explain how to achieve that. my filter function is not allowed.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This should work for you:

function adultFilter(persons) {
  var valid = [];
  for (var i in persons) {
    if (persons[i].age >= 18) {    //eighteen or over, not over eighteen
      valid.push(persons[i]);
    }
  }
  return valid;
}

const persons = [
    {name: 'Paul', age: 16},
    {name: 'George', age: 17},
    {name: 'Lucas', age: 21},
    {name: 'Marco', age: 32},
    {name: 'Peter', age: 18},
    {name: 'Carl', age: 13},
    {name: 'Simon', age: 24},
    {name: 'Mark', age: 15},
    {name: 'Sandra', age: 34},
    {name: 'Alice', age: 28}
];

const adults = adultFilter(persons);

console.log(persons);
console.log(adults);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!