Battleships in a Board LeetCode solution in JavaScript
1var countBattleships = function (board) {2let count = 0;34for (let rowIndex = 0; rowIndex < board.length; rowIndex++) {5for (6let columnIndex = 0;7columnIndex < board[rowIndex].length;8columnIndex++9) {10const isTopEmpty =11rowIndex === 0 || board[rowIndex - 1][columnIndex] !== "X";12const isLeftEmpty =13columnIndex === 0 || board[rowIndex][columnIndex - 1] !== "X";14const isBattleShip =15board[rowIndex][columnIndex] === "X" && isTopEmpty && isLeftEmpty;16if (isBattleShip) {17count++;18}19}20}2122return count;23};