Skip to content

Commit 267e825

Browse files
committed
Made the test cases easier to read.
1 parent 8e86159 commit 267e825

File tree

1 file changed

+14
-49
lines changed
  • 1-js/06-advanced-functions/06-function-object/5-sum-many-brackets/_js.view

1 file changed

+14
-49
lines changed
Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,19 @@
1-
/*
2-
* Instead of hard-coding a bunch of separate test cases and their solutions,
3-
* this code takes several argument lists, and compares the chain call answer
4-
* you made for "sum()" to a more traditional way of adding the contents of an
5-
* array.
6-
*/
7-
81
describe("sum", function(){
2+
3+
it("sum(1)(2) == 3", function(){
4+
assert.equal(3, sum(1)(2));
5+
});
96

10-
let testArgumentLists = [
11-
[1, 2],
12-
[5, -1, 2],
13-
[6, -1, -2, -3],
14-
[0, 1, 2, 3, 4, 5],
15-
];
7+
it("sum(5)(-1)(2) == 6", function(){
8+
assert.equal(6, sum(5)(-1)(2));
9+
});
10+
11+
it("sum(6)(-1)(-2)(-3) == 0", function(){
12+
assert.equal(0, sum(6)(-1)(-2)(-3));
13+
});
1614

17-
for (let argumentList of testArgumentLists){
18-
it(makeTestCaseName(argumentList), function(){
19-
assert.equal(traditionalSum(argumentList), chainCallSum(argumentList));
20-
});
21-
}
15+
it("sum(0)(1)(2)(3)(4)(5) == 15", function(){
16+
assert.equal(15, sum(0)(1)(2)(3)(4)(5));
17+
});
2218
});
2319

24-
function traditionalSum(arr){
25-
return arr.reduce(
26-
function(accumulator, item){
27-
return accumulator + item;
28-
}, 0);
29-
}
30-
31-
function makeTestCaseName(arr){
32-
return `sum${makeChainCallString(arr)} == ${traditionalSum(arr)}`;
33-
}
34-
35-
/* Takes the elements of an array, and puts them in a string where each element
36-
* is enclosed in parentheses. Example:
37-
*
38-
* (["a", "b", "c"]) => "(a)(b)(c)"
39-
*
40-
* Useful for making pretty test case names.
41-
*/
42-
function makeChainCallString(arr){
43-
return arr.reduce(
44-
function(accumulator, item){
45-
return `${accumulator}(${item})`;
46-
}, "");
47-
}
48-
49-
function chainCallSum(arr){
50-
return arr.reduce(
51-
function(accumulator, item){
52-
return accumulator(item);
53-
}, sum);
54-
}

0 commit comments

Comments
 (0)