Created
May 30, 2016 11:23
-
-
Save dolvik/57e654eec2a4e7948f0c80997ddb55e8 to your computer and use it in GitHub Desktop.
Баннерокрутилка
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 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