Skip to content
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

Added filles for the new rest and spread exericse #1989

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
25 changes: 25 additions & 0 deletions exercises/concept/train-driver/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Hints

## 1. Determine the total number of birds that you counted so far

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

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

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

## 3. Fix a counting mistake

- Again, you need to set up a for loop to iterate over the whole bird count array.
- This time you only need to visit every second entry in the array.
- Change the step so the counter variable is increased accordingly after each iteration.
- 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.

[concept-arrays]: /tracks/javascript/concepts/arrays
111 changes: 111 additions & 0 deletions exercises/concept/train-driver/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# Instructions

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.


```exercism/note
To practice, use a for the rest or spread to solve each of the tasks below.
meatball133 marked this conversation as resolved.
Show resolved Hide resolved
```

## 1. Convert the data to an array
meatball133 marked this conversation as resolved.
Show resolved Hide resolved

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.

SleeplessByte marked this conversation as resolved.
Show resolved Hide resolved
```exercism/note
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note blocks should not be used for the normal instructions of the exercise, only for special things that need to stand out (like the note at the top about using rest and spread).

Applies to all the tasks.


Implement a function `getListOfWagons` that accepts an unknown amount of whole numbers that contains the weight of each wagon.
SleeplessByte marked this conversation as resolved.
Show resolved Hide resolved
It should return an array of all the wagon weights.

```

```javascript
getListOfWagons(5, 7, 12, 3, 14, 8, 3);
// => [5, 7, 12, 3, 14, 8, 3]
```

## 2. Move the first two elements to the end of the array

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.

```exercism/note

Implement a function `fixListOfWagons` that accepts an array of the weight of each wagon.
It returns an array where the 2 first elements are moved to the end of the array.
SleeplessByte marked this conversation as resolved.
Show resolved Hide resolved

```

```javascript
eachWagonsWieght = [2, 5, 0, 7, 4, 0, 1, 3, 1];
fixListOfWagons(eachWagonsWieght);
// => [0, 7, 4, 0, 1, 3, 1, 2, 5]
```

## 3. Add missing values

Your friend realized that all data wasn't added and found another array which contains the missing values.
Your friend would like you to add the missing values to the array.
All they can remember is that the missing values should be placed after the first element in the array.


```exercism/note

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.
The second array should be added after the first element of the first array.

```


```javascript
eachWagonsWieght = [2, 5, 0, 7, 4, 1];
missingWagons = [3, 0, 6, 1];
CorrectListOfWagons(eachWagonsWieght, missingWagons);
// => [2, 3, 0, 6, 1, 5, 0, 7, 4, 1]
```

## 4. Update routing information

Now that the wagon data is correct, your friend would like you to update the routing information.
Your friend has an object with the routing information and would like you to add more routing information to the object.
Every route requires a bit different information so your friend would prefer a generic solution.


```exercism/note

Implement a function `updateRoutingInformation` that accepts two objects.
The first object contains which city the train should go between and the second object contains more routing information.
The function should return an object with the updated routing information.

```

```javascript
route = {from: "Berlin", to: "Hamburg"};
moreRouteInformation = {length: 100, speed: 50};
updateRoutingInformation(route, moreRouteInformation);
meatball133 marked this conversation as resolved.
Show resolved Hide resolved
// => {from: "Berlin", to: "Hamburg", length: 100, speed: 50}
```

## 5. Remove arrival time from routing information

Your friend has noticed that they don't need the arrival time in the routing information.
Therefore your friend would like you to remove the arrival time from the routing information.

```exercism/note

Implement a function `removeArrivalTime` that accepts an object with the routing information.
The function should return an object

```

```javascript
routeInformation= {
from: "Berlin",
to: "Hamburg",
length: 100,
timeOfArrival: "10:10"
};
removeTimeOfArrival(routeInformation);
// => {from: "Berlin", to: "Hamburg", length: 100}
```
SleeplessByte marked this conversation as resolved.
Show resolved Hide resolved
88 changes: 88 additions & 0 deletions exercises/concept/train-driver/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Introduction

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_.

## Rest operator

### Rest elements

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.

```javascript
const [a, b, ...everythingElse] = [0, 1, 1, 2, 3, 5, 8];
a;
// => 0
b;
// => 1
everythingElse;
// => [1, 2, 3, 5, 8]
```

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`:

```javascript
const [...items, last] = [2, 4, 8, 16]
```

### Rest properties

Similarly to arrays, the rest operator can also be used to collect one or more object properties and store them in a single object.

```javascript
const { street, ...address } = {
street: 'Platz der Republik 1',
postalCode: '11011',
city: 'Berlin',
};
street;
// => 'Platz der Republik 1'
address;
// => {postalCode: '11011', city: 'Berlin'}
```

## Rest parameters

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.

```javascript
function concat(...strings) {
return strings.join(' ');
}
concat('one');
// => 'one'
concat('one', 'two', 'three');
// => 'one two three'
```

## Spread

### Spread elements

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.

```javascript
const oneToFive = [1, 2, 3, 4, 5];
const oneToTen = [...oneToFive, 6, 7, 8, 9, 10];
oneToTen;
// => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const woow = ['A', ...oneToFive, 'B', 'C', 'D', 'E', ...oneToFive, 42];
woow;
// => ["A", 1, 2, 3, 4, 5, "B", "C", "D", "E", 1, 2, 3, 4, 5, 42]
```

### Spread properties

Similarly to arrays, the spread operator can also be used to copy properties from one object to another.

```javascript
let address = {
postalCode: '11011',
city: 'Berlin',
};
address = { ...address, country: 'Germany' };
// => {
// postalCode: '11011',
// city: 'Berlin',
// country: 'Germany',
// }
```
14 changes: 14 additions & 0 deletions exercises/concept/train-driver/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"root": true,
"extends": "@exercism/eslint-config-javascript",
"env": {
"jest": true
},
"overrides": [
{
"files": [".meta/proof.ci.js", ".meta/exemplar.js", "*.spec.js"],
"excludedFiles": ["custom.spec.js"],
"extends": "@exercism/eslint-config-javascript/maintainers"
}
]
}
3 changes: 3 additions & 0 deletions exercises/concept/train-driver/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
yarn-error.log

23 changes: 23 additions & 0 deletions exercises/concept/train-driver/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"authors": [
"meatball"
],
"files": {
"solution": [
"train-driver.js"
],
"test": [
"train-driver.spec.js"
],
"exemplar": [
".meta/exemplar.js"
]
},
"blurb": "Professionalize using rest and spread operators.",
"custom": {
"version.tests.compatibility": "jest-27",
"flag.tests.task-per-describe": true,
"flag.tests.may-run-long": false,
"flag.tests.includes-optional": false
}
}
61 changes: 61 additions & 0 deletions exercises/concept/train-driver/.meta/design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Design

## Learning objectives

- What does a for loop do
- Syntax `for(...){...}`
- What are the different parts of the for loop header
- How to iterate over an array with a for loop
- What is the increment/decrement operator `i++`/`i--`

## Out of Scope

The following topics are out of scope because they are covered by another concept exercise.

- Other loops like `while`
- Other possibilities of iterating over an array
- `break` and `continue` are only mentioned in the about.md file here because they will be more in focus in the `while` exercise

## Concepts

The Concepts this exercise unlocks are:

- `for-loops`
- `increment-decrement`

## Prerequisites

- `arrays` because they are used to iterate over them in the exercise
- `comparison` for writing the condition in the loop header
- `conditionals` because they introduced the student to the concept of conditional execution

## Analyzer

This exercise could benefit from the following rules in the [analyzer][analyzer]:

For all tasks check that the student actually used a for loop.

1. `totalBirdCount`

- Verify that the condition is written with `< x.length` instead of `<= y.length -1`.
- Check whether a shorthand assignment `+=` was used to increase the sum (non-essential feedback).
- Verify the total was properly initialized with `0` instead of e.g. `null`
- Verify the increment operator was used in loop header step

2. `birdsInWeek`

- Verify a helper variable was used instead of duplicating the calculation in the initialization and condition of the loop
- Other checks should be the same as for `totalBirdCount`

3. `fixBirdCountLog`

- Check whether a shorthand assignment `+=` was used to increase the loop counter (non-essential feedback)
- Check whether the increment operator was used in the loop body

## Notes

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.
Since the exercise for the JavaScript track should be focussed on the for loop, the tasks where reduced and changed accordingly.

[analyzer]: https://github.com/exercism/javascript-analyzer
[csharp-bird-watcher]: https://github.com/exercism/csharp/blob/main/exercises/concept/bird-watcher/.docs/instructions.md
60 changes: 60 additions & 0 deletions exercises/concept/train-driver/.meta/exemplar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// @ts-check
//
// The line above enables type checking for this file. Various IDEs interpret
// the @ts-check directive. It will give you helpful autocompletion when
// implementing this exercise.

/**
* Return each Wagons Wiegth.
*
* @param {number[]} eachWagonsWiegth
* @returns {number[]} each Wagons Wiegth
*/
export function getListOfWagons(...eachWagonsWiegth) {
return eachWagonsWiegth;
}

/**
* Reorder the array of wagons by moving the first 2 wagons to the end of the array.
*
* @param {number[]} eachWagonsWieght
* @returns {number[]} reorderd list of wagons
*/
export function fixListOfWagons(eachWagonsWieght) {
const [first, second, ...rest] = eachWagonsWieght;
return [...rest, first, second];
}

/**
* Fixes the list of wagons by inserting an array of wagons after the first element in eachWagonsWieght.
*
* @param {number[]} eachWagonsWieght
* @param {number[]} missingWagons
* @returns {number[]} corrected list of wagons
*/
export function correctListOfWagons(eachWagonsWieght, missingWagons) {
const [first, ...rest] = eachWagonsWieght;
return [first, ...missingWagons, ...rest];
}

/**
* Updates the route information by adding more routing information
*
* @param {Record<string, string>} route
* @param {Record<string, string>} moreRouteInformation
* @returns {Record<string, string>} updates route information
*/
export function updateRoutingInformation(route, moreRouteInformation) {
return { ...route, ...moreRouteInformation };
}

/**
* Remove arrival time from the route information object
*
* @param {Record<string, string>} route
* @returns {Record<string, string>} object without arrival time
*/
export function removeTimeOfArrival(route) {
const { arrivalTime, ...rest } = route;
return rest;
}
1 change: 1 addition & 0 deletions exercises/concept/train-driver/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
audit=false
Loading