Learn by Building Workshop Skills GitHub Türkçe Workshop › Battleship › Step 5 of 35
Grids and ships as lists What we are doing Two small helpers. grid(value) makes a 10 by 10 table: a list of 10 rows, each a list of 10 squares. shipCells makes
the list of squares a ship covers.
```js
const grid = (value) => Array.from({ length: N }, () => Array(N).fill(value))
const shipCells = (r, c, length, down) => Array.from({ length }, (_, i) => (down ? [r + i, c] : [r, c + i]))
```What it means
These are arrow functions stored in constants; a one-line arrow function returns its result by itself.
Array(N).fill(value) is one row; Array.from({ length: N }, ...) makes 10 separate rows.
In shipCells, i counts 0, 1, 2...; a ship going down adds i to the row, otherwise to the column.
Your turn Write the two lines above function drawSea(, followed by an empty line.
Skills: Arrays and objects, Functions
← Draw again and again A random fleet →
All steps A 10 by 10 sea A function for any sea Your own sea Draw again and again Grids and ships as lists A random fleet Two fleets Which ship is here? Fire at a square Show the shots Your shot Tap to fire A message line Hit or miss? See your own fleet Sinking ships Darker hits on a sunk ship Outline sunk ships Win in as few shots as you can A record of their shots Take turns The computer shoots back You can lose too A heat map Shoot the hottest square Hunt around a hit See the heat map H for heat Show their ships at the end A keyboard cursor Move with the arrows Fire with Enter, play again Your best game A side panel Build it yourself: ships apart