-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathspec.js
More file actions
143 lines (117 loc) · 4.88 KB
/
spec.js
File metadata and controls
143 lines (117 loc) · 4.88 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
var assertArrayEquals = function(array1, array2) {
expect(array1).to.have.length(array2.length);
for (var i = 0; i < array1.length; i++) {
expect(array1[i]).to.equal(array2[i]);
}
};
describe("Basic building blocks", function() {
describe("map", function() {
it("doubling numbers", function() {
var result = map([1, 2, 3], function(x) {return x*2;});
assertArrayEquals(result, [2, 4, 6]);
});
it("lengths of strings", function() {
var result = map(["hello", "you", "prig"], function(s) {return s.length; });
assertArrayEquals(result, [5, 3, 4]);
});
it("truthiness", function() {
var result = map([null, "", 1, "a", undefined], function(x) {return !!x;});
assertArrayEquals(result, [false, false, true, true, false]);
});
it("can use the index", function() {
var result = map([1, 2, 3, 2, 1], function(x, i) {return x + i;});
assertArrayEquals(result, [1, 3, 5, 5, 5]);
});
});
describe("filter", function() {
it("even numbers", function() {
var result = filter([1, 2, 3, 4, 5], function(x) {return x % 2 === 0;});
assertArrayEquals(result, [2, 4]);
});
it("strings", function() {
var result = filter(["a", 1, true, "goodbye"], function(x) {return typeof x === "string";});
assertArrayEquals(result, ["a", "goodbye"]);
});
it("can use the index", function() {
var result = filter([1, 2, 3, 4, 5, 6], function(x, i) {return i === 3 || i === 2;});
assertArrayEquals(result, [3, 4]);
});
});
describe("reduce", function() {
it("sum of array", function() {
var result = reduce([1, 2, 3, 4, 5], function(partial, elem) {return partial + elem;}, 0);
expect(result).to.equal(15);
});
it("concats strings", function() {
var result = reduce(["bi", "bim", "bap"], function(partial, elem) {return partial + elem;}, '');
expect(result).to.equal("bibimbap");
});
});
});
describe("More specialized functions", function() {
it("every", function() {
expect(every([1, 2, 3, 4, 5, 6], function(x) {return x < 10;})).to.be(true);
expect(every([1, 2, 3, 4, 25, 6], function(x) {return x < 10;})).to.be(false);
});
it("some", function() {
expect(some([1, 2, 3, 4, 25, 6], function(x) {return x > 10;})).to.be(true);
expect(some([1, 2, 3, 4, 5, 6], function(x) {return x > 10;})).to.be(false);
});
it("unique", function() {
var result = unique(["a", "ab", "a", "b", "ab", "ba"]);
assertArrayEquals(result, ["a", "ab", "b", "ba"]);
});
it("flatten", function() {
var result = flatten([1, 2, [3, [4]]]);
assertArrayEquals(result, [1, 2, 3, 4]);
});
it("contains", function() {
expect(contains([1, 2, 3, 4], 1)).to.be(true);
expect(contains([1, 2, 3, 4], 0)).to.be(false);
});
});
checkIfFunctionalSolution = function(func) {
func = func.toString();
expect(func).to.not.contain("for(");
expect(func).to.not.contain("for (");
expect(func).to.not.contain("if(");
expect(func).to.not.contain("if (");
expect(func).to.not.contain("while(");
expect(func).to.not.contain("while (");
};
describe("Applied problems", function() {
it("Sum an array", function() {
expect(sumOfArray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])).to.be(55);
checkIfFunctionalSolution(sumOfArray);
});
it("All numbers that equal their position in the array", function() {
assertArrayEquals(positionMatch([0, 2, 1, 4, 3, 5]), [0, 5]);
checkIfFunctionalSolution(positionMatch);
});
it("Counting the number of unique first names in this sequence", function() {
var result = uniqueFirstNames(["John Bonham", "Roger Waters", "John Lennon", "Nick Moon", "Roger Daltry", "Nick Cave", "Jimmy Hendrix", "Jimmy Buffet", "Jimmy Page", "Frank Zappa"]);
expect(result).to.equal(5);
checkIfFunctionalSolution(uniqueFirstNames);
});
it("Palindrome numbers", function() {
assertArrayEquals(palindromeNumbers([1, 12, 131, 34534, 34543, 198, 19891]), [1, 131, 34543, 19891]);
checkIfFunctionalSolution(palindromeNumbers);
});
it("Indexing strings by their length", function() {
var result = indexByLength(["hello", "satan", "this", "is", "your", "dog"]);
var answer = [undefined, undefined, ["is"], ["dog"], ["this", "your"], ["hello", "satan"]];
expect(JSON.stringify(result)).to.equal(JSON.stringify(answer));
checkIfFunctionalSolution(indexByLength);
});
it("Returns the names of all people over 65 or with two or more children", function() {
var people = [
{name: "Fred", age: 74, children: ["Bob", "Jane"]},
{name: "Sal", age: 59, children: ["Sam", "Sally"]},
{name: "Rita", age: 64, children: ["Rob"]}, //Rita returning true bc age > 65
{name: "Linda", age: 56, children: ["Rick", "James", "Jose"]}
];
var filtered = olderOrWithChildren(people);
assertArrayEquals(filtered, ["Fred", "Sal", "Linda"]);
checkIfFunctionalSolution(olderOrWithChildren);
});
});