* fix: move "Explore Differences Between..." to basic JS, update seed and tests * fix: resequence "Declare String Variables" * fix: move "Declare a Read-Only Variable..." to basic JS, update seed and tests * fix: revert changes to non-English "Explore Differences Between..." test text * fix: revert test strings, solutions, and seeds for non-English challenges * fix: update "Declare String Variables" description * fix: sync quotation marks in description and seed * fix: modify note in "Declare a Read-Only..." challenge * fix: update operator and compound assignment challenges * fix: update string challenges * fix: update array and array method challenges * fix: update function and scope challenges, resequence slightly * fix: "Word Blanks" solution * fix: add spacing to seed * fix: concatenating += challenge spacing * fix: appending variables to strings spacing * fix: find the length of a string spacing * fix: removed instances of removedFromMyArray = 0 * fix: switch challenges * fix: function argument and param spacing * fix: update counting cards, object challenges, and record collection * fix: finish rest of Basic JS section * fix: introducing else statements solution * fix: update spacing and wording * fix: update wording for const challenge * fix: update functional programming challenges * fix: intermediate algorithms and cert challenges * fix: revert some spacing and remove comments for fp challenge solutions * feat: add notes with links to moved let and const challenges in first two es6 challenges * fix: update es6 intro text * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/concatenating-strings-with-the-plus-equals-operator.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/finding-a-remainder-in-javascript.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/global-scope-and-functions.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-through-an-array-with-a-for-loop.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-through-an-array-with-a-for-loop.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/implement-map-on-a-prototype.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/declare-a-read-only-variable-with-the-const-keyword.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * fix: concatenating strings with plus operator seed * fix: add comments back to Declare a Read-Only Variable... seed * feat: add es6 to basic javascript redirect tests for let and const challenges * fix: revert "Concatenating Strings with Plus Operator" seed * fix: move test file to cypress/integration/learn/redirects, separate redirect tests Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>
2.0 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
587d7da9367417b2b2512b67 | Add Elements to the End of an Array Using concat Instead of push | 1 | 301226 | add-elements-to-the-end-of-an-array-using-concat-instead-of-push |
--description--
Functional programming is all about creating and using non-mutating functions.
The last challenge introduced the concat
method as a way to combine arrays into a new one without mutating the original arrays. Compare concat
to the push
method. push
adds an item to the end of the same array it is called on, which mutates that array. Here's an example:
const arr = [1, 2, 3];
arr.push([4, 5, 6]);
arr
would have a modified value of [1, 2, 3, [4, 5, 6]]
, which is not the functional programming way.
concat
offers a way to add new items to the end of an array without any mutating side effects.
--instructions--
Change the nonMutatingPush
function so it uses concat
to add newItem
to the end of original
instead of push
. The function should return an array.
--hints--
Your code should use the concat
method.
assert(code.match(/\.concat/g));
Your code should not use the push
method.
assert(!code.match(/\.?[\s\S]*?push/g));
The first
array should not change.
assert(JSON.stringify(first) === JSON.stringify([1, 2, 3]));
The second
array should not change.
assert(JSON.stringify(second) === JSON.stringify([4, 5]));
nonMutatingPush([1, 2, 3], [4, 5])
should return [1, 2, 3, 4, 5]
.
assert(
JSON.stringify(nonMutatingPush([1, 2, 3], [4, 5])) ===
JSON.stringify([1, 2, 3, 4, 5])
);
--seed--
--seed-contents--
function nonMutatingPush(original, newItem) {
// Only change code below this line
return original.push(newItem);
// Only change code above this line
}
const first = [1, 2, 3];
const second = [4, 5];
nonMutatingPush(first, second);
--solutions--
function nonMutatingPush(original, newItem) {
return original.concat(newItem);
}
const first = [1, 2, 3];
const second = [4, 5];