From c96196fa6971ffb93644d06520095b2437ccf1f2 Mon Sep 17 00:00:00 2001 From: Abhisek Pattnaik Date: Wed, 7 Sep 2016 18:45:59 +0530 Subject: [PATCH 1/3] test(challenge): Add tests to symdiff challenge Add tests which would make the solutions easier to create. --- .../advanced-bonfires.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/challenges/01-front-end-development-certification/advanced-bonfires.json b/challenges/01-front-end-development-certification/advanced-bonfires.json index e720ebcf90..c7c90a3693 100644 --- a/challenges/01-front-end-development-certification/advanced-bonfires.json +++ b/challenges/01-front-end-development-certification/advanced-bonfires.json @@ -105,6 +105,10 @@ "tests": [ "assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4]), [3, 4, 5], 'message: sym([1, 2, 3], [5, 2, 1, 4]) should return [3, 4, 5].');", "assert.equal(sym([1, 2, 3], [5, 2, 1, 4]).length, 3, 'message: sym([1, 2, 3], [5, 2, 1, 4]) should contain only three elements.');", + "assert.sameMembers(sym([1, 2, 3, 3], [5, 2, 1, 4]), [3, 4, 5], 'message: sym([1, 2, 3, 3], [5, 2, 1, 4]) should return [3, 4, 5].');", + "assert.equal(sym([1, 2, 3, 3], [5, 2, 1, 4]).length, 3, 'message: sym([1, 2, 3, 3], [5, 2, 1, 4]) should contain only three elements.');", + "assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4, 5]), [3, 4, 5], 'message: sym([1, 2, 3], [5, 2, 1, 4, 5]) should return [3, 4, 5].');", + "assert.equal(sym([1, 2, 3], [5, 2, 1, 4, 5]).length, 3, 'message: sym([1, 2, 3], [5, 2, 1, 4, 5]) should contain only three elements.');", "assert.sameMembers(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]), [1, 4, 5], 'message: sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) should return [1, 4, 5]');", "assert.equal(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]).length, 3, 'message: sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) should contain only three elements.');", "assert.sameMembers(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]), [1, 4, 5], 'message: sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]) should return [1, 4, 5].');", From d64915ff378beca5c29ee38405836f7c0d33c4b2 Mon Sep 17 00:00:00 2001 From: Abhisek Pattnaik Date: Wed, 7 Sep 2016 19:01:21 +0530 Subject: [PATCH 2/3] refactor(challenge): Update symdiff solution Refactor previous solution to use a one liner solution. --- .../advanced-bonfires.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenges/01-front-end-development-certification/advanced-bonfires.json b/challenges/01-front-end-development-certification/advanced-bonfires.json index c7c90a3693..85198e3517 100644 --- a/challenges/01-front-end-development-certification/advanced-bonfires.json +++ b/challenges/01-front-end-development-certification/advanced-bonfires.json @@ -100,7 +100,7 @@ "sym([1, 2, 3], [5, 2, 1, 4]);" ], "solutions": [ - "function sym(args) {\n var index = -1;\n var length = arguments.length;\n var result;\n while (++index < length) {\n var array = arguments[index];\n result = result ? diff(result, array).concat(diff(array, result)) : array;\n }\n return result ? uniq(result) : [];\n}\n\nfunction uniq(arr) {\n var h = Object.create(null);\n var u = [];\n arr.forEach(function(v) {\n if (v in h) return;\n h[v] = true;\n u.push(v);\n });\n return u;\n}\n\nfunction diff(a, b) {\n var h = Object.create(null);\n b.forEach(function(v) {\n h[v] = true; \n });\n return a.filter(function(v) { return !(v in h);});\n}\nsym([1, 2, 3], [5, 2, 1, 4]);\n" + "function sym() {\n var arrays = [].slice.call(arguments);\n return arrays.reduce(function (symDiff, arr) {\n return symDiff.concat(arr).filter(function (val, idx, theArr) {\n return theArr.indexOf(val) === idx \n && (symDiff.indexOf(val) === -1 || arr.indexOf(val) === -1);\n });\n });\n}\nsym([1, 2, 3], [5, 2, 1, 4]);\n" ], "tests": [ "assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4]), [3, 4, 5], 'message: sym([1, 2, 3], [5, 2, 1, 4]) should return [3, 4, 5].');", From f221bb3163ee87f30360e4a643f82ac476172dfd Mon Sep 17 00:00:00 2001 From: Abhisek Pattnaik Date: Wed, 7 Sep 2016 20:10:07 +0530 Subject: [PATCH 3/3] docs(challenge): Add instruction to symdiff challenge Add instructions about the expected return value. --- .../advanced-bonfires.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenges/01-front-end-development-certification/advanced-bonfires.json b/challenges/01-front-end-development-certification/advanced-bonfires.json index 85198e3517..e14aa63db9 100644 --- a/challenges/01-front-end-development-certification/advanced-bonfires.json +++ b/challenges/01-front-end-development-certification/advanced-bonfires.json @@ -89,7 +89,7 @@ "title": "Symmetric Difference", "description": [ "Create a function that takes two or more arrays and returns an array of the symmetric difference ( or ) of the provided arrays.", - "Given two sets (for example set A = {1, 2, 3} and set B = {2, 3, 4}), the mathematical term \"symmetric difference\" of two sets is the set of elements which are in either of the two sets, but not in both (A △ B = C = {1, 4}). For every additional symmetric difference you take (say on a set D = {2, 3}), you should get the set with elements which are in either of the two the sets but not both (C △ D = {1, 4} △ {2, 3} = {1, 2, 3, 4}).", + "Given two sets (for example set A = {1, 2, 3} and set B = {2, 3, 4}), the mathematical term \"symmetric difference\" of two sets is the set of elements which are in either of the two sets, but not in both (A △ B = C = {1, 4}). For every additional symmetric difference you take (say on a set D = {2, 3}), you should get the set with elements which are in either of the two the sets but not both (C △ D = {1, 4} △ {2, 3} = {1, 2, 3, 4}). The resulting array must contain only unique values (no duplicates).", "Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code." ], "challengeSeed": [