4.1 KiB
4.1 KiB
id, title, challengeType, forumTopicId, localeTitle
id | title | challengeType | forumTopicId | localeTitle |
---|---|---|---|---|
587d7b7b367417b2b2512b13 | Copy an Array with the Spread Operator | 1 | 301157 | Скопируйте массив с помощью оператора распространения |
Description
slice()
позволяет нам выбирать, какие элементы массива копировать, среди нескольких других полезных задач, новый оператор с расширением ES6 позволяет нам легко скопировать все элементы массива в порядке, с простым и хорошо читаемым синтаксисом. Синтаксис распространения просто выглядит так: ...
На практике мы можем использовать оператор спредов для копирования массива следующим образом: let thisArray = [true, true, undefined, false, null];
let thatArray = [... thisArray];
// thisArray равно [true, true, undefined, false, null]
// thisArray остается неизменным и идентичен этому массиву
Instructions
copyMachine
которая принимает arr
(массив) и num
(число) в качестве аргументов. Функция должна возвращать новый массив, состоящий из num
копий arr
. Мы выполнили большую часть работы для вас, но пока это не работает. Измените функцию, используя синтаксис распространения, чтобы он работал правильно (подсказка: здесь может быть полезен другой метод, который мы уже рассмотрели!).
Tests
tests:
- text: <code>copyMachine([true, false, true], 2)</code> should return <code>[[true, false, true], [true, false, true]]</code>
testString: assert.deepEqual(copyMachine([true, false, true], 2), [[true, false, true], [true, false, true]]);
- text: <code>copyMachine([1, 2, 3], 5)</code> should return <code>[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]</code>
testString: assert.deepEqual(copyMachine([1, 2, 3], 5), [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]);
- text: <code>copyMachine([true, true, null], 1)</code> should return <code>[[true, true, null]]</code>
testString: assert.deepEqual(copyMachine([true, true, null], 1), [[true, true, null]]);
- text: <code>copyMachine(["it works"], 3)</code> should return <code>[["it works"], ["it works"], ["it works"]]</code>
testString: assert.deepEqual(copyMachine(['it works'], 3), [['it works'], ['it works'], ['it works']]);
- text: The <code>copyMachine</code> function should utilize the <code>spread operator</code> with array <code>arr</code>
testString: assert(removeJSComments(code).match(/\.\.\.arr/));
Challenge Seed
function copyMachine(arr, num) {
let newArr = [];
while (num >= 1) {
// change code below this line
// change code above this line
num--;
}
return newArr;
}
// change code here to test different cases:
console.log(copyMachine([true, false, true], 2));
After Tests
const removeJSComments = str => str.replace(/\/\*[\s\S]*?\*\/|\/\/.*$/gm, '');
Solution
function copyMachine(arr,num){
let newArr=[];
while(num >=1){
// change code below this line
newArr.push([...arr]);
//change code above this line
num--;
}
return newArr;
}
console.log(copyMachine([true, false, true], 2));