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

250
Views
How can I make a JavaScript function that takes two arguments (numRows, numColumns), and returns a 2D array with the correct grid values?

Write a function makeGrid that accepts two arguments:

numColumns (number) - how many columns we want our grid to have

numRows (number) - how many rows we want our grid to have

makeGrid should return a two-dimensional array that represents a grid of the given dimensions.

`makeGrid(3,4);
  /* => [[1, 2, 3],
        [1, 2, 3],
        [1, 2, 3],
        [1, 2, 3]]
   */`

I've tried multiple variations of the same code below, both complicated and simpler with the same results. It returns a grid with the correct number of columns and the correct number of rows, but it keeps returning the wrong cell values. I am beyond confused at this point so any other insight or depth into what I'm missing would be greatly appreciated!

`function makeGrid(numColumns, numRows){
  arr = [];
  for (let i = 0; i < numRows; i++){
    arr[i] = [];
    for (let j = 0; j < numColumns; j++){
      arr[i].push(j)
    }
  }return arr;
}`
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Try pushing j+1 to the arr array eg. arr[i].push(j+1).

Working code:

function makeGrid(numColumns, numRows) {
  arr = [];
  for (let i = 0; i < numRows; i++) {
    arr[i] = [];
    for (let j = 0; j < numColumns; j++) {
      arr[i].push(j + 1)
    }
  }
  return arr;
}

console.log(makeGrid(3, 4))

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!