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

212
Views
How to create multiple arrays of random numbers in js?

I am attempting to create a matrix of 3 arrays with 10 elements in each array. Each element should be a random number between 1 and 10. I wanted to use a single function to generate each of the arrays, and used this:

var array1 = [];
var array2 = [];
var array3 = [];

var tempName;

function fcnArrayGenerate(){
    let i = 1;
    while (i < 4){
        tempName = "array" + i;
        let j = 0;
        while (j < 10){
            tempName.push((Math.floor(Math.random() * 10) + 1));
            j++;
        }
        i++;
    }
    console.log(array1);
    console.log(array2);
    console.log(array3);
}

However, when I run the function, I receive an error stating that "tempName.push is not a function." Any assistance on how to correct this would be appreciated. Thank you.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Instead of using three variables for three different arrays, you can use a single multidimensional array to store those three arrays.

let array = [[], [], []];

function fcnArrayGenerate() {
  let i = 0;
  while (i <= 2) {
    let j = 0;
    while (j < 10) {
      array[i].push(Math.floor(Math.random() * 10) + 1);
      j++;
    }
    i++;
  }
}
// call the function
fcnArrayGenerate();
// now print the arrays as tables
console.table(array);
console.table(array[0]);
console.table(array[1]);
console.table(array[2]);

Note: console.table() allows you to print out arrays and objects to the console in tabular form.

about 4 years ago · Juan Pablo Isaza Report

0

If you want to push into tempName you have to initialise it with an empty array first. Right now it’s undefined. Hence you’re seeing the error as you can’t push something to undefined

Do this: var tempName = []

about 4 years ago · Juan Pablo Isaza Report

0

To make variable names incremental, you can pass them as objects.

var data = {
  array1: [],
  array2: [],
  array3: []
}

function fcnArrayGenerate(){
    let i = 1;
    while (i < 4){
        let j = 0;
        while (j < 10){
            data['array' + i].push((Math.floor(Math.random() * 10) + 1));
            j++;
        }
        i++;
    }
    console.log(data.array1);
    console.log(data.array2);
    console.log(data.array3);
}

fcnArrayGenerate();

about 4 years ago · Juan Pablo Isaza Report
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!