-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnonMutatingPush.js
More file actions
29 lines (21 loc) · 1.07 KB
/
Copy pathnonMutatingPush.js
File metadata and controls
29 lines (21 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// *** Functional Programming: Add Elements to the End of an Array Using concat Instead of push ***
// 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:
// var arr = [1, 2, 3];
// arr.push([4, 5, 6]);
// arr is changed to [1, 2, 3, [4, 5, 6]]
// Not the functional programming way
// Concat offers a way to add new items to the end of an array without any mutating side effects.
// 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.
function nonMutatingPush(original, newItem) {
// Add your code below this line
return original.concat(newItem);
// Add your code above this line
}
var first = [1, 2, 3];
var second = [4, 5];
nonMutatingPush(first, second);