Skip to content

Instantly share code, notes, and snippets.

@motleytech
Last active July 3, 2016 02:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save motleytech/3b975363a44e0ce3e92b7176ce545384 to your computer and use it in GitHub Desktop.
Save motleytech/3b975363a44e0ce3e92b7176ce545384 to your computer and use it in GitHub Desktop.
Javascript useful methods
// 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