What do you think about my approach?


function frankenSplice(arr1, arr2, n) {

let myArr = [];

for (let i = 0; i < n; i++) {
  myArr.push(arr2[i]);
}

myArr = myArr.concat(arr1);
for (let i = n; i < arr2.length; i++) {
  myArr.push(arr2[i]);
}

return myArr;
}

let test = frankenSplice([1, 2, 3], [4, 5, 6], 2);

console.log(test);

What I did for this challenge is to write out the algorithm first and then implement the solution. Here is the algorithm below;

Copy all contents to arr2 to myArr up to index n and pause.
Copy all contents of arr1 starting from the end of myArr.
Copy remaining contents of arr2 to myArr.

I passed all the tests but just wanted to sure about my approach.

Challenge: Slice and Splice

Link to the challenge:

This is OK. But you might want to consider using slice() method. :slightly_smiling_face:

2 Likes

Your approach is perfectly fine. Don’t overthink it at this stage.

I think trying to come up with different approaches is a good idea.

I’m not saying to get stuck trying to solve the challenge in 50 different ways but just to think about the problem from a few different angles. It may also introduce you to different built-in methods and how to use them. Personally, I don’t see the main point of the challenges being just to solve them, but to learn as much as possible solving them.

The first thing I thought about was how to do it without looping using slice and spread. Might be worth a try.

return [...startValues, ...all, ...endValues]
1 Like