空数组的长度(JavaScript Koans)

时间:2014-07-23 04:38:34

标签: javascript arrays

致力于JavaScript Koans。下面我提到了提示,然后是我的答案。

我的第一部分是正确的,但是当我把 0 作为空数组的预期长度时,它表示答案是错误的并且读取,"预期0到be'在'。"

中填写此值

我尝试过 0 ' 0' "" null 和未定义的(我是编程的新手,我确定其中一些没有意义,但他们会浮现在脑海中。)

提示:

it("should create arrays", function() {
   var emptyArray = [];
   expect(typeof(emptyArray)).toBe(FILL_ME_IN);
   expect(emptyArray.length).toBe(FILL_ME_IN);

我的回答:

it("should create arrays", function() {
   var emptyArray = [];
   expect(typeof(emptyArray)).toBe('object');
   expect(emptyArray.length).toBe(0);

4 个答案:

答案 0 :(得分:0)

确保你的答案是这样的(没有FILL_ME_IN),这将有效。

it("should create arrays", function() {
   var emptyArray = [];
   expect(typeof(emptyArray)).toBe('object');
   expect(emptyArray.length).toBe(0);
});

提示:expect(0).toBe(FILL_ME_IN)会向您显示错误"预期为0'在'中填写此值。"

答案 1 :(得分:0)

JSKoans中这个问题的编写方式,看起来它结束了我原始帖子中的代码结束的地方。但是,在查看打印出错误的行后,我意识到我应该继续编辑更多行。谢谢大家。

答案 2 :(得分:0)

对于仍然对此感到困惑的人: 原始海报有正确的答案。 问题在于,与之前的公案相反,整个“它”功能必须在能够完成这个特定的公案之前解决, 比如:

it("should create arrays", function() {
var emptyArray = [];
expect(typeof(emptyArray)).toBe('object');
expect(emptyArray.length).toBe(0);

var multiTypeArray = [0, 1, "two", function () { return 3; }, {value1: 4, value2: 5}, [6, 7]];
expect(multiTypeArray[0]).toBe(0);
expect(multiTypeArray[2]).toBe('two');
expect(multiTypeArray[3]()).toBe(3);
expect(multiTypeArray[4].value1).toBe(4);
expect(multiTypeArray[4]["value2"]).toBe(5);
expect(multiTypeArray[5][0]).toBe(6);
});

答案 3 :(得分:0)

你需要停止过度思考(我也是这样做)并且只是回答所要求的内容,即

var multiTypeArray = [0, 1, "two", function () { return 3; }, {value1: 4, value2: 5}, [6, 7]];
expect(multiTypeArray[0]).toBe(FILL_ME_IN); //What is Array's value at 0?

expect(multiTypeArray[2]).toBe(FILL_ME_IN); //What is Array's value at 2? (remember array key:value key starts at 0 then 1, 2 and so on)

expect(multiTypeArray[3]()).toBe(FILL_ME_IN); //What is Array's value at key 3 return?

expect(multiTypeArray[4].value1).toBe(FILL_ME_IN); //What is Array 4's value at key 0? (remember this is an array inside an array)

expect(multiTypeArray[4]["value2"]).toBe(FILL_ME_IN);//What is Array 4's value at value2? (remember this is an array inside an array)

expect(multiTypeArray[5][0]).toBe(FILL_ME_IN);//What is Array 5's value at key 0? (remember this is again an array inside an array)

我希望这会有所帮助。