/
index.js
41 lines (33 loc) · 1.09 KB
/
index.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
/**
* @param {string} part
* @param {string} total
* @returns string
*/
function getCompleted(part, total) {
const convertToSeconds = raw =>
raw.split(':').reduce((acc, value) => acc * 60 + Number(value))
const getGreatestCommonDivisor = (number1, number2) => {
while (number2) [number1, number2] = [number2, number1 % number2]
return number1
}
const partInSeconds = convertToSeconds(part)
const totalInSeconds = convertToSeconds(total)
const gcd = getGreatestCommonDivisor(totalInSeconds, partInSeconds)
return `${partInSeconds / gcd}/${totalInSeconds / gcd}`
}
function getCompletedOptimized(part, total) {
const convertToSeconds = raw =>
raw.split(':').reduce((acc, value) => acc * 60 + Number(value))
const partInSeconds = convertToSeconds(part)
const totalInSeconds = convertToSeconds(total)
// Greatest Common Divisor
let gcd1 = totalInSeconds
let gcd2 = partInSeconds
while (gcd2) {
const tmp = gcd1 % gcd2
gcd1 = gcd2
gcd2 = tmp
}
return `${partInSeconds / gcd1}/${totalInSeconds / gcd1}`
}
export {getCompleted, getCompletedOptimized}