Eloquent JS - Higher Order Functions

Flattening

Use the reduce method in combination with the concat method to “flatten” an array of arrays into a single array that has all the elements of the input arrays.

1
2
3
arr.reduce(function(total, val){
return total.concat(val);
},[])

Mother-child age difference

Using the example data set from this chapter, compute the average age difference between mothers and children (the age of the mother when the child is born). You can use the average function defined earlier in this chapter.

Note that not all the mothers mentioned in the data are themselves present in the array. The byName object, which makes it easy to find a person’s object from their name, might be useful here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function average(array) {
function plus(a, b) { return a + b; }
return array.reduce(plus) / array.length;
}

var byName = {};
ancestry.forEach(function(person) {
byName[person.name] = person;
});

function getAgeDiff(person){
return person.born - byName[person.mother].born;
}

hasKnownMother = function(person){
return person.mother in byName;
};

console.log(average(ancestry.filter(hasKnownMother).map(age)));

Historical life expectancy

When we looked up all the people in our data set that lived more than 90 years, only the latest generation in the data came out. Let’s take a closer look at that phenomenon.

Compute and output the average age of the people in the ancestry data set per century. A person is assigned to a century by taking their year of death, dividing it by 100, and rounding it up, as in Math.ceil(person.died / 100).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

function average(array) {
function plus(a, b) { return a + b; }
return array.reduce(plus) / array.length;
}


var perCentry ={}
ancestry.forEach(function(p){
var cent = String(Math.ceil(p.died/100));

if(!(cent in perCentry)){
perCentry[cent] = []
}
perCentry_t[cent].push(p.died - p.born);

})

for(cent in perCentry){
console.log(cent +":"+ average(perCentry_t[cent]))
}

Every and then some

Arrays also come with the standard methods every and some. Both take a predicate function that, when called with an array element as argument, returns true or false. Just like && returns a true value only when the expressions on both sides are true, every returns true only when the predicate returns true for all elements of the array. Similarly, some returns true as soon as the predicate returns true for any of the elements. They do not process more elements than necessary—for example, if some finds that the predicate holds for the first element of the array, it will not look at the values after that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Your code here.

function some(arr, predicate){
var temp = arr.filter(function(a){return predicate(a)});
return temp.length>0 ? true : false
}

every = function(arr, b){
for(var i = 0; i< arr.length; i++){
if(!b(arr[i])){
return false;
}
}
return true;
}

console.log(every([NaN, NaN, NaN], isNaN));
// → true
console.log(every([NaN, NaN, 4], isNaN));
// → false
console.log(some([NaN, 3, 4], isNaN));
// → true
console.log(some([2, 3, 4], isNaN));
// → false