Slice and Splice add solution?

Tell us what’s happening:

I believe the solution i came up with would be easier than the one posted, but i dont know how to post it. Can any moderator include it?

Your code so far


function frankenSplice(arr1, arr2, n) {
  // It's alive. It's alive!
  let x = arr2.slice();
 x.splice(n,0,...arr1);
 return x;
  
}

console.log(frankenSplice([1, 2, 3], [4, 5, 6], 1));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:69.0) Gecko/20100101 Firefox/69.0.

Link to the challenge:

1 Like

as for now guide modifications are not being accepted. the guide is in the process to being trasferred to the #guide subforum here in the forum.

Solution #2 is just like yours in the new hints/solutions topic.

2 Likes

sorry i only saw one solution at the time.

Hi. I have this one :

function frankenSplice(arr1, arr2, n) {

const sliced = arr1.slice()
arr2.splice(n,0,arr1)
return arr2.flat();

}

3 Likes

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 :wink:

I have a solution using splice() only, in case somebody finds it useful.

function frankenSplice(arr1, arr2, n) {
for (let i = 0; i < arr1.length; i++) {
arr2.splice((n + i), 0, arr1[i]);
}
return arr2;
}

2 Likes

doesn’t the challenge say to not change arr1 or arr2?

2 Likes

Yes, you’re correct. I have changed arr2, instead of using slice() to make a copy.
It was accepted, though. :upside_down_face:

Can you report it as a bug?

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.

I have reported it here.

Thanks!

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);
1 Like

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.

Thank you.

i’m so new at programming so this is probably the worst way to go about it
here’s what i did

function frankenSplice(arr1, arr2, n) {
let result = [ ] ;
for(let i = 0 ; i < arr2.length ; i++){
result[i] = arr2[i] ;
}

for(let i = 0 ; i < arr1.length ; i++){
result.splice(n + i ,0 , arr1[i]) ;
}

return result;
}

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

i did it diffrently first but it kept saying i shouldnt change arr2 i couldnt , maybe one day when i’m better i’ll come back and do a better job

1 Like

it’s not a bad way, you could avoid the first loop by using the slice method to copy arr2, but the second loop is a good way of doing it :+1:

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

Didn’t it change the original array?
Nice solution tho!

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!

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

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