← Back to challenges

You Sunk My Battleship!

JavaScriptHardgamesarrayslanguage_fundamentalsvalidation

Instructions

Remember the game Battleship? Ships are floating in a matrix. You have to fire torpedos at their suspected coordinates, to try and hit them.

Create a function that takes an array of arrays (matrix) and a coordinate as a string. If the coordinate contains only water " . ", return "splash" and if the coordinate contains a ship " * ", return "BOOM".

Instruction text contains two blank spaces between quotation marks; the code/test does not. See examples below.

Examples

[
  [".", ".", ".", "*", "*"],
  [".", "*", ".", ".", "."],
  [".", "*", ".", ".", "."],
  [".", "*", ".", ".", "."],
  [".", ".", "*", "*", "."],
]

fire(matrix, "A1") ➞ "splash"

fire(matrix, "A4") ➞ "BOOM"

fire(matrix, "D2") ➞ "BOOM"

Notes

  • The provided matrix is always a square.
  • The provided matrix will not be larger than 5 * 5 ( A1 * E5).
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.