Battleships in a Board LeetCode solution in JavaScript

1
var countBattleships = function (board) {
2
let count = 0;
3

4
for (let rowIndex = 0; rowIndex < board.length; rowIndex++) {
5
for (
6
let columnIndex = 0;
7
columnIndex < board[rowIndex].length;
8
columnIndex++
9
) {
10
const isTopEmpty =
11
rowIndex === 0 || board[rowIndex - 1][columnIndex] !== "X";
12
const isLeftEmpty =
13
columnIndex === 0 || board[rowIndex][columnIndex - 1] !== "X";
14
const isBattleShip =
15
board[rowIndex][columnIndex] === "X" && isTopEmpty && isLeftEmpty;
16
if (isBattleShip) {
17
count++;
18
}
19
}
20
}
21

22
return count;
23
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0