Skip to content

Commit 35ecd28

Browse files
committed
✨ Challenge #2: Factory in Action!
1 parent ca46e80 commit 35ecd28

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

2023/challenge-02/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Challenge #2: 🏭 Factory in Action!
2+
3+
## 🔢 Instructions
4+
5+
In Santa's workshop, the elves have a list of gifts they want to manufacture and a limited set of materials.
6+
7+
The gifts are strings, and the materials are characters. Your task is to write a function that, given a list of gifts and the available materials, returns a list of gifts that can be manufactured.
8+
9+
A gift can be manufactured if we have all the necessary materials to make it.
10+
11+
### Example:
12+
13+
```javascript
14+
const gifts1 = ['tren', 'oso', 'pelota'];
15+
const materials1 = 'tronesa';
16+
console.log(manufacture(gifts1, materials1)); // ["tren", "oso"]
17+
// 'tren' YES because its letters are in 'tronesa'
18+
// 'oso' YES because its letters are in 'tronesa'
19+
// 'pelota' NO because its letters are NOT in 'tronesa'
20+
21+
const gifts2 = ['juego', 'puzzle'];
22+
const materials2 = 'jlepuz';
23+
console.log(manufacture(gifts2, materials2)); // ["puzzle"]
24+
25+
const gifts3 = ['libro', 'ps5'];
26+
const materials3 = 'psli';
27+
console.log(manufacture(gifts3, materials3)); // []
28+
```

2023/challenge-02/solution.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function manufacture(gifts, materials) {
2+
if (!gifts?.length || !materials) {
3+
return [];
4+
}
5+
6+
const uniqueMaterials = new Set(materials);
7+
8+
const doableGifts = gifts.filter((gift) =>
9+
[...new Set(gift)].every((char) => uniqueMaterials.has(char))
10+
);
11+
12+
return doableGifts;
13+
}

2023/challenge-02/solution.spec.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { manufacture } from "./solution";
2+
3+
describe("Challenge #2: 🏭 Factory in Action!", () => {
4+
const testCases = [
5+
createTestCase(
6+
[["tren", "oso", "pelota"], "tronesa"],
7+
["tren", "oso"],
8+
"should return gifts that can be manufactured"
9+
),
10+
createTestCase(
11+
[["juego", "puzzle"], "jlepuz"],
12+
["puzzle"],
13+
"should return gifts that can be manufactured"
14+
),
15+
createTestCase(
16+
[["libro", "ps5"], "psli"],
17+
[],
18+
"should return an empty array when no gifts can be manufactured"
19+
),
20+
createTestCase(
21+
[["calcetin", "sueter", "gorro"], "saetr"],
22+
["calcetin", "sueter", "gorro"],
23+
"should return all gifts when all can be manufactured"
24+
),
25+
createTestCase(
26+
[[], "abc"],
27+
[],
28+
"should return an empty array when no gifts are given"
29+
),
30+
createTestCase(
31+
[["robot", "laptop", "helicoptero"], "abc"],
32+
[],
33+
"should return an empty array when no gifts can be manufactured"
34+
),
35+
];
36+
37+
it.each(testCases)("#$# $description", ({ input, expected, description }) => {
38+
expect(manufacture(...input)).toEqual(expected);
39+
});
40+
});
41+
42+
function createTestCase(input, expected, description) {
43+
return { input, expected, description };
44+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ This repository contains the solutions to the challenges proposed by [@midudev](
9898
| # | Challenge | Difficulty[^1] | Solution |
9999
| :-: | :-----------------------------------------------------------: | :------------: | :-----------------------------------: |
100100
| 01 | [First repeat gift!](2023/challenge-01) | 🟢 | [Show](2023/challenge-01/solution.js) |
101+
| 02 | [Factory in Action!](2023/challenge-02) | 🟢 | [Show](2023/challenge-02/solution.js) |
101102

102103
[^1]: **Difficulty**: 🟢 Easy 🟠 Medium 🔴 Hard 🟣 Very Hard
103104

0 commit comments

Comments
 (0)