From 82dc880b0b8d8d779b1654dcafd25f590814dded Mon Sep 17 00:00:00 2001 From: SomayaB Date: Tue, 25 Apr 2017 11:00:58 -0700 Subject: [PATCH 1/4] Complete factorial algorithm w tests --- CONTRACT.md | 4 ++-- src/factorial.js | 17 +++++++++++++++++ test/factorial_test.js | 14 ++++++++++++++ test/makeChange_test.js | 4 ++-- 4 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 src/factorial.js create mode 100644 test/factorial_test.js diff --git a/CONTRACT.md b/CONTRACT.md index 831bfbc..31fbf2d 100644 --- a/CONTRACT.md +++ b/CONTRACT.md @@ -32,8 +32,8 @@ 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. +- [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. - [ ] `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]. diff --git a/src/factorial.js b/src/factorial.js new file mode 100644 index 0000000..c15d20a --- /dev/null +++ b/src/factorial.js @@ -0,0 +1,17 @@ +// 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) +} +console.log('array', factorialElements) +return factorialElements.reduce(function(a,b){ +return a*b +}, 1) +} diff --git a/test/factorial_test.js b/test/factorial_test.js new file mode 100644 index 0000000..12daa9e --- /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/makeChange_test.js b/test/makeChange_test.js index 2ca1a8a..9956816 100644 --- a/test/makeChange_test.js +++ b/test/makeChange_test.js @@ -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' ) }) }) From 25d0906f8bdf35630aaad47268f19397eae806e6 Mon Sep 17 00:00:00 2001 From: SomayaB Date: Tue, 25 Apr 2017 12:55:30 -0700 Subject: [PATCH 2/4] Complete fibonacci algorithm w tests --- CONTRACT.md | 4 ++-- src/fibonacci.js | 24 ++++++++++++++++++++++++ test/fibonacci_test.js | 18 ++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 src/fibonacci.js create mode 100644 test/fibonacci_test.js diff --git a/CONTRACT.md b/CONTRACT.md index 31fbf2d..c70420a 100644 --- a/CONTRACT.md +++ b/CONTRACT.md @@ -34,8 +34,8 @@ Complete **ONLY** the [Classic](https://github.com/GuildCrafts/core-algorithms/b - [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. -- [ ] `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. +- [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. - [ ] `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. - [ ] `setUnion()` algorithm is implemented according to the description in [algorithms.md][algorithms-list]. diff --git a/src/fibonacci.js b/src/fibonacci.js new file mode 100644 index 0000000..88a9a4a --- /dev/null +++ b/src/fibonacci.js @@ -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 +} diff --git a/test/fibonacci_test.js b/test/fibonacci_test.js new file mode 100644 index 0000000..06154f6 --- /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') + }) +}) From 2e657470900797801077b5094f85db7bc56b54bf Mon Sep 17 00:00:00 2001 From: SomayaB Date: Tue, 25 Apr 2017 14:12:18 -0700 Subject: [PATCH 3/4] Complete collatzConjecture algorithm w tests --- CONTRACT.md | 4 ++-- src/collatzConjecture.js | 32 ++++++++++++++++++++++++++++++++ test/collatzConjecture_test.js | 18 ++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 src/collatzConjecture.js create mode 100644 test/collatzConjecture_test.js diff --git a/CONTRACT.md b/CONTRACT.md index c70420a..5b01d97 100644 --- a/CONTRACT.md +++ b/CONTRACT.md @@ -36,8 +36,8 @@ Complete **ONLY** the [Classic](https://github.com/GuildCrafts/core-algorithms/b - [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. -- [ ] `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] `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]. diff --git a/src/collatzConjecture.js b/src/collatzConjecture.js new file mode 100644 index 0000000..3f5a5e1 --- /dev/null +++ b/src/collatzConjecture.js @@ -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 +} diff --git a/test/collatzConjecture_test.js b/test/collatzConjecture_test.js new file mode 100644 index 0000000..c895ca0 --- /dev/null +++ b/test/collatzConjecture_test.js @@ -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') + }) +}) From e07820ee2d2f791f918546138f17d07ce5692820 Mon Sep 17 00:00:00 2001 From: SomayaB Date: Tue, 25 Apr 2017 14:14:17 -0700 Subject: [PATCH 4/4] Clean code --- src/factorial.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/factorial.js b/src/factorial.js index c15d20a..cb8372f 100644 --- a/src/factorial.js +++ b/src/factorial.js @@ -6,12 +6,11 @@ // // => 120 export default function factorial(num) { -var factorialElements = [num] -for (var i = num; i > 1; i--) { -factorialElements.push(i - 1) -} -console.log('array', factorialElements) -return factorialElements.reduce(function(a,b){ -return a*b -}, 1) + var factorialElements = [num] + for (var i = num; i > 1; i--) { + factorialElements.push(i - 1) + } + return factorialElements.reduce(function(a,b){ + return a*b + }, 1) }