Skip to content

Instantly share code, notes, and snippets.

@softwarespot
softwarespot / linkedlist.js
Last active October 31, 2015 08:24
A linked list in JavaScript with ES2015
// 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
@softwarespot
softwarespot / stack.js
Last active October 31, 2015 08:24
A stack in JavaScript with ES2015
// 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 = [];
@softwarespot
softwarespot / 7.js
Last active September 30, 2015 06:14
Kata
(() => {
// 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;
}
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;
}
@softwarespot
softwarespot / infixtopostfix.js
Last active September 28, 2015 10:02
Infix to Postfix
// Based on the Shunting-Yard algorithm
function toPostfix(infix) {
// Convert a token to precedence value
const _precedenceValue = (token) => {
switch (token) {
case '+':
case '-':
return 4;
@softwarespot
softwarespot / bigsum.js
Last active September 30, 2015 06:12
Sum big numbers
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);
@softwarespot
softwarespot / parsecsv.js
Created September 28, 2015 10:17
Simple CSV parser
// 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
*/
@softwarespot
softwarespot / Tern.sublime-settings
Created October 6, 2015 04:33
tern_for_sublime preferences
{
"tern_argument_hints": true,
"tern_argument_hints_type": "tooltip",
"tern_argument_completion": true
}
@softwarespot
softwarespot / Company.js
Created October 7, 2015 20:55
Interview example
// 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;
@softwarespot
softwarespot / josephus.js
Created October 11, 2015 07:15
Example of Josephus problem
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) {