Birthday Cake Candles
Problem
You are in charge of the cake for your niece's birthday and have decided 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. Your task is to find out how many candles she can successfully blow out.
For example, if your niece is turning 4years old, and the cake will have 4candles of height 4,4,1,3 she will be able to blow out 2 candles successfully since the tallest candles are of height 4 and there are 2 such candles.
Solution in JavaScript
const arr = [2, 7, 4, 6, 7];
function birthdayCakeCandles(arr) {
//Math.max returns maximum value in the array.
const max = Math.max(...arr);
//filter() methods creates a new array with all elements
//that pass the test implemented by the provided function
const filtered = arr.filter( elem => elem === max);
const result = filtered.length;
console.log(result); //2
}
birthdayCakeCandles(arr);
Reference
https://www.hackerrank.com/challenges/birthday-cake-candles/problem
Birthday Cake Candles | HackerRank
Determine the number of candles that are blown out.
www.hackerrank.com
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
Array.prototype.filter()
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
developer.mozilla.org
https://www.w3schools.com/js/js_math.asp
JavaScript Math Object
JavaScript Math Object The JavaScript Math object allows you to perform mathematical tasks on numbers. Example Math.PI; // returns 3.141592653589793 Try it Yourself » Math.round() Math.round(x) returns the value of x rounded to its
www.w3schools.com