JSFiddle - React, Tailwind, and code Playground
by pashkes
JavaScript
const getRandomInt = (max = 1, min = 0) => {
min = Math.ceil(min);
max = Math.floor(max);
if (max < min || max < 0 || min < 0) {
return 0;
}
return Math.floor(Math.random() * (max - min + 1)) + min;
};
const array = [1, 2, 3, 4, 5, 6];
const mixArray = (array, elements)=> {
const clone = [...array];
const result = [];
for(let i = 0; i < elements; i++) {
let indexRandomElement = getRandomInt(clone.length - 1, 0);
result.push(clone[indexRandomElement]);
clone.splice(indexRandomElement, 1);
}
return result;
}
console.log(mixArray(array, getRandomInt(4, 0)))