Skip to content

Commit 93e8d09

Browse files
committed
slice and splice 🔪
1 parent 1572ec6 commit 93e8d09

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Diff for: README.md

+27
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ All of these examples are from FreeCodeCamp's [course](https://www.freecodecamp.
2222
- [9. Finders Keepers](#9-finders-keepers)
2323
- [10. Boo who](#10-boo-who)
2424
- [11. Title Case a Sentence](#11-title-case-a-sentence)
25+
- [12. Slice and Splice](#12-slice-and-splice)
2526

2627
## Basic Algorithm Scripting
2728

@@ -385,3 +386,29 @@ function titleCase(str) {
385386

386387
titleCase("I'm a little tea pot");
387388
```
389+
390+
### 12. Slice and Splice
391+
392+
_Difficulty: Beginner_
393+
394+
You are given two arrays and an index.
395+
396+
Copy each element of the first array into the second array, in order.
397+
398+
Begin inserting elements at index n of the second array.
399+
400+
Return the resulting array. The input arrays should remain the same after the function runs.
401+
402+
---
403+
404+
#### Solution
405+
406+
```js
407+
function frankenSplice(arr1, arr2, n) {
408+
let localArr = [...arr2];
409+
localArr.splice(n, 0, ...arr1);
410+
return localArr;
411+
}
412+
413+
frankenSplice([1, 2, 3], [4, 5], 1);
414+
```

0 commit comments

Comments
 (0)