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

188
Visualizações
Why am I unable to change the contents of this array

I am writing a test using jest, where I am retrieveing a 10x10 grid from my class. I want to loop over the array which contains this grid, and switch the status from true to 'hit' where the name of the grid is 'battleship1'.

let grid = battleships.returnGrid()
for (let i = 0; i < 10; i++) {
    for (let y = 0; y < 10; y++) {
        if (grid[i][y].shipName == 'battleship1') {
            console.log(grid[i][y].square)
            grid[i][y].status == 'hit'
        }
    }
}
console.log(grid)

This is the code which I am running. After running the for loop, and logging the grid, it is still in the original form, with the status of the grid being set to true.

Why am I unable to change the status to 'hit'?

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

0

You are comparing and not setting:

grid[i][y].status == 'hit'

When you assign a value, just use it like this (only one =):

grid[i][y].status = 'hit'
about 4 years ago · Juan Pablo Isaza Relatório

0

Have you tried changing operators a bit?

let grid = battleships.returnGrid()
for (let i = 0; i < 10; i++) {
    for (let y = 0; y < 10; y++) {
        if (grid[i][y].shipName === 'battleship1') {
            console.log(grid[i][y].square)
            grid[i][y].status = 'hit'
        }
    }
}
console.log(grid)
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