I am a student who still needs to study a lot.
I want to make random arrangement between 1-12, and I have twelve contents, so I tried to give them numbers randomly (1-12).
so I tried this for random number:
function getRandom(min, max) {
return Math.floor((Math.random() * (max - min + 1)) + min);
}
function getRandomArray(min, max, count) {
if (max - min + 1 < count) return;
var rdm = [];
while (1) {
var index = getRandom(min, max);
if (rdm.indexOf(index) > -1) {
continue;
}
rdm.push(index);
if (rdm.length == count) {
break;
}
}
return rdm
}
and got random numbers (of 12)...
getRandomArray(1, 12, 12)
and give class name... but It wouldn't work the way I thought.
var contentNumber = [getRandomArray(1, 12, 12)];
$content.eq(0).addClass(contentNumber[0]);
$content.eq(1).addClass(contentNumber[1]);
...
$content.eq(11).addClass(contentNumber[11]);
I thought it could be an 'Array' but it was just a bunch of numbers. Can I make this an Array?
Or maybe I have to change my initial expectation of it...
As mentioned in comments you can generate an array of consecutive numbers (use index + 1) and shuffle it (sort by a random number):
const arr = Array
// generate random number for sorting(shuffling) and consecutive numbers
.from({length: 12}, (_, idx) => [Math.random(), idx + 1])
// sort(shuffle) by the random number
.sort(([a],[b]) => a - b)
// map the sorted array into a suffled consecutive numbers array
.map(([_, num]) => num);
console.log(...arr);