0

我是 js 新手,我无法解决这个问题。

test("splice", function() {
    var daysOfWeek = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
    var workingWeek = daysOfWeek.splice(__);
    ok(workingWeek.equalTo([__]), 'what is the value of workingWeek?');
    ok(daysOfWeek.equalTo([__]), 'what is the value of daysOfWeek?');
});

我的尝试在下面,但它不起作用。

test("splice", function() {
    var daysOfWeek = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
    var workingWeek = daysOfWeek.splice(5,6);
    ok(workingWeek.equalTo([5]), 'what is the value of workingWeek?');
    ok(daysOfWeek.equalTo([2]), 'what is the value of daysOfWeek?');
});

有人可以为我提供任何线索吗?

4

1 回答 1

0

文档

[Array splice方法]

array.splice(index, howMany, […])

更改数组的内容,在删除旧元素的同时添加新元素

返回 […] 一个包含已删除元素的数组。

因此,您当前的代码将删除 6 个元素,从第 6 个元素开始(在 index 上5,即 ie 'Saturday')。这有点太多了,只删除 2 就足够了,尽管在这里不会有任何区别——它们位于数组的末尾。

您可能想要实现的是

ok(workingWeek.equalTo(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']), 'what is the value of workingWeek?');
// though it makes no practical sense, you are using splice():
ok(daysOfWeek.equalTo(['Saturday', 'Sunday']), 'what is the value of daysOfWeek?');

我将把更改参数的任务留给splice(…)你:-)

于 2013-03-21T18:16:28.583 回答