freeCodeCamp/curriculum/challenges/russian/02-javascript-algorithms-an.../basic-data-structures/copy-array-items-using-slic...

59 lines
3.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
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 = [&#39;rain&#39;, &#39;snow&#39;, &#39;sleet&#39;, &#39;hail&#39;, &#39;clear&#39;]; <br><br> let todaysWeather = weatherConditions.slice (1, 3); <br> // todaysWeather равно [&#39;snow&#39;, &#39;sleet&#39;]; <br> // weatherConditions по-прежнему равно [&#39;rain&#39;, &#39;snow&#39;, &#39;sleet&#39;, &#39;hail&#39;, &#39;clear&#39;] <br></blockquote> Фактически, мы создали новый массив, извлекая элементы из существующего массива. </section>
## Instructions
<section id="instructions"> Мы определили функцию, <code>forecast</code> , которая принимает массив в качестве аргумента. Измените функцию, используя <code>slice()</code> чтобы извлечь информацию из массива аргументов и вернуть новый массив, содержащий элементы <code>&#39;warm&#39;</code> и <code>&#39;sunny&#39;</code> . </section>
## Tests
<section id='tests'>
```yml
tests:
- text: '<code>forecast</code> должен вернуться <code>[&quot;warm&quot;, &quot;sunny&quot;]</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>