Build a Pile of Cubes

Marcos Lipic
4 min readJan 8, 2021
Photo by Elena Gordienko on Unsplash

It’s time to work through another CodeWars challenge. This time, I’m going to be showing you how to complete a kata called: “Build a Pile of Cubes” with JavaScript.

The task, as described in the CodeWars kata, is to construct a building which will be a pile of n cubes. The cube at the bottom will have a volume of n³, the cube above will have volume of (n-1)³ and so on until the top which will have a volume of 1³. You are given the total volume m of the building .

Your mission, should you choose to accept it, is, given m, can you find the number n of cubes you have to build?

Okay, so how should we approach this problem? Let’s first try to visualize what’s going on here.

Here is an example of a pile of three cubes as described by the problem statement. Since there is a total of three cubes, the cube at the bottom has a volume of 27, which is 3³. Above that is cube 2, with a volume of 8, and lastly cube 1 has a volume of 1.

We know that every pile of cubes will look exactly like the one above for the top 3 cubes. In fact, every pile of cubes is guaranteed to at least have cube with n = 1, whose volume is also 1. So given the volume, we can subtract 1 from that volume to account for that first cube. If the remaining value, or difference, is positive, we can then subtract 8 to account for the volume of the second cube.

Wait a minute. This is beginning to sound like a while loop. While m is positive, continue to subtract incrementally larger and larger cubes. At the same time, we should be keeping track of the amount of cubes we have piled up as we attempt to reach the volume inputted into the function.

The kata also states that we should return -1 if there is no such n that perfectly corresponds to the volume inputted into the function. Therefore, we need an if statement that will return -1 if m becomes negative once our while loop is complete. If the while loop terminates with m being equal to 0, that would signify that the given volume corresponds perfectly to some pile of cubes, with no volume left over.

If the while loop indeed results in m equaling 0, we can return the total number of cubes in the pile, and consider our task complete.

Awesome! So what does the actual JavaScript code look like?

Let’s start with declaring the function.

Now, let’s create a variable that will keep track of the number of cubes in our pile.

Now we can make that while loop, that will be running when the volume, m, is greater than zero. First, the counter variable will be incremented. Then a cubes variable will be calculated using Math.pow to find the cube of the counter variable. Once that is completed, the new value of the m variable will be the difference between the current m and the cubes variable.

After the while loop, we will use an if statement to determine whether the new m value is positive or negative. This is accomplished using Math.sign. If m is negative, we’ll return -1.

If the m value is not negative, we return counter, which will be the total number of cubes piled on top of each other.

And there you go. You are now all set to test whether a given volume corresponds to the pile of cubes, and if so, just how many cubes are in that pile.

--

--