From 9baca8b3f20cf6dd128d626b13868ead2033541a Mon Sep 17 00:00:00 2001 From: Derk-Jan Karrenbeld Date: Wed, 11 Jun 2025 00:35:31 +0200 Subject: [PATCH 1/2] Prevent not following the instructions --- .../concept/train-driver/.meta/exemplar.js | 6 ++-- .../concept/train-driver/train-driver.js | 6 ++-- .../concept/train-driver/train-driver.spec.js | 32 +++++++++++++------ 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/exercises/concept/train-driver/.meta/exemplar.js b/exercises/concept/train-driver/.meta/exemplar.js index b1e8c42096..716e020729 100644 --- a/exercises/concept/train-driver/.meta/exemplar.js +++ b/exercises/concept/train-driver/.meta/exemplar.js @@ -17,7 +17,7 @@ export function getListOfWagons(...ids) { /** * Reorder the array of wagons by moving the first 2 wagons to the end of the array. * - * @param {number[]} ids + * @param {Iterable} ids * @returns {number[]} reordered list of wagons */ export function fixListOfWagons([first, second, ...rest]) { @@ -27,8 +27,8 @@ export function fixListOfWagons([first, second, ...rest]) { /** * Fixes the array of wagons by inserting an array of wagons after the first element in eachWagonsID. * - * @param {number[]} ids - * @param {number[]} missingWagons + * @param {Iterable} ids + * @param {Iterable} missingWagons * @returns {number[]} corrected list of wagons */ export function correctListOfWagons([first, ...rest], missingWagons) { diff --git a/exercises/concept/train-driver/train-driver.js b/exercises/concept/train-driver/train-driver.js index b8601eec78..0e62d4a501 100644 --- a/exercises/concept/train-driver/train-driver.js +++ b/exercises/concept/train-driver/train-driver.js @@ -17,7 +17,7 @@ export function getListOfWagons(a, b, c, d, e, f, g, h, i, j, k, l, m, n) { /** * Reorder the array of wagons by moving the first 2 wagons to the end of the array. * - * @param {number[]} ids + * @param {Iterable} ids * @returns {number[]} reorderd list of wagons */ export function fixListOfWagons(ids) { @@ -27,8 +27,8 @@ export function fixListOfWagons(ids) { /** * Fixes the array of wagons by inserting an array of wagons after the first element in eachWagonsID. * - * @param {number[]} ids - * @param {number[]} missingWagons + * @param {Iterable} ids + * @param {Iterable} missingWagons * @returns {number[]} corrected list of wagons */ export function correctListOfWagons(ids, missingWagons) { diff --git a/exercises/concept/train-driver/train-driver.spec.js b/exercises/concept/train-driver/train-driver.spec.js index ebbb09edde..0fe6e12568 100644 --- a/exercises/concept/train-driver/train-driver.spec.js +++ b/exercises/concept/train-driver/train-driver.spec.js @@ -8,6 +8,20 @@ import { separateTimeOfArrival, } from './train-driver'; +class LimitedArray { + constructor(values) { + this.values = values; + } + + [Symbol.iterator]() { + return this.values[Symbol.iterator](); + } +} + +function list(...values) { + return new LimitedArray(values); +} + describe('getListOfWagons', () => { test('returns the correct array', () => { expect(getListOfWagons(1, 5, 2, 7, 4)).toEqual([1, 5, 2, 7, 4]); @@ -30,20 +44,20 @@ describe('getListOfWagons', () => { describe('fixListOfWagons', () => { test('reorders the first 2 wagons to the end of the array', () => { - const eachWagonsID = [3, 7, 1, 14, 10, 4, 12, 6, 23, 17, 13, 20, 8, 19]; + const eachWagonsID = list(3, 7, 1, 14, 10, 4, 12, 6, 23, 17, 13, 20, 8, 19); const expected = [1, 14, 10, 4, 12, 6, 23, 17, 13, 20, 8, 19, 3, 7]; expect(fixListOfWagons(eachWagonsID)).toEqual(expected); }); test('works when only 3 wagons given', () => { - const eachWagonsID = [4, 2, 1]; + const eachWagonsID = list(4, 2, 1); expect(fixListOfWagons(eachWagonsID)).toEqual([1, 4, 2]); }); test('works for a few wagons', () => { - const eachWagonsID = [3, 4, 1, 5, 7, 9, 10]; + const eachWagonsID = list(3, 4, 1, 5, 7, 9, 10); expect(fixListOfWagons(eachWagonsID)).toEqual([1, 5, 7, 9, 10, 3, 4]); }); @@ -51,8 +65,8 @@ describe('fixListOfWagons', () => { describe('correctListOfWagons', () => { test('returns a wagon weight list with the inserted array of values', () => { - const eachWagonsID = [1, 6, 11, 15, 13, 14, 17, 22, 2, 16, 19, 21]; - const missingWagons = [8, 10, 5, 9, 3, 7, 20]; + const eachWagonsID = list(1, 6, 11, 15, 13, 14, 17, 22, 2, 16, 19, 21); + const missingWagons = list(8, 10, 5, 9, 3, 7, 20); const expected = [ 1, 8, 10, 5, 9, 3, 7, 20, 6, 11, 15, 13, 14, 17, 22, 2, 16, 19, 21, ]; @@ -61,16 +75,16 @@ describe('correctListOfWagons', () => { }); test('works for short arrays', () => { - const eachWagonsID = [1, 7, 15, 24]; - const missingWagons = [8, 6, 4]; + const eachWagonsID = list(1, 7, 15, 24); + const missingWagons = list(8, 6, 4); const expected = [1, 8, 6, 4, 7, 15, 24]; expect(correctListOfWagons(eachWagonsID, missingWagons)).toEqual(expected); }); test('works when missingWagons is longer', () => { - const eachWagonsID = [1, 7, 15, 24]; - const missingWagons = [8, 6, 4, 5, 9, 21, 2, 13]; + const eachWagonsID = list(1, 7, 15, 24); + const missingWagons = list(8, 6, 4, 5, 9, 21, 2, 13); const expected = [1, 8, 6, 4, 5, 9, 21, 2, 13, 7, 15, 24]; expect(correctListOfWagons(eachWagonsID, missingWagons)).toEqual(expected); From 51fae43f527bcb288807829605ca08b6473069fe Mon Sep 17 00:00:00 2001 From: Derk-Jan Karrenbeld Date: Wed, 11 Jun 2025 00:46:35 +0200 Subject: [PATCH 2/2] Add logging overrides --- .../concept/train-driver/train-driver.spec.js | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/exercises/concept/train-driver/train-driver.spec.js b/exercises/concept/train-driver/train-driver.spec.js index 0fe6e12568..565cbeef60 100644 --- a/exercises/concept/train-driver/train-driver.spec.js +++ b/exercises/concept/train-driver/train-driver.spec.js @@ -8,14 +8,42 @@ import { separateTimeOfArrival, } from './train-driver'; +const customInspectSymbol = Symbol.for('nodejs.util.inspect.custom'); +const customLogSymbol = Symbol.for('exercism.javascript.util.log'); + +// Follow the instructions in case you are stuck on "list.method is not a function" class LimitedArray { constructor(values) { this.values = values; } + // Enables rest syntax and spread operator, as wel as for of, etc. [Symbol.iterator]() { return this.values[Symbol.iterator](); } + + // Log value in non-upgraded environments + toString() { + return this.values.toString(); + } + + // Overrides logging in node (ie. students working locally) + [customInspectSymbol](depth, inspectOptions, inspect) { + const inner = this.values[customInspectSymbol] + ? this.values[customInspectSymbol](depth, inspectOptions, inspect) + : this.values.toString(); + + return `List of (${inner})`; + } + + // Overrides log overrides in web environment (ie. students working in editor) + [customLogSymbol](depth, inspectOptions, inspect) { + const inner = this.values[customLogSymbol] + ? this.values[customLogSymbol](depth, inspectOptions, inspect) + : this.values.toString(); + + return `List of (${inner})`; + } } function list(...values) {