Solving "Slice and Splice" / freeCodeCamp Algorithm Challenges 
Tags: freecodecamp, algorithms, challenge, javascript
Published: 2020-06-16 14:42:59

---
title: Solving "Slice and Splice" / freeCodeCamp Algorithm Challenges 
published: true
description: My guide, notes, and solution to freeCodeCamp's basic algorithm challenge, "Slice and Splice" 
tags: #freeCodeCamp, #algorithms, #challenge, #javascript
canonical_url: https://virenb.cc/fcc-012-slice-and-splice
series: Solving freeCodeCamp's Algorithm Challenges
---

Post can also be found on my website [https://virenb.cc/fcc-012-slice-and-splice](https://virenb.cc/fcc-012-slice-and-splice "Post on virenb.cc")

!['Slice and Splice' Challenge](//images.ctfassets.net/yzwpq1epaq68/4nLb8yLopcfQYyR5FY6Ick/ade9a96e87694c4ec26ae2259e5a0243/ss-012.png)

Let us solve freeCodeCamp's Basic Algorithm Challenge, "Slice and Splice"


### Starter Code

```javascript
function frankenSplice(arr1, arr2, n) {
  return arr2;
}

frankenSplice([1, 2, 3], [4, 5, 6], 1);
```

### Tests

```
frankenSplice([1, 2, 3], [4, 5], 1) should return [4, 1, 2, 3, 5].
frankenSplice([1, 2], ["a", "b"], 1) should return ["a", 1, 2, "b"].
frankenSplice(["claw", "tentacle"], ["head", "shoulders", "knees", "toes"], 2) should return ["head", "shoulders", "claw", "tentacle", "knees", "toes"].
All elements from the first array should be added to the second array in their original order.
The first array should remain the same after the function runs.
The second array should remain the same after the function runs.
```

### Instructions


You are given two arrays and an index.

Use the array methods `slice` and `splice` to copy each element of the first array into the second array, in order.

Begin inserting elements at index `n` of the second array.

Return the resulting array. The input arrays should remain the same after the function runs.

---

# Read

* Our input is two arrays and an integer, an index.
* We will use `slice` and `splice` methods to copy first array into the second array, beginning the insertion at index `n`.
* Do not alter the given arrays (`arr1`, `arr2`).

# Thoughts

We have two arrays, we have to combine the values into one array without altering the given arrays `arr1`, `arr2`. We will have to make a new array.

Since we are instructed to copy each element of the first array into the second array, we can make a copy of the second array instead of starting out with a new empty array.

We can use the spread operator for this, example below:

```
arr2 = [4, 5];
let newArr2 = [...arr2];
console.log(newArr2)
Array [ 4, 5 ]
```

[MDN Documentation: Spread syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax "MDN Docs: Spread Syntax")

Next, we will look at how to slice or splice the arrays.

[MDN Documentation: splice()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice "MDN Docs: splice()")

The syntax for usage of `splice()` is as follows, with everything but _start_ being optional:

`let arrDeletedItems = array.splice(start[, deleteCount[, item1[, item2[, ...]]]])`

We will now focus on the third argument which was given to us, `n`, which is the index to start inserting into.

The below example, `n` is 1, so we want to start inserting at index one. We do not want to delete anything, so we put a 0 for the `deleteCount` in `splice`. The last optional argument for `splice()` is what we want to insert. We want to insert the values from `arr1`.

We can again resort back to using the spread operator, `...arr1`.

Lastly, we must ensure we return our new array.

```
frankenSplice(arr1, arr2, n) {
	make copy of arr2 (calling it newArr2)
	newArr2.splice(n, 0, copy of arr1)
	return newArr2
}

```

# Solving [SPOILER: CONTAINS SOLUTION]


<details>
  <summary>**[SPOILER: SOLUTION TO CODE BELOW]**</summary>
 	
```javascript
function frankenSplice(arr1, arr2, n) {
  let newArr2 = [...arr2];
  newArr2.splice(n, 0, ...arr1);
  return newArr2;
}
```
 
</details>

---

### Links & Resources

['Slice and Splice' Challenge on fCC](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/slice-and-splice "Basic Algorithm Scripting: Slice and Splice Challenge on fCC Website")

[freeCodeCamp](https://www.freecodecamp.org/, "freeCodeCamp")

[Donate to FCC!](https://www.freecodecamp.org/donate "fCC Donation Page")

[Solution on my GitHub](https://github.com/virenb/fcc-algorithms/blob/master/012---slice-and-splice.js "GitHub Solution")

Thank you for reading!