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
12 changes: 6 additions & 6 deletions CONTRACT.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ Complete **ONLY** the [Classic](https://github.com/GuildCrafts/core-algorithms/b
- [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.
- [ ] `factorial()` algorithm is implemented according to the description in [algorithms.md][algorithms-list].
- [ ] Tests for `factorial()` exist with at least 2 unit tests using valid inputs.
- [ ] `fibonacci()` algorithm is implemented according to the description in [algorithms.md][algorithms-list].
- [ ] Tests for `fibonacci()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs.
- [ ] `collatzConjecture()` algorithm is implemented according to the description in [algorithms.md][algorithms-list].
- [ ] Tests for `collatzConjecture()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid 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.
- [ ] `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].
Expand Down
32 changes: 32 additions & 0 deletions src/collatzConjecture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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]


export default function 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
}
16 changes: 16 additions & 0 deletions src/factorial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// factorial
//
// Return the factorial of a number.
//
// factorial(5)
// // => 120

export default function factorial(num) {
var factorialElements = [num]
for (var i = num; i > 1; i--) {
factorialElements.push(i - 1)
}
return factorialElements.reduce(function(a,b){
return a*b
}, 1)
}
24 changes: 24 additions & 0 deletions src/fibonacci.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// fibonacci
//
// Return an array of Fibonacci numbers to the nth position.
//
// fibonacci(10)
// // => [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

export default function fibonacci(num) {
if (typeof num !== 'number') {
throw new Error('Invalid input format. Expected a number')
}
var first = 0
var second = 1
var result
var fibonacciArr = [0]

for (var i = 1; i < num; i++) {
result = first + second
first = second
second = result
fibonacciArr.push(first)
}
return fibonacciArr
}
18 changes: 18 additions & 0 deletions test/collatzConjecture_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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')
})
})
14 changes: 14 additions & 0 deletions test/factorial_test.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
18 changes: 18 additions & 0 deletions test/fibonacci_test.js
Original file line number Diff line number Diff line change
@@ -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')
})
})
4 changes: 2 additions & 2 deletions test/makeChange_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ describe('makeChange()', function(){

it('throws an error when given invalid inputs', function() {
expect(function(){ makeChange(100, 170) }).to.throw(
'Invalid input format. Expected an object literal'
'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 literal'
'Invalid input format. Expected an object'
)
})
})