Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
TypeError "cannot set properties of undefined" when Shuffling Array from Module

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)
  }

}

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!