Last active
August 29, 2015 14:16
-
-
Save hansspiess/95c4bb4ddfc0c5946953 to your computer and use it in GitHub Desktop.
Collecting my solutions for a great tutorial...
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
Every chapter of eloquentjavascript.net has some exercises, which i keep here for reference. |
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
function deepEqual(obj1, obj2){ | |
if ( isObject(obj1) && isObject(obj2) ) { | |
if ( samePropCount(obj1, obj2) ) { | |
for (var property in obj1) { | |
if ( obj2.hasOwnProperty(property) ){ | |
return deepEqual( obj1[property], obj2[property] ); | |
} else { | |
return false; | |
} | |
} | |
} else { | |
return false; | |
} | |
} else { | |
return obj1 === obj2; | |
} | |
} | |
function isObject(obj){ | |
return (obj == null) ? false : typeof obj == 'object'; | |
} | |
function samePropCount(obj1, obj2){ | |
return Object.keys(obj1).length === Object.keys(obj2).length; | |
} | |
var obj = {here: {is: "an"}, object: 2}; | |
console.log(deepEqual(obj, obj)); | |
// → true | |
console.log(deepEqual(obj, {here: 1, object: 2})); | |
// → false | |
console.log(deepEqual(obj, {here: {is: "an"}, object: 2})); | |
// → true |
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
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. | |
function motherAge(person) { | |
return person.born - byName[person.mother].born; | |
} | |
function hasMother(person){ | |
return byName[person.mother]; | |
} | |
console.log( average( ancestry.filter(hasMother).map(motherAge) ) ); | |
// → 31.2 |
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
// Your code here. | |
function every(array, f){ | |
for( i=0; i<array.length; i++ ) { | |
if ( f(array[i]) === false ) return false; | |
} | |
return true; | |
} | |
function some(array, f){ | |
for( i=0; i<array.length; i++ ) { | |
if ( f(array[i]) === true ) 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 |
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
function transparentWrapping(f) { | |
return function() { | |
return f.apply(null, arguments); | |
}; | |
} | |
var funcWithArgs = transparentWrapping(function() { | |
return arguments; | |
}); | |
var funcWithoutArgs = function(arguments) { | |
return arguments; | |
}; | |
console.log( funcWithArgs( "all args are available inside funcWithArgs even if not declared", [1,3,5], '3', {a:33,b:{a:4,b:44}} ) ); | |
console.log( funcWithoutArgs( "only first arg available inside funcWithoutArgs", [1,3,5], '3', {a:33,b:{a:4}} ) ); |
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
function average(array) { | |
function plus(a, b) { return a + b; } | |
return array.reduce(plus) / array.length; | |
} | |
// Your code here. | |
function age(p) { return p.died - p.born; } | |
function century(p){ | |
return Math.ceil(p.died / 100); | |
} | |
function gender(p){ | |
return p.sex; | |
} | |
function gender(p){ | |
return p.sex; | |
} | |
function group(array, by){ | |
var grouped = {}; | |
array.forEach(function(p) { | |
var prop = by(p); | |
if ( grouped.hasOwnProperty( prop ) ) { | |
grouped[prop].push(p); | |
} else { | |
grouped[prop] = []; | |
grouped[prop].push(p); | |
} | |
}); | |
return grouped; | |
} | |
function averageAge(array){ | |
return average( array.map(age) ); | |
} | |
function logAverageAge(obj){ | |
for (var prop in obj) { | |
console.log( prop + ': ' + averageAge(obj[prop]) ); | |
} | |
} | |
logAverageAge( group(ancestry, century) ); | |
// Bonus!! | |
logAverageAge( group(ancestry, gender) ); | |
// → 16: 43.5 | |
// 17: 51.2 | |
// 18: 52.8 | |
// 19: 54.8 | |
// 20: 84.7 | |
// 21: 94 |
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
var arrays = [[1, 2, 3], [4, 5], [6]]; | |
// Your code here. | |
console.log(arrays.reduce(function(newArr, currArr) { | |
return newArr.concat( currArr ); | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment