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

128
Views
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 answers
Answer question

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 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!