Skip to content

Instantly share code, notes, and snippets.

@hansspiess
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hansspiess/95c4bb4ddfc0c5946953 to your computer and use it in GitHub Desktop.
Save hansspiess/95c4bb4ddfc0c5946953 to your computer and use it in GitHub Desktop.
Collecting my solutions for a great tutorial...
Every chapter of eloquentjavascript.net has some exercises, which i keep here for reference.
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
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
// 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
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}} ) );
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
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