-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbirthday-cake-candles.js
69 lines (58 loc) · 1.31 KB
/
birthday-cake-candles.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
*
* -----------------------
* | BIRTHDAY CAKE CANDLES |
* -----------------------
*
*
* Given an integer for the number of birthdsy candles and
* an array of the heights of each of those candles. Return
* how many candles will be blown if only the tallest candles
* are blown.
*
* e.g.
*
* candleHeights = [1, 3, 4, 4 ]
*
* birthdayCakeCandles(numArr) // returns 2 (two candles of height 4)
*
*
* */
/*
*
* ***** SOLUTION
*
* */
function birthdayCakeCandles(arr) {
// initialize counter
let counter = 0;
// initialize maxValue
let maxValue = 0;
// loop over array
for(let num of arr) {
// f arr[i] === maxValue
if(num === maxValue){
// counter = 1
counter ++;
// else if arr[i] > maxValue
} else if (num > maxValue){
// maxValue = arr[1]
maxValue = num;
// counter = 1
counter = 1;
}
}
console.log("counter - ", counter);
return counter;
};
/*
*
* ***** TESTS
*
* */
const args1 = [3,2,1,3];
console.log("## TEST 1 ----- > ", birthdayCakeCandles(args1)); // 6
const args2 = [3,2,1,3,3, 4, 5];
console.log("## TEST 2 ----- > ", birthdayCakeCandles(args2)); // 6
// const args3 = null;
// console.log("## TEST 3 ----- > ", birthdayCakeCandles(args3)); // 128