Skip to content

Commit 476f570

Browse files
get tallest candle counts
1 parent 74cc47a commit 476f570

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Complete the 'birthdayCakeCandles' function below.
3+
*
4+
* Q. You are in charge of the cake for a child's birthday.
5+
* It will have one candle for each year of their total age.
6+
* They will only be able to blow out the tallest of the candles.
7+
* Your task is to count how many candles are the tallest.
8+
*
9+
* Example: [4, 4, 1, 3]
10+
* The tallest candles are 4 units high. There are 2 candles with this height, so the function should return 2.
11+
*
12+
* The function is expected to return an INTEGER.
13+
* The function accepts INTEGER_ARRAY candles as parameter.
14+
*/
15+
16+
function birthdayCakeCandles(candles) {
17+
let maxVal = 0
18+
let frequencyObj = {}
19+
for(let i=0; i<candles.length; i++) {
20+
if(frequencyObj[candles[i]]) {
21+
frequencyObj[candles[i]] = frequencyObj[candles[i]] + 1
22+
} else {
23+
frequencyObj[candles[i]] = 1
24+
}
25+
if(maxVal < candles[i]) {
26+
maxVal = candles[i]
27+
}
28+
}
29+
console.log(maxVal, frequencyObj)
30+
return frequencyObj[maxVal]
31+
}
32+
33+
console.log(birthdayCakeCandles([3, 2, 1, 1, 3, 3, 7]))

0 commit comments

Comments
 (0)