Skip to content

Commit 8e6b360

Browse files
committed
Various fixes and updated test file
1 parent 439ddd2 commit 8e6b360

File tree

5 files changed

+90
-94
lines changed

5 files changed

+90
-94
lines changed
Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
# Hints
22

3-
## 1. Determine the total number of birds that you counted so far
3+
## 1. Create a list of all wagons
44

5-
- Refer to the exercise introduction for an example of how to use a for loop to iterate over an array.
6-
- Use a helper variable to store the total count and increase that variable as you go through the array.
7-
- Think about the correct initial value for that helper variable.
8-
- Refer back to the [array concept][concept-arrays] to recap how to retrieve values from an array.
95

10-
## 2. Calculate the number of visiting birds in a specific week
116

12-
- This task is similar to the first one.
13-
You can copy your code as a starting point.
14-
- Think about which indexes in the array you would need to take into account for week number 1 and 2, respectively.
15-
- Now, find a general way to calculate the first and the last index that should be considered.
16-
- With that, you can set up the for loop to only iterate over the relevant section of the array.
7+
## 2. Move the first two elements to the end of the array
178

18-
## 3. Fix a counting mistake
199

20-
- Again, you need to set up a for loop to iterate over the whole bird count array.
21-
- This time you only need to visit every second entry in the array.
22-
- Change the step so the counter variable is increased accordingly after each iteration.
23-
- In the body of the for loop you can use the increment operator to change the value of an element in an array in place.
2410

25-
[concept-arrays]: /tracks/javascript/concepts/arrays
11+
## 3. Add missing values
12+
13+
14+
15+
## 4. Extend routing information
16+
17+
18+
19+
## 5. Separate arrival time from routing information
20+
21+

exercises/concept/train-driver/.docs/instructions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Your friend has noticed that they don't need the arrival time in the routing inf
8080
Therefore your friend would like you to separate the arrival time from the routing information.
8181

8282
Implement a function `separateArrivalTime` that accepts an object with the routing information.
83-
The function should return two objects.
83+
The function should return an array there the first elment of the arrival time and the second element is an object with the routing information.
8484

8585
```javascript
8686
routeInformation= {
@@ -90,5 +90,5 @@ routeInformation= {
9090
timeOfArrival: "10:10"
9191
};
9292
separateTimeOfArrival(routeInformation);
93-
// => {timeOfArrival: "10:10"}, {from: "Berlin", to: "Hamburg", length: "100"}
93+
// => ["10:10", {from: "Berlin", to: "Hamburg", length: "100"}]
9494
```

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,56 +5,56 @@
55
// implementing this exercise.
66

77
/**
8-
* Return each Wagons Wiegth.
8+
* Return each Wagons id in form of an array.
99
*
10-
* @param {number[]} eachWagonsWiegth
10+
* @param {number[]} eachWagonsID
1111
* @returns {number[]} each Wagons Wiegth
1212
*/
13-
export function getListOfWagons(...eachWagonsWiegth) {
14-
return eachWagonsWiegth;
13+
export function getListOfWagons(...eachWagonsID) {
14+
return eachWagonsID
1515
}
1616

1717
/**
1818
* Reorder the array of wagons by moving the first 2 wagons to the end of the array.
1919
*
20-
* @param {number[]} eachWagonsWieght
20+
* @param {number[]} eachWagonsID
2121
* @returns {number[]} reorderd list of wagons
2222
*/
23-
export function fixListOfWagons(eachWagonsWieght) {
24-
const [first, second, ...rest] = eachWagonsWieght;
23+
export function fixListOfWagons(eachWagonsID) {
24+
const [first, second, ...rest] = eachWagonsID;
2525
return [...rest, first, second];
2626
}
2727

2828
/**
29-
* Fixes the list of wagons by inserting an array of wagons after the first element in eachWagonsWieght.
29+
* Fixes the array of wagons by inserting an array of wagons after the first element in eachWagonsID.
3030
*
31-
* @param {number[]} eachWagonsWieght
31+
* @param {number[]} eachWagonsID
3232
* @param {number[]} missingWagons
3333
* @returns {number[]} corrected list of wagons
3434
*/
35-
export function correctListOfWagons(eachWagonsWieght, missingWagons) {
36-
const [first, ...rest] = eachWagonsWieght;
35+
export function correctListOfWagons(eachWagonsID, missingWagons) {
36+
const [first, ...rest] = eachWagonsID;
3737
return [first, ...missingWagons, ...rest];
3838
}
3939

4040
/**
41-
* Updates the route information by adding more routing information
41+
* Extend route information by adding another object
4242
*
4343
* @param {Record<string, string>} route
4444
* @param {Record<string, string>} moreRouteInformation
45-
* @returns {Record<string, string>} updates route information
45+
* @returns {Record<string, string>} extended route information
4646
*/
47-
export function updateRoutingInformation(route, moreRouteInformation) {
47+
export function extendRouteInformation(route, moreRouteInformation) {
4848
return { ...route, ...moreRouteInformation };
4949
}
5050

5151
/**
52-
* Remove arrival time from the route information object
52+
* Separate arrival time from the route information object
5353
*
5454
* @param {Record<string, string>} route
55-
* @returns {Record<string, string>} object without arrival time
55+
* @returns {[string, Record<string, string>]} array with arrival time and object without arrival time
5656
*/
57-
export function removeTimeOfArrival(route) {
58-
const { arrivalTime, ...rest } = route;
59-
return rest;
57+
export function separateTimeOfArrival(route) {
58+
const { timeOfArrival, ...rest } = route;
59+
return [timeOfArrival, rest];
6060
}

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,53 @@
55
// implementing this exercise.
66

77
/**
8-
* Return each Wagons Wiegth.
8+
* Return each Wagons id in form of an array.
99
*
10-
* @param {number[]} eachWagonsWiegth
10+
* @param {number[]} eachWagonsID
1111
* @returns {number[]} each Wagons Wiegth
1212
*/
13-
export function getListOfWagons(eachWagonsWiegth) {
13+
export function getListOfWagons(eachWagonsID) {
1414
throw new Error('Please implement the getListOfWagons function');
1515
}
1616

1717
/**
1818
* Reorder the array of wagons by moving the first 2 wagons to the end of the array.
1919
*
20-
* @param {number[]} eachWagonsWieght
20+
* @param {number[]} eachWagonsID
2121
* @returns {number[]} reorderd list of wagons
2222
*/
23-
export function fixListOfWagons(eachWagonsWieght) {
23+
export function fixListOfWagons(eachWagonsID) {
2424
throw new Error('Please implement the fixListOfWagons function');
2525
}
2626

2727
/**
28-
* Fixes the list of wagons by inserting an array of wagons after the first element in eachWagonsWieght.
28+
* Fixes the array of wagons by inserting an array of wagons after the first element in eachWagonsID.
2929
*
30-
* @param {number[]} eachWagonsWieght
30+
* @param {number[]} eachWagonsID
3131
* @param {number[]} missingWagons
3232
* @returns {number[]} corrected list of wagons
3333
*/
34-
export function correctListOfWagons(eachWagonsWieght, missingWagons) {
34+
export function correctListOfWagons(eachWagonsID, missingWagons) {
3535
throw new Error('Please implement the correctListOfWagons function');
3636
}
3737

3838
/**
39-
* Updates the route information by adding more routing information
39+
* Extend route information by adding another object
4040
*
4141
* @param {Record<string, string>} route
4242
* @param {Record<string, string>} moreRouteInformation
43-
* @returns {Record<string, string>} updates route information
43+
* @returns {Record<string, string>} extended route information
4444
*/
45-
export function updateRoutingInformation(route, moreRouteInformation) {
46-
throw new Error('Please implement the updateRoutingInformation function');
45+
export function extendRouteInformation(route, moreRouteInformation) {
46+
throw new Error('Please implement the extendRouteInformation function');
4747
}
4848

4949
/**
50-
* Remove arrival time from the route information object
50+
* Separate arrival time from the route information object
5151
*
5252
* @param {Record<string, string>} route
53-
* @returns {Record<string, string>} object without arrival time
53+
* @returns {[string, Record<string, string>]} array with arrival time and object without arrival time
5454
*/
55-
export function removeTimeOfArrival(route) {
56-
throw new Error('Please implement the removeTimeOfArrival function');
55+
export function separateTimeOfArrival(route) {
56+
throw new Error('Please implement the separateTimeOfArrival function');
5757
}
Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,103 @@
1-
import { getListOfWagons, fixListOfWagons, correctListOfWagons, updateRoutingInformation, removeTimeOfArrival } from './train-driver';
1+
import { getListOfWagons, fixListOfWagons, correctListOfWagons, extendRouteInformation, separateTimeOfArrival } from './train-driver';
22

33
describe('getListOfWagons', () => {
44
test('return the correct array', () => {
5-
expect(getListOfWagons(4,5,2,7,4)).toEqual([4,5,2,7,4]);
5+
expect(getListOfWagons(1,5,2,7,4)).toEqual([1,5,2,7,4]);
66
});
77

88
test('works for a few arrgument', () => {
9-
expect(getListOfWagons(4,5)).toEqual([4,5]);
9+
expect(getListOfWagons(1,5)).toEqual([1,5]);
1010
});
1111

1212
test('works for a one arrgument', () => {
13-
expect(getListOfWagons(5)).toEqual([5]);
13+
expect(getListOfWagons(1)).toEqual([1]);
1414
});
1515

1616
test('works for many argument', () => {
17-
expect(getListOfWagons(4,5,6,3,6,8,4,1,4,7)).toEqual([4,5,6,3,6,8,4,1,4,7]);
17+
expect(getListOfWagons(1,5,6,3,9,8,4,14,24,7)).toEqual([1,5,6,3,9,8,4,14,24,7]);
1818
});
1919
});
2020

2121
describe('fixListOfWagons', () => {
2222
test('reorder the first 2 wagons to the end of the array', () => {
23-
const eachWagonsWieght = [3, 0, 5, 1, 0, 4, 1, 0, 3, 4, 3, 0, 8, 0];
24-
const expected = [5, 1, 0, 4, 1, 0, 3, 4, 3, 0, 8, 0, 3, 0];
25-
expect(fixListOfWagons(eachWagonsWieght)).toEqual(expected);
23+
const eachWagonsID = [3, 7, 1, 14, 10, 4, 12, 6, 23, 17, 13, 20, 8, 19];
24+
const expected = [1, 14, 10, 4, 12, 6, 23, 17, 13, 20, 8, 19, 3, 7];
25+
expect(fixListOfWagons(eachWagonsID)).toEqual(expected);
2626
});
2727

28-
test('works when only 2 wagons given', () => {
29-
const eachWagonsWieght = [1, 2];
30-
expect(fixListOfWagons(eachWagonsWieght)).toEqual([1, 2]);
28+
test('works when only 3 wagons given', () => {
29+
const eachWagonsID = [4, 2, 1];
30+
expect(fixListOfWagons(eachWagonsID)).toEqual([1, 4, 2]);
3131
});
3232

3333
test('works for a few wagons', () => {
34-
const eachWagonsWieght = [3, 4, 3, 3, 2, 1, 0];
35-
expect(fixListOfWagons(eachWagonsWieght)).toEqual([3, 3, 2, 1, 0, 3, 4]);
34+
const eachWagonsID = [3, 4, 1, 5, 7, 9, 10];
35+
expect(fixListOfWagons(eachWagonsID)).toEqual([1, 5, 7, 9, 10, 3, 4]);
3636
});
3737
});
3838

3939
describe('correctListOfWagons', () => {
4040
test('returns a wagon wieght list with the inserted array of values', () => {
41-
const eachWagonsWieght = [3, 0, 5, 1, 0, 4, 1, 0, 3, 4, 3, 0];
42-
const missingWagons = [8, 0, 5, 8, 0, 8, 0];
43-
const expected = [3, 8, 0, 5, 8, 0, 8, 0, 0, 5, 1, 0, 4, 1, 0, 3, 4, 3, 0];
44-
expect(correctListOfWagons(eachWagonsWieght, missingWagons)).toEqual(expected);
41+
const eachWagonsID = [1, 6, 11, 15, 13, 14, 17, 22, 2, 16, 19, 21];
42+
const missingWagons = [8, 10, 5, 9, 3, 7, 20];
43+
const expected = [1, 8, 10, 5, 9, 3, 7, 20, 6, 11, 15, 13, 14, 17, 22, 2, 16, 19, 21];
44+
expect(correctListOfWagons(eachWagonsID, missingWagons)).toEqual(expected);
4545
});
4646

4747
test('works for short arrays', () => {
48-
const eachWagonsWieght = [2, 0, 1, 0];
49-
const missingWagons = [8, 6, 0];
50-
const expected = [2, 8, 6, 0, 0, 1, 0];
51-
expect(correctListOfWagons(eachWagonsWieght, missingWagons)).toEqual(expected);
48+
const eachWagonsID = [1, 7, 15, 24];
49+
const missingWagons = [8, 6, 4];
50+
const expected = [1, 8, 6, 4, 7, 15, 24];
51+
expect(correctListOfWagons(eachWagonsID, missingWagons)).toEqual(expected);
5252
});
5353

5454
test('works when missingWagons is longer', () => {
55-
const eachWagonsWieght = [2, 0, 1, 0];
56-
const missingWagons = [8, 6, 0, 5, 8, 0, 8, 0];
57-
const expected = [2, 8, 6, 0, 5, 8, 0, 8, 0, 0, 1, 0];
58-
expect(correctListOfWagons(eachWagonsWieght, missingWagons)).toEqual(expected);
55+
const eachWagonsID = [1, 7, 15, 24];
56+
const missingWagons = [8, 6, 4, 5, 9, 21, 2, 13];
57+
const expected = [1, 8, 6, 4, 5, 9, 21, 2, 13, 7, 15, 24];
58+
expect(correctListOfWagons(eachWagonsID, missingWagons)).toEqual(expected);
5959
});
6060
});
6161

62-
describe('updateRoutingInformation', () => {
63-
test('correctly updates route information', () => {
62+
describe('extendRouteInformation', () => {
63+
test('correctly extend route information', () => {
6464
const route = {from: "Berlin", to: "Hamburg"};
6565
const moreRouteInformation = {timeOfArrival: "12:00", precipitation: "10", temperature: "5"};
6666
const expected = {from: "Berlin", to: "Hamburg", timeOfArrival: "12:00", precipitation: "10", temperature: "5"};
67-
expect(updateRoutingInformation(route, moreRouteInformation)).toEqual(expected);
67+
expect(extendRouteInformation(route, moreRouteInformation)).toEqual(expected);
6868
});
6969

7070
test('works when not adding precipitation', () => {
7171
const route = {from: "Paris", to: "London"};
7272
const moreRouteInformation = {timeOfArrival: "10:30", temperature: "20"};
7373
const expected = {from: "Paris", to: "London", timeOfArrival: "10:30", temperature: "20"};
74-
expect(updateRoutingInformation(route, moreRouteInformation)).toEqual(expected);
74+
expect(extendRouteInformation(route, moreRouteInformation)).toEqual(expected);
7575
});
7676

7777
test('works when written in diffrent order', () => {
7878
const route = {from: "Gothenburg", to: "Copenhagen"};
7979
const moreRouteInformation = {precipitation: "1", timeOfArrival: "21:20", temperature: "-6"};
8080
const expected = {from: "Gothenburg", to: "Copenhagen", precipitation: "1", timeOfArrival: "21:20", temperature: "-6"};
81-
expect(updateRoutingInformation(route, moreRouteInformation)).toEqual(expected);
81+
expect(extendRouteInformation(route, moreRouteInformation)).toEqual(expected);
8282
});
8383
});
8484

85-
describe('removeTimeOfArrival', () => {
86-
test('remove removeTimeOfArrival from object', () => {
85+
describe('separateTimeOfArrival', () => {
86+
test('seperate timeOfArrival from object', () => {
8787
const route = {from: "Berlin", to: "Hamburg", timeOfArrival: "12:00", precipitation: "10", temperature: "5"};
88-
const expected = {from: "Berlin", to: "Hamburg", precipitation: "10", temperature: "5"};
89-
expect(removeTimeOfArrival(route)).toEqual(expected);
88+
const expected = ["12:00", {from: "Berlin", to: "Hamburg", precipitation: "10", temperature: "5"}]
89+
expect(separateTimeOfArrival(route)).toEqual(expected);
9090
});
9191

92-
test('remove removeTimeOfArrival with shorter object', () => {
92+
test('seperate timeOfArrival with shorter object', () => {
9393
const route = {from: "Paris", to: "London", timeOfArrival: "10:30", temperature: "20"};
94-
const expected = {from: "Paris", to: "London", temperature: "20"};
95-
expect(removeTimeOfArrival(route)).toEqual(expected);
94+
const expected = ["10:30", {from: "Paris", to: "London", temperature: "20"}]
95+
expect(separateTimeOfArrival(route)).toEqual(expected);
9696
});
9797

98-
test('remove removeTimeOfArrival from object', () => {
98+
test('seperate timeOfArrival from object', () => {
9999
const route = {from: "Gothenburg", to: "Copenhagen", precipitation: "1", timeOfArrival: "21:20", temperature: "-6"};
100-
const expected = {from: "Gothenburg", to: "Copenhagen", precipitation: "1", temperature: "-6"};
101-
expect(removeTimeOfArrival(route)).toEqual(expected);
100+
const expected = ["21:20", {from: "Gothenburg", to: "Copenhagen", precipitation: "1", temperature: "-6"}]
101+
expect(separateTimeOfArrival(route)).toEqual(expected);
102102
});
103103
});

0 commit comments

Comments
 (0)