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
/* 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}, |
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 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)); |
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 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++) { |
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(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]); |
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 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; |
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 flaten (array) { | |
return array.reduce( (acc, curr) => acc.concat(curr), []); | |
} | |
let aoa = [ | |
[1, 2, 3, 4], | |
[5, 6], | |
[7, 8, 9] | |
]; |
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 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)); |
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
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}`; |
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
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) { |
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 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; |
OlderNewer