4Geeks logo
4Geeks logo

Courses

Explore our extensive collection of courses designed to help you master various subjects and skills. Whether you're a beginner or an advanced learner, there's something here for everyone.

Coding Bootcamp

Learn live

Join us for our free workshops, webinars, and other events to learn more about our programs and get started on your journey to becoming a developer.

Upcoming live events

Learning library

For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.

It makes sense to start learning by reading and watching videos about fundamentals and how things work.

Full-Stack Software Developer - 16w

Data Science and Machine Learning - 16 wks

Search from all Lessons


LoginGet Started
← Back to Projects

Battleship with Vanilla.js

Goal

4Geeks Coding Projects tutorials and exercises for people learning to code or improving their coding skills

Difficulty

intermediate

Repository

Click to open

Video

Not available

Solution video

Not available

Average duration

8 hrs

Technologies

Weekly Coding Challenge

Every week, we pick a real-life project to build your portfolio and get ready for a job. All projects are built with ChatGPT as co-pilot!

Start the Challenge

Podcast: Code Sets You Free

A tech-culture podcast where you learn to fight the enemies that blocks your way to become a successful professional in tech.

Listen the podcast

Battleship In Vanilla.js

Write a battleship game using Vanilla.js

Demonstration of Battleship

🌱 How to start this project

Do not clone this repository.

The first step to start coding is cloning the vanillajs boilerplate on your local computer or opening it using gitpod.

a) If using Gitpod (recommended) you can clone the boilerplate by clicking here.

b) If working locally type the following command from your command line:

1$ git clone https://github.com/4GeeksAcademy/vanillajs-hello`.

💡 Important: Remember to create a new repository, update the remote (git remote set-url origin <your new url>), and upload the code to your new repository using add, commit and push.

📝Instructions

  1. Create your HTML/CSS. First, we recomend using a parent div with display:flex and flex-wrap: wrap (to allow multiple rows), the container must have 100 divs inside.
  2. After your HTML/CSS looks good, start thinking on how to make it dynamic using JS.
  3. To represent the gameBoard you can use a JS matrix like this:
1// 0 = empty 2// 1 = part of a ship 3// 2 = a sunken part of a ship 4// 3 = a missed shot 5let gameBoard = [ 6 [1,1,1,1,1,0,0,0,0,1], 7 [0,0,0,0,0,0,0,0,0,1], 8 [0,0,0,0,0,0,0,0,0,1], 9 [0,0,0,0,0,0,0,0,0,1], 10 [0,0,0,0,0,0,0,0,0,0], 11 [1,0,0,1,1,0,0,0,0,0], 12 [1,0,0,0,0,0,0,0,0,0], 13 [1,0,0,0,0,0,0,0,0,0], 14 [0,0,0,0,0,0,0,0,0,0], 15 [1,1,1,1,0,0,0,0,0,0] 16];
  1. Add one onClick to every <div> and call the fireTorpedo function with the coordinates of the div.
  2. Replace the value on the gameBoard and render the board again.

😎 Feeling confident?

  • Try implementing a "showShips" function that shows the ship positions when clicked.

Goal

4Geeks Coding Projects tutorials and exercises for people learning to code or improving their coding skills

Difficulty

intermediate

Repository

Click to open

Video

Not available

Solution video

Not available

Average duration

8 hrs

Technologies

Weekly Coding Challenge

Every week, we pick a real-life project to build your portfolio and get ready for a job. All projects are built with ChatGPT as co-pilot!

Start the Challenge

Podcast: Code Sets You Free

A tech-culture podcast where you learn to fight the enemies that blocks your way to become a successful professional in tech.

Listen the podcast