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 () { | |
const SINGLES = [ | |
'zero', | |
'one', | |
'two', | |
'three', | |
'four', | |
'five', | |
'six', | |
'seven', |
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
//we have an array of objects A and an array of indexes B. Reorder objects | |
//in array A with given indexes in array B. Do not change array A's length; | |
var A = ['C', 'D', 'E', 'F', 'G']; | |
var B = [3, 0, 4, 1, 2]; | |
function resort(arr, order) { | |
let idx=0; | |
for(var start=0;start<arr.length;start++) { |
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
//1, 1, 2, 3, 5, 8, 13, 21 | |
(function() { | |
let result = []; | |
window.fibonacci = function(n) { | |
if (n < 1) { | |
throw new Error("n must be greater than 0"); | |
} |
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
// SLC -> AUS -> LAX -> JFK -> SJC | |
let passes = [ | |
{depart: 'JFK', arrive: 'SJC'}, | |
{depart: 'LAX', arrive: 'JFK'}, | |
{depart: 'AUS', arrive: 'LAX'}, | |
{depart: 'SLC', arrive: 'AUS'} | |
]; | |
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
Array.prototype.swap = function(idx1, idx2) { | |
let first = Math.max(idx1, idx2); | |
let last = Math.min(idx1, idx2); | |
this.splice(last, 0, this.splice(first, 1)[0]); | |
}; | |
function moveToFront(arr, idx) { | |
arr.swap(0,idx); |
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
//Given an array of positive integers and a target total of X, find if there exists a contiguous subarray with sum = X | |
var list=[1,3,5,18]; | |
function isValid(arr, target) { | |
var left=0; | |
var right=0; | |
while(true) { | |
var sum = 0; |
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
Array.prototype.swap = function(idx1, idx2) { | |
if(idx1 >= this.length || idx2 >= this.length) { | |
throw new Error("index out of bounds"); | |
} | |
let first = Math.max(idx1, idx2); | |
let last = Math.min(idx1, idx2); | |
let value = this.splice(first, 1, this[last])[0]; |
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 flattenObj(obj, keyChain=[], flattened={}) { | |
for(let key in obj) { | |
let value = obj[key]; | |
let keys = keyChain.concat(key); | |
if(value instanceof Object) { | |
flattenObj(value, keys, flattened); | |
}else { | |
flattened[keys.join('.')] = value; | |
} |
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
// a script that processes arithmetic in reverse polish notation | |
//https://en.wikipedia.org/wiki/Reverse_Polish_notation | |
const operate = { | |
'+': (a,b) => a+b, | |
'-': (a,b) => a-b, | |
'*': (a,b) => a*b, | |
'/': (a,b) => 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
/* | |
A script that processes a matrix that represents bodies of water. Any values that equal 0 represent water. | |
The script outputs a list of bodies of water found, with their respective sizes. For Example: | |
let water = [ | |
[0, 2, 1, 0], | |
[0, 1, 0, 1], | |
[1, 1, 0, 1], | |
[0, 1, 0, 1] | |
]; |