Good one ! I first did it raw with splice(n,0,arr1) but it returned nested array.
I knew I read about flat function to flat the array (only 1 level nested) but would like to do it more vanilla way
Thank you for helping make FCC better. Bugs can be reported as GitHub Issues. Whenever reporting a bug, please check first that there isn’t already an issue for it and provide as much detail as possible.
Here is my solution. I didn’t think I could alter the arr1 array so I made a copy of both arrays before I changed anything. I see that in the freeCodeCamp solution they use splice on arr1 and it, apparently, doesn’t mutate the array the way they did it. Can anyone confirm that you can use the third parameter on splice and it will not mutate an array? Hope that makes sense.
function frankenSplice(arr1, arr2, n) {
let temp1 = [];
let temp2 = [];
temp1 = arr2.slice(0);
temp2 = arr1.slice(0);
temp1.splice(n,0,...arr1);
return temp1;
frankenSplice([1, 2, 3], [4, 5, 6], 1);
splice mutate the array on which it is used
where do you see the solution that uses directly splice on input array?
if you mean something like localArr.splice(n, 0, ...arr1);, this is not using the splice method on arr1, as ...arr1 will copy the elements inside arr1. arr1 wouldn’t be affected even if used as argument of splice, without copying it.
Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.
@fxded
You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
We have set your post to unlisted. Thanks for your understanding.
All you need to do is to google as much as you can, understand the new approaches you find, and try to avoid typing a traditional for-loop (That’s only in case you want a more elegant solution, but I don’t mean that for-loops are bad at all)
But overall, what you did is good since you are now experimenting for-loops more closely, and trying new things with them!