Skip to content

Train Driver: Prevent not following the instructions #2664

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions exercises/concept/train-driver/.meta/exemplar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>} ids
* @returns {number[]} reordered list of wagons
*/
export function fixListOfWagons([first, second, ...rest]) {
Expand All @@ -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<number>} ids
* @param {Iterable<number>} missingWagons
* @returns {number[]} corrected list of wagons
*/
export function correctListOfWagons([first, ...rest], missingWagons) {
Expand Down
6 changes: 3 additions & 3 deletions exercises/concept/train-driver/train-driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>} ids
* @returns {number[]} reorderd list of wagons
*/
export function fixListOfWagons(ids) {
Expand All @@ -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<number>} ids
* @param {Iterable<number>} missingWagons
* @returns {number[]} corrected list of wagons
*/
export function correctListOfWagons(ids, missingWagons) {
Expand Down
60 changes: 51 additions & 9 deletions exercises/concept/train-driver/train-driver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,48 @@ 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) {
return new LimitedArray(values);
}

describe('getListOfWagons', () => {
test('returns the correct array', () => {
expect(getListOfWagons(1, 5, 2, 7, 4)).toEqual([1, 5, 2, 7, 4]);
Expand All @@ -30,29 +72,29 @@ 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]);
});
});

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,
];
Expand All @@ -61,16 +103,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);
Expand Down