Skip to content

Instantly share code, notes, and snippets.

@dolvik
Created May 30, 2016 11:23
Show Gist options
  • Save dolvik/57e654eec2a4e7948f0c80997ddb55e8 to your computer and use it in GitHub Desktop.
Save dolvik/57e654eec2a4e7948f0c80997ddb55e8 to your computer and use it in GitHub Desktop.
Баннерокрутилка
//взять случайное число из пространства длины массива
//переставить элемент с этим индексом и последний элемент
//уменьшить индекс длины на единицу
//повторить все это в цикле пока не дойдем до первого элемента
function getNewArray(arr){
var res = arr.slice();
var max = res.length - 1;
var i, e;
while(max > 0){
i = getRandomIntInclusive(0, max);
e = res[i];
res[i] = res[max];
res[max] = e;
max--;
}
return res;
}
function getRandomIntInclusive(min, max){
return Math.floor(Math.random() * (max - min + 1)) + min;
}
var arr = [5, 4, 3, 2, 1];
console.log(getNewArray(arr));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment