fanhypermax: ок. А куки там зачем?
Решение такой задачи намного проще и банальней.
Вот пример:
"use strict";
const arr = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"];
// Oneliner to obtain random value from range. This is an arrow function
const getIndex = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
// Rolling random values till we get one not used before
const generateUniqueIndex = (storage, min, max) => {
var index;
while (index === undefined || storage.includes(index)) {
index = getIndex(min, max);
}
return index;
};
function printRandom (arr) {
const indexes = []; //Array to store used indexes
const [min, max] = [0, arr.length - 1];
// Iterating with random indexes but from 0 to array length
for (let i = 0; i <= max; i++) {
let index = generateUniqueIndex(indexes, min, max);
console.log(arr[index]); // Do some action with active index
indexes.push(index); // Save used index
}
}
printRandom(arr);