59 lines
3.1 KiB
Markdown
59 lines
3.1 KiB
Markdown
---
|
||
id: 587d7b7a367417b2b2512b12
|
||
title: Copy Array Items Using slice()
|
||
challengeType: 1
|
||
videoUrl: ''
|
||
localeTitle: Копирование элементов массива Использование среза ()
|
||
---
|
||
|
||
## Description
|
||
<section id="description"> Следующий метод, который мы рассмотрим, - <code>slice()</code> . <code>slice()</code> , вместо того, чтобы модифицировать массив, копировать или <em>извлекать</em> заданное количество элементов в новый массив, оставляя массив вызываемым без изменений. <code>slice()</code> принимает только 2 параметра - первый - это индекс, с которого нужно начинать извлечение, а второй - это индекс, в котором останавливается извлечение (извлечение произойдет до, но не включает элемент в этот индекс). Учти это: <blockquote> пусть weatherConditions = ['rain', 'snow', 'sleet', 'hail', 'clear']; <br><br> let todaysWeather = weatherConditions.slice (1, 3); <br> // todaysWeather равно ['snow', 'sleet']; <br> // weatherConditions по-прежнему равно ['rain', 'snow', 'sleet', 'hail', 'clear'] <br></blockquote> Фактически, мы создали новый массив, извлекая элементы из существующего массива. </section>
|
||
|
||
## Instructions
|
||
<section id="instructions"> Мы определили функцию, <code>forecast</code> , которая принимает массив в качестве аргумента. Измените функцию, используя <code>slice()</code> чтобы извлечь информацию из массива аргументов и вернуть новый массив, содержащий элементы <code>'warm'</code> и <code>'sunny'</code> . </section>
|
||
|
||
## Tests
|
||
<section id='tests'>
|
||
|
||
```yml
|
||
tests:
|
||
- text: '<code>forecast</code> должен вернуться <code>["warm", "sunny"]</code>'
|
||
testString: 'assert.deepEqual(forecast(["cold", "rainy", "warm", "sunny", "cool", "thunderstorms"]), ["warm", "sunny"], "<code>forecast</code> should return <code>["warm", "sunny"]");'
|
||
- text: Функция <code>forecast</code> должна использовать метод <code>slice()</code>
|
||
testString: 'assert(/\.slice\(/.test(code), "The <code>forecast</code> function should utilize the <code>slice()</code> method");'
|
||
|
||
```
|
||
|
||
</section>
|
||
|
||
## Challenge Seed
|
||
<section id='challengeSeed'>
|
||
|
||
<div id='js-seed'>
|
||
|
||
```js
|
||
function forecast(arr) {
|
||
// change code below this line
|
||
|
||
return arr;
|
||
}
|
||
|
||
// do not change code below this line
|
||
console.log(forecast(['cold', 'rainy', 'warm', 'sunny', 'cool', 'thunderstorms']));
|
||
|
||
```
|
||
|
||
</div>
|
||
|
||
|
||
|
||
</section>
|
||
|
||
## Solution
|
||
<section id='solution'>
|
||
|
||
```js
|
||
// solution required
|
||
```
|
||
</section>
|