Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

259
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda