Last active
August 29, 2015 14:07
-
-
Save Shiggiddie/333f9c40cee1ca8b2989 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Flattening | |
var arrays = [[1, 2, 3], [4, 5], [6]]; | |
//arrays = arrays.concat([5000]); | |
arrays = arrays.reduce(function(prev, cur) { return prev.concat(cur); }); | |
console.log(arrays); | |
// → [1, 2, 3, 4, 5, 6] | |
// Mother-child age difference | |
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; | |
}); | |
// Your code here | |
//console.log(average([1, 2, 3, null])) | |
//console.log(mothersAge(ancestry.map(function(person) { | |
// if (person.mother == null) { return null; } | |
// else { return person } | |
//}).filter(function(el){ | |
// return el != null; | |
//}))); | |
console.log(average(ancestry.map(function(person) { | |
if (byName[person.mother] == null) { return null; } | |
else { return person.born - byName[person.mother].born } | |
}).filter(function(el){ | |
return el != null; | |
}))); | |
// → 31.2 | |
// Historical Life Expectancy | |
function average(array) { | |
function plus(a, b) { return a + b; } | |
return array.reduce(plus) / array.length; | |
} | |
function averageLifeByCentury(century) { | |
return average(ancestry.map(function(person){ | |
if (Math.ceil(person.died/100) == century){ | |
return person.died - person.born; | |
} | |
else { | |
return null; | |
} | |
}).filter(function(el){ | |
return el != null; | |
})) | |
} | |
console.log(averageLifeByCentury(16)); | |
console.log(averageLifeByCentury(17)); | |
console.log(averageLifeByCentury(18)); | |
console.log(averageLifeByCentury(19)); | |
console.log(averageLifeByCentury(20)); | |
console.log(averageLifeByCentury(21)); | |
// Your code here. | |
// → 16: 43.5 | |
// 17: 51.2 | |
// 18: 52.8 | |
// 19: 54.8 | |
// 20: 84.7 | |
// 21: 94 | |
// Every and then some | |
function forEachWithBreak(array, action){ | |
} | |
function every(array, pred){ | |
for(var i = 0; i < array.length; i++){ | |
if (!pred(array[i])) { | |
return false; | |
} | |
} | |
return true; | |
} | |
function some(array, pred){ | |
for(var i = 0; i < array.length; i++) { | |
if (pred(array[i])) { | |
return true; | |
} | |
} | |
return false; | |
} | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment