Skip to content

Commit 486d201

Browse files
Train Driver: Prevent not following the instructions (#2664)
* Prevent not following the instructions * Add logging overrides
1 parent 5b229bb commit 486d201

File tree

3 files changed

+57
-15
lines changed

3 files changed

+57
-15
lines changed

exercises/concept/train-driver/.meta/exemplar.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function getListOfWagons(...ids) {
1717
/**
1818
* Reorder the array of wagons by moving the first 2 wagons to the end of the array.
1919
*
20-
* @param {number[]} ids
20+
* @param {Iterable<number>} ids
2121
* @returns {number[]} reordered list of wagons
2222
*/
2323
export function fixListOfWagons([first, second, ...rest]) {
@@ -27,8 +27,8 @@ export function fixListOfWagons([first, second, ...rest]) {
2727
/**
2828
* Fixes the array of wagons by inserting an array of wagons after the first element in eachWagonsID.
2929
*
30-
* @param {number[]} ids
31-
* @param {number[]} missingWagons
30+
* @param {Iterable<number>} ids
31+
* @param {Iterable<number>} missingWagons
3232
* @returns {number[]} corrected list of wagons
3333
*/
3434
export function correctListOfWagons([first, ...rest], missingWagons) {

exercises/concept/train-driver/train-driver.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function getListOfWagons(a, b, c, d, e, f, g, h, i, j, k, l, m, n) {
1717
/**
1818
* Reorder the array of wagons by moving the first 2 wagons to the end of the array.
1919
*
20-
* @param {number[]} ids
20+
* @param {Iterable<number>} ids
2121
* @returns {number[]} reorderd list of wagons
2222
*/
2323
export function fixListOfWagons(ids) {
@@ -27,8 +27,8 @@ export function fixListOfWagons(ids) {
2727
/**
2828
* Fixes the array of wagons by inserting an array of wagons after the first element in eachWagonsID.
2929
*
30-
* @param {number[]} ids
31-
* @param {number[]} missingWagons
30+
* @param {Iterable<number>} ids
31+
* @param {Iterable<number>} missingWagons
3232
* @returns {number[]} corrected list of wagons
3333
*/
3434
export function correctListOfWagons(ids, missingWagons) {

exercises/concept/train-driver/train-driver.spec.js

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,48 @@ import {
88
separateTimeOfArrival,
99
} from './train-driver';
1010

11+
const customInspectSymbol = Symbol.for('nodejs.util.inspect.custom');
12+
const customLogSymbol = Symbol.for('exercism.javascript.util.log');
13+
14+
// Follow the instructions in case you are stuck on "list.method is not a function"
15+
class LimitedArray {
16+
constructor(values) {
17+
this.values = values;
18+
}
19+
20+
// Enables rest syntax and spread operator, as wel as for of, etc.
21+
[Symbol.iterator]() {
22+
return this.values[Symbol.iterator]();
23+
}
24+
25+
// Log value in non-upgraded environments
26+
toString() {
27+
return this.values.toString();
28+
}
29+
30+
// Overrides logging in node (ie. students working locally)
31+
[customInspectSymbol](depth, inspectOptions, inspect) {
32+
const inner = this.values[customInspectSymbol]
33+
? this.values[customInspectSymbol](depth, inspectOptions, inspect)
34+
: this.values.toString();
35+
36+
return `List of (${inner})`;
37+
}
38+
39+
// Overrides log overrides in web environment (ie. students working in editor)
40+
[customLogSymbol](depth, inspectOptions, inspect) {
41+
const inner = this.values[customLogSymbol]
42+
? this.values[customLogSymbol](depth, inspectOptions, inspect)
43+
: this.values.toString();
44+
45+
return `List of (${inner})`;
46+
}
47+
}
48+
49+
function list(...values) {
50+
return new LimitedArray(values);
51+
}
52+
1153
describe('getListOfWagons', () => {
1254
test('returns the correct array', () => {
1355
expect(getListOfWagons(1, 5, 2, 7, 4)).toEqual([1, 5, 2, 7, 4]);
@@ -30,29 +72,29 @@ describe('getListOfWagons', () => {
3072

3173
describe('fixListOfWagons', () => {
3274
test('reorders the first 2 wagons to the end of the array', () => {
33-
const eachWagonsID = [3, 7, 1, 14, 10, 4, 12, 6, 23, 17, 13, 20, 8, 19];
75+
const eachWagonsID = list(3, 7, 1, 14, 10, 4, 12, 6, 23, 17, 13, 20, 8, 19);
3476
const expected = [1, 14, 10, 4, 12, 6, 23, 17, 13, 20, 8, 19, 3, 7];
3577

3678
expect(fixListOfWagons(eachWagonsID)).toEqual(expected);
3779
});
3880

3981
test('works when only 3 wagons given', () => {
40-
const eachWagonsID = [4, 2, 1];
82+
const eachWagonsID = list(4, 2, 1);
4183

4284
expect(fixListOfWagons(eachWagonsID)).toEqual([1, 4, 2]);
4385
});
4486

4587
test('works for a few wagons', () => {
46-
const eachWagonsID = [3, 4, 1, 5, 7, 9, 10];
88+
const eachWagonsID = list(3, 4, 1, 5, 7, 9, 10);
4789

4890
expect(fixListOfWagons(eachWagonsID)).toEqual([1, 5, 7, 9, 10, 3, 4]);
4991
});
5092
});
5193

5294
describe('correctListOfWagons', () => {
5395
test('returns a wagon weight list with the inserted array of values', () => {
54-
const eachWagonsID = [1, 6, 11, 15, 13, 14, 17, 22, 2, 16, 19, 21];
55-
const missingWagons = [8, 10, 5, 9, 3, 7, 20];
96+
const eachWagonsID = list(1, 6, 11, 15, 13, 14, 17, 22, 2, 16, 19, 21);
97+
const missingWagons = list(8, 10, 5, 9, 3, 7, 20);
5698
const expected = [
5799
1, 8, 10, 5, 9, 3, 7, 20, 6, 11, 15, 13, 14, 17, 22, 2, 16, 19, 21,
58100
];
@@ -61,16 +103,16 @@ describe('correctListOfWagons', () => {
61103
});
62104

63105
test('works for short arrays', () => {
64-
const eachWagonsID = [1, 7, 15, 24];
65-
const missingWagons = [8, 6, 4];
106+
const eachWagonsID = list(1, 7, 15, 24);
107+
const missingWagons = list(8, 6, 4);
66108
const expected = [1, 8, 6, 4, 7, 15, 24];
67109

68110
expect(correctListOfWagons(eachWagonsID, missingWagons)).toEqual(expected);
69111
});
70112

71113
test('works when missingWagons is longer', () => {
72-
const eachWagonsID = [1, 7, 15, 24];
73-
const missingWagons = [8, 6, 4, 5, 9, 21, 2, 13];
114+
const eachWagonsID = list(1, 7, 15, 24);
115+
const missingWagons = list(8, 6, 4, 5, 9, 21, 2, 13);
74116
const expected = [1, 8, 6, 4, 5, 9, 21, 2, 13, 7, 15, 24];
75117

76118
expect(correctListOfWagons(eachWagonsID, missingWagons)).toEqual(expected);

0 commit comments

Comments
 (0)