-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLesson_001_BinaryGap.js
43 lines (31 loc) · 969 Bytes
/
Lesson_001_BinaryGap.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
const BinaryGap = {
solution: function(N) {
let binary = N.toString(2)
binary = binary.replace(/0+$/, '')
longest = 0
strings = binary.match(/0+/g)
if (strings) {
for (const match of strings) {
if (match.length > longest) {
longest = match.length
}
}
}
return longest
}
}
result = BinaryGap.solution(657876)
console.log(`${result}, pass: ${result == 5}`)
result = BinaryGap.solution(8)
console.log(`${result}, pass: ${result == 0}`)
result = BinaryGap.solution(129)
console.log(`${result}, pass: ${result == 6}`)
result = BinaryGap.solution(2147483647)
console.log(`${result}, pass: ${result == 0}`)
result = BinaryGap.solution(2147483646)
console.log(`${result}, pass: ${result == 0}`)
result = BinaryGap.solution(462876534)
console.log(`${result}, pass: ${result == 2}`)
result = BinaryGap.solution(1041)
console.log(`${result}, pass: ${result == 5}`)
// TESTED AND PASSED ON Codility