Skip to content

Instantly share code, notes, and snippets.

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);
// SLC -> AUS -> LAX -> JFK -> SJC
let passes = [
{depart: 'JFK', arrive: 'SJC'},
{depart: 'LAX', arrive: 'JFK'},
{depart: 'AUS', arrive: 'LAX'},
{depart: 'SLC', arrive: 'AUS'}
];
//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");
}
//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++) {
(function () {
const SINGLES = [
'zero',
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
function findSqRt(target) {
var upper = target,
lower = 0,
square=0,
value=0;
while(Math.abs(target-square) >= 0.00000001) {
value = (upper + lower) /2;
square = value * value;
var a = [1, {a: [2, [3]]}, 'four', [[5], [6]], [[7], 8, 9], 10];
function flatten(arr) {
let idx = 0;
while(idx < arr.length) {
if(arr[idx] instanceof Array) {
Array.prototype.splice.apply(arr, [idx, 1].concat(arr[idx]));
}else {
@jasonwaters
jasonwaters / deferImages.html
Created September 9, 2016 23:35
Jason's defer images solution
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.js"></script>
</head>
<body>
<p><img src="https://securecdn.disqus.com/uploads/mediaembed/images/509/9511/original.jpg?w=128"></p>
<p><img src="https://securecdn.disqus.com/uploads/mediaembed/images/509/9512/original.jpg?w=128"></p>
<p><img src="https://securecdn.disqus.com/uploads/mediaembed/images/509/9513/original.jpg?w=128"></p>
<p><img src="https://securecdn.disqus.com/uploads/mediaembed/images/509/9514/original.jpg?w=128"></p>
@jasonwaters
jasonwaters / index.html
Created September 9, 2016 23:12
Jason's Template Renderer
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="output"></div>
<hr/>
<form id="myForm">
<input placeholder="Name&hellip;" type="text" id="myName"/>
<input placeholder="Number&hellip;" type="number" id="myNumber"/>
@jasonwaters
jasonwaters / droneDeploy.js
Created July 11, 2016 04:57
Drone Deploy Coding Exercise
// Below is a drone object with six methods.
// Each method takes a callback and after some amount of time will execute that callback (for ease of testing, it will also console.log a value).
// At the bottom of the page the expected output is logged.
// And we invoke the chained call of the drone object. However the drone object does not yet have a chained api.
// Your job is to add a chained api to the drone object by only modifying the code in the middle of the page.
// A good answer should include the following criteria.
// 1. Code was only added between the below comments.
// 2. Each method in the chain executes only after the previous method has resolved