--- id: 579e2a2c335b9d72dd32e05c title: Slice and Splice isRequired: true isBeta: true challengeType: 5 forumTopicId: 301148 localeTitle: Нарезка и сращивание --- ## Description
Вам даны два массива и индекс. Используйте метод массива slice и splice для копирования каждого элемента первого массива во второй массив по порядку. Начните вставлять элементы в индекс n второго массива. Верните результирующий массив. Входные массивы должны оставаться неизменными после запуска функции. Не забудьте использовать Read-Search-Ask, если вы застряли. Напишите свой собственный код.
## Instructions
## Tests
```yml tests: - text: frankenSplice([1, 2, 3], [4, 5], 1) should return [4, 1, 2, 3, 5]. testString: assert.deepEqual(frankenSplice([1, 2, 3], [4, 5], 1), [4, 1, 2, 3, 5]); - text: frankenSplice([1, 2], ["a", "b"], 1) should return ["a", 1, 2, "b"]. testString: assert.deepEqual(frankenSplice(testArr1, testArr2, 1), ["a", 1, 2, "b"]); - text: frankenSplice(["claw", "tentacle"], ["head", "shoulders", "knees", "toes"], 2) should return ["head", "shoulders", "claw", "tentacle", "knees", "toes"]. testString: assert.deepEqual(frankenSplice(["claw", "tentacle"], ["head", "shoulders", "knees", "toes"], 2), ["head", "shoulders", "claw", "tentacle", "knees", "toes"]); - text: All elements from the first array should be added to the second array in their original order. testString: assert.deepEqual(frankenSplice([1, 2, 3, 4], [], 0), [1, 2, 3, 4]); - text: The first array should remain the same after the function runs. testString: assert(testArr1[0] === 1 && testArr1[1] === 2); - text: The second array should remain the same after the function runs. testString: assert(testArr2[0] === "a" && testArr2[1] === "b"); ```
## Challenge Seed
```js function frankenSplice(arr1, arr2, n) { // It's alive. It's alive! return arr2; } frankenSplice([1, 2, 3], [4, 5, 6], 1); ```
### After Tests
```js let testArr1 = [1, 2]; let testArr2 = ["a", "b"]; ```
## Solution
```js function frankenSplice(arr1, arr2, n) { // It's alive. It's alive! let result = arr2.slice(); for (let i = 0; i < arr1.length; i++) { result.splice(n+i, 0, arr1[i]); } return result; } frankenSplice([1, 2, 3], [4, 5], 1); ```