-
Notifications
You must be signed in to change notification settings - Fork 140
/
recursionSpec.js
171 lines (165 loc) · 3.91 KB
/
recursionSpec.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
var expect = chai.expect
describe("#productOfArray", function(){
it("returns the product of all the numbers in an array", function(){
expect(productOfArray([1,2,3])).to.equal(6)
expect(productOfArray([0,1,2,3])).to.equal(0)
expect(productOfArray([1,-2,3])).to.equal(-6)
});
});
describe("#collectStrings", function(){
it("returns all the strings in a nested object", function(){
var obj = {
stuff: "foo",
data: {
val: {
thing: {
info: "bar",
moreInfo: {
evenMoreInfo: {
weMadeIt: "baz"
}
}
}
}
}
}
expect(collectStrings(obj)).to.deep.equal(["foo", "bar", "baz"])
});
});
describe("#contains", function(){
it("should return true if a value is found in an object", function(){
var nestedObject = {
data: {
info: {
stuff: {
thing: {
moreStuff: {
magicNumber: 44
}
}
}
}
}
}
expect(contains(nestedObject, 44)).to.equal(true) // true)
});
});
describe("#search", function(){
it("should find the index of a value in an array", function(){
expect(search([1,2,3,4],4)).to.equal(3)
expect(search([1,2],1)).to.equal(0)
expect(search([1,2,3,4,5,6,7],6)).to.equal(5)
});
it("should return -1 if the value is not found", function(){
expect(search([1,2,3,4]),0).to.equal(-1)
expect(search([1,2]),11).to.equal(-1)
});
});
describe("#binarySearch", function(){
it("should find the index of a value in an array", function(){
expect(binarySearch([1,2,3,4],4)).to.equal(3)
expect(binarySearch([1,2],1)).to.equal(0)
expect(binarySearch([1,2,3,4,5,6,7],6)).to.equal(5)
});
it("should return -1 if the value is not found", function(){
expect(binarySearch([1,2,3,4],0)).to.equal(-1)
expect(binarySearch([1,2],11)).to.equal(-1)
});
});
describe("#stringifyNumbers", function(){
it("should not modify an existing object", function(){
var obj = {
num: 1,
test: [],
data: {
val: 4,
info: {
isRight: true,
random: 66
}
}
}
var answer = {
num: "1",
test: [],
data: {
val: "4",
info: {
isRight: true,
random: "66"
}
}
}
stringifyNumbers(obj)
expect(obj.num).to.be.a('number')
expect(obj.num).to.equal(1)
});
it("should convert all numbers in a nested object to strings", function(){
var obj = {
num: 1,
test: [],
data: {
val: 4,
info: {
isRight: true,
random: 66
}
}
}
var answer = {
num: "1",
test: [],
data: {
val: "4",
info: {
isRight: true,
random: "66"
}
}
}
expect(stringifyNumbers(obj)).to.deep.equal(answer)
});
it("should convert all numbers in a highly nested object to strings", function(){
var obj = {
num: 1,
test: [],
data: {
val: 4,
info: {
isRight: true,
random: 66,
nested: {
inner: {
nestedInner: {
another: {
num: 10
}
}
}
}
}
}
}
var answer = {
num: '1',
test: [],
data: {
val: '4',
info: {
isRight: true,
random: '66',
nested: {
inner: {
nestedInner: {
another: {
num: '10'
}
}
}
}
}
}
}
expect(stringifyNumbers(obj)).to.deep.equal(answer)
});
});