* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
79 lines
1.8 KiB
Markdown
79 lines
1.8 KiB
Markdown
---
|
|
id: 587d7da9367417b2b2512b66
|
|
title: Combine Two Arrays Using the concat Method
|
|
challengeType: 1
|
|
forumTopicId: 301229
|
|
dashedName: combine-two-arrays-using-the-concat-method
|
|
---
|
|
|
|
# --description--
|
|
|
|
<dfn>Concatenation</dfn> means to join items end to end. JavaScript offers the `concat` method for both strings and arrays that work in the same way. For arrays, the method is called on one, then another array is provided as the argument to `concat`, which is added to the end of the first array. It returns a new array and does not mutate either of the original arrays. Here's an example:
|
|
|
|
```js
|
|
[1, 2, 3].concat([4, 5, 6]);
|
|
// Returns a new array [1, 2, 3, 4, 5, 6]
|
|
```
|
|
|
|
# --instructions--
|
|
|
|
Use the `concat` method in the `nonMutatingConcat` function to concatenate `attach` to the end of `original`. The function should return the concatenated array.
|
|
|
|
# --hints--
|
|
|
|
Your code should use the `concat` method.
|
|
|
|
```js
|
|
assert(code.match(/\.concat/g));
|
|
```
|
|
|
|
The `first` array should not change.
|
|
|
|
```js
|
|
assert(JSON.stringify(first) === JSON.stringify([1, 2, 3]));
|
|
```
|
|
|
|
The `second` array should not change.
|
|
|
|
```js
|
|
assert(JSON.stringify(second) === JSON.stringify([4, 5]));
|
|
```
|
|
|
|
`nonMutatingConcat([1, 2, 3], [4, 5])` should return `[1, 2, 3, 4, 5]`.
|
|
|
|
```js
|
|
assert(
|
|
JSON.stringify(nonMutatingConcat([1, 2, 3], [4, 5])) ===
|
|
JSON.stringify([1, 2, 3, 4, 5])
|
|
);
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```js
|
|
function nonMutatingConcat(original, attach) {
|
|
// Only change code below this line
|
|
|
|
|
|
// Only change code above this line
|
|
}
|
|
var first = [1, 2, 3];
|
|
var second = [4, 5];
|
|
nonMutatingConcat(first, second);
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
function nonMutatingConcat(original, attach) {
|
|
// Only change code below this line
|
|
return original.concat(attach);
|
|
// Only change code above this line
|
|
}
|
|
var first = [1, 2, 3];
|
|
var second = [4, 5];
|
|
nonMutatingConcat(first, second);
|
|
```
|