diff --git a/README.md b/README.md index 783e9c5..23b4de8 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,25 @@ Base repository for the [Core Algorithms](http://jsdev.learnersguild.org/goals/1 ## Installation and Setup -_Fill this out_ +`npm i` ## Usage and Examples -_...and this_ +`npm test` to run all tests + +## Specs +completed: makeChange, fizzBuzz, isPalindrome, Factorial out of 10 function in ALGORITHMS.md for CLassic, Numeric, and Set (not done) + + - [X] Artifact produced is a fork of the core-algorithms repo. + - [X] Can run all tests with npm test. + - [X] All tests are passing. + - [X] For each algorithm in the algorithms list, there exists: + - [X] a test file with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs. + an implementation file with a correct implementation of the algorithm. + - [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. + - [ ] 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. diff --git a/src/factorial.js b/src/factorial.js new file mode 100644 index 0000000..57b7b8f --- /dev/null +++ b/src/factorial.js @@ -0,0 +1,16 @@ +const factorial = value => { + + if( typeof value !== 'number' ){ + return 'value is not a number' + } + + let factorialNumber = 1 + for(let index = 1; index <= value; index++){ + factorialNumber *= index + } + + return factorialNumber + +} + +export default factorial diff --git a/src/fizzBuzz.js b/src/fizzBuzz.js new file mode 100644 index 0000000..06d83bd --- /dev/null +++ b/src/fizzBuzz.js @@ -0,0 +1,19 @@ +export default function fizzBuzz(value) { + const returnArray = [] + for (let index = 1; index <= value; index++) { + let fizzBuzzyValue = '' + + if(i % 3 === 0) { + fizzBuzzyValue = fizzBuzzyValue.concat('Fizz') + } + if(i % 5 === 0) { + fizzBuzzyValue = fizzBuzzyValue.concat('Buzz') + } + if(fizzBuzzyValue === '') { + fizzBuzzyValue = index + } + returnArray.push(fizzBuzzyValue) + } + + return returnArray +} diff --git a/src/isPalindrome.js b/src/isPalindrome.js new file mode 100644 index 0000000..802d125 --- /dev/null +++ b/src/isPalindrome.js @@ -0,0 +1,19 @@ +const isPalidrome = string => { + + if ( typeof string !== 'string') { + return "This is not a string" + } + + let reversedString = '', + originalOnlyLetters = string.replace(/[\W_]/g, '').toLowerCase() + + for ( let index = string.length - 1; index >= 0; index-- ) { + if ( string[index].search(/[\W_]/g) === -1 ) { + reversedString += string[index] + } + } + + return originalOnlyLetters === reversedString.toLowerCase() +} + +export default isPalidrome diff --git a/src/makeChange.js b/src/makeChange.js index 59d89b1..84d7250 100644 --- a/src/makeChange.js +++ b/src/makeChange.js @@ -1,3 +1,39 @@ -export default function makeChange({price, amountGiven}) { - // your code here +const makeChange = ({price, amountGiven}) => { + let moneyBack = amountGiven - price + if ( moneyBack === NaN ) { + return 'no money given' + } + if ( moneyBack < 0 ) { + return 'You need to give more money!!!' + } + + let change = { + quarters: 0, + dimes: 0, + nickels: 0, + pennies: 0, + } + + while ( moneyBack > 24 ) { + change.quarters++ + moneyBack -= 25 + } + + while ( moneyBack > 9 ) { + change.dimes++ + moneyBack -= 10 + } + + while ( moneyBack > 4 ) { + change.nickels++ + moneyBack -= 5 + } + + while ( moneyBack > 0 ) { + change.pennies++ + moneyBack -= 1 + } + return change } + +export default makeChange diff --git a/test/factorial_test.js b/test/factorial_test.js new file mode 100644 index 0000000..7a90b92 --- /dev/null +++ b/test/factorial_test.js @@ -0,0 +1,19 @@ +import { expect } from 'chai' +import factorial from '../src/factorial' + +describe('factorial()', () => { + + it('should be a function', () => { + expect(factorial).to.be.a('function') + }) + + it('returns the factorial of a number', () => { + expect(factorial(7)).to.equal(5040) + expect(factorial(4)).to.equal(24) + expect(factorial(5)).to.equal(120) + }) + + it('only takes numbers', () => { + expect(factorial('blah blah')).to.equal('value is not a number') + }) +}) diff --git a/test/fizzBuzz_test.js b/test/fizzBuzz_test.js new file mode 100644 index 0000000..5911fa7 --- /dev/null +++ b/test/fizzBuzz_test.js @@ -0,0 +1,26 @@ +import { expect } from 'chai' +import fizzBuzz from '../src/fizzBuzz' + +describe('fizzBuzz()', () => { + + it('should be a function', () => { + expect(fizzBuzz).to.be.a('function') + }) + + it('returns an array of numbers from 1 to value given', () => { + const returnArray = fizzBuzz(87) + expect(returnArray.length).to.equal(87) + }) + + it('replaces mutiples of 3 with the string\'Fizz\', multiples of 5 with \'Buzz\', and multiples of 3 & 5 with \'FizzBuzz\'', () => { + const returnArray = fizzBuzz(64) + + expect(returnArray[0]).to.eql(1) + expect(returnArray[2]).to.eql('Fizz') + expect(returnArray[5]).to.eql('Fizz') + expect(returnArray[4]).to.eql('Buzz') + expect(returnArray[14]).to.eql('FizzBuzz') + expect(returnArray[13]).to.eql(14) + expect(returnArray[29]).to.eql('FizzBuzz') + }) +}) diff --git a/test/isPalindrome_test.js b/test/isPalindrome_test.js new file mode 100644 index 0000000..ece1d24 --- /dev/null +++ b/test/isPalindrome_test.js @@ -0,0 +1,27 @@ +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 value given is a isPalindrome, false if not', () => { + expect(isPalindrome('ana')).to.be.true + + expect(isPalindrome('beyonce')).to.be.false + }) + + it('Gets rid of punctiuation and white space to evaluate entire statement given', () => { + expect(isPalindrome('A man, a plan, a canal: Panama')).to.be.true + + expect(isPalindrome('Hi my name is Ana!')).to.be.false + }) + + it('Only take strings', () => { + expect(isPalindrome(300)).to.equal('This is not a string') + + expect(isPalindrome(null)).to.equal('This is not a string') + }) +}) diff --git a/test/makeChange_test.js b/test/makeChange_test.js index d17098e..829a46a 100644 --- a/test/makeChange_test.js +++ b/test/makeChange_test.js @@ -1,13 +1,13 @@ import { expect } from 'chai' import makeChange from '../src/makeChange' -describe('makeChange()', function(){ +describe('makeChange()', () => { - it('should be a function', function(){ + it('should be a function', () => { expect(makeChange).to.be.a('function') }) - it('returns an object with all coin types (quarters, dimes, nickels, and pennies)', function(){ + it('returns an object with all coin types (quarters, dimes, nickels, and pennies)', () => { const change = makeChange({price: 100, amountGiven: 100}) expect(change).to.be.an('object') expect(change).to.have.keys('quarters', 'dimes', 'nickels', 'pennies') @@ -19,7 +19,7 @@ describe('makeChange()', function(){ }) }) - it('returns correct change', function(){ + it('returns correct change', () => { expect(makeChange({price: 100, amountGiven: 141})).to.deep.equal({ quarters: 1, dimes: 1, @@ -28,7 +28,7 @@ describe('makeChange()', function(){ }) }) - it('minimizes the number of coins given by using the most high-value coins', function(){ + it('minimizes the number of coins given by using the most high-value coins', () => { expect(makeChange({price: 100, amountGiven: 168})).to.deep.equal({ quarters: 2, dimes: 1,