JavaScript Algorithm: Birthday Cake Candles

Erica N
Erica N
Nov 25 · 3 min read

For today’s algorithm, we will create a function called birthdayCakeCandles.

To explain the function, let’s say you are in charge of the cake for your niece’s birthday. You are her uncle and you really don’t want to go to her birthday party as that’ll mean spending hours in a room full of screaming four to six-year-olds instead of playing video games with your bros at home.

But being the good uncle that you are, you get the cake. You decide that the cake will have one candle for each year of her total age. When she blows out the candles, she’ll only be able to blow out the tallest ones.

So if your niece is turning 4 years old, what that’ll mean for our birthdayCakeCandles function is there will be an array of four numbers. Let’s say the height of the candles is the following:

[2,4,4,1]

Since she can blow only the tallest candles, in the array above 4 is the highest number. There are two candles that are of height 4 so the function will output 2. Your niece will blow out those two candles.

To turn this into code, we will find out what is the highest number in the array using the Math.max method. Let’s assign that number to the maxNum variable:

let maxNum = Math.max(...ar); // ar is the array variable

Next, we will use the filter method. If you want to know more in detail how the filter method works, you can read this article where I explain how the filter method works in the Mini-Max Sum algorithm:

To put it in short, the filter method filters out all values in the array that don’t meet a certain criteria in the return statement.

let filtered = ar.filter(function(value, index, arr) {
return value === maxNum;
});

In this case, the filter method will only return values that are equal to the highest number in the ar array. All values that are not equal to the highest number won’t go in. The filter method creates a new array of the accepted values so we assign it to the filtered variable.

Now we have an array that only has the highest values, we calculate the length of the array using the Array.length property to tell us how many candles your niece can blow. We return that number.

return filtered.length;

Here is the full code:

function birthdayCakeCandles(ar) {
let maxNum = Math.max(...ar);
let filtered = ar.filter(function(value, index, arr) {
return value === maxNum;
});
return filtered.length;
}

And that concludes our algorithm.

Erica N

Written by

Erica N

An artist that uses illustrations to solve easy programming algorithms.

JavaScript in Plain English

Learn the web's most important programming language.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade