Last active
July 3, 2016 02:12
-
-
Save motleytech/3b975363a44e0ce3e92b7176ce545384 to your computer and use it in GitHub Desktop.
Javascript useful methods
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
// returns a range as a list | |
function range(start, stop, step) { | |
let result = [] | |
if (step === undefined) { | |
step = 1 | |
} | |
if (stop === undefined) { | |
stop = start | |
start = 0 | |
} | |
if (step > 0) { | |
for (let ix = start; ix < stop; ix += step) { | |
result.push(ix) | |
} | |
} else { | |
for (let ix = start; ix > stop; ix += step) { | |
result.push(ix) | |
} | |
} | |
return result | |
} | |
let xrange = range | |
function shuffle(array) { | |
var m = array.length, t, i; | |
// While there remain elements to shuffle… | |
while (m) { | |
// Pick a remaining element… | |
i = Math.floor(Math.random() * m--); | |
// And swap it with the current element. | |
t = array[m]; | |
array[m] = array[i]; | |
array[i] = t; | |
} | |
return array; | |
} | |
// shortcut to sort things | |
let points = [5,4,2,3,7,1] | |
points.sort(function(a, b){return b-a}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment