.github
api-server
client
config
curriculum
challenges
_meta
arabic
chinese
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
access-an-arrays-contents-using-bracket-notation.chinese.md
access-property-names-with-bracket-notation.chinese.md
add-items-to-an-array-with-push-and-unshift.chinese.md
add-items-using-splice.chinese.md
add-key-value-pairs-to-javascript-objects.chinese.md
check-for-the-presence-of-an-element-with-indexof.chinese.md
check-if-an-object-has-a-property.chinese.md
combine-arrays-with-the-spread-operator.chinese.md
copy-an-array-with-the-spread-operator.chinese.md
copy-array-items-using-slice.chinese.md
create-complex-multi-dimensional-arrays.chinese.md
generate-an-array-of-all-object-keys-with-object.keys.chinese.md
iterate-through-all-an-arrays-items-using-for-loops.chinese.md
iterate-through-the-keys-of-an-object-with-a-for...in-statement.chinese.md
modify-an-array-stored-in-an-object.chinese.md
modify-an-object-nested-within-an-object.chinese.md
remove-items-from-an-array-with-pop-and-shift.chinese.md
remove-items-using-splice.chinese.md
use-an-array-to-store-a-collection-of-data.chinese.md
use-the-delete-keyword-to-remove-object-properties.chinese.md
basic-javascript
debugging
es6
functional-programming
intermediate-algorithm-scripting
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
03-front-end-libraries
04-data-visualization
05-apis-and-microservices
06-information-security-and-quality-assurance
08-coding-interview-prep
09-certificates
english
portuguese
russian
spanish
schema
test
.babelrc
.editorconfig
.npmignore
.travis.yml
CHANGELOG.md
LICENSE.md
commitizen.config.js
commitlint.config.js
create-challenge-bundle.js
getChallenges.js
gulpfile.js
lib.js
package-entry.js
package-lock.json
package.json
utils.js
cypress
docs
search-indexing
tools
utils
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.gitpod.yml
.node-inspectorrc
.npmrc
.prettierignore
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile.tests
HoF.md
LICENSE.md
README.md
SECURITY.md
change_volumes_owner.sh
cypress-install.js
cypress.json
docker-compose-shared.yml
docker-compose.tests.yml
docker-compose.yml
jest.config.js
lerna.json
package-lock.json
package.json
sample.env
* fix: Chinese test suite Add localeTiltes, descriptions, and adjust test text and testStrings to get the automated test suite working. * fix: ran script, updated testStrings and solutions
59 lines
2.1 KiB
Markdown
59 lines
2.1 KiB
Markdown
---
|
||
id: 587d7b7a367417b2b2512b12
|
||
title: Copy Array Items Using slice()
|
||
challengeType: 1
|
||
videoUrl: ''
|
||
localeTitle: 使用slice()复制数组项
|
||
---
|
||
|
||
## 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>让todaysWeather = weatherConditions.slice(1,3); <br> //今天天气等于['雪','雨夹雪']; <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']);
|
||
- text: <code>forecast</code>函数应该使用<code>slice()</code>方法
|
||
testString: assert(/\.slice\(/.test(code));
|
||
|
||
```
|
||
|
||
</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>
|