Javascript: Sea Battle

JS: Arrays
Last update: 14 Sep 11:30
1
Student

We brought you the popular game "Battleship".

solution.js

Write and export the calcShipsCount() function that takes a square two-dimensional array of zeros and ones as a grid. Zero is an empty cell, one is a part of the ship. The function should return the number of ships on the battlefield.

Since ships should not be adjacent, implement and export the isValidField() function that checks whether the placement of ships is correct.

calcShipsCount([]); // 0
calcShipsCount([
  [0, 1, 0, 0, 0, 0],
  [0, 1, 0, 1, 1, 1],
  [0, 0, 0, 0, 0, 0],
  [0, 1, 1, 1, 0, 1],
  [0, 0, 0, 0, 0, 1],
  [1, 1, 0, 1, 0, 0],
]); // 6
isValidField([
  [0, 1, 0, 0],
  [1, 0, 0, 1],
  [0, 0, 0, 0],
  [0, 1, 1, 1],
]); // false

For full access to the challenge you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests