-
-
Notifications
You must be signed in to change notification settings - Fork 636
Added filles for the new rest and spread exericse #1989
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
Open
meatball133
wants to merge
25
commits into
exercism:main
Choose a base branch
from
meatball133:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
f98dbb8
Added filles
meatball133 77ca83d
fixes
meatball133 92ecfba
fixes
meatball133 d25d85e
further fixes and updated last exercise
meatball133 fb96f63
Replace whole numbers
meatball133 439ddd2
Update instructions.md
meatball133 8e6b360
Various fixes and updated test file
meatball133 1a0a3fc
fix smal typo
meatball133 71a9dac
fix small typo
meatball133 a327c00
Small typo fix
meatball133 e1af39e
Updated the story and added contributors
meatball133 d5ae91d
fix typo
meatball133 6d38528
fix
meatball133 94163e1
Update instructions.md
meatball133 4e1b623
Further fixes
meatball133 b86a994
fix bug created
meatball133 d73f782
Fixes and changes to instructions
meatball133 166a823
Runned prettier and aded hints
meatball133 f826acd
Merge branch 'exercism:main' into main
meatball133 47f0ffe
Runed babel
meatball133 96c8bcb
runed babel again
meatball133 a5fef2f
Runned prettier again
meatball133 c45f0ff
Add links
meatball133 9f35761
Merge branch 'exercism:main' into main
meatball133 c48bd27
Sync and format
meatball133 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
``` | ||
|
||
## 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
// } | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
yarn-error.log | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
audit=false |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.