Skip to content

Instantly share code, notes, and snippets.

@gitfaf
gitfaf / solution.js
Created August 16, 2017 11:47
Solves lycanthrope's log problem from eloquent javascript.
/* Data from http://eloquentjavascript.net/code/#4 */
/* phi logic from http://www.pmean.com/definitions/phi.htm */
var JOURNAL = [
{"events":["carrot","exercise","weekend"],"squirrel":false},
{"events":["bread","pudding","brushed teeth","weekend","touched tree"],"squirrel":false},
{"events":["carrot","nachos","brushed teeth","cycling","weekend"],"squirrel":false},
{"events":["brussel sprouts","ice cream","brushed teeth","computer","weekend"],"squirrel":false},
{"events":["potatoes","candy","brushed teeth","exercise","weekend","dentist"],"squirrel":false},
{"events":["brussel sprouts","pudding","brushed teeth","running","weekend"],"squirrel":false},
@gitfaf
gitfaf / EJS Exercise - Reversing an array.js
Created August 17, 2017 18:31
Reverse an array exercise from Eloquent JS book
function reverseArray (input) {
let newArray = [];
for (let i = input.length - 1; i >= 0; i--) {
newArray.push(input[i]);
}
return newArray;
}
var array = [1, 2, 3, 4, 5];
console.log('original array:', array, 'reversed array:', reverseArray(array));
@gitfaf
gitfaf / EJS Exercise - A list.js
Created August 17, 2017 18:49
A list exercise from Eloquent JS
function List (value, rest) {
this.value = value;
this.rest = rest || null;
}
function arrayToList (array) {
if (!array) return null;
let list = new List(array[0], null);
let tmp = list;
for (let i = 1, len = array.length; i < len; i++) {
@gitfaf
gitfaf / EJS Exercise - Deep equal.js
Created August 17, 2017 19:18
Deep equal check for objects
function deepEqual(a, b) {
let keysA = Object.keys(a);
let keysB = Object.keys(b);
if (keysA.length !== keysB.length) {
return false;
}
for (let i = 0, len = keysA.length; i < len; i++) {
let key = keysA[i];
if (typeof a[key] === 'object' && typeof b[key] === 'object') {
return deepEqual(a[key], b[key]);
@gitfaf
gitfaf / EJS - HOF - sumOf.js
Created August 17, 2017 21:21
Eloquent JS - Higher Order Function snippet.
function forEach (array, action) {
for(let i = 0, length = array.length; i < length; i++) {
action(array[i]);
}
}
function sumOf(array) {
let sum = 0;
forEach(array, value => sum += value);
return sum;
function flaten (array) {
return array.reduce( (acc, curr) => acc.concat(curr), []);
}
let aoa = [
[1, 2, 3, 4],
[5, 6],
[7, 8, 9]
];
@gitfaf
gitfaf / EJS Exercise - Every and Some
Last active August 17, 2017 23:13
Every and Some functions
function every(array, predicate) {
let holdsTrue = true;
for (let i = 0, length = array.length; i < length && holdsTrue; i++) {
holdsTrue = predicate(array[i]);
}
return holdsTrue;
}
console.log(every([1, 2, 3, 4, 5], x=>x < 6));
console.log(every([1, 2, 3, 4, 5], x=>x > 6));
class Person {
constructor (fname, lname) {
this.lastName = lname;
this.firstName = fname;
}
get fullName () {
return this.firstName + " " + this.lastName;
}
toString () {
return `${this.fullName} aka ${this.nickName}`;
export function Vector (x, y) {
this.x = x;
this.y = y;
}
Vector.prototype = {
plus: function (vector) {
return new Vector(this.x + vector.x, this.y + vector.y);
},
minus: function (vector) {
@gitfaf
gitfaf / EJS Exercise - Retry.js
Last active August 18, 2017 15:00
Retry until you have a value.
function MultiplicatorUnitFailure (message) {
this.stack = (new Error()).stack;
this.message = message;
}
MultiplicatorUnitFailure.prototype = Object.create(Error.prototype);
MultiplicatorUnitFailure.prototype.name = 'MultiplicatorUnitFailure';
function primitiveMultiply (a, b) {
console.log(`a: ${a}; b: ${b}.`);
let val = a * b;