* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2.5 KiB
2.5 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
9d7123c8c441eeafaeb5bdef | 使用 slice 而不是 splice 从数组中移除元素 | 1 | 301236 | remove-elements-from-an-array-using-slice-instead-of-splice |
--description--
使用数组时经常遇到要删除一些元素并保留数组剩余部分的情况。为此,JavaScript 提供了splice
方法,它接收两个参数:从哪里开始删除项目的索引,和要删除的项目数。如果没有提供第二个参数,默认情况下是移除到结尾的元素。但splice
方法会改变调用它的原始数组。举个例子:
var cities = ["Chicago", "Delhi", "Islamabad", "London", "Berlin"];
cities.splice(3, 1); // Returns "London" and deletes it from the cities array
// cities is now ["Chicago", "Delhi", "Islamabad", "Berlin"]
正如我们在上一次挑战中看到的那样,slice
方法不会改变原始数组,而是返回一个可以保存到变量中的新数组。回想一下,slice
方法接收两个参数,从开始索引开始选取到结束(不包括该元素),并在新数组中返回这些元素。使用slice
方法替代splice
有助于避免数组变化产生的副作用。
--instructions--
用slice
代替splice
重写nonMutatingSplice
函数。将cities
数组长度限制为3,并返回一个仅包含前 3 项的新数组。
不要改变提供给函数的原始数组。
--hints--
应该使用slice
方法。
assert(code.match(/\.slice/g));
不能使用splice
方法。
assert(!code.match(/\.splice/g));
不能改变inputCities
数组。
assert(
JSON.stringify(inputCities) ===
JSON.stringify(['Chicago', 'Delhi', 'Islamabad', 'London', 'Berlin'])
);
nonMutatingSplice(['Chicago', 'Delhi', 'Islamabad', 'London', 'Berlin'])
应返回['Chicago', 'Delhi', 'Islamabad']
。
assert(
JSON.stringify(
nonMutatingSplice(['Chicago', 'Delhi', 'Islamabad', 'London', 'Berlin'])
) === JSON.stringify(['Chicago', 'Delhi', 'Islamabad'])
);
--seed--
--seed-contents--
function nonMutatingSplice(cities) {
// Only change code below this line
return cities.splice(3);
// Only change code above this line
}
var inputCities = ["Chicago", "Delhi", "Islamabad", "London", "Berlin"];
nonMutatingSplice(inputCities);
--solutions--
function nonMutatingSplice(cities) {
// Only change code below this line
return cities.slice(0,3);
// Only change code above this line
}
var inputCities = ["Chicago", "Delhi", "Islamabad", "London", "Berlin"];
nonMutatingSplice(inputCities);