简体   繁体   中英

Length of empty array (JavaScript Koans)

Working on JavaScript Koans. Below I've included the prompt and then my answer.

I got the first part right, but when I put 0 as the expected length of an empty array, it says the answer is incorrect and reads, "Expected 0 to be 'Fill this value in'."

I've tried 0 , '0' , "" , null and undefined (I'm new to programming, I'm sure a few of those don't make sense, but they came to mind.)

Prompt:

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

My answer:

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

Make sure your answer is this(without FILL_ME_IN), this will work.

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

hint: expect(0).toBe(FILL_ME_IN) will show you the error "Expected 0 to be 'Fill this value in'."

The way this question in JSKoans was written, it looks like it ends where my code in the original post ended. However, after looking at the line printed out where the error occurred, I realized that I was supposed to keep on editing more lines. Thanks all.

For people still confused about this: The original poster had the right answer. The problem is that, as opposed to the previous koans, the entire "it" function has to be solved before being able to complete this particular koan, such as this:

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);
});

You need to stop overthinking( which I was doing as well) and just answer what is asked ie,

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)

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM