Kuba-rz
Kuba-rz

Reputation: 11

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'?

Upvotes: 0

Views: 46

Answers (2)

Shadowcrusher
Shadowcrusher

Reputation: 18

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)

Upvotes: 0

Rob
Rob

Reputation: 11788

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'

Upvotes: 3

Related Questions