let field = [
'xxx x ' ,
' x ' ,
'x ' ,
'x x ' ,
' x x' ,
' x x' ,
' ' ,
'x xxxx' ,
' ' ,
' xx x'
]
... write your code below
|
We’ll use the following pattern to count ships:
Solution:
let field = [
'xxx x ' ,
' x ' ,
'x ' ,
'x x ' ,
' x x' ,
' x x' ,
' ' ,
'x xxxx' ,
' ' ,
' xx x'
]
let ship_counter = 0
for ( let i = 0; i < 10; i++) for ( let j = 0; j < 10; j++)
if (field[i][j] === 'x' &&
(i === 0 || field[i-1][j] === ' ' ) &&
(j === 0 || field[i][j-1] === ' ' )) ship_counter++
console.log( 'ship_counter = ' , ship_counter)
|
Output: