DEV Community

Viren B
Viren B

Posted on • Originally published at virenb.cc

Solving "Slice and Splice" / freeCodeCamp Algorithm Challenges

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

'Slice and Splice' Challenge

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

Starter Code

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

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

MDN Documentation: 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]

[SPOILER: SOLUTION TO CODE BELOW]

function frankenSplice(arr1, arr2, n) {
  let newArr2 = [...arr2];
  newArr2.splice(n, 0, ...arr1);
  return newArr2;
}

Links & Resources

'Slice and Splice' Challenge on fCC

freeCodeCamp

Donate to FCC!

Solution on my GitHub

Thank you for reading!

Top comments (0)