var fruits = ["Яблоко", "Апельсин", "Слива"];
fruits[RandomInt(0,fruits.length)];
"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);
Что бы данные выводились, из массива и не повторялись
function randomer(arr) {
let array = arr.slice();
let rnd = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
return () => array.length !== 0 ? array.splice(rnd(0, array.length - 1), 1)[0] : false;
}
const getRndItem = randomer(['Яблоко', 'Апельсин', 'Слива', 'Арбуз', 'Клубника']);
console.log(getRndItem()); // 'Слива'
console.log(getRndItem()); // 'Клубника'
console.log(getRndItem()); // 'Арбуз'
console.log(getRndItem()); // 'Апельсин'
console.log(getRndItem()); // 'Яблоко'
console.log(getRndItem()); // false
console.log(getRndItem()); // false