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

135
Visualizações
Trouble adding 2d array contents on an HTML table

I'm making a battleship game implementation with HTML/JavaScript. This function is supposed to add the positions of the ships on an HTML table by copying the elements from a 2d array(p1Board) with the positions of the ships.

let displayTable = () => {
        document.getElementById("player1turn").hidden = true;
        let table = document.getElementById("player1board");
        for(var i = 1; i < table.rows.length; i++){
          for(var j = 1; j < table.rows.cells; j++){
            table.rows[i].cells[j].innerHTML = p1Board[i - 1][j - 1];
          }
        }
        document.getElementById("player1table").hidden = false;
      }
document.getElementById("p1start").addEventListener("click", displayTable);

Then, when I open the program on my browser, the table appears but without the positions of the ships: [Battleship Board][1] [1]: https://i.stack.imgur.com/Nv1kA.png

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The problem is that you are not specifying the row from which you want to get the cells. Also, once you get the cells, you need to access their .length property.

Change this line

for(var j = 1; j < table.rows.cells; j++)

for this

for(var j = 1; j < table.rows[i].cells.length; j++)
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