# Solving JS Algorithm Problem: Birthday Cake Candles Photo by Richard Burlton on Unsplash

This week I will be going over a problem from Hackerrank. The problem is titled Birthday Cake Candles.

1)Birthday Cake Candles

The problem reads:

You are in charge of the cake for a child’s birthday. You have decided the cake will have one candle for each year of their total age. They will only be able to blow out the tallest of the candles. Count how many candles are tallest. Count how many candles are tallest.

Given function:

function birthdayCakeCandles(candles) {

}

Candles represents the sample case array [3, 2 , 1, 3].

In this case, just by looking at the sample array, we know that there are 2 of the largest candles within the array and that is 3. So our output will be 2.

So since we will need to find the maximum number of the tallest candles, we can define max as being the number that is the largest out of the bunch.

let max = Math.max(…candles)

We can use the Math.max() function in order to calculate the max amount within the array. We will ultimately get 3. I defined the candles array using the spread operator (…candles).

After that, we will need to define our answer to the problem by creating a new array with the amounts that are equal to the maximum amount (3).

let ans = candles.filter(candle => candle === max)

return ans.length

To create a new array we use the built-in JS function filter(). And the case we will need to return when it’s true is when any individual candle is equal to the max amount (3). This will result in the array for ans being [3, 3]. So then all we need to do is return the length of the array in order to show that there are two of the tallest candles on the birthday cake. This solution is the cleanest and most understandable code I can come up with for this problem. If there are any other solutions more straight forward than this please let me know!

Flatiron School Graduate with finance, budget, and tax experience.

## More from Perezchristian

Flatiron School Graduate with finance, budget, and tax experience.