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
/* Linked List in EcmaScript */ | |
class Node { | |
constructor (value, next = null) { | |
this.value = value; | |
this.next = next; | |
} | |
} |
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
// http://eloquentjavascript.net/08_error.html | |
var box = { | |
locked: true, | |
unlock: function() { this.locked = false; }, | |
lock: function() { this.locked = true; }, | |
_content: [], | |
get content() { | |
if (this.locked) throw new Error("Locked!"); | |
return this._content; |
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; |
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
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
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
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 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 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 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++) { |
NewerOlder