when I try to shuffle an array I get a TypeError: "cannot set properties of undefined (setting '10')" where 10 is the value of randomNmbrArr[0]. I have 2 other functions that I copied this code from that do work without any problems. I cant find the difference between the working code and the part that doesn't.
let nmr always logs a valid number between the range of 10 and 72. But for some reason randomNmbrArr[nmr] is undefined. Any idea's?
p.s: The code is running from a module but gives the same error in the main file. The array and function are declared before they are called in any way.
working code:
let unicodeArr = [];
for (let i = 0; i < 65537; i++) {
unicodeArr.push(i);
};
function randomNumber2() {
let o = 0;
let nmr2;
while (++o < 65536) {
nmr2 = Math.floor(Math.random() * (65536 + 1));
[unicodeArr[nmr2], unicodeArr[0]] = [unicodeArr[0], unicodeArr[nmr2]];
};
return unicodeArr[0];
};
Code that does not work:
let randomNmbrArr = [];
for (let i = 10; i < 100; i++) {
randomNmbrArr.push(i);
}
console.log(randomNmbrArr)
function makeRandomNmbrs() {
let k = 0;
let nmr;
while (++k < 62) {
nmr = Math.floor(Math.random() * (62 + 1)) + 10;
console.log(nmr);
[randomNmbrArr[nmr], randomNmbrArr[0]] = [randomNmbrArr[0], randomNmbrArr[nmr]]; //TypeError at this line
console.log(randomNmbrArr)
}
}