Skip to content

Commit f98dbb8

Browse files
committed
Added filles
1 parent cd68424 commit f98dbb8

File tree

14 files changed

+596
-0
lines changed

14 files changed

+596
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Hints
2+
3+
## 1. Determine the total number of birds that you counted so far
4+
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.
9+
10+
## 2. Calculate the number of visiting birds in a specific week
11+
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.
17+
18+
## 3. Fix a counting mistake
19+
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.
24+
25+
[concept-arrays]: /tracks/javascript/concepts/arrays
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# Instructions
2+
3+
Your friend is a train driver and has to drive cargo trains between cities. Although your friend isn't amazing with handling the computers and would like some help with it. Your friend would like your help organizing the train and correcting the mistakes in the data.
4+
5+
6+
```exercism/note
7+
To practice, use a for the rest or spread to solve each of the tasks below.
8+
```
9+
10+
## 1. Convert the data to an array
11+
12+
Your friend has been keeping track of how much each wagon weighs. Although they are not sure how many wagons and would like the data to be returned as an array.
13+
14+
```exercism/note
15+
16+
Implement a function `getListOfWagons` that accepts an unknown amount of whole numbers that contains the weight of each wagon. It should return an array of all the wagon weights.
17+
18+
```
19+
20+
```javascript
21+
getListOfWagons(5, 7, 12, 3, 14, 8, 3);
22+
// => [5, 7, 12, 3, 14, 8, 3]
23+
```
24+
25+
## 2. Move the first two elements to the end of the array
26+
27+
Now that you got a general feel for handling your friend's data. Your friend has noticed that the first two days' values are not in the correct place. Your friend would like you to move the first two days' value to the end of the array.
28+
29+
```exercism/note
30+
31+
Implement a function `fixListOfWagons` that accepts an array of the weight of each wagon.
32+
It returns an array where the 2 first elements are moved to the end of the array.
33+
34+
```
35+
36+
```javascript
37+
eachWagonsWieght = [2, 5, 0, 7, 4, 0, 1, 3, 1];
38+
fixListOfWagons(eachWagonsWieght);
39+
// => [0, 7, 4, 0, 1, 3, 1, 2, 5]
40+
```
41+
42+
## 3. Add missing values
43+
44+
Your friend realized that all data wasn't added and found another array which contains the missing values.
45+
Your friend would like you to add the missing values to the array.
46+
All they can remember is that the missing values should be placed after the first element in the array.
47+
48+
49+
```exercism/note
50+
51+
Given this new information, write a function called `CorrectListOfWagons` that takes two arrays which have the values of the weight of each wagon as an argument.
52+
The second array should be added after the first element of the first array.
53+
54+
```
55+
56+
57+
```javascript
58+
eachWagonsWieght = [2, 5, 0, 7, 4, 1];
59+
missingWagons = [3, 0, 6, 1];
60+
CorrectListOfWagons(eachWagonsWieght, missingWagons);
61+
// => [2, 3, 0, 6, 1, 5, 0, 7, 4, 1]
62+
```
63+
64+
## 4. Update routing information
65+
66+
Now that the wagon data is correct, your friend would like you to update the routing information.
67+
Your friend has an object with the routing information and would like you to add more routing information to the object.
68+
Every route requires a bit different information so your friend would prefer a generic solution.
69+
70+
71+
```exercism/note
72+
73+
Implement a function `updateRoutingInformation` that accepts two objects.
74+
The first object contains which city the train should go between and the second object contains more routing information.
75+
The function should return an object with the updated routing information.
76+
77+
```
78+
79+
```javascript
80+
route = {from: "Berlin", to: "Hamburg"};
81+
moreRouteInformation = {length: 100, speed: 50};
82+
updateRoutingInformation(route, moreRouteInformation);
83+
// => {from: "Berlin", to: "Hamburg", length: 100, speed: 50}
84+
```
85+
86+
## 5. Remove arrival time from routing information
87+
88+
Your friend has noticed that they don't need the arrival time in the routing information.
89+
Therefore your friend would like you to remove the arrival time from the routing information.
90+
91+
```exercism/note
92+
93+
Implement a function `removeArrivalTime` that accepts an object with the routing information.
94+
The function should return an object
95+
96+
```
97+
98+
```javascript
99+
routeInformation= {
100+
from: "Berlin",
101+
to: "Hamburg",
102+
length: 100,
103+
timeOfArrival: "10:10"
104+
};
105+
removeTimeOfArrival(routeInformation);
106+
// => {from: "Berlin", to: "Hamburg", length: 100}
107+
```
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Introduction
2+
3+
JavaScript has a built-in `...` operator that makes it easier to work with indefinite numbers of elements. Depending on the context, it's called either a _rest operator_ or _spread operator_.
4+
5+
## Rest operator
6+
7+
### Rest elements
8+
9+
When `...` appears on the left-hand side of an assignment, those three dots are known as the `rest` operator. The three dots together with a variable name is called a rest element. It collects zero or more values, and stores them into a single array.
10+
11+
```javascript
12+
const [a, b, ...everythingElse] = [0, 1, 1, 2, 3, 5, 8];
13+
a;
14+
// => 0
15+
b;
16+
// => 1
17+
everythingElse;
18+
// => [1, 2, 3, 5, 8]
19+
```
20+
21+
Note that in JavaScript, unlike some other languages, a `rest` element cannot have a trailing comma. It _must_ be the last element in a destructuring assignment. The example below throws a `SyntaxError`:
22+
23+
```javascript
24+
const [...items, last] = [2, 4, 8, 16]
25+
```
26+
27+
### Rest properties
28+
29+
Similarly to arrays, the rest operator can also be used to collect one or more object properties and store them in a single object.
30+
31+
```javascript
32+
const { street, ...address } = {
33+
street: 'Platz der Republik 1',
34+
postalCode: '11011',
35+
city: 'Berlin',
36+
};
37+
street;
38+
// => 'Platz der Republik 1'
39+
address;
40+
// => {postalCode: '11011', city: 'Berlin'}
41+
```
42+
43+
## Rest parameters
44+
45+
When `...` appears in a function definition next to its last argument, that parameter is called a _rest parameter_. It allows the function to accept an indefinite number of arguments as an array.
46+
47+
```javascript
48+
function concat(...strings) {
49+
return strings.join(' ');
50+
}
51+
concat('one');
52+
// => 'one'
53+
concat('one', 'two', 'three');
54+
// => 'one two three'
55+
```
56+
57+
## Spread
58+
59+
### Spread elements
60+
61+
When `...` appears on the right-hand side of an assignment, it's known as the `spread` operator. It expands an array into a list of elements. Unlike the rest element, it can appear anywhere in an array literal expression, and there can be more than one.
62+
63+
```javascript
64+
const oneToFive = [1, 2, 3, 4, 5];
65+
const oneToTen = [...oneToFive, 6, 7, 8, 9, 10];
66+
oneToTen;
67+
// => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
68+
const woow = ['A', ...oneToFive, 'B', 'C', 'D', 'E', ...oneToFive, 42];
69+
woow;
70+
// => ["A", 1, 2, 3, 4, 5, "B", "C", "D", "E", 1, 2, 3, 4, 5, 42]
71+
```
72+
73+
### Spread properties
74+
75+
Similarly to arrays, the spread operator can also be used to copy properties from one object to another.
76+
77+
```javascript
78+
let address = {
79+
postalCode: '11011',
80+
city: 'Berlin',
81+
};
82+
address = { ...address, country: 'Germany' };
83+
// => {
84+
// postalCode: '11011',
85+
// city: 'Berlin',
86+
// country: 'Germany',
87+
// }
88+
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"root": true,
3+
"extends": "@exercism/eslint-config-javascript",
4+
"env": {
5+
"jest": true
6+
},
7+
"overrides": [
8+
{
9+
"files": [".meta/proof.ci.js", ".meta/exemplar.js", "*.spec.js"],
10+
"excludedFiles": ["custom.spec.js"],
11+
"extends": "@exercism/eslint-config-javascript/maintainers"
12+
}
13+
]
14+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
yarn-error.log
3+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"authors": [
3+
"meatball"
4+
],
5+
"files": {
6+
"solution": [
7+
"train-driver.js"
8+
],
9+
"test": [
10+
"train-driver.spec.js"
11+
],
12+
"exemplar": [
13+
".meta/exemplar.js"
14+
]
15+
},
16+
"blurb": "Professionalize using rest and spread operators.",
17+
"custom": {
18+
"version.tests.compatibility": "jest-27",
19+
"flag.tests.task-per-describe": true,
20+
"flag.tests.may-run-long": false,
21+
"flag.tests.includes-optional": false
22+
}
23+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Design
2+
3+
## Learning objectives
4+
5+
- What does a for loop do
6+
- Syntax `for(...){...}`
7+
- What are the different parts of the for loop header
8+
- How to iterate over an array with a for loop
9+
- What is the increment/decrement operator `i++`/`i--`
10+
11+
## Out of Scope
12+
13+
The following topics are out of scope because they are covered by another concept exercise.
14+
15+
- Other loops like `while`
16+
- Other possibilities of iterating over an array
17+
- `break` and `continue` are only mentioned in the about.md file here because they will be more in focus in the `while` exercise
18+
19+
## Concepts
20+
21+
The Concepts this exercise unlocks are:
22+
23+
- `for-loops`
24+
- `increment-decrement`
25+
26+
## Prerequisites
27+
28+
- `arrays` because they are used to iterate over them in the exercise
29+
- `comparison` for writing the condition in the loop header
30+
- `conditionals` because they introduced the student to the concept of conditional execution
31+
32+
## Analyzer
33+
34+
This exercise could benefit from the following rules in the [analyzer][analyzer]:
35+
36+
For all tasks check that the student actually used a for loop.
37+
38+
1. `totalBirdCount`
39+
40+
- Verify that the condition is written with `< x.length` instead of `<= y.length -1`.
41+
- Check whether a shorthand assignment `+=` was used to increase the sum (non-essential feedback).
42+
- Verify the total was properly initialized with `0` instead of e.g. `null`
43+
- Verify the increment operator was used in loop header step
44+
45+
2. `birdsInWeek`
46+
47+
- Verify a helper variable was used instead of duplicating the calculation in the initialization and condition of the loop
48+
- Other checks should be the same as for `totalBirdCount`
49+
50+
3. `fixBirdCountLog`
51+
52+
- Check whether a shorthand assignment `+=` was used to increase the loop counter (non-essential feedback)
53+
- Check whether the increment operator was used in the loop body
54+
55+
## Notes
56+
57+
The exercise is inspired by [Bird Watcher Exercise in the C# track][csharp-bird-watcher] but the original exercise included more concepts and subsequently also tasks that cover all of these concepts.
58+
Since the exercise for the JavaScript track should be focussed on the for loop, the tasks where reduced and changed accordingly.
59+
60+
[analyzer]: https://github.com/exercism/javascript-analyzer
61+
[csharp-bird-watcher]: https://github.com/exercism/csharp/blob/main/exercises/concept/bird-watcher/.docs/instructions.md
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// @ts-check
2+
//
3+
// The line above enables type checking for this file. Various IDEs interpret
4+
// the @ts-check directive. It will give you helpful autocompletion when
5+
// implementing this exercise.
6+
7+
/**
8+
* Return each Wagons Wiegth.
9+
*
10+
* @param {number[]} eachWagonsWiegth
11+
* @returns {number[]} each Wagons Wiegth
12+
*/
13+
export function getListOfWagons(...eachWagonsWiegth) {
14+
return eachWagonsWiegth;
15+
}
16+
17+
/**
18+
* Reorder the array of wagons by moving the first 2 wagons to the end of the array.
19+
*
20+
* @param {number[]} eachWagonsWieght
21+
* @returns {number[]} reorderd list of wagons
22+
*/
23+
export function fixListOfWagons(eachWagonsWieght) {
24+
const [first, second, ...rest] = eachWagonsWieght;
25+
return [...rest, first, second];
26+
}
27+
28+
/**
29+
* Fixes the list of wagons by inserting an array of wagons after the first element in eachWagonsWieght.
30+
*
31+
* @param {number[]} eachWagonsWieght
32+
* @param {number[]} missingWagons
33+
* @returns {number[]} corrected list of wagons
34+
*/
35+
export function correctListOfWagons(eachWagonsWieght, missingWagons) {
36+
const [first, ...rest] = eachWagonsWieght;
37+
return [first, ...missingWagons, ...rest];
38+
}
39+
40+
/**
41+
* Updates the route information by adding more routing information
42+
*
43+
* @param {Record<string, string>} route
44+
* @param {Record<string, string>} moreRouteInformation
45+
* @returns {Record<string, string>} updates route information
46+
*/
47+
export function updateRoutingInformation(route, moreRouteInformation) {
48+
return { ...route, ...moreRouteInformation };
49+
}
50+
51+
/**
52+
* Remove arrival time from the route information object
53+
*
54+
* @param {Record<string, string>} route
55+
* @returns {Record<string, string>} object without arrival time
56+
*/
57+
export function removeTimeOfArrival(route) {
58+
const { arrivalTime, ...rest } = route;
59+
return rest;
60+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
audit=false

0 commit comments

Comments
 (0)