diff --git a/.babelrc b/.babelrc index 2f01e1d..002b4aa 100644 --- a/.babelrc +++ b/.babelrc @@ -1,3 +1,3 @@ { "presets": ["env"] -} \ No newline at end of file +} diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..8b2834c --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,29 @@ +{ + "env": { + "es6": true, + "node": true, + "mocha": true + }, + "extends": "eslint:recommended", + "parserOptions": { + "sourceType": "module" + }, + "rules": { + "indent": [ + "error", + 2 + ], + "linebreak-style": [ + "error", + "unix" + ], + "quotes": [ + "error", + "single" + ], + "semi": [ + "error", + "never" + ] + } +} diff --git a/CONTRACT.md b/CONTRACT.md new file mode 100644 index 0000000..d228caf --- /dev/null +++ b/CONTRACT.md @@ -0,0 +1,69 @@ +#158 Core Algorithms [Classic, Numeric, and Set] +#filthy-gerbil +Somaya Bounouar +http://jsdev.learnersguild.org/goals/158-Core_Algorithms-Classic_Numeric_and_Set.html + + +# Core Algorithms [Classic, Numeric, and Set] + +## Challenge Rating + +This goal will likely be within your ZPD if you... + +- Can write programs and tests using Node.js and npm +- Can use problem decomposition to split difficult problems into smaller parts +- Have used pseudocode as an intermediary step in writing a program +- Are interested in common computer science algorithms +- Are interested in critical thinking and problem solving + +## Description + +This is a sub-goal of [Core Algorithms][core-algos]. Reference that goal for more description, context, and resources. + +Complete **ONLY** the [Classic](https://github.com/GuildCrafts/core-algorithms/blob/master/algorithms.md#classic), [Numeric](https://github.com/GuildCrafts/core-algorithms/blob/master/algorithms.md#numeric), and [Set Operations](https://github.com/GuildCrafts/core-algorithms/blob/master/algorithms.md#set-operations) algorithms, not the Sorting/Searching or Graphing/Geometry. That's a [separate goal][core-algos-adv]. :) + +## Specifications + +- [x] Artifact produced is a fork of the [core-algorithms][core-algorithms] repo. +- [x] Can run all tests with `npm test`. +- [x] `makeChange()` algorithm is implemented according to the description in [algorithms.md][algorithms-list]. +- [x] Tests for `makeChange()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs. +- [x] `fizzBuzz()` algorithm is implemented according to the description in [algorithms.md][algorithms-list]. +- [x] Tests for `fizzBuzz()` exist. +- [x] `isPalindrome()` algorithm is implemented according to the description in [algorithms.md][algorithms-list]. +- [x] Tests for `isPalindrome()` exist with at least 2 unit tests using valid inputs. +- [x] `factorial()` algorithm is implemented according to the description in [algorithms.md][algorithms-list]. +- [x] Tests for `factorial()` exist with at least 2 unit tests using valid inputs. +- [x] `fibonacci()` algorithm is implemented according to the description in [algorithms.md][algorithms-list]. +- [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. +- [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. +- [x] Repository includes a README file with basic installation and setup instructions. +- [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. +- [x] Code uses a linter and there are no linting errors. +- [x] Variables, functions, files, etc. have appropriate and meaningful names. +- [x] Functions are small and serve a single purpose. +- [x] The artifact produced is properly licensed, preferably with the [MIT license][mit-license]. + +### Stretch + +Pick a _different_ programming language from JavaScript (e.g. Ruby, Swift, Python, C, Java...) and write tests & implementations for each. + +- [ ] Can run all non-JavaScript tests with a single command. +- [ ] For each algorithm identified above, there exists: + - [ ] a test file with multiple unit tests for each algorithm _in a language other than JavaScript_. + - [ ] an implementation file with a correct implementation of the algorithm _in a language other than JavaScript_. + + +[core-algos]: {{ site.url }}{% link _goals/123-Core_Algorithms.md %} +[core-algos-adv]: {{ site.url }}{% link _goals/159-Core_Algorithms-Sorting_Searching_Graphing_and_Geometry.md %} +[algorithms-list]: https://github.com/GuildCrafts/core-algorithms/blob/master/algorithms.md diff --git a/README.md b/README.md index 783e9c5..18769e8 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,9 @@ # Core Algorithms +#158 Core Algorithms [Classic, Numeric, and Set] +#filthy-gerbil +Somaya Bounouar +http://jsdev.learnersguild.org/goals/158-Core_Algorithms-Classic_Numeric_and_Set.html + Tests and implementations for algorithms commonly used in job interviews. See the full list in the [algorithms.md](algorithms.md) file. @@ -6,8 +11,10 @@ Base repository for the [Core Algorithms](http://jsdev.learnersguild.org/goals/1 ## Installation and Setup -_Fill this out_ +Use the "npm i" command to install the dependencies. -## Usage and Examples +Use the "npm run test" command to run the tests in the terminal. -_...and this_ +Use the "npm install --save-dev eslint" command to install the linter ESlint in the JSON file. + Use the "eslint --init" command to configure ESlint. + Use the "eslint [filename or folder name]" command to lint through your files. diff --git a/package.json b/package.json index 71f93c5..809abf3 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "babel-preset-env": "^1.1.4", "babel-register": "^6.18.0", "chai": "~1.8.0", + "eslint": "^3.19.0", "mocha": "2.0.1" }, "scripts": { diff --git a/src/collatzConjecture.js b/src/collatzConjecture.js new file mode 100644 index 0000000..e4c8348 --- /dev/null +++ b/src/collatzConjecture.js @@ -0,0 +1,34 @@ +// collatzConjecture +// +// Return the Collatz sequence for a given number. +// +// The Collatz sequence for any positive integer n is defined as follows: +// +// If n is even, divide it by 2 to get n / 2. If n is odd, multiply it by 3 and add 1 to obtain 3n + 1. Repeat the process until you reach 1. +// collatzConjecture(1) +// // => [1] +// +// collatzConjecture(7) +// // => [7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1] + + +let collatzConjecture = num => { + if (!Number.isInteger(num) || num < 0) { + throw new Error('Invalid input format. Expected a positive integer') + } + let collatzArr = [num] + let result = num + + while (result > 1) { + if (result % 2 === 0) { + result = result / 2 + collatzArr.push(result) + } else { + result = (result * 3) + 1 + collatzArr.push(result) + } + } + return collatzArr +} + +module.exports = { collatzConjecture } diff --git a/src/factorial.js b/src/factorial.js new file mode 100644 index 0000000..867ed5e --- /dev/null +++ b/src/factorial.js @@ -0,0 +1,26 @@ +// factorial +// +// Return the factorial of a number. +// +// factorial(5) +// // => 120 + +let factorial = num => { + let accumulator = num + for (let i = num; i > 1; i--) { + accumulator = accumulator * (i - 1) + } + return accumulator +} + +module.exports = { factorial } + +//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/fibonacci.js b/src/fibonacci.js new file mode 100644 index 0000000..8c17b2f --- /dev/null +++ b/src/fibonacci.js @@ -0,0 +1,26 @@ +// fibonacci +// +// Return an array of Fibonacci numbers to the nth position. +// +// fibonacci(10) +// // => [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] + +let fibonacci = num => { + if (typeof num !== 'number') { + throw new Error('Invalid input format. Expected a number') + } + let first = 0 + let second = 1 + let result + let fibonacciArr = [0] + + for (let i = 1; i < num; i++) { + result = first + second + first = second + second = result + fibonacciArr.push(first) + } + return fibonacciArr +} + +module.exports = { fibonacci } diff --git a/src/fizzBuzz.js b/src/fizzBuzz.js new file mode 100644 index 0000000..17922e7 --- /dev/null +++ b/src/fizzBuzz.js @@ -0,0 +1,28 @@ +// fizzBuzz +// +// Return an array of numbers from 1 to 100. +// +// For multiples of three, use the string Fizz instead of the number and for multiples of five replace with Buzz. +// +// For numbers which are multiples of both three and five replace with FizzBuzz. +// +// fizzBuzz() +// // => [1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz', ...] + +let fizzBuzz = () => { + let fizzBuzzArray = [] + for (let i = 1; i <= 100; i++){ + if (i % 3 == 0 && i % 5 == 0) { + fizzBuzzArray.push('FizzBuzz') + } else if (i % 3 == 0) { + fizzBuzzArray.push('Fizz') + } else if (i % 5 == 0) { + fizzBuzzArray.push('Buzz') + } else { + fizzBuzzArray.push(i) + } + } + return fizzBuzzArray +} + +module.exports = { fizzBuzz } diff --git a/src/isPalindrome.js b/src/isPalindrome.js new file mode 100644 index 0000000..2008062 --- /dev/null +++ b/src/isPalindrome.js @@ -0,0 +1,38 @@ +// isPalindrome +// +// Determine if a string is a palindrome. Return true or false. +// +// Ignore punctuation, spacing, and case sensitivity. +// +// isPalindrome('radar') +// // => true +// +// isPalindrome('bananas') +// // => false +// +// isPalindrome('A man, a plan, a canal: Panama') +// // => true + +let isPalindrome = string => { + let strToLowerCase = string.toLowerCase() + let cleanStr = strToLowerCase.replace(/[^a-zA-Z]/g, '') + let isPalindromeArray = cleanStr.split('') + + let shiftArray = [] + let popArray = [] + + for (let i = 0; i < isPalindromeArray.length; i++) { + shiftArray.push(isPalindromeArray.shift([i])) + popArray.push(isPalindromeArray.pop([i])) + } + let shiftString = shiftArray.join('') + let popString = popArray.join('') + + if (shiftString == popString) { + return true + } else { + return false + } +} + +module.exports = { isPalindrome } diff --git a/src/makeChange.js b/src/makeChange.js index 59d89b1..ae6e03d 100644 --- a/src/makeChange.js +++ b/src/makeChange.js @@ -1,3 +1,52 @@ -export default function makeChange({price, amountGiven}) { - // your code here +// makeChange: +// +// Given a price and an amount paid, return the number of coins in each denomination that should be given as change. +// +// Note: because JavaScript is bad at decimal math, the inputs are given as integers that represent dollar + cent amounts, so 139 = $1.39. +// +// makeChange({ price: 100, amountGiven: 100 }) +// // => { quarters: 0, dimes: 0, nickels: 0, pennies: 0 } +// +// makeChange({ price: 159, amountGiven: 200 }) +// // => { quarters: 1, dimes: 1, nickels: 1, pennies: 1 } +// +// makeChange({ price: 432, amountGiven: 500 }) +// // => { quarters: 2, dimes: 1, nickels: 1, pennies: 3 } + + +let makeChange = options => { + if (typeof options !== 'object') { + throw new Error('Invalid input format. Expected an object') + } + const {price, amountGiven} = options + let change = { + quarters: 0, + dimes: 0, + nickels: 0, + pennies: 0, + } + let result = amountGiven - price + + if (result === 0) { + return change + } + while (result >= 25) { + result = result - 25 + change.quarters += 1 + } + while (result >= 10) { + result = result - 10 + change.dimes += 1 + } + while (result >= 5) { + result = result - 5 + change.nickels += 1 + } + while (result >= 1) { + result = result - 1 + change.pennies += 1 + } + return change } + +module.exports = { makeChange } diff --git a/src/setComplement.js b/src/setComplement.js new file mode 100644 index 0000000..fc30fbd --- /dev/null +++ b/src/setComplement.js @@ -0,0 +1,24 @@ +// setComplement +// +// Return the complement of two sets. +// +// const a = [1, 2, 3, 4] +// const b = [2, 4, 6, 8] +// setComplement(a, b) +// // => [6, 8] + + +let 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 +} + +module.exports = { setComplement } diff --git a/src/setIntersection.js b/src/setIntersection.js new file mode 100644 index 0000000..b655c49 --- /dev/null +++ b/src/setIntersection.js @@ -0,0 +1,34 @@ +// setIntersection +// +// Return the intersection of two sets. +// +// const a = [1, 2, 3, 4] +// const b = [2, 4, 6, 8] +// setIntersection(a, b) +// // => [2, 4] + + +let setIntersection = (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) + } + } + function isDuplicate(value, index, array){ + return array.indexOf(value) !== index + } + let filteredArr = complementArray.filter(isDuplicate) + return filteredArr.sort() +} + +module.exports = { setIntersection } diff --git a/src/setSymmetricDifference.js b/src/setSymmetricDifference.js new file mode 100644 index 0000000..2384ff6 --- /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] + + +let 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() +} + +module.exports = { setSymmetricDifference } diff --git a/src/setUnion.js b/src/setUnion.js new file mode 100644 index 0000000..350e7db --- /dev/null +++ b/src/setUnion.js @@ -0,0 +1,24 @@ +// 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] + +let 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 +} + +module.exports = { setUnion } diff --git a/test/collatzConjecture_test.js b/test/collatzConjecture_test.js new file mode 100644 index 0000000..4149886 --- /dev/null +++ b/test/collatzConjecture_test.js @@ -0,0 +1,19 @@ +import { expect } from 'chai' +import { collatzConjecture } from '../src/collatzConjecture' + +describe('collatzConjecture()', function(){ + + it('should be a function', function(){ + expect(collatzConjecture).to.be.a('function') + }) + + it('returns a Collatz sequence for any positive integer', function(){ + expect(collatzConjecture(7)).to.eql([7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]) + expect(collatzConjecture(3)).to.eql([3, 10, 5, 16, 8, 4, 2, 1]) + }) + + it('throws an error for any invalid inputs', function() { + expect(function() {collatzConjecture(-1)}).to.throw('Invalid input format. Expected a positive integer') + expect(function() {collatzConjecture('seven')}).to.throw('Invalid input format. Expected a positive integer') + }) +}) diff --git a/test/factorial_test.js b/test/factorial_test.js new file mode 100644 index 0000000..ef9b7d6 --- /dev/null +++ b/test/factorial_test.js @@ -0,0 +1,14 @@ +import { expect } from 'chai' +import { factorial } from '../src/factorial' + +describe('factorial()', function(){ + + it('should be a function', function(){ + expect(factorial).to.be.a('function') + }) + + it('returns the factorial of any fiven number', function() { + expect(factorial(5)).to.eql(120) + expect(factorial(4)).to.eql(24) + }) +}) diff --git a/test/fibonacci_test.js b/test/fibonacci_test.js new file mode 100644 index 0000000..6c9c221 --- /dev/null +++ b/test/fibonacci_test.js @@ -0,0 +1,18 @@ +import { expect } from 'chai' +import { fibonacci } from '../src/fibonacci' + +describe ('fibonacci', function(){ + + it('should be a function', function() { + expect(fibonacci).to.be.a('function') + }) + + it('returns an array of Fibonacci numbers to the nth position.', function() { + expect(fibonacci(10)).to.eql([0, 1, 1, 2, 3, 5, 8, 13, 21, 34]) + expect(fibonacci(5)).to.eql([0, 1, 1, 2, 3]) + }) + + it('throws an error when given invalid inputs', function() { + expect(function() {fibonacci('five') }).to.throw('Invalid input format. Expected a number') + }) +}) diff --git a/test/fizzBuzz_test.js b/test/fizzBuzz_test.js new file mode 100644 index 0000000..ff58373 --- /dev/null +++ b/test/fizzBuzz_test.js @@ -0,0 +1,22 @@ +import { expect } from 'chai' +import { fizzBuzz } from '../src/fizzBuzz' + +describe('fizzBuzz()', function(){ + + it('should be a function', function(){ + expect(fizzBuzz).to.be.a('function') + }) + + it('returns an array of 100 values', function() { + var fizzBuzzArray = fizzBuzz(100) + expect(fizzBuzzArray.length).to.deep.equal(100) + }) + + it('replace multiples of 3 as \'Fizz\', multiples of 5 as \'Buzz\' and multiples of 3 and 5 as \'FizzBuzz\' and return numbers that aren\'t multiples of 3 or 5 ', function(){ + var fizzBuzzArray = fizzBuzz(100) + expect(fizzBuzzArray[2]).to.eql('Fizz') + expect(fizzBuzzArray[4]).to.eql('Buzz') + expect(fizzBuzzArray[14]).to.eql('FizzBuzz') + expect(fizzBuzzArray[1]).to.eql(2) + }) +}) diff --git a/test/isPalindrome_test.js b/test/isPalindrome_test.js new file mode 100644 index 0000000..70c2d77 --- /dev/null +++ b/test/isPalindrome_test.js @@ -0,0 +1,17 @@ +import { expect } from 'chai' +import { isPalindrome } from '../src/isPalindrome' + +describe('isPalindrome', function() { + + it('should be a function', function(){ + expect(isPalindrome).to.be.a('function') + }) + + it('returns true if it is a palindrome', function() { + expect(isPalindrome('A man, a plan, a canal: Panama')).to.eql(true) + }) + + it('returns false if it is not a palindrome', function() { + expect(isPalindrome('bananas')).to.eql(false) + }) +}) diff --git a/test/makeChange_test.js b/test/makeChange_test.js index d17098e..e512241 100644 --- a/test/makeChange_test.js +++ b/test/makeChange_test.js @@ -1,5 +1,5 @@ import { expect } from 'chai' -import makeChange from '../src/makeChange' +import { makeChange } from '../src/makeChange' describe('makeChange()', function(){ @@ -50,4 +50,16 @@ describe('makeChange()', function(){ pennies: 0, }) }) + + it('throws an error when given invalid inputs', function() { + expect(function(){ makeChange(100, 170) }).to.throw( + 'Invalid input format. Expected an object' + ) + }) + + it('throws an error when given no arguments', function() { + expect(function(){ makeChange() }).to.throw( + 'Invalid input format. Expected an object' + ) + }) }) diff --git a/test/setComplement_test.js b/test/setComplement_test.js new file mode 100644 index 0000000..c3e7121 --- /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..383836a --- /dev/null +++ b/test/setIntersection_test.js @@ -0,0 +1,21 @@ +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]) + expect(setIntersection([1,1,2,3,4], [2,4,6,8])).to.eql([2,4]) + }) + + 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..c52a57e --- /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..74f7ef9 --- /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' + ) + }) +})