forked from mrdavidlaing/javascript-koans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAboutArrays.js
97 lines (75 loc) · 2.93 KB
/
AboutArrays.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
describe("About Arrays", function() {
//We shall contemplate truth by testing reality, via spec expectations.
it("should create arrays", function() {
var emptyArray = [];
expect(typeof(emptyArray)).toBe(FILL_ME_IN); //A mistake? - http://javascript.crockford.com/remedial.html
expect(emptyArray.length).toBe(FILL_ME_IN);
var multiTypeArray = [0, 1, "two", function () { return 3; }, {value1: 4, value2: 5}, [6, 7]];
expect(multiTypeArray[0]).toBe(FILL_ME_IN);
expect(multiTypeArray[2]).toBe(FILL_ME_IN);
expect(multiTypeArray[3]()).toBe(FILL_ME_IN);
expect(multiTypeArray[4].value1).toBe(FILL_ME_IN);
expect(multiTypeArray[4]["value2"]).toBe(FILL_ME_IN);
expect(multiTypeArray[5][0]).toBe(FILL_ME_IN);
});
it("should understand array literals", function () {
var array = [];
expect(array).toEqual([]);
array[0] = 1;
expect(array).toEqual([1]);
array[1] = 2;
expect(array).toEqual([1, FILL_ME_IN]);
array.push(3);
expect(array).toEqual(FILL_ME_IN);
});
it("should understand array length", function () {
var fourNumberArray = [1, 2, 3, 4];
expect(fourNumberArray.length).toBe(FILL_ME_IN);
fourNumberArray.push(5, 6);
expect(fourNumberArray.length).toBe(FILL_ME_IN);
var tenEmptyElementArray = new Array(10);
expect(tenEmptyElementArray.length).toBe(FILL_ME_IN);
tenEmptyElementArray.length = 5;
expect(tenEmptyElementArray.length).toBe(FILL_ME_IN);
});
it("should slice arrays", function () {
var array = ["peanut", "butter", "and", "jelly"];
expect(array.slice(0, 1)).toEqual(FILL_ME_IN);
expect(array.slice(0, 2)).toEqual(FILL_ME_IN);
expect(array.slice(2, 2)).toEqual(FILL_ME_IN);
expect(array.slice(2, 20)).toEqual(FILL_ME_IN);
expect(array.slice(3, 0)).toEqual(FILL_ME_IN);
expect(array.slice(3, 100)).toEqual(FILL_ME_IN);
expect(array.slice(5, 1)).toEqual(FILL_ME_IN);
});
it("should know array references", function () {
var array = [ "zero", "one", "two", "three", "four", "five" ];
function passedByReference(refArray) {
refArray[1] = "changed in function";
}
passedByReference(array);
expect(array[1]).toBe(FILL_ME_IN);
var assignedArray = array;
assignedArray[5] = "changed in assignedArray";
expect(array[5]).toBe(FILL_ME_IN);
var copyOfArray = array.slice();
copyOfArray[3] = "changed in copyOfArray";
expect(array[3]).toBe(FILL_ME_IN);
});
it("should push and pop", function () {
var array = [1, 2];
array.push(3);
expect(array).toEqual(FILL_ME_IN);
var poppedValue = array.pop();
expect(poppedValue).toBe(FILL_ME_IN);
expect(array).toEqual(FILL_ME_IN);
});
it("should know about shifting arrays", function () {
var array = [1, 2];
array.unshift(3);
expect(array).toEqual(FILL_ME_IN);
var shiftedValue = array.shift();
expect(shiftedValue).toEqual(FILL_ME_IN);
expect(array).toEqual(FILL_ME_IN);
});
});