From 83077afac6330e76d093b7880076562a29d01acc Mon Sep 17 00:00:00 2001 From: Derk-Jan Karrenbeld Date: Mon, 9 Jun 2025 03:45:28 +0200 Subject: [PATCH 1/2] Fix typos and missing import --- .../concept/train-driver/train-driver.spec.js | 42 ++++++++++++++----- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/exercises/concept/train-driver/train-driver.spec.js b/exercises/concept/train-driver/train-driver.spec.js index fca6d1cc5b..ebbb09edde 100644 --- a/exercises/concept/train-driver/train-driver.spec.js +++ b/exercises/concept/train-driver/train-driver.spec.js @@ -1,3 +1,5 @@ +import { describe, test, expect } from '@jest/globals'; + import { getListOfWagons, fixListOfWagons, @@ -7,19 +9,19 @@ import { } from './train-driver'; describe('getListOfWagons', () => { - test('return the correct array', () => { + test('returns the correct array', () => { expect(getListOfWagons(1, 5, 2, 7, 4)).toEqual([1, 5, 2, 7, 4]); }); - test('works for a few arrgument', () => { + test('works for a few arguments', () => { expect(getListOfWagons(1, 5)).toEqual([1, 5]); }); - test('works for a one arrgument', () => { + test('works for a one argument', () => { expect(getListOfWagons(1)).toEqual([1]); }); - test('works for many argument', () => { + test('works for many arguments', () => { expect(getListOfWagons(1, 5, 6, 3, 9, 8, 4, 14, 24, 7)).toEqual([ 1, 5, 6, 3, 9, 8, 4, 14, 24, 7, ]); @@ -27,30 +29,34 @@ describe('getListOfWagons', () => { }); describe('fixListOfWagons', () => { - test('reorder the first 2 wagons to the end of the array', () => { + 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 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]; + expect(fixListOfWagons(eachWagonsID)).toEqual([1, 4, 2]); }); test('works for a few wagons', () => { const eachWagonsID = [3, 4, 1, 5, 7, 9, 10]; + expect(fixListOfWagons(eachWagonsID)).toEqual([1, 5, 7, 9, 10, 3, 4]); }); }); describe('correctListOfWagons', () => { - test('returns a wagon wieght list with the inserted array of values', () => { + 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 expected = [ 1, 8, 10, 5, 9, 3, 7, 20, 6, 11, 15, 13, 14, 17, 22, 2, 16, 19, 21, ]; + expect(correctListOfWagons(eachWagonsID, missingWagons)).toEqual(expected); }); @@ -58,6 +64,7 @@ describe('correctListOfWagons', () => { const eachWagonsID = [1, 7, 15, 24]; const missingWagons = [8, 6, 4]; const expected = [1, 8, 6, 4, 7, 15, 24]; + expect(correctListOfWagons(eachWagonsID, missingWagons)).toEqual(expected); }); @@ -65,18 +72,20 @@ describe('correctListOfWagons', () => { const eachWagonsID = [1, 7, 15, 24]; const missingWagons = [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); }); }); describe('extendRouteInformation', () => { - test('correctly extend route information', () => { + test('correctly extends route information', () => { const route = { from: 'Berlin', to: 'Hamburg' }; const moreRouteInformation = { timeOfArrival: '12:00', precipitation: '10', temperature: '5', }; + const expected = { from: 'Berlin', to: 'Hamburg', @@ -84,6 +93,7 @@ describe('extendRouteInformation', () => { precipitation: '10', temperature: '5', }; + expect(extendRouteInformation(route, moreRouteInformation)).toEqual( expected, ); @@ -92,24 +102,27 @@ describe('extendRouteInformation', () => { test('works when not adding precipitation', () => { const route = { from: 'Paris', to: 'London' }; const moreRouteInformation = { timeOfArrival: '10:30', temperature: '20' }; + const expected = { from: 'Paris', to: 'London', timeOfArrival: '10:30', temperature: '20', }; + expect(extendRouteInformation(route, moreRouteInformation)).toEqual( expected, ); }); - test('works when written in diffrent order', () => { + test('works when written in different order', () => { const route = { from: 'Gothenburg', to: 'Copenhagen' }; const moreRouteInformation = { precipitation: '1', timeOfArrival: '21:20', temperature: '-6', }; + const expected = { from: 'Gothenburg', to: 'Copenhagen', @@ -117,6 +130,7 @@ describe('extendRouteInformation', () => { timeOfArrival: '21:20', temperature: '-6', }; + expect(extendRouteInformation(route, moreRouteInformation)).toEqual( expected, ); @@ -124,7 +138,7 @@ describe('extendRouteInformation', () => { }); describe('separateTimeOfArrival', () => { - test('seperate timeOfArrival from object', () => { + test('separates timeOfArrival from complete object', () => { const route = { from: 'Berlin', to: 'Hamburg', @@ -132,28 +146,32 @@ describe('separateTimeOfArrival', () => { precipitation: '10', temperature: '5', }; + const expected = [ '12:00', { from: 'Berlin', to: 'Hamburg', precipitation: '10', temperature: '5' }, ]; + expect(separateTimeOfArrival(route)).toEqual(expected); }); - test('seperate timeOfArrival with shorter object', () => { + test('separates timeOfArrival with smaller object', () => { const route = { from: 'Paris', to: 'London', timeOfArrival: '10:30', temperature: '20', }; + const expected = [ '10:30', { from: 'Paris', to: 'London', temperature: '20' }, ]; + expect(separateTimeOfArrival(route)).toEqual(expected); }); - test('seperate timeOfArrival from object', () => { + test('separates timeOfArrival from differently ordered object', () => { const route = { from: 'Gothenburg', to: 'Copenhagen', @@ -161,6 +179,7 @@ describe('separateTimeOfArrival', () => { timeOfArrival: '21:20', temperature: '-6', }; + const expected = [ '21:20', { @@ -170,6 +189,7 @@ describe('separateTimeOfArrival', () => { temperature: '-6', }, ]; + expect(separateTimeOfArrival(route)).toEqual(expected); }); }); From 2fbc4aeae1589863797868ac91bff0aef580f412 Mon Sep 17 00:00:00 2001 From: Derk-Jan Karrenbeld Date: Mon, 9 Jun 2025 04:24:49 +0200 Subject: [PATCH 2/2] More clean-up --- .vscode/settings.json | 3 +- .../train-driver/.docs/instructions.md | 21 +++++++---- .../concept/train-driver/.meta/exemplar.js | 37 +++++++++---------- .../concept/train-driver/train-driver.js | 26 ++++++------- 4 files changed, 46 insertions(+), 41 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 47c6a8b260..3344c414bc 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -7,5 +7,6 @@ // because of how whitespace is (not) rendered. 65 ] - } + }, + "cSpell.words": ["reorderd"] } diff --git a/exercises/concept/train-driver/.docs/instructions.md b/exercises/concept/train-driver/.docs/instructions.md index 6c466db264..186c112501 100644 --- a/exercises/concept/train-driver/.docs/instructions.md +++ b/exercises/concept/train-driver/.docs/instructions.md @@ -1,6 +1,8 @@ # Instructions -Your friend Linus is a train driver who drives cargo trains between cities. Although they are amazing at handling trains, they are not amazing at handling logistics or computers. They would like to enlist your programming help organizing train details and correcting mistakes in route data. +Your friend Linus is a train driver who drives cargo trains between cities. +Although they are amazing at handling trains, they are not amazing at handling logistics or computers. +They would like to enlist your programming help organizing train details and correcting mistakes in route data. ```exercism/note To practice, use the rest or spread operator to solve each of the tasks below. @@ -8,7 +10,8 @@ To practice, use the rest or spread operator to solve each of the tasks below. ## 1. Create a list of all wagons -Your friend has been keeping track of each wagon identifier (ID), but they are never sure how many wagons the system is going to have to process at any given time. It would be much easier for the rest of the logistics program to have this data packaged into a unified `array`. +Your friend has been keeping track of each wagon identifier (ID), but they are never sure how many wagons the system is going to have to process at any given time. +It would be much easier for the rest of the logistics program to have this data packaged into a unified `array`. Implement a function `getListOfWagons` that accepts an arbitrary number of wagon IDs which are the IDs of each wagon. Each ID will be a positive integer. @@ -21,14 +24,16 @@ getListOfWagons(1, 7, 12, 3, 14, 8, 5); ## 2. Move the first two elements to the end of the array -At this point, you are starting to get a feel for the data and how it's used in the logistics program. The ID system always assigns the locomotive an ID of **1**, with the remainder of the wagons in the train assigned a randomly chosen ID greater than **1**. +At this point, you are starting to get a feel for the data and how it's used in the logistics program. +The ID system always assigns the locomotive an ID of **1**, with the remainder of the wagons in the train assigned a randomly chosen ID greater than **1**. -Your friend had to connect two new wagons to the train and forgot to update the system! Now, the first two wagons in the train `array` have to be moved to the end, or everything will be out of order. +Your friend had to connect two new wagons to the train and forgot to update the system! +Now, the first two wagons in the train `array` have to be moved to the end, or everything will be out of order. Linus would be really grateful to you for fixing their mistakes. Implement a function `fixListOfWagons` that accepts an array of the id of each wagon. -It `return` an `array` where the 2 first elements repositioned to the end of the `array` so that the locomotive can be in the front. +It `return`s an `array` where the 2 first elements repositioned to the end of the `array` so that the locomotive can be in the front. ```javascript eachWagonsID = [2, 5, 1, 7, 4, 12, 6, 3, 13]; @@ -43,7 +48,7 @@ Uh-oh. some wagons seem to have gone missing. Fortunately, your friend just found another `array` which appears to contain the missing wagon IDs, and would like you to add them into the main wagon ID `array`. All they can remember is that the missing values should be placed directly after the designated locomotive. -Given this new information, write a function called `CorrectListOfWagons` that takes two arrays which have the IDs of each wagon as the arguments. +Given this new information, write a function called `correctListOfWagons` that takes two arrays which have the IDs of each wagon as the arguments. The wagon IDs of the second `array` should be added into the first `array` directly after the locomotive (ID 1). ```javascript @@ -59,7 +64,7 @@ Now that all the wagon data is correct, your friend would like you to update the Initial routing information has been constructed as an `object`, and you friend would like you to update it with the additions provided. Every route requires slightly different information, so your friend would really prefer a generic solution. -Implement a function extendRouteInformation that accepts two `objects`. +Implement a function `extendRouteInformation` that accepts two `objects`. The first `object` contains which cities the train route moves between. The second `object` contains other routing details such as train speed or length. @@ -81,7 +86,7 @@ extendRouteInformation(route, moreRouteInformation); Your friend has noticed that they don't need the arrival time in the routing information. Therefore your friend would like you to separate the arrival time from the routing information. -Implement a function `separateArrivalTime` that accepts an object with the routing information. +Implement a function `separateTimeOfArrival` that accepts an object with the routing information. The function should return an array there the first element of the array is the arrival time and the second element is an object with the routing information without arrival time. ```javascript diff --git a/exercises/concept/train-driver/.meta/exemplar.js b/exercises/concept/train-driver/.meta/exemplar.js index 8588849b45..b1e8c42096 100644 --- a/exercises/concept/train-driver/.meta/exemplar.js +++ b/exercises/concept/train-driver/.meta/exemplar.js @@ -5,56 +5,55 @@ // implementing this exercise. /** - * Return each Wagons id in form of an array. + * Return each wagon's id in form of an array. * - * @param {number[]} eachWagonsID - * @returns {number[]} each Wagons Wiegth + * @param {number[]} ids + * @returns {number[]} wagon ids */ -export function getListOfWagons(...eachWagonsID) { - return eachWagonsID; +export function getListOfWagons(...ids) { + return ids; } /** * Reorder the array of wagons by moving the first 2 wagons to the end of the array. * - * @param {number[]} eachWagonsID - * @returns {number[]} reorderd list of wagons + * @param {number[]} ids + * @returns {number[]} reordered list of wagons */ -export function fixListOfWagons(eachWagonsID) { - const [first, second, ...rest] = eachWagonsID; +export function fixListOfWagons([first, second, ...rest]) { return [...rest, first, second]; } /** * Fixes the array of wagons by inserting an array of wagons after the first element in eachWagonsID. * - * @param {number[]} eachWagonsID + * @param {number[]} ids * @param {number[]} missingWagons * @returns {number[]} corrected list of wagons */ -export function correctListOfWagons(eachWagonsID, missingWagons) { - const [first, ...rest] = eachWagonsID; +export function correctListOfWagons([first, ...rest], missingWagons) { return [first, ...missingWagons, ...rest]; } /** * Extend route information by adding another object * - * @param {Record} route - * @param {Record} moreRouteInformation + * @param {Record} information + * @param {Record} additional * @returns {Record} extended route information */ -export function extendRouteInformation(route, moreRouteInformation) { - return { ...route, ...moreRouteInformation }; +export function extendRouteInformation(information, additional) { + return { ...information, ...additional }; } /** * Separate arrival time from the route information object * - * @param {Record} route + * @param {Record} information * @returns {[string, Record]} array with arrival time and object without arrival time */ -export function separateTimeOfArrival(route) { - const { timeOfArrival, ...rest } = route; +export function separateTimeOfArrival(information) { + const { timeOfArrival, ...rest } = information; + return [timeOfArrival, rest]; } diff --git a/exercises/concept/train-driver/train-driver.js b/exercises/concept/train-driver/train-driver.js index d7c06e9c26..b8601eec78 100644 --- a/exercises/concept/train-driver/train-driver.js +++ b/exercises/concept/train-driver/train-driver.js @@ -5,53 +5,53 @@ // implementing this exercise. /** - * Return each Wagons id in form of an array. + * Return each wagon's id in form of an array. * - * @param {number[]} eachWagonsID - * @returns {number[]} each Wagons Wiegth + * @param {number[]} ids + * @returns {number[]} wagon ids */ -export function getListOfWagons(eachWagonsID) { +export function getListOfWagons(a, b, c, d, e, f, g, h, i, j, k, l, m, n) { throw new Error('Please implement the getListOfWagons function'); } /** * Reorder the array of wagons by moving the first 2 wagons to the end of the array. * - * @param {number[]} eachWagonsID + * @param {number[]} ids * @returns {number[]} reorderd list of wagons */ -export function fixListOfWagons(eachWagonsID) { +export function fixListOfWagons(ids) { throw new Error('Please implement the fixListOfWagons function'); } /** * Fixes the array of wagons by inserting an array of wagons after the first element in eachWagonsID. * - * @param {number[]} eachWagonsID + * @param {number[]} ids * @param {number[]} missingWagons * @returns {number[]} corrected list of wagons */ -export function correctListOfWagons(eachWagonsID, missingWagons) { +export function correctListOfWagons(ids, missingWagons) { throw new Error('Please implement the correctListOfWagons function'); } /** * Extend route information by adding another object * - * @param {Record} route - * @param {Record} moreRouteInformation + * @param {Record} information + * @param {Record} additional * @returns {Record} extended route information */ -export function extendRouteInformation(route, moreRouteInformation) { +export function extendRouteInformation(information, additional) { throw new Error('Please implement the extendRouteInformation function'); } /** * Separate arrival time from the route information object * - * @param {Record} route + * @param {Record} information * @returns {[string, Record]} array with arrival time and object without arrival time */ -export function separateTimeOfArrival(route) { +export function separateTimeOfArrival(information) { throw new Error('Please implement the separateTimeOfArrival function'); }