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

156
Visualizações
Uncaught TypeError: Cannot read properties of undefined (reading 'locations')

I am trying to built a simple battleship game that I've found in a book, however I think some of the code in the book is outdated. I am trying to get the locations of ships in a table and give the user if the guess is a hit or a miss, however when I try to access locations of ships console throw this error. I would appreciate any help. This is the code:

let model = {
    boardSize: 7,
    numShips: 3,
    shipLength: 3,
    shipsSunk: 0,

    ships: [ { locations: ["06", "16", "26"], hits: ["", "", ""] },
             { locations: ["24", "34", "44"], hits: ["", "", ""] },
             { locations: ["10", "11", "12"], hits: ["", "", ""] } ],

    fire: function(guess) {
        for(let i; 0 < this.numShips; i++) {
            let ship = this.ships[i];
            let index = ship.locations.indexOf(guess);
            if(index >= 0) {
                ship.hits[index] = "hit";
                view.displayHit(guess);
                view.displayMessage("HIT!");
                if (this.isSunk(ship)) {
                    view.displayMessage("A battleship has sank!")
                    this.shipsSunk++;
                }
                return true;
            }
            view.displayMiss(guess);
            view.displayMessage("You missed.");
            return false;
        }
    },
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You have a typo in the for loop condition:

for(let i; 0 < this.numShips; i++) {

should be:

for(let i = 0; i < this.numShips; i++) {
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