Skip to content Skip to sidebar Skip to footer

Birthday Cake Candles- Hackerrank

My code for this problem works on 8/9 test cases. but i cant figure out why it wont work for the last case. function birthdayCakeCandles(ar) { const candleHeights = {}; for

Solution 1:

This was my first try, I thought it is the best because it looks pretty cool, however it failed on some test cases:

functionbirthdayCakeCandles(candles) {
  return candles.filter(n => n === Math.max(...candles)).length;
}

Then I tried this and it passes on all test cases:

functionbirthdayCakeCandles(candles) {
    let max = 0;
    let counter = 0;

    candles.forEach(item => {
        if (item > max) {
            max = item;
            counter = 1;
        } elseif (item === max) {
            counter++;
        }
    });
    return counter;
}

Post a Comment for "Birthday Cake Candles- Hackerrank"