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

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

### Starter Code

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.

You are watching: Basic algorithm scripting: slice and splice

### 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.

ReadOur 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). ThoughtsWe 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:

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:

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.

See more: What Does It Mean When A Bearded Dragon Opens Its Mouth S, Bearded Dragon Opening Mouth

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

function frankenSplice(arr1, arr2, n) let newArr2 = <...arr2>; newArr2.splice(n, 0, ...arr1); return newArr2;

### Links & Resources

"Slice and Splice" Challenge on fCCfreeCodeCamp

Donate to FCC!

Solution on my GitHub

Thank you for reading!

1 FCC Algorithm Challenges / Convert Celsius to Fahrenheit 2 FCC Algorithm Challenges / Reverse a Strinns ... 33 more parts... 3 FCC Algorithm Challenges / Factorialize a Number 4 FCC Algorithm Challenges / Find the Longest Word in a Strinns 5 FCC Algorithm Challenges / Return Largest Numbers in Arrays 6 Solving "Confirm the Ending" / FreeCodeCamp Algorithm Challengens 7 Solving "Repeat a String Repeat a String" / FreeCodeCamp Algorithm Challenges 8 Solving "Truncate a String" / freeCodeCamp Algorithm Challengens 9 Solving "Finders Keepers" / freeCodeCamp Algorithm Challenges 10 Solving "Boo who" / freeCodeCamp Algorithm Challenges 11 Solving "Title Case a Sentence" / freeCodeCamp Algorithm Challengens 12 Solving "Slice and Splice" / freeCodeCamp Algorithm Challenges 13 Solving "Falsy Bouncer" / freeCodeCamp Algorithm Challengens 14 Solving "Where do I Belong" / freeCodeCamp Algorithm Challengens 15 Solving "Mutations" / freeCodeCamp Algorithm Challengens 16 Solving "Chunky Monkey" / freeCodeCamp Algorithm Challenges 17 Solving "Sum All Numbers in a Range" / freeCodeCamp Algorithm Challenges 18 Solving "Diff Two Arrays" / freeCodeCamp Algorithm Challenges 19 Solving "Seek and Destroy" / freeCodeCamp Algorithm Challenges 20 Solving "Wherefore art thou" / freeCodeCamp Algorithm Challenges 21 Solving "Spinal Tap Case" / freeCodeCamp Algorithm Challenges 22 Solving "Pig Latin" / freeCodeCamp Algorithm Challenges 23 Solving "Search and Replace" / freeCodeCamp Algorithm Challenges 24 Solving "DNA Pairing" / freeCodeCamp Algorithm Challengens 25 Solving "Missing letters" / freeCodeCamp Algorithm Challengens 26 Solving "Sorted Union" / freeCodeCamp Algorithm Challenges 27 Solving "Convert HTML Entities" / freeCodeCamp Algorithm Challengens 28 Solving "Sum All Odd Fibonacci Numbers" / freeCodeCamp Algorithm Challenges 29 Solving "Sum All Primes" / freeCodeCamp Algorithm Challenges 30 Solving "Smallest Common Multiple" / freeCodeCamp Algorithm Challenges 31 Solving "Drop it" / freeCodeCamp Algorithm Challenges 32 Solving "Steamroller" / freeCodeCamp Algorithm Challenges 33 Solving "Binary Agents" / freeCodeCamp Algorithm Challenges 34 Solving "Everything Be True" / freeCodeCamp Algorithm Challenges 35 Solving "Arguments Optional" / freeCodeCamp Algorithm Challengens 36 Solving "Make a Person" / freeCodeCamp Algorithm Challenges 37 Solving "Map the Debris" / freeCodeCamp Algorithm Challenges