Ad
Code
Diff
  • // this code should fail the tests // it does
    
    function totalAgeYoungerThan0(people, age) {
      let totalAge = 0;
      for ( let i=0; i in people; i++ ) // this will just always return 0 with the arguments we send
        if ( people[i].age < age )
          totalAge += people[i].age;
      return totalAge;
    }
    
    // this code should fail the tests // it does
    
    function totalAgeYoungerThan1(people,tooOld) {
      let totalAge = 0;
      for ( const {age} of people ) // this will throw because our argument isn't iterable
        if ( age < tooOld )
          totalAge += age;
      return totalAge;
    }
    
    // this code should fail the tests // it does
    
    function totalAgeYoungerThan2(xs, age) {
      const [person,...persons] = xs; // this will throw on not iterable
      if ( person===undefined )
        return 0;
      else if ( person.age < age )
        return totalAgeYoungerThan2(persons,age) + person.age;
      else
        return totalAgeYoungerThan2(persons,age);
    }
    
    // this code should theoretically pass the tests // it does
    // it includes the word `for` but not in a for loop
    
    function totalAgeYoungerThan3(people, age) {
      return people
        .filter(person => person.age < age)
        .reduce((formula, person) => formula + person.age, 0)
    }
    
    // passes
    
    function totalAgeYoungerThan4(people, age) {
      return people
        .filter(person => person.age < age)
        .reduce((sum, person) => sum + person.age, 0)
    }
    
    
    // fails
    
    function totalAgeYoungerThan5(people, age) {
      let totalAge = 0;
      for ( let i=0; i < people.length; i++ )
        if ( people[i].age < age )
          totalAge += people[i].age;
      return totalAge;
    }
    • // this code should fail the tests // it does
    • function totalAgeYoungerThan0(people, age) {
    • let totalAge = 0;
    • for ( let i=0; i in people; i++ ) // this will just always return 0 with the arguments we send
    • if ( people[i].age < age )
    • totalAge += people[i].age;
    • return totalAge;
    • }
    • // this code should fail the tests // it does
    • function totalAgeYoungerThan1(people,tooOld) {
    • let totalAge = 0;
    • for ( const {age} of people ) // this will throw because our argument isn't iterable
    • if ( age < tooOld )
    • totalAge += age;
    • return totalAge;
    • }
    • // this code should fail the tests // it does
    • function totalAgeYoungerThan2(xs, age) {
    • const [person,...persons] = xs; // this will throw on not iterable
    • if ( person===undefined )
    • return 0;
    • else if ( person.age < age )
    • return totalAgeYoungerThan2(persons,age) + person.age;
    • else
    • return totalAgeYoungerThan2(persons,age);
    • }
    • // this code should theoretically pass the tests // it does
    • // it includes the word `for` but not in a for loop
    • function totalAgeYoungerThan3(people, age) {
    • return people
    • .filter(person => person.age < age)
    • .reduce((formula, person) => formula + person.age, 0)
    • }
    • // passes
    • function totalAgeYoungerThan4(people, age) {
    • return people
    • .filter(person => person.age < age)
    • .reduce((sum, person) => sum + person.age, 0)
    • }
    • // fails
    • function totalAgeYoungerThan5(people, age) {
    • let totalAge = 0;
    • for ( let i=0; i < people.length; i++ )
    • if ( people[i].age < age )
    • totalAge += people[i].age;
    • return totalAge;
    • }

(Moved here per the discussion in https://www.codewars.com/kata/5fbc297af8ed8d0008e971c9/discuss)


Assume you have an array of Person objects that looks like this:

[
  {age: 10, hairColor: "brown"},
  {age: 30, hairColor: "red"},
  {age: 20, hairColor: "brown"},
  {age: 40, hairColor: "black"},
];

Create the following functions:

  • totalAgeYoungerThan - returns the total ages of the people younger than the given age
  • hairColorsYoungerThan - returns an array of the hair colors of the people younger than the given age

In each of these new functions, you may not use for loops, and instead must solve them using any of .map, .filter and/or .reduce.

// this code should fail the tests
// const totalAgeYoungerThan = (people, age) => {
//   let totalAge = 0;
//   for (let i = 0; i < people.length; i++) {
//     if (people[i].age < age) totalAge += people[i].age;
//   }
//   return totalAge;
// }

// this code should fail the tests
// const totalAgeYoungerThan = (people, age) => {
//   let totalAge = 0;
//   for (let person of people) {
//     if (person < age) totalAge += person.age;
//   }
//   return totalAge;
// }

// this code should fail the tests
// const totalAgeYoungerThan = (people, age) => {
//   if (people.length === 0) return 0;
//   const [first, ...rest] = people;
//   return (first.age < age ? first.age : 0) + totalAgeYoungerThan(rest, age);
// }

// this code should theoretically pass the tests
// it includes the word `for` but not in a for loop
// const totalAgeYoungerThan = (people, age) => {
//   return people
//     .filter(person => person.age < age)
//     .reduce((formula, person) => formula + person.age, 0)
// }

const totalAgeYoungerThan = (people, age) => {
  return people
    .filter(person => person.age < age)
    .reduce((sum, person) => sum + person.age, 0)
}

const hairColorsYoungerThan = (people, age) => {
  return people
    .filter(person => person.age < age)
    .map(person => person.hairColor)
}