-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem1.js
More file actions
34 lines (30 loc) · 759 Bytes
/
problem1.js
File metadata and controls
34 lines (30 loc) · 759 Bytes
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
// https://www.hackerrank.com/challenges/the-birthday-bar/problem
function birthday(s, d, m) {
let month = [];
month.length =m;
month.fill('',0,m);
const rtn = s.map((value , i , arr) =>{
const total = month.reduce((acc,cur,j)=>{
acc+=arr[i+j]
return acc;
},0)
return total
})
const res = rtn.filter(v=>v===d)
return res.length
}
//method 2
function birthday(s, d, m) {
let count = 0;
if (s.length < m) {
return count;
}
function arrSum(arr) {
return arr.reduce((a, c) => a + c);
}
s.forEach((val, index) => {
const sum = arrSum(s.slice(index, m + index))
if (sum === d) count += 1;
})
return count;
}