# Slice and Splice add solution?

**URL:** <https://forum.freecodecamp.org/t/slice-and-splice-add-solution/318901>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [October 2, 2019, 9:29am UTC](https://forum.freecodecamp.org/t/slice-and-splice-add-solution/318901 "2019-10-02T09:29:35Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jislas](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jislas/32/125371_2.png) [@Jislas](https://forum.freecodecamp.org/u/Jislas)\
**Post date:** [October 2, 2019, 9:29am UTC](https://forum.freecodecamp.org/t/slice-and-splice-add-solution/318901/1 "2019-10-02T09:29:35Z")

</div>

**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**

```js

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

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/slice-and-splice)**
>
> Learn to Code — For Free

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [October 5, 2019, 1:58pm UTC](https://forum.freecodecamp.org/t/slice-and-splice-add-solution/318901/2 "2019-10-05T13:58:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![camperextraordinaire](https://avatars.discourse-cdn.com/v4/letter/c/5f9b8f/32.png) [@camperextraordinaire](https://forum.freecodecamp.org/u/camperextraordinaire)\
**Post date:** [October 5, 2019, 2:02pm UTC](https://forum.freecodecamp.org/t/slice-and-splice-add-solution/318901/3 "2019-10-05T14:02:41Z")

</div>

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

> [@freeCodeCamp Challenge Guide: Slice and Splice](http://forum.freecodecamp.org/t/freecodecamp-challenge-guide-slice-and-splice/301148):
>
> [Slice and Splice](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/slice-and-splice)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 [Array.slice()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice)[Array.splice()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice)Hints 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 sec…

---

<div class="post-metadata">

**Author:** ![Jislas](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jislas/32/125371_2.png) [@Jislas](https://forum.freecodecamp.org/u/Jislas)\
**Post date:** [October 5, 2019, 2:16pm UTC](https://forum.freecodecamp.org/t/slice-and-splice-add-solution/318901/4 "2019-10-05T14:16:03Z")

</div>

sorry i only saw one solution at the time.

---

<div class="post-metadata">

**Author:** ![Thomas088](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/thomas088/32/125932_2.png) [@Thomas088](https://forum.freecodecamp.org/u/Thomas088)\
**Post date:** [January 30, 2020, 10:53am UTC](https://forum.freecodecamp.org/t/slice-and-splice-add-solution/318901/6 "2020-01-30T10:53:05Z")

</div>

Hi. I have this one :

function frankenSplice(arr1, arr2, n) {

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

}

---

<div class="post-metadata">

**Author:** ![Mohican1989](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@Mohican1989](https://forum.freecodecamp.org/u/Mohican1989)\
**Post date:** [January 30, 2020, 10:19pm UTC](https://forum.freecodecamp.org/t/slice-and-splice-add-solution/318901/7 "2020-01-30T22:19:12Z")

</div>

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 😉

---

<div class="post-metadata">

**Author:** ![jovan-nikolic-dev](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jovan-nikolic-dev/32/135025_2.png) [@jovan-nikolic-dev](https://forum.freecodecamp.org/u/jovan-nikolic-dev)\
**Post date:** [February 16, 2020, 2:18pm UTC](https://forum.freecodecamp.org/t/slice-and-splice-add-solution/318901/8 "2020-02-16T14:18:23Z")

</div>

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;  
}

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [February 16, 2020, 3:34pm UTC](https://forum.freecodecamp.org/t/slice-and-splice-add-solution/318901/9 "2020-02-16T15:34:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jovan-nikolic-dev](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jovan-nikolic-dev/32/135025_2.png) [@jovan-nikolic-dev](https://forum.freecodecamp.org/u/jovan-nikolic-dev)\
**Post date:** [February 16, 2020, 3:52pm UTC](https://forum.freecodecamp.org/t/slice-and-splice-add-solution/318901/10 "2020-02-16T15:52:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [February 16, 2020, 5:34pm UTC](https://forum.freecodecamp.org/t/slice-and-splice-add-solution/318901/11 "2020-02-16T17:34:18Z")

</div>

Can you report it as a bug?

> Thank you for helping make FCC better. Bugs can be reported as [GitHub Issues](https://github.com/freeCodeCamp/freeCodeCamp/issues). Whenever reporting a bug, please check first that there isn’t already an issue for it and provide as much detail as possible.

---

<div class="post-metadata">

**Author:** ![jovan-nikolic-dev](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jovan-nikolic-dev/32/135025_2.png) [@jovan-nikolic-dev](https://forum.freecodecamp.org/u/jovan-nikolic-dev)\
**Post date:** [February 16, 2020, 9:23pm UTC](https://forum.freecodecamp.org/t/slice-and-splice-add-solution/318901/12 "2020-02-16T21:23:00Z")

</div>

I have reported it here.

> <https://github.com/freeCodeCamp/freeCodeCamp/issues/38223>
>
> JavaScript Algorithms and Data Structures -\> Basic Algorithm Scripting: Slice and Splice
> problem description
> The instruction for this challenge says "The input arrays...

Thanks!

---

<div class="post-metadata">

**Author:** ![henryarguedas](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/henryarguedas/32/137488_2.png) [@henryarguedas](https://forum.freecodecamp.org/u/henryarguedas)\
**Post date:** [March 20, 2020, 2:10am UTC](https://forum.freecodecamp.org/t/slice-and-splice-add-solution/318901/13 "2020-03-20T02:10:02Z")

</div>

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.

```auto
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);

```

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [March 20, 2020, 8:50am UTC](https://forum.freecodecamp.org/t/slice-and-splice-add-solution/318901/14 "2020-03-20T08:50:08Z")

</div>

> [@henryarguedas](#):
>
> 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?

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

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [March 20, 2020, 8:51am UTC](https://forum.freecodecamp.org/t/slice-and-splice-add-solution/318901/15 "2020-03-20T08:51:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kwinchi](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kwinchi/32/138473_2.png) [@kwinchi](https://forum.freecodecamp.org/u/kwinchi)\
**Post date:** [March 22, 2020, 12:16pm UTC](https://forum.freecodecamp.org/t/slice-and-splice-add-solution/318901/16 "2020-03-22T12:16:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [March 22, 2020, 3:27pm UTC](https://forum.freecodecamp.org/t/slice-and-splice-add-solution/318901/17 "2020-03-22T15:27:52Z")

</div>

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 👍

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [April 4, 2020, 1:46pm UTC](https://forum.freecodecamp.org/t/slice-and-splice-add-solution/318901/19 "2020-04-04T13:46:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mustafa\_alsoffi](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mustafa_alsoffi/32/135521_2.png) [@mustafa\_alsoffi](https://forum.freecodecamp.org/u/mustafa_alsoffi)\
**Post date:** [May 8, 2020, 2:38pm UTC](https://forum.freecodecamp.org/t/slice-and-splice-add-solution/318901/20 "2020-05-08T14:38:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mustafa\_alsoffi](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mustafa_alsoffi/32/135521_2.png) [@mustafa\_alsoffi](https://forum.freecodecamp.org/u/mustafa_alsoffi)\
**Post date:** [May 8, 2020, 2:42pm UTC](https://forum.freecodecamp.org/t/slice-and-splice-add-solution/318901/21 "2020-05-08T14:42:47Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Leon147](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/leon147/32/134324_2.png) [@Leon147](https://forum.freecodecamp.org/u/Leon147)\
**Post date:** [May 12, 2020, 8:36pm UTC](https://forum.freecodecamp.org/t/slice-and-splice-add-solution/318901/22 "2020-05-12T20:36:11Z")

</div>

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

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

```

[Next page](https://forum.freecodecamp.org/t/slice-and-splice-add-solution/318901.md?page=2)
