Skip to content

Commit 5281d85

Browse files
committed
feat(2022): add challenge 12
1 parent f909e00 commit 5281d85

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Electric sleighs, wow!
2+
3+
## πŸ”’ Instructions
4+
5+
Santa Claus has new electric sleighs but he must control the electricity consumption because **he only has a 20W battery**.
6+
7+
We are given an **array of sleighs, from worst to best**, with their respective consumptions. Each sleigh is an object with two properties: `name` and `consumption`.
8+
9+
The `consumption` field is a number that represents the amount of watts (W) that **consumes** the sleigh for each **distance unit**. The `name` field is a string that represents the sleigh name.
10+
11+
Create a program that returns the name of the best sleigh available that allows us to cover the distance.
12+
13+
```javascript
14+
const distance = 30;
15+
const sleighs = [
16+
{ name: 'Gorusuke', consumption: 0.3 },
17+
{ name: 'Madeval', consumption: 0.5 },
18+
{ name: 'Lolivier', consumption: 0.7 },
19+
{ name: 'Hyuuh', consumption: 1 }
20+
];
21+
22+
selectSleigh(distance, sleighs); // => "Madeval"
23+
24+
// Gorusuke consumes 9W to cover 30 distance
25+
// Madeval consumes 15W to cover 30 distance
26+
// Lolivier consumes 21W to cover 30 distance
27+
// Hyuuh consumes 30W to cover 30 distance
28+
29+
// The best sleigh to cover the distance is Madeval.
30+
31+
// Gorusuke covers the distance but it's a worse sleigh
32+
// Lolivier and Hyuuh can't cover the distance with 20W.
33+
```
34+
35+
**Remember that**:
36+
37+
- The battery is always 20W.
38+
- The list of sleighs is ordered from worst to best sleigh.
39+
- You have to return the name of the best sleigh that allows us to cover the distance with the watts we have available.
40+
- If no sleigh can be used, then return `null`.

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const selectSleigh = (distance, sleighs) => {
2+
const eligibleSleighs = sleighs.filter(sleigh => {
3+
return sleigh.consumption * distance <= 20;
4+
});
5+
6+
return eligibleSleighs.length > 0 ? eligibleSleighs.at(-1).name : null;
7+
};
8+
9+
const selectSleighAlt = (distance, sleighs) => {
10+
const eligibleSleighs = sleighs.filter(sleigh => {
11+
return sleigh.consumption * distance <= 20;
12+
});
13+
14+
eligibleSleighs.unshift({ name: null, consumption: Infinity });
15+
16+
return eligibleSleighs.at(-1).name;
17+
};
18+
19+
export { selectSleigh, selectSleighAlt };

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { selectSleigh, selectSleighAlt } from './solution';
2+
3+
describe('Challenge 12: Electric sleighs, wow!', () => {
4+
describe('selectSleigh(...)', () => {
5+
const testCases = [
6+
createTestCase(
7+
[
8+
1,
9+
[
10+
{ name: 'pheralb', consumption: 0.3 },
11+
{ name: 'TMChein', consumption: 0.5 }
12+
]
13+
],
14+
'TMChein',
15+
'should return the name of the sleigh with the lowest consumption (1)'
16+
),
17+
createTestCase(
18+
[
19+
10,
20+
[
21+
{ name: 'Pedrosillano', consumption: 1 },
22+
{ name: 'SamarJaffal', consumption: 5 }
23+
]
24+
],
25+
'Pedrosillano',
26+
'should return the name of the sleigh with the lowest consumption (2)'
27+
),
28+
createTestCase(
29+
[
30+
10,
31+
[
32+
{ name: 'Pedrosillano', consumption: 1 },
33+
{ name: 'SamarJaffal', consumption: 2 },
34+
{ name: 'marcospage', consumption: 3 }
35+
]
36+
],
37+
'SamarJaffal',
38+
'should return the name of the sleigh with the lowest consumption (3)'
39+
),
40+
createTestCase(
41+
[
42+
50,
43+
[
44+
{ name: 'Pedrosillano', consumption: 1 },
45+
{ name: 'SamarJaffal', consumption: 2 },
46+
{ name: 'marcospage', consumption: 3 }
47+
]
48+
],
49+
null,
50+
'should return null if no sleigh can be used'
51+
)
52+
];
53+
54+
it('#T should return a string', () => {
55+
const distance = 10;
56+
const sleighs = [
57+
{ name: 'Pedrosillano', consumption: 1 },
58+
{ name: 'SamarJaffal', consumption: 2 },
59+
{ name: 'marcospage', consumption: 3 }
60+
];
61+
expect(typeof selectSleigh(distance, sleighs)).toBe('string');
62+
expect(typeof selectSleighAlt(distance, sleighs)).toBe('string');
63+
});
64+
65+
it.each(testCases)('#$# $description', ({ args, expected }) => {
66+
expect(selectSleigh(...args)).toEqual(expected);
67+
expect(selectSleighAlt(...args)).toEqual(expected);
68+
});
69+
});
70+
});

β€ŽREADME.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ This repository contains the solutions to the challenges proposed by [@midudev](
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) |
7070
| 11 | [Santa Claus is Scrum Master](2022/challenge-11) | πŸ”΄ | [Show](2022/challenge-11/solution.js) |
71+
| 12 | [Electric sleighs, wow!](2022/challenge-12) | 🟠 | [Show](2022/challenge-12/solution.js) |
7172

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

0 commit comments

Comments
Β (0)