-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreduce.html
More file actions
183 lines (168 loc) · 6.33 KB
/
reduce.html
File metadata and controls
183 lines (168 loc) · 6.33 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
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
172
173
174
175
176
177
178
179
180
181
182
183
<script src="../tinytest.js"></script>
<script>
// Prototype function:
//
// Reduce(array, callback, initialValue) {
// var previousValue = initialValue;
// for(var i = 0; i < array.length; i++){
// previousValue = callback(previousValue, array[i], i, array);
// }
// return previousValue;
// }
//
// Function signature:
// reduce(array,callback[, initialValue])
// Callback parameters:
// - previousValue
// - currentValue (i.e., array[i])
// - currentIndex (i.e., i)
// - array
// Return:
// - Returns a single value
// Requirements
// function map(originalArray, callback, optionalThisObject) {
// var newCallback = callback;
// var returnedArray = [];
// var originalLength = originalArray.length;
// if(optionalThisObject) {
// newCallback = newCallback.bind(optionalThisObject);
// }
// for(var i = 0; i < originalLength; i++) {
// if(i in originalArray) {
// returnedArray[i] = newCallback(originalArray[i], i, originalArray);
// }
// }
// return returnedArray;
// };
function reduce(array, callback, initialValue) {
var startingIndex = 0;
var previousValue;
var arrayLength = array.length;
var elements = Object.keys(array);
if (arguments.length < 3) {
if(elements.length === 0) {
throw new TypeError('Reduce of empty array with no initial value');
}
if(elements.length == 1) {
var onlyIndex = elements[0];
var onlyElement = array[onlyIndex];
return onlyElement;
}
while (startingIndex in array === false && startingIndex < arrayLength) {
startingIndex++;
}
previousValue = array[startingIndex];
startingIndex++;
} else {
if (elements.length === 0) {
return initialValue;
}
previousValue = initialValue;
}
for (var i = startingIndex; i < arrayLength; i++) {
if(i in array) {
previousValue = callback(previousValue, array[i], i, array);
}
}
return previousValue;
};
tests({
'If initial value it should run the callback the array.length times': function () {
var originalArray = [1, 2];
var runCount = 0;
reduce(originalArray, function () {
runCount++;
}, 0);
eq(runCount, 2);
},
'If no initial value it should run the callback the (array.length - 1) times': function () {
var originalArray = [1, 2];
var runCount = 0;
reduce(originalArray, function () {
runCount++;
});
eq(runCount, 1);
},
'If Initial Value Provided previousValue should be equal to initialValue': function () {
reduce([1], function (previousValue) {
eq(previousValue, 0);
}, 0);
},
'If Initial Value Provided currentValue should be equal to the first item in the array': function () {
reduce([1], function (previousValue, currentValue) {
eq(currentValue, 1);
}, 0);
},
'If Initial Value Provided callback should execute starting at array[0]': function () {
reduce([1], function (previousValue, currentValue, currentIndex) {
eq(currentIndex, 0);
}, 0);
},
'If Initial Value Provided and array is empty, initialValue should be returned without executing callback': function () {
var runCount = 0;
var reduceResult = reduce([], function () {
runCount++;
}, 0);
eq(reduceResult, 0);
eq(runCount, 0);
},
'If no initialValue provided, previousValue should be equal to the first item in the array': function () {
reduce([1, 2], function (previousValue) {
eq(previousValue, 1);
});
},
'If no initialValue provided, currentValue should be equal to the second item in the array': function () {
reduce([1, 2], function (previousValue, currentValue) {
eq(currentValue, 2);
});
},
'If no initialValue provided, callback should execute starting at array[1]': function () {
reduce([1, 2], function (previousValue, currentValue, currentIndex) {
eq(currentIndex, 1);
});
},
'If no initialValue provided, if array has only 1 element, the only value should be returned without executing callback': function () {
var runCount = 0;
var reduceResult = reduce([1], function () {
runCount++;
});
eq(reduceResult, 1);
eq(runCount, 0);
},
'It should actually reduce': function () {
var sum = reduce([1, 2, 3], function (a, b) {
return a + b;
}, 0);
eq(sum, 6);
},
'If initial value, the callback should not be run on holes': function () {
//debugger;
var sum = reduce([1, ,2, , 3], function (a, b) {
return a + b;
}, 0);
eq(sum, 6);
},
'If no initial value, the callback should not be run on holes': function () {
//debugger;
var sum = reduce([,1, ,2, , 3], function (a, b) {
return a + b;
});
eq(sum, 6);
},
'If no initialValue provided and array is empty, TypeError should be thrown': function () {
var isTypeError = false;
try {
reduce([],function(){});
} catch(e) {
isTypeError = e instanceof TypeError;
}
eq(isTypeError,true);
},
'It should pass in the original array as the fourth argument to the callback': function () {
var testArray = [1,2];
reduce(testArray, function(previousValue, currentValue, currentIndex,targetArray){
eq(testArray,targetArray);
},0)
}
});
</script>