Skip to content

Commit 38f5df6

Browse files
Add solution and spec files for elyses-transformative-enchantments (#997)
Co-authored-by: Tejas Bubane <tejasbubane@gmail.com>
1 parent fea83a8 commit 38f5df6

File tree

4 files changed

+261
-6
lines changed

4 files changed

+261
-6
lines changed

exercises/concept/elyses-transformative-enchantments/.meta/config.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
}
88
],
99
"files": {
10-
"solution": [],
11-
"test": [],
12-
"exemplar": [".meta/exemplar.js"]
10+
"solution": ["enchantments.js"],
11+
"test": ["enchantments.spec.js"],
12+
"exemplar": ["exemplar.js"]
1313
}
1414
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* Double every card in the deck
3+
*
4+
* @param {number[]} deck
5+
*
6+
* @returns {number[]} deck with every card doubled
7+
*/
8+
export function seeingDouble(deck) {
9+
throw new Error('Implement the seeingDouble function');
10+
}
11+
12+
/**
13+
* Creates triplicates of every 3 found in the deck
14+
*
15+
* @param {number[]} deck
16+
*
17+
* @returns {number[]} deck with triplicate 3s
18+
*/
19+
export function threeOfEachThree(deck) {
20+
throw new Error('Implement the threeOfEachThree function');
21+
}
22+
23+
/**
24+
* Removes every card from the deck but the middle two
25+
* Assumes a deck is always 10 cards
26+
*
27+
* @param {number[]} deck of 10 cards
28+
*
29+
* @returns {number[]} deck with only two middle cards
30+
*/
31+
export function middleTwo(deck) {
32+
throw new Error('Implement the middleTwo function');
33+
}
34+
35+
/**
36+
* Moves the outside two cards to the middle
37+
*
38+
* @param {number[]} deck with 10 cards
39+
*
40+
* @returns {number[]} transformed deck
41+
*/
42+
43+
export function sandwichTrick(deck) {
44+
throw new Error('Implement the sandwichTrick function');
45+
}
46+
47+
/**
48+
* Removes every card from the deck except 2s
49+
*
50+
* @param {number[]} deck
51+
*
52+
* @returns {number[]} deck with only 2s
53+
*/
54+
export function twoIsSpecial(deck) {
55+
throw new Error('Implement the twoIsSpecial function');
56+
}
57+
58+
/**
59+
* Returns a perfectly order deck from lowest to highest
60+
*
61+
* @param {number[]} shuffled deck
62+
*
63+
* @returns {number[]} ordered deck
64+
*/
65+
export function perfectlyOrdered(deck) {
66+
throw new Error('Implement the perfectlyOrdered function');
67+
}
68+
69+
/**
70+
* Returns a deck with every card equal to the total number of cards
71+
*
72+
* @param {number[]} deck
73+
*
74+
* @returns {number[]} deck
75+
*/
76+
export function countingCards(deck) {
77+
throw new Error('Implement the countingCards function');
78+
}
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
// @ts-check
2+
3+
/**
4+
* @template T
5+
* @template Expected
6+
* @typedef {[T, Expected]} TestCase
7+
*/
8+
9+
/**
10+
* @template T
11+
* @template Expected
12+
* @typedef {TestCase<T, Expected>[]} TestCases
13+
*/
14+
15+
import {
16+
seeingDouble,
17+
threeOfEachThree,
18+
middleTwo,
19+
sandwichTrick,
20+
twoIsSpecial,
21+
perfectlyOrdered,
22+
countingCards,
23+
} from './enchantments';
24+
25+
describe('array', () => {
26+
describe('seeingDouble', () => {
27+
/** @type {TestCases<Card[], Card[]>} */
28+
const seeingDoubleTestCases = [
29+
[[], []],
30+
[[3], [6]],
31+
[
32+
[1, 2, 3, 4],
33+
[2, 4, 6, 8],
34+
],
35+
[
36+
[2, 5, 1, 9],
37+
[4, 10, 2, 18],
38+
],
39+
];
40+
41+
seeingDoubleTestCases.forEach(([deck, expected]) => {
42+
test(`seeingDouble([${deck}])`, () => {
43+
expect(seeingDouble(deck)).toStrictEqual(expected);
44+
});
45+
});
46+
});
47+
48+
describe('threeOfEachThree', () => {
49+
/** @type {TestCases<Card[], Card[]>} */
50+
const threeOfEachThreeTestCases = [
51+
[[], []],
52+
[[3], [3, 3, 3]],
53+
[[2], [2]],
54+
[
55+
[1, 3, 9, 3, 7],
56+
[1, 3, 3, 3, 9, 3, 3, 3, 7],
57+
],
58+
];
59+
60+
threeOfEachThreeTestCases.forEach(([deck, expected]) => {
61+
test(`threeOfEachThree([${deck}])`, () => {
62+
expect(threeOfEachThree(deck)).toStrictEqual(expected);
63+
});
64+
});
65+
});
66+
67+
describe('middleTwo', () => {
68+
/** @type {TestCases<Card[], Card[]>} */
69+
const middleTwoTestCases = [
70+
[
71+
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
72+
[5, 6],
73+
],
74+
[
75+
[12, 2, 36, 45, 15, 96, 27, 86, 1, 29],
76+
[15, 96],
77+
],
78+
];
79+
80+
middleTwoTestCases.forEach(([deck, expected]) => {
81+
test(`middleTwo([${deck}])`, () => {
82+
expect(middleTwo(deck)).toStrictEqual(expected);
83+
});
84+
});
85+
});
86+
87+
describe('sandwichTrick', () => {
88+
/** @type {TestCases<Card[], Card[]>} */
89+
const sandwichTrickTestCases = [
90+
[
91+
[1, 2, 3, 5, 6, 10],
92+
[2, 3, 10, 1, 5, 6],
93+
],
94+
[
95+
[12, 3, 5, 87],
96+
[3, 87, 12, 5],
97+
],
98+
];
99+
100+
sandwichTrickTestCases.forEach(([deck, expected]) => {
101+
test(`sandwichTrick([${deck}])`, () => {
102+
expect(sandwichTrick(deck)).toStrictEqual(expected);
103+
});
104+
});
105+
});
106+
107+
describe('twoIsSpecial', () => {
108+
/** @type {TestCases<Card[], Card[]>} */
109+
const twoIsSpecialTestCases = [
110+
[[], []],
111+
[[1, 9, 1], []],
112+
[[1, 2, 9, 1], [2]],
113+
[
114+
[121, 22, 9, 12, 2, 2, 2, 23, 2],
115+
[2, 2, 2, 2],
116+
],
117+
];
118+
119+
twoIsSpecialTestCases.forEach(([deck, expected]) => {
120+
test(`twoIsSpecial([${deck}])`, () => {
121+
expect(twoIsSpecial(deck)).toStrictEqual(expected);
122+
});
123+
});
124+
});
125+
126+
describe('perfectlyOrdered', () => {
127+
/** @type {TestCases<Card[], Card[]>} */
128+
const perfectlyOrderedTestCases = [
129+
[[], []],
130+
[
131+
[1, 9, 1],
132+
[1, 1, 9],
133+
],
134+
[
135+
[1, 2, 9, 1],
136+
[1, 1, 2, 9],
137+
],
138+
[
139+
[10, 1, 5, 3, 2, 8, 7],
140+
[1, 2, 3, 5, 7, 8, 10],
141+
],
142+
];
143+
144+
perfectlyOrderedTestCases.forEach(([deck, expected]) => {
145+
test(`perfectlyOrdered([${deck}])`, () => {
146+
expect(perfectlyOrdered(deck)).toStrictEqual(expected);
147+
});
148+
});
149+
});
150+
151+
describe('countingCards', () => {
152+
/** @type {TestCases<Card[], Card[]>} */
153+
const countingCardsTestCases = [
154+
[[], []],
155+
[
156+
[1, 9, 1],
157+
[3, 3, 3],
158+
],
159+
[
160+
[1, 2, 9, 1],
161+
[4, 4, 4, 4],
162+
],
163+
[
164+
[121, 22, 9, 12, 2, 2, 23],
165+
[7, 7, 7, 7, 7, 7, 7],
166+
],
167+
];
168+
169+
countingCardsTestCases.forEach(([deck, expected]) => {
170+
test(`countingCards([${deck}])`, () => {
171+
expect(countingCards(deck)).toStrictEqual(expected);
172+
});
173+
});
174+
});
175+
});

exercises/concept/elyses-transformative-enchantments/.meta/exemplar.js renamed to exercises/concept/elyses-transformative-enchantments/exemplar.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export function middleTwo(deck) {
3939
// TODO: which implementation?
4040
// const middle = Math.floor(deck.length / 2)
4141
// return deck.slice(middle, middle + 1)
42-
return deck.slice(4, 5);
42+
return deck.slice(4, 6);
4343
}
4444

4545
/**
@@ -53,7 +53,9 @@ export function middleTwo(deck) {
5353
export function sandwichTrick(deck) {
5454
const firstCard = deck.shift();
5555
const lastCard = deck.pop();
56-
return deck.splice(3, 0, lastCard, firstCard);
56+
const mid = deck.length / 2;
57+
deck.splice(mid, 0, lastCard, firstCard);
58+
return deck;
5759
}
5860

5961
/**
@@ -75,7 +77,7 @@ export function twoIsSpecial(deck) {
7577
* @returns {number[]} ordered deck
7678
*/
7779
export function perfectlyOrdered(deck) {
78-
return deck.sort();
80+
return deck.sort((a, b) => a - b);
7981
}
8082

8183
/**

0 commit comments

Comments
 (0)