Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions CONTRACT.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 15 additions & 7 deletions src/factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
// }
22 changes: 22 additions & 0 deletions src/setComplement.js
Original file line number Diff line number Diff line change
@@ -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
}
23 changes: 23 additions & 0 deletions src/setIntersection.js
Original file line number Diff line number Diff line change
@@ -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
}
30 changes: 30 additions & 0 deletions src/setSymmetricDifference.js
Original file line number Diff line number Diff line change
@@ -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()
}

//
22 changes: 22 additions & 0 deletions src/setUnion.js
Original file line number Diff line number Diff line change
@@ -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
}
27 changes: 27 additions & 0 deletions test/setComplement_test.js
Original file line number Diff line number Diff line change
@@ -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'
)
})
})
20 changes: 20 additions & 0 deletions test/setIntersection_test.js
Original file line number Diff line number Diff line change
@@ -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'
)
})
})
20 changes: 20 additions & 0 deletions test/setSymmetricDifference_test.js
Original file line number Diff line number Diff line change
@@ -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')
})
})
20 changes: 20 additions & 0 deletions test/setUnion_test.js
Original file line number Diff line number Diff line change
@@ -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'
)
})
})