Skip to content

Commit ae8e9fe

Browse files
committed
feat(2022): add challenge 11
1 parent dedfc98 commit ae8e9fe

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

β€Ž2022/challenge-11/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Santa Claus is Scrum Master
2+
3+
## πŸ”’ Instructions
4+
5+
Santa Claus is a bit worried because the preparations are taking a long time. He has got a Scrum certification and has decided to use the Scrum methodology to organize the work of his elves.
6+
7+
The elfs tell him the expected duration of the tasks with a string with the format `hh:mm:ss` and in the same format how long they have been working on it.
8+
9+
But Santa Claus does not get quickly if there is too much or too little left to finish, so he has asked us to make a program that tells us what portion of the task has already been completed.
10+
11+
For example, if the task lasts `03:00:00` and they have been working for `01:00:00` then they have already completed `1/3` of the task. In code:
12+
13+
```javascript
14+
getCompleted('01:00:00', '03:00:00'); // '1/3'
15+
getCompleted('02:00:00', '04:00:00'); // '1/2'
16+
getCompleted('01:00:00', '01:00:00'); // '1/1'
17+
getCompleted('00:10:00', '01:00:00'); // '1/6'
18+
getCompleted('01:10:10', '03:30:30'); // '1/3'
19+
getCompleted('03:30:30', '05:50:50'); // '3/5
20+
```
21+
22+
**Note**:
23+
24+
- The time format is `hh:mm:ss`.
25+
- The output format is a string `a/b` where a is the portion of the task that has already been completed and b is the portion of the task that is left to complete.
26+
- The portion is always shown with the smallest fraction possible. (for example, `2/4` will never be shown because it can be represented as `1/2`).
27+
- If the task has already been completed, the fraction would be `1/1`.
28+
- No elf has been mistreated during the execution of this challenge nor have they had to use Scrum for real.

β€Ž2022/challenge-11/solution.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const getCompleted = (part, total) => {
2+
const [hoursPart, minutesPart, secondsPart] = part.split(':');
3+
const [hoursTotal, minutesTotal, secondsTotal] = total.split(':');
4+
5+
const partSeconds = (+hoursPart * 60 + +minutesPart) * 60 + +secondsPart;
6+
const totalSeconds = (+hoursTotal * 60 + +minutesTotal) * 60 + +secondsTotal;
7+
8+
const calculateGreatestCommonDivisor = (a, b) => {
9+
while (b) {
10+
const t = b;
11+
b = a % b;
12+
a = t;
13+
}
14+
return a;
15+
};
16+
17+
const gcd = calculateGreatestCommonDivisor(partSeconds, totalSeconds);
18+
19+
const numerator = partSeconds / gcd;
20+
const denominator = totalSeconds / gcd;
21+
22+
return `${numerator}/${denominator}`;
23+
};
24+
25+
export { getCompleted };

β€Ž2022/challenge-11/solution.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { getCompleted } from './solution';
2+
3+
describe('Challenge 11: Santa Claus is Scrum Master', () => {
4+
describe('getCompleted(...)', () => {
5+
const testCases = [
6+
createTestCase(['01:00:00', '03:00:00'], '1/3'),
7+
createTestCase(['02:00:00', '04:00:00'], '1/2'),
8+
createTestCase(['01:00:00', '01:00:00'], '1/1'),
9+
createTestCase(['00:10:00', '01:00:00'], '1/6'),
10+
createTestCase(['01:10:10', '03:30:30'], '1/3'),
11+
createTestCase(['02:20:20', '03:30:30'], '2/3'),
12+
createTestCase(['03:30:30', '05:50:50'], '3/5')
13+
];
14+
15+
it('#T should return a string', () => {
16+
expect(typeof getCompleted('01:00:00', '03:00:00')).toBe('string');
17+
});
18+
19+
it.each(testCases)('#$# should return $expected when the input is $args.0', ({ args, expected }) => {
20+
expect(getCompleted(...args)).toEqual(expected);
21+
});
22+
});
23+
});

β€ŽREADME.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ This repository contains the solutions to the challenges proposed by [@midudev](
6767
| 08 | [We need a mechanic!](2022/challenge-08) | 🟠 | [Show](2022/challenge-08/solution.js) |
6868
| 09 | [Crazy Xmas lights](2022/challenge-09) | 🟒 | [Show](2022/challenge-09/solution.js) |
6969
| 10 | [The Santa Claus sleigh jump](2022/challenge-10) | 🟠 | [Show](2022/challenge-10/solution.js) |
70+
| 11 | [Santa Claus is Scrum Master](2022/challenge-11) | πŸ”΄ | [Show](2022/challenge-11/solution.js) |
7071

7172
[^1]: **Difficulty**: 🟒 Easy 🟠 Medium πŸ”΄ Hard 🟣 Very Hard
7273

0 commit comments

Comments
Β (0)