From f20fb43f70153e0879274f57f3c014141e0fb488 Mon Sep 17 00:00:00 2001 From: Logan Schelly Date: Wed, 10 Jun 2020 15:28:20 -0600 Subject: [PATCH 1/3] Add test case/sandbox for 1-js/06-advanced-functions/06-function-object/5-sum-many-brackets I used the solution and examples already in the article. I chose to make the test cases use Array.reduce() to compute the sum, instead of hard-coding the answer myself. Not sure how good of practice that is. --- .../5-sum-many-brackets/_js.view/solution.js | 15 +++++ .../5-sum-many-brackets/_js.view/source.js | 13 +++++ .../5-sum-many-brackets/_js.view/test.js | 57 +++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 1-js/06-advanced-functions/06-function-object/5-sum-many-brackets/_js.view/solution.js create mode 100644 1-js/06-advanced-functions/06-function-object/5-sum-many-brackets/_js.view/source.js create mode 100644 1-js/06-advanced-functions/06-function-object/5-sum-many-brackets/_js.view/test.js diff --git a/1-js/06-advanced-functions/06-function-object/5-sum-many-brackets/_js.view/solution.js b/1-js/06-advanced-functions/06-function-object/5-sum-many-brackets/_js.view/solution.js new file mode 100644 index 00000000..c7d7d734 --- /dev/null +++ b/1-js/06-advanced-functions/06-function-object/5-sum-many-brackets/_js.view/solution.js @@ -0,0 +1,15 @@ +function sum(a) { + + let currentSum = a; + + function f(b) { + currentSum += b; + return f; + } + + f.toString = function() { + return currentSum; + }; + + return f; +} diff --git a/1-js/06-advanced-functions/06-function-object/5-sum-many-brackets/_js.view/source.js b/1-js/06-advanced-functions/06-function-object/5-sum-many-brackets/_js.view/source.js new file mode 100644 index 00000000..09037734 --- /dev/null +++ b/1-js/06-advanced-functions/06-function-object/5-sum-many-brackets/_js.view/source.js @@ -0,0 +1,13 @@ +function sum(a){ + + // Your code goes here. + +} + +/* +sum(1)(2) == 3; // 1 + 2 +sum(1)(2)(3) == 6; // 1 + 2 + 3 +sum(5)(-1)(2) == 6 +sum(6)(-1)(-2)(-3) == 0 +sum(0)(1)(2)(3)(4)(5) == 15 +*/ diff --git a/1-js/06-advanced-functions/06-function-object/5-sum-many-brackets/_js.view/test.js b/1-js/06-advanced-functions/06-function-object/5-sum-many-brackets/_js.view/test.js new file mode 100644 index 00000000..1d3e1ebe --- /dev/null +++ b/1-js/06-advanced-functions/06-function-object/5-sum-many-brackets/_js.view/test.js @@ -0,0 +1,57 @@ +/* + * Instead of hard-coding a bunch of separate test cases and their solutions, + * this code takes several argument lists, and compares the chain call answer + * you made for "sum()" to a more traditional way of adding the contents of an + * array. + */ + +describe("sum", function(){ + + let testArgumentLists = [ + [1, 2], + [5, -1, 2], + [6, -1, -2, -3], + [0, 1, 2, 3, 4, 5], + ]; + + for (let argumentList of testArgumentLists){ + + it(makeTestCaseName(argumentList), function(){ + assert.equal(traditionalSum(argumentList), chainCallSum(argumentList)); + }); + } + +}); + + +function traditionalSum(arr){ + return arr.reduce( + function(accumulator, item){ + return accumulator + item; + }, 0); +} + +function makeTestCaseName(arr){ + return `sum${makeChainCallString(arr)} == ${traditionalSum(arr)}`; +} + +/* Takes the elements of an array, and puts them in a string where each element + * is enclosed in parentheses. Example: + * + * (["a", "b", "c"]) => "(a)(b)(c)" + * + * Useful for making pretty test case names. + */ +function makeChainCallString(arr){ + return arr.reduce( + function(accumulator, item){ + return `${accumulator}(${item})`; + }, ""); +} + +function chainCallSum(arr){ + return arr.reduce( + function(accumulator, item){ + return accumulator(item); + }, sum); +} From e5fe4a08bdd3b9c985d4f4a4cedc2e3caf9683eb Mon Sep 17 00:00:00 2001 From: Logan Schelly Date: Wed, 10 Jun 2020 15:38:18 -0600 Subject: [PATCH 2/3] Got rid of excessive newlines in task/sum-many-brackets sandbox code. --- .../5-sum-many-brackets/_js.view/source.js | 1 - .../06-function-object/5-sum-many-brackets/_js.view/test.js | 5 +---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/1-js/06-advanced-functions/06-function-object/5-sum-many-brackets/_js.view/source.js b/1-js/06-advanced-functions/06-function-object/5-sum-many-brackets/_js.view/source.js index 09037734..f10dca5d 100644 --- a/1-js/06-advanced-functions/06-function-object/5-sum-many-brackets/_js.view/source.js +++ b/1-js/06-advanced-functions/06-function-object/5-sum-many-brackets/_js.view/source.js @@ -1,5 +1,4 @@ function sum(a){ - // Your code goes here. } diff --git a/1-js/06-advanced-functions/06-function-object/5-sum-many-brackets/_js.view/test.js b/1-js/06-advanced-functions/06-function-object/5-sum-many-brackets/_js.view/test.js index 1d3e1ebe..d1f856e8 100644 --- a/1-js/06-advanced-functions/06-function-object/5-sum-many-brackets/_js.view/test.js +++ b/1-js/06-advanced-functions/06-function-object/5-sum-many-brackets/_js.view/test.js @@ -14,16 +14,13 @@ describe("sum", function(){ [0, 1, 2, 3, 4, 5], ]; - for (let argumentList of testArgumentLists){ - + for (let argumentList of testArgumentLists){ it(makeTestCaseName(argumentList), function(){ assert.equal(traditionalSum(argumentList), chainCallSum(argumentList)); }); } - }); - function traditionalSum(arr){ return arr.reduce( function(accumulator, item){ From 267e8255608520a2e6351cc29f988dd11b87566b Mon Sep 17 00:00:00 2001 From: Logan Schelly Date: Mon, 15 Jun 2020 15:13:01 -0600 Subject: [PATCH 3/3] Made the test cases easier to read. --- .../5-sum-many-brackets/_js.view/test.js | 63 +++++-------------- 1 file changed, 14 insertions(+), 49 deletions(-) diff --git a/1-js/06-advanced-functions/06-function-object/5-sum-many-brackets/_js.view/test.js b/1-js/06-advanced-functions/06-function-object/5-sum-many-brackets/_js.view/test.js index d1f856e8..ed567d33 100644 --- a/1-js/06-advanced-functions/06-function-object/5-sum-many-brackets/_js.view/test.js +++ b/1-js/06-advanced-functions/06-function-object/5-sum-many-brackets/_js.view/test.js @@ -1,54 +1,19 @@ -/* - * Instead of hard-coding a bunch of separate test cases and their solutions, - * this code takes several argument lists, and compares the chain call answer - * you made for "sum()" to a more traditional way of adding the contents of an - * array. - */ - describe("sum", function(){ + + it("sum(1)(2) == 3", function(){ + assert.equal(3, sum(1)(2)); + }); - let testArgumentLists = [ - [1, 2], - [5, -1, 2], - [6, -1, -2, -3], - [0, 1, 2, 3, 4, 5], - ]; + it("sum(5)(-1)(2) == 6", function(){ + assert.equal(6, sum(5)(-1)(2)); + }); + + it("sum(6)(-1)(-2)(-3) == 0", function(){ + assert.equal(0, sum(6)(-1)(-2)(-3)); + }); - for (let argumentList of testArgumentLists){ - it(makeTestCaseName(argumentList), function(){ - assert.equal(traditionalSum(argumentList), chainCallSum(argumentList)); - }); - } + it("sum(0)(1)(2)(3)(4)(5) == 15", function(){ + assert.equal(15, sum(0)(1)(2)(3)(4)(5)); + }); }); -function traditionalSum(arr){ - return arr.reduce( - function(accumulator, item){ - return accumulator + item; - }, 0); -} - -function makeTestCaseName(arr){ - return `sum${makeChainCallString(arr)} == ${traditionalSum(arr)}`; -} - -/* Takes the elements of an array, and puts them in a string where each element - * is enclosed in parentheses. Example: - * - * (["a", "b", "c"]) => "(a)(b)(c)" - * - * Useful for making pretty test case names. - */ -function makeChainCallString(arr){ - return arr.reduce( - function(accumulator, item){ - return `${accumulator}(${item})`; - }, ""); -} - -function chainCallSum(arr){ - return arr.reduce( - function(accumulator, item){ - return accumulator(item); - }, sum); -}