diff --git a/CONTRACT.md b/CONTRACT.md index 5b01d97..7b1f973 100644 --- a/CONTRACT.md +++ b/CONTRACT.md @@ -38,21 +38,21 @@ Complete **ONLY** the [Classic](https://github.com/GuildCrafts/core-algorithms/b - [x] Tests for `fibonacci()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs. - [x] `collatzConjecture()` algorithm is implemented according to the description in [algorithms.md][algorithms-list]. - [x] Tests for `collatzConjecture()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs. -- [ ] `setUnion()` algorithm is implemented according to the description in [algorithms.md][algorithms-list]. -- [ ] Tests for `setUnion()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs. -- [ ] `setIntersection()` algorithm is implemented according to the description in [algorithms.md][algorithms-list]. -- [ ] Tests for `setIntersection()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs. -- [ ] `setComplement()` algorithm is implemented according to the description in [algorithms.md][algorithms-list]. -- [ ] Tests for `setComplement()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs. -- [ ] `setSymmetricDifference()` algorithm is implemented according to the description in [algorithms.md][algorithms-list]. -- [ ] Tests for `setSymmetricDifference()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs. +- [x] `setUnion()` algorithm is implemented according to the description in [algorithms.md][algorithms-list]. +- [x] Tests for `setUnion()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs. +- [x] `setIntersection()` algorithm is implemented according to the description in [algorithms.md][algorithms-list]. +- [x] Tests for `setIntersection()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs. +- [x] `setComplement()` algorithm is implemented according to the description in [algorithms.md][algorithms-list]. +- [x] Tests for `setComplement()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs. +- [x] `setSymmetricDifference()` algorithm is implemented according to the description in [algorithms.md][algorithms-list]. +- [x] Tests for `setSymmetricDifference()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs. - [ ] Repository includes a README file with basic installation and setup instructions. -- [ ] All dependencies are properly declared in `package.json`. -- [ ] All major features are added via pull requests with a clear description and concise commit messages. +- [x] All dependencies are properly declared in `package.json`. +- [x] All major features are added via pull requests with a clear description and concise commit messages. - [ ] Code uses a linter and there are no linting errors. -- [ ] Variables, functions, files, etc. have appropriate and meaningful names. +- [x] Variables, functions, files, etc. have appropriate and meaningful names. - [ ] Functions are small and serve a single purpose. -- [ ] The artifact produced is properly licensed, preferably with the [MIT license][mit-license]. +- [x] The artifact produced is properly licensed, preferably with the [MIT license][mit-license]. ### Stretch diff --git a/src/factorial.js b/src/factorial.js index cb8372f..3b13a65 100644 --- a/src/factorial.js +++ b/src/factorial.js @@ -6,11 +6,19 @@ // // => 120 export default function factorial(num) { - var factorialElements = [num] - for (var i = num; i > 1; i--) { - factorialElements.push(i - 1) + var accumulator = num + for (var i = num; i > 1; i--) { + accumulator = accumulator * (i - 1) + } + return accumulator } - return factorialElements.reduce(function(a,b){ - return a*b - }, 1) -} + +//Other way: +// var factorialElements = [num] +// for (var i = num; i > 1; i--) { +// factorialElements.push(i - 1) +// } +// return factorialElements.reduce(function(a,b){ +// return a*b +// }, 1) +// } diff --git a/src/setComplement.js b/src/setComplement.js new file mode 100644 index 0000000..208675c --- /dev/null +++ b/src/setComplement.js @@ -0,0 +1,22 @@ +// setComplement +// +// Return the complement of two sets. +// +// const a = [1, 2, 3, 4] +// const b = [2, 4, 6, 8] +// setComplement(a, b) +// // => [6, 8] + + +export default function setComplement(setA, setB) { + if ( !Array.isArray(setA) || !Array.isArray(setB) ) { + throw new Error('Invalid input format. Expected two arrays') + } + let complementArray = [] + for(let element of setB) { + if(!setA.includes(element)) { + complementArray.push(element) + } + } + return complementArray + } diff --git a/src/setIntersection.js b/src/setIntersection.js new file mode 100644 index 0000000..a5a1118 --- /dev/null +++ b/src/setIntersection.js @@ -0,0 +1,23 @@ +// setIntersection +// +// Return the intersection of two sets. +// +// const a = [1, 2, 3, 4] +// const b = [2, 4, 6, 8] +// setIntersection(a, b) +// // => [2, 4] + + +export default function setIntersection(a, b){ + if ( !Array.isArray(a) || !Array.isArray(b) ) { + throw new Error('Invalid input format. Expected two arrays') + } + let concatArr = a.concat(b) + let sortedArr = concatArr.sort() + + function isDuplicate(value, index, array){ + return array.indexOf(value) !== index + } + let filteredArr = sortedArr.filter(isDuplicate) + return filteredArr +} diff --git a/src/setSymmetricDifference.js b/src/setSymmetricDifference.js new file mode 100644 index 0000000..945cec0 --- /dev/null +++ b/src/setSymmetricDifference.js @@ -0,0 +1,30 @@ +// setSymmetricDifference +// +// Return the symmetric difference of two sets. +// +// const a = [1, 2, 3, 4] +// const b = [2, 4, 6, 8] +// setSymmetricDifference(a, b) +// // => [1, 3, 6, 8] + + +export default function setSymmetricDifference(setA, setB) { + if ( !Array.isArray(setA) || !Array.isArray(setB) ) { + throw new Error('Invalid input format. Expected two arrays') + } + let complementArray = [] + + for(let element of setB) { + if(!setA.includes(element)) { + complementArray.push(element) + } + } + for(let element of setA){ + if(!setB.includes(element)) { + complementArray.push(element) + } + } + return complementArray.sort() + } + +// diff --git a/src/setUnion.js b/src/setUnion.js new file mode 100644 index 0000000..79f5b94 --- /dev/null +++ b/src/setUnion.js @@ -0,0 +1,22 @@ +// setUnion +// +// Return the union of two sets. +// +// const a = [1, 2, 3, 4] +// const b = [2, 4, 6, 8] +// setUnion(a, b) +// // => [1, 2, 3, 4, 6, 8] + +export default function setUnion(a, b){ + if (!Array.isArray(a) || !Array.isArray(b)){ + throw new Error('Invalid input format. Expected two arrays') + } + let concatArr = a.concat(b) + let sortedArr = concatArr.sort() + + function isUnique(value, index, array){ + return array.indexOf(value) === index + } + let filteredArr = sortedArr.filter(isUnique) + return filteredArr +} diff --git a/test/setComplement_test.js b/test/setComplement_test.js new file mode 100644 index 0000000..a8159ec --- /dev/null +++ b/test/setComplement_test.js @@ -0,0 +1,27 @@ +import { expect } from 'chai' +import setComplement from '../src/setComplement' + +describe('setComplement', function(){ + + it('should be a function', function() { + expect(setComplement).to.be.a('function') + }) + + it('should show the items from set B that are not present in set A', function() { + const setA = ['fluffy', 'cat', 'scratchy'] + const setB = ['scratchy', 'fluffy', 'dog'] + expect(setComplement(setA, setB)).to.eql(['dog']) + }) + + it('should show empty set if no items in B are not in A', function() { + const setA = ['fluffy', 'cat', 'scratchy'] + const setB = ['scratchy', 'fluffy'] + expect(setComplement(setA, setB)).to.eql([]) + }) + + it('throws an error when given invalid inputs', function() { + expect(function() {setComplement({}, false)}).to.throw( + 'Invalid input format. Expected two arrays' + ) + }) +}) diff --git a/test/setIntersection_test.js b/test/setIntersection_test.js new file mode 100644 index 0000000..871a6a0 --- /dev/null +++ b/test/setIntersection_test.js @@ -0,0 +1,20 @@ +import { expect } from 'chai' +import setIntersection from '../src/setIntersection' + +describe('setIntersection', function(){ + + it('should be a function', function() { + expect(setIntersection).to.be.a('function') + }) + + it('returns the intersection of two sets', function () { + expect(setIntersection([1, 2, 3, 4],[2, 4, 6, 8])).to.eql([2,4]) + expect(setIntersection([82,90,1], [1, 2, 90])).to.eql([1,90]) + }) + + it('throws an error when given invalid inputs', function() { + expect(function() {setIntersection('string')}).to.throw( + 'Invalid input format. Expected two arrays' + ) + }) +}) diff --git a/test/setSymmetricDifference_test.js b/test/setSymmetricDifference_test.js new file mode 100644 index 0000000..11f9554 --- /dev/null +++ b/test/setSymmetricDifference_test.js @@ -0,0 +1,20 @@ +import { expect } from 'chai' +import setSymmetricDifference from '../src/setSymmetricDifference' + + + +describe('setSymmetricDifference', function() { + + it('should be a function', function() { + expect(setSymmetricDifference).to.be.a('function') + }) + + it('returns the symmetric difference of two sets', function() { + expect(setSymmetricDifference([1, 2, 3, 4], [2, 4, 6, 8])).to.eql([1, 3, 6, 8]) + }) + + it('throws an error when given invalid inputs', function() { + expect(function() {setSymmetricDifference('string', [1,2])}). + to.throw('Invalid input format. Expected two arrays') + }) +}) diff --git a/test/setUnion_test.js b/test/setUnion_test.js new file mode 100644 index 0000000..154031e --- /dev/null +++ b/test/setUnion_test.js @@ -0,0 +1,20 @@ +import { expect } from 'chai' +import setUnion from '../src/setUnion' + +describe('setUnion', function(){ + + it('should be a function', function() { + expect(setUnion).to.be.a('function') + }) + + it('returns the union of two sets', function() { + expect(setUnion([1,2,3,4],[2,4,6,8])).to.eql([1, 2, 3, 4, 6, 8]) + expect(setUnion([2,3,4,4],[1,2,3,4])).to.eql([1,2,3,4]) + }) + + it('throws an error when given invalid inputs', function() { + expect(function() {setUnion('string')}).to.throw( + 'Invalid input format. Expected two arrays' + ) + }) +})