HackerRank - Birthday Cake Candles and the counting loop
The algorithm is to count the number of elements in the array that are equal to the max of the array.  Let the code express that.  Both the original code and the procedural accepted answer are a clutter of bookkeeping details.

function birthdayCakeCandles(n, ar) {
    const max = Math.max(...ar)
    return ar.filter(x =&gt; x === max).length
}