This file contains hidden or 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
// Note: Save as linkedlist.js and run 'babel-node linkedlist.js' to display in the command window. This is written in ES2015 | |
((global) => { | |
// Return strings of toString() found on the Object prototype | |
// Based on the implementation by lodash inc. is* function as well | |
const _objectStrings = { | |
BOOLEAN: '[object Boolean]', | |
}; | |
// Store a reference of the toString function on the Object prototype chain |
This file contains hidden or 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
// Note: Save as stack.js and run 'babel-node stack.js' to display in the command window. This is written in ES2015 | |
((global) => { | |
// Note: I'm aware .pop() and .push() exist natively with JavaScript arrays, but where's the fun in that? =) | |
/** | |
* Stack interface | |
*/ | |
function Stack() { | |
this._stack = []; |
This file contains hidden or 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
(() => { | |
// Add max to the Array prototype chain | |
Array.prototype.max = function () { | |
// I know they say the array will have at least one element, but that can't be guaranteed IMHO | |
const length = this.length; | |
if (length === 0) { | |
return Number.NaN; | |
} |
This file contains hidden or 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 binaryToString(binary) { | |
// Split for readability, though it could be in-lined too | |
const binaryToInteger = (string) => { | |
let final = 0; | |
for (let i = string.length - 1, digits = string.split(''), exp = 1; i >= 0; i--) { | |
final += +digits[i] * exp; | |
// Bit shift left by 1 i.e. 1, 2, 4, 8, 16, 32, 64.... | |
exp <<= 1; | |
} |
This file contains hidden or 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
// Based on the Shunting-Yard algorithm | |
function toPostfix(infix) { | |
// Convert a token to precedence value | |
const _precedenceValue = (token) => { | |
switch (token) { | |
case '+': | |
case '-': | |
return 4; |
This file contains hidden or 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 add(a, b) { | |
// A queue to enqueue the result (minus the remainder) | |
const queue = []; | |
// Store the remainder | |
let remainder = 0; | |
// Determine the largest array length | |
const maxLength = Math.max(a.length, b.length); |
This file contains hidden or 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
// The following kata is going back to basics with creating a parser. So nothing fancy here, just good 'ol best practices | |
/** | |
* CSV Parser. Takes a string as input and returns an array of arrays (for each row) | |
* | |
* @param {string} string CSV input string | |
* @param {string} separator Single character used to quote simple fields. Default is " | |
* @param {string} quote Single character used to separate fields. Default is , | |
* @return {array} A 2-dimensional array which consists of an array of each row split into columns; otherwise, an empty 2-dimensional array | |
*/ |
This file contains hidden or 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
Show hidden characters
{ | |
"tern_argument_hints": true, | |
"tern_argument_hints_type": "tooltip", | |
"tern_argument_completion": true | |
} |
This file contains hidden or 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
// Note: Save as Company.js and run 'babel-node Company.js' to display in the command window. This is written in ES2015 | |
const Company = ((window) => { | |
// Store an array of callbacks | |
const _callbacks = []; | |
// Store whether the 'init' function has already been called | |
let _isInitialised = false; | |
function init() { | |
_isInitialised = true; |
This file contains hidden or 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 josephus(array, count) { | |
// Return store | |
const store = []; | |
// Counter for each time the element should be spliced | |
let counter = 0; | |
// Array index position | |
let index = 0; | |
while (array.length > 0) { |