Skip to content

Instantly share code, notes, and snippets.

@gitfaf
gitfaf / Simple linked list in JavaScript.js
Created September 12, 2017 13:18
Simple linked list in JavaScript
/* Linked List in EcmaScript */
class Node {
constructor (value, next = null) {
this.value = value;
this.next = next;
}
}
// 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;
@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;
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) {
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}`;
@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));
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 - 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;
@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 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++) {