A little experiment with Javascript

Gajindu Bandara
2 min readOct 13, 2021

Recently I started studying the “Head First Javascript Programming” book. I went through the chapters from the beginning and l learned lots of things. Now I’m in the 8th chapter. That chapter was about creating a board game with javascript. We can play it in the web browser. I started to try that one and that was cool. The game was a basic board game called “Battleship”.The below image shows the user interface of the game.

“Battleship” board game

It was on a 7x7 board. The objective is to guess where the ships are and destroy them. There is a box to add our inputs. We have to guess a location and add the location of the ship by the index number of the cell. As an example, you can input the locations like B4, C1, C2. After that, we just have to press the “Fire ” button. So, if there is a ship on that cell location it will be destroyed else it shows as “MISS”.

At the start, I just hardcoded the locations of the ships in Javascript but after going through the next steps of the tutorial I learned the way to generate ship locations randomly so every time when I reload the page ships were randomly placed on the board. To develop this I needed HTML, CSS, and Javascript knowledge. Though the whole code for the game was given by the tutorial, I tried to add some modifications to the code too.

--

--