-
-
Notifications
You must be signed in to change notification settings - Fork 632
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
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 22 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 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
This file contains 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,28 @@ | ||||||||
# Hints | ||||||||
|
||||||||
## General | ||||||||
|
||||||||
- To extract multiple arguments in the function parameters so can you pack them with the `...<args>`. | ||||||||
- To use rest and spread use the `...` operator. | ||||||||
|
||||||||
## 1. Create a list of all wagons | ||||||||
|
||||||||
- Multiple arguments in the function parameters can be packed with the `...<args>` operator. | ||||||||
|
||||||||
## 2. Move the first two elements to the end of the array | ||||||||
|
||||||||
- Using unpacking with the rest operator(`...`), lets you extract the first two elements of a `array` while keeping the rest intact. | ||||||||
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.
Suggested change
|
||||||||
- To add another `array` into an existing `array`, you can use the `...` operator to "spread" the `array`. | ||||||||
|
||||||||
## 3. Add missing values | ||||||||
|
||||||||
- Using unpacking with the rest operator(`...`), lets you extract the first two elements of a `array` while keeping the rest intact. | ||||||||
- To add another `array` into an existing `array`, you can use the `...` operator to "spread" the `array`. | ||||||||
|
||||||||
## 4. Extend routing information | ||||||||
|
||||||||
- To add another `object` into an existing `object`, you can use the `...` operator to "spread" the `object`. | ||||||||
|
||||||||
## 5. Separate arrival time from routing information | ||||||||
|
||||||||
- To extract a value from an `object` while keeping the rest intact, you can use the rest operator(`...`). |
This file contains 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,96 @@ | ||
# 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. | ||
|
||
```exercism/note | ||
To practice, use the rest or spread operator to solve each of the tasks below. | ||
``` | ||
|
||
## 1. Create a list of all wagons | ||
|
||
SleeplessByte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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. | ||
The function should then return the given IDs as a single `array`. | ||
|
||
```javascript | ||
getListOfWagons(1, 7, 12, 3, 14, 8, 5); | ||
// => [1, 7, 12, 3, 14, 8, 3] | ||
``` | ||
|
||
## 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**. | ||
|
||
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. | ||
|
||
```javascript | ||
eachWagonsID = [2, 5, 1, 7, 4, 12, 6, 3, 13]; | ||
fixListOfWagons(eachWagonsID); | ||
// => [1, 7, 4, 12, 6, 3, 13, 2, 5] | ||
``` | ||
|
||
## 3. Add missing values | ||
|
||
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. | ||
The wagon IDs of the second `array` should be added into the first `array` directly after the locomotive (ID 1). | ||
|
||
```javascript | ||
eachWagonsID = [1, 5, 20, 7, 4, 8]; | ||
missingWagons = [3, 17, 6, 15]; | ||
correctListOfWagons(eachWagonsID, missingWagons); | ||
// => [1, 3, 17, 6, 15, 5, 20, 7, 4, 8] | ||
``` | ||
|
||
## 4. Extend routing information | ||
|
||
Now that all the wagon data is correct, your friend would like you to update the systems routing information. | ||
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`. | ||
The first `object` contains which cities the train route moves between. | ||
|
||
The second `object` contains other routing details such as train speed or length. | ||
The function should return a consolidated `object` with all routing information. | ||
|
||
```exercism/note | ||
The variable `moreRouteInformation` can contain different properties. | ||
``` | ||
|
||
```javascript | ||
route = { from: 'Berlin', to: 'Hamburg' }; | ||
moreRouteInformation = { length: '100', speed: '50' }; | ||
extendRouteInformation(route, moreRouteInformation); | ||
// => {from: "Berlin", to: "Hamburg", length: "100", speed: "50"} | ||
``` | ||
|
||
## 5. Separate 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 separate the arrival time from the routing information. | ||
|
||
Implement a function `separateArrivalTime` 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 | ||
routeInformation = { | ||
from: 'Berlin', | ||
to: 'Hamburg', | ||
length: '100', | ||
timeOfArrival: '10:10', | ||
}; | ||
separateTimeOfArrival(routeInformation); | ||
// => ["10:10", {from: "Berlin", to: "Hamburg", length: "100"}] | ||
``` | ||
SleeplessByte marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains 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 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 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 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,27 @@ | ||
{ | ||
"authors": [ | ||
"meatball133" | ||
], | ||
"contributors": [ | ||
"bethanyg", | ||
"junedev" | ||
], | ||
"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 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,24 @@ | ||
# Design | ||
|
||
## Learning objectives | ||
|
||
- Using spread to turn an array into a list of parameters | ||
- Using rest elements to turn a list of parameters into an array | ||
- Using spread to turn an extract value out of an object | ||
- Using spread to combine objects | ||
- Using rest to collect multiple parameters into an array | ||
|
||
## Out of scope | ||
|
||
- Default values | ||
|
||
## Concepts | ||
|
||
- `rest-and-spread` | ||
|
||
## Prerequisites | ||
|
||
- `arrays` are needed to understand array restructuring | ||
- `functions` are needed as basis for rest parameters | ||
- `objects` are needed for object spread etc. | ||
- `array-destructuring` are needed to understand rest elements |
This file contains 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 id in form of an array. | ||
* | ||
* @param {number[]} eachWagonsID | ||
* @returns {number[]} each Wagons Wiegth | ||
*/ | ||
export function getListOfWagons(...eachWagonsID) { | ||
return eachWagonsID; | ||
} | ||
|
||
/** | ||
* 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 | ||
*/ | ||
export function fixListOfWagons(eachWagonsID) { | ||
const [first, second, ...rest] = eachWagonsID; | ||
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[]} missingWagons | ||
* @returns {number[]} corrected list of wagons | ||
*/ | ||
export function correctListOfWagons(eachWagonsID, missingWagons) { | ||
const [first, ...rest] = eachWagonsID; | ||
return [first, ...missingWagons, ...rest]; | ||
} | ||
|
||
/** | ||
* Extend route information by adding another object | ||
* | ||
* @param {Record<string, string>} route | ||
* @param {Record<string, string>} moreRouteInformation | ||
* @returns {Record<string, string>} extended route information | ||
*/ | ||
export function extendRouteInformation(route, moreRouteInformation) { | ||
return { ...route, ...moreRouteInformation }; | ||
} | ||
|
||
/** | ||
* Separate arrival time from the route information object | ||
* | ||
* @param {Record<string, string>} route | ||
* @returns {[string, Record<string, string>]} array with arrival time and object without arrival time | ||
*/ | ||
export function separateTimeOfArrival(route) { | ||
const { timeOfArrival, ...rest } = route; | ||
return [timeOfArrival, rest]; | ||
} |
This file contains 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 |
This file contains 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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 Exercism | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains 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,4 @@ | ||
module.exports = { | ||
presets: ['@exercism/babel-preset-javascript'], | ||
plugins: [], | ||
}; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.