settingsAccountsettings
Menusettings

Q: Filter By Age - JavaScript Task

+4 votes

I need to write a JS function that stores the name and age of two persons in objects and then filters them by minimum age.

The input comes as five arguments:

  • The first element is the minimum age.
  • The second and third elements are the name and age of the first person
  • The fourth and fifth elements – the name and age of the second person.

Input:

12, 'Ivan', 15, 'Asen', 9   

Output:

{ name: 'Ivan', age: 15 }

The three age parameters will be numbers, the names will be strings. The output should be printed to the console.
 

asked in JavaScript category by user golearnweb
edited by user golearnweb

1 Answer

+4 votes

Here is my solution:

function filterByAge(minAge, nameA, ageA, nameB, ageB) {
    var personA = {name: nameA, age: Number(ageA)};
    var personB = {name: nameB, age: Number(ageB)};

    if (personA.age >= minAge) {
        console.log(personA);
    }

    if (personB.age >= minAge) {
        console.log(personB);
    }
}

filterByAge(12, "Ivan", 15, "Asen", 9);

 

answered by user eiorgert
edited by user golearnweb
...