diff --git a/client/src/pages/guide/english/certifications/javascript-algorithms-and-data-structures/basic-algorithm-scripting/slice-and-splice/index.md b/client/src/pages/guide/english/certifications/javascript-algorithms-and-data-structures/basic-algorithm-scripting/slice-and-splice/index.md index 9bae5d1c62..30828f5891 100644 --- a/client/src/pages/guide/english/certifications/javascript-algorithms-and-data-structures/basic-algorithm-scripting/slice-and-splice/index.md +++ b/client/src/pages/guide/english/certifications/javascript-algorithms-and-data-structures/basic-algorithm-scripting/slice-and-splice/index.md @@ -39,18 +39,18 @@ Increment the index after performing the splice. **Solution ahead!** ## ![:beginner:](https://forum.freecodecamp.com/images/emoji/emoji_one/beginner.png?v=3 ":beginner:") 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; - } - -![:rocket:](https://forum.freecodecamp.com/images/emoji/emoji_one/rocket.png?v=3 ":rocket:") Run Code +```js +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; +} +``` +![:rocket:](https://forum.freecodecamp.com/images/emoji/emoji_one/rocket.png?v=3 ":rocket:") Run Code ### Code Explanation: @@ -64,6 +64,24 @@ Increment the index after performing the splice. * Finally, we return the `localArray` and end the function. + +## 🌻 Intermediate Code Solution: +```js +function frankenSplice(arr1, arr2, n) { + // It's alive. It's alive! + let localArr = arr2.slice(); + localArr.splice(n, 0, ...arr1); + return localArr; +} +``` +![:rocket:](https://forum.freecodecamp.com/images/emoji/emoji_one/rocket.png?v=3 ":rocket:") Run Code + +* Since our goal is to return the new array with out altering `arr1` or `arr2` we create a `localArr` and add all the items from `arr2` using the `slice()` 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 of `arr1` into `localArr`. `n` is the starting position where our content will be inserted. We won't be deleting any elements so the next argument is `0`. Then we add the entire contents of `arr1` using spread syntax `...`. + +* `localArr` is returned and the function is complete. + ## ![:clipboard:](https://forum.freecodecamp.com/images/emoji/emoji_one/clipboard.png?v=3 ":clipboard:") NOTES FOR CONTRIBUTIONS: * ![:warning:](https://forum.freecodecamp.com/images/emoji/emoji_one/warning.png?v=3 ":warning:") **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.