-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmanual.js
More file actions
87 lines (74 loc) · 2.47 KB
/
Copy pathmanual.js
File metadata and controls
87 lines (74 loc) · 2.47 KB
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
// @target indesign
// @include ./assertions.js
// @include ../index.js
describe("Array functions", function() {
// array.every
test("array every element of [1,2,3] should be smaller then 4", function() {
const arr = [1, 2, 3];
expect(
arr.every(function(ele) {
return ele < 4;
})
).toBe(true);
});
// array.filter
test("array filter (x<3) of [1, 2, 3, 4] should return [1, 2]", function() {
const arr = [1, 2, 3, 4];
expect(
arr.filter(function(ele) {
return ele < 3 ? true : false;
})
).toEqualArray([1,2]);
});
// array.forEach
test("array forEach (val + 1) of [1, 2, 3] should return [2, 3, 4]", function() {
const arr = [1, 2, 3];
const newArr = [];
arr.forEach(function(ele) {
newArr.push(ele + 1)
});
expect(newArr).toEqualArray([2, 3, 4]);
});
// array.indexOf
test("array indexOf (4) in [1, 2, 3, 4, 5, 6] should return 3", function() {
const arr = [1, 2, 3, 4, 5, 6];
expect(arr.indexOf(4)).toBe(3);
});
// array.isArray
test('array isArray on [1,2,3], should return true\n\tarray isArray on 1 should return false', function() {
const arr = [1,2,3];
expect(Array.isArray(arr)).toBe(true);
expect(Array.isArray(1)).toBe(false);
});
// array.lastIndexOf
test('array lastIndexOf(4) on [4, 1, 2, 3, 4] should return 4', function() {
const arr = [4, 1, 2, 3, 4];
expect(arr.lastIndexOf(4)).toBe(4);
});
// array.map
test('array map (val * 2) on [4, 1, 2, 3, 4] should return [8, 2, 4, 6, 8]', function() {
const arr = [4, 1, 2, 3, 4];
expect(arr.map(function(x) {return x * 2})).toEqualArray([8, 2, 4, 6, 8]);
});
// array.reduce
test('array reduce (sum) on [1, 2, 3, 4, 5] should return 15', function() {
const arr = [1, 2, 3, 4, 5];
expect(arr.reduce(function(acc, val) { return acc += val},0)).toBe(15);
});
// array.reduceRight
test('array reduceRight on [1, 2, 3, 4, 5] should return 15, and index should iterate this way [4, 3, 2, 1, 0]', function(){
const arr = [1, 2, 3, 4, 5];
const indexArr = [];
const sum = arr.reduceRight(function(acc, val, index) {
indexArr.push(index);
return acc += val;
}, 0);
expect(sum).toBe(15);
expect(indexArr).toEqualArray([4, 3, 2, 1, 0]);
});
// array.some
test('array some (val % 2 === 0) should return true', function() {
const arr = [1, 2, 3, 4, 5];
expect(arr.some(function(val) {return val % 2 === 0})).toBe(true);
});
});