5.7 KiB
title |
---|
Slice and Splice |
Slice and Splice
Remember to use Read-Search-Ask
if you get stuck. Try to pair program and write your own code
Problem Explanation:
We need to copy each element from the first array into the second array starting at the index n. We've also got to ensure that the original arrays are not mutated. That is, we cannot make any changes to the original arrays.
Relevant Links
Hint: 1
Create a copy of the second array inside of the function. This will ensure that the original array is not mutated. This can be done by using the slice operation on the second array, and assign it to a variable.
try to solve the problem now
Hint: 2
Loop through all of the items in the first array. For each item in the first array splice it into the copied array in the index given as argument.
try to solve the problem now
Hint: 3
Increment the index after performing the splice.
try to solve the problem now
Spoiler Alert!
Solution ahead!
Basic Code Solution:
function frankenSplice(arr1, arr2, n) {
// It's alive. It's alive!
let localArray = arr2.slice();
for (let i = 0; i < arr1.length; i++) {
localArray.splice(n, 0, arr1[i]);
n++;
}
return localArray;
}
Code Explanation:
-
Our goal is to take all of the elements from
arr1
and insert them intoarr2
starting with index positionn
. At the same time we must ensurethat neitherarr
orarr2
have been mutated. -
Using the
slice()
function we can create an exact replica ofarr2
and assign the result of the operation to a variable,localArray
. -
Now that we have an array that we can mutate on, we can iterate through every item in the first array. For each item in the first array we can use the
splice()
function to insert the item into indexn
oflocalArray
. -
We increment the index
n
by one. This will ensure that every item from thearr1
is inserted intolocalArray
in the proper index position. -
Finally, we return the
localArray
and end the function.
🌻 Intermediate Code Solution:
function frankenSplice(arr1, arr2, n) {
// It's alive. It's alive!
let localArr = arr2.slice();
localArr.splice(n, 0, ...arr1);
return localArr;
}
-
Since our goal is to return the new array with out altering
arr1
orarr2
we create alocalArr
and add all the items fromarr2
using theslice()
function -
Since the
splice()
function will mutate (alter) arrays and can be used to add new elements we will use it to add the contents ofarr1
intolocalArr
.n
is the starting position where our content will be inserted. We won't be deleting any elements so the next argument is0
. Then we add the entire contents ofarr1
using spread syntax...
. -
localArr
is returned and the function is complete.
NOTES FOR CONTRIBUTIONS:
- DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.
- Add an explanation of your solution.
- Categorize the solution in one of the following categories — Basic, Intermediate and Advanced.
- Please add your username only if you have added any relevant main contents. ( DO NOT remove any existing usernames)
See
Wiki Challenge Solution Template
for reference.