From c013f56c643132bdbcc70ef3a847577c1a7d217c Mon Sep 17 00:00:00 2001 From: Leah Ann Mitchell Date: Fri, 28 Apr 2017 12:25:27 -0700 Subject: [PATCH 1/2] complete makechange challenge --- src/comment from make_change.js | 87 +++++++++++++++++++++++++++++++++ src/makeChange.js | 53 +++++++++++++++++++- 2 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 src/comment from make_change.js diff --git a/src/comment from make_change.js b/src/comment from make_change.js new file mode 100644 index 0000000..d3a5028 --- /dev/null +++ b/src/comment from make_change.js @@ -0,0 +1,87 @@ +/*const useQaurter = function(amountGiven, price) { + amountGiven = amountGiven - 25 + coins.qaurters++ + return amountGiven + + //console.log(amountGiven) + } + + + + + const useDime = function(amountGiven, price) { + amountGiven = amountGiven - 10 + coins.dimes++ + return amountGiven + + //console.log(amountGiven) + } + + + + const useNickel = function(amountGiven, price) { + amountGiven = amountGiven - 5 + coins.nickels++ + return amountGiven + + //console.log(amountGiven) + } + + + + const usePenny = function(amountGiven, price) { + amountGiven = amountGiven - 1 + coins.pennies++ + return amountGiven + + //console.log(amountGiven) + } + + function makeChange({price, amountGiven}) { + let results = {} + if(price === amountGiven) { + + results.quarters = coins.quarters + results.dimes = coins.dimes + results.nickels = coins.nickels + results.pennies = coins.pennies + + coins.quarters = 0 + coins.dimes = 0 + coins.nickels = 0 + coins.pennies = 0 + + + } + + if( difference >= 25 ) { + amountGiven = takeQuarter(amountGiven, coins) + return makeChange({price: price, amountGiven: amountGiven}) + }else if( difference >= 10) { + amountGiven = takeDime(amountGiven, price) + return makeChange({price: price, amountGiven: amountGiven}) + }else if(difference >= 5){ + amountGiven = takeNickel(amountGiven, price) + return makeChange({price: price, amountGiven: amountGiven}) + }else if(difference >= 1){ + amountGiven = takePenny(amountGiven, price) + return makeChange({price: price, amountGiven: amountGiven}) + } + } +this worked + +function makeChange({price, amountGiven}) { + + const coins = { + // quarters: 0, + // dimes: 0, + // nickels: 0, + pennies: 0, + } + + let difference = amountGiven - price + + coins.pennies = difference + return coins + +} \ No newline at end of file diff --git a/src/makeChange.js b/src/makeChange.js index 59d89b1..05b1da0 100644 --- a/src/makeChange.js +++ b/src/makeChange.js @@ -1,3 +1,54 @@ +const coins = { + quarters: 0, + dimes: 0, + nickels: 0, + pennies: 0 +} +const takeQuarter = function(amountGiven, price){ + amountGiven = amountGiven - 25 + coins.quarters++ + return amountGiven +} +const takeDime = function(amountGiven, price){ + amountGiven = amountGiven - 10 + coins.dimes++ + return amountGiven +} +const takeNickel = function(amountGiven, price){ + amountGiven = amountGiven - 5 + coins.nickels++ + return amountGiven +} +const takePenny = function(amountGiven, price){ + amountGiven = amountGiven - 1 + coins.pennies++ + return amountGiven +} export default function makeChange({price, amountGiven}) { - // your code here + let results = {} + if(price === amountGiven) { + results.quarters = coins.quarters + results.dimes = coins.dimes + results.nickels = coins.nickels + results.pennies = coins.pennies + + coins.quarters = 0 + coins.dimes = 0 + coins.nickels = 0 + coins.pennies = 0 + return results + } + if(amountGiven - price >= 25){ + amountGiven = takeQuarter(amountGiven, coins) + return makeChange({price: price, amountGiven: amountGiven}) + }else if(amountGiven - price >= 10){ + amountGiven = takeDime(amountGiven, price) + return makeChange({price: price, amountGiven: amountGiven}) + }else if(amountGiven - price >= 5){ + amountGiven = takeNickel(amountGiven, price) + return makeChange({price: price, amountGiven: amountGiven}) + }else if(amountGiven - price >= 1){ + amountGiven = takePenny(amountGiven, price) + return makeChange({price: price, amountGiven: amountGiven}) + } } From 9bad0aacc39ce2b6aed961e21a61d032daaa4a39 Mon Sep 17 00:00:00 2001 From: Leah Ann Mitchell Date: Fri, 28 Apr 2017 12:39:25 -0700 Subject: [PATCH 2/2] Solution and test --- src/collatzConjection,js | 0 src/collatzConjection.js | 17 ++++++++++++++ src/collatzConjecture.js | 17 ++++++++++++++ src/comment from make_change.js | 41 +++++++++++++++++++++++++++++++++ test/collatzConjection_test.js | 15 ++++++++++++ test/collatzConjecture_test.js | 15 ++++++++++++ 6 files changed, 105 insertions(+) create mode 100644 src/collatzConjection,js create mode 100644 src/collatzConjection.js create mode 100644 src/collatzConjecture.js create mode 100644 test/collatzConjection_test.js create mode 100644 test/collatzConjecture_test.js diff --git a/src/collatzConjection,js b/src/collatzConjection,js new file mode 100644 index 0000000..e69de29 diff --git a/src/collatzConjection.js b/src/collatzConjection.js new file mode 100644 index 0000000..e8e281e --- /dev/null +++ b/src/collatzConjection.js @@ -0,0 +1,17 @@ + +let arr = [] +export default function collatzConjecture(num){ + if(num === 1){ + arr.push(1) + return arr + } + if(num % 2 === 0){ + arr.push(num) + num = num / 2 + return collatzConjecture(num) + }else if(num % 2 !== 0){ + arr.push(num) + num = num * 3 + 1 + return collatzConjecture(num) + } +} \ No newline at end of file diff --git a/src/collatzConjecture.js b/src/collatzConjecture.js new file mode 100644 index 0000000..e8e281e --- /dev/null +++ b/src/collatzConjecture.js @@ -0,0 +1,17 @@ + +let arr = [] +export default function collatzConjecture(num){ + if(num === 1){ + arr.push(1) + return arr + } + if(num % 2 === 0){ + arr.push(num) + num = num / 2 + return collatzConjecture(num) + }else if(num % 2 !== 0){ + arr.push(num) + num = num * 3 + 1 + return collatzConjecture(num) + } +} \ No newline at end of file diff --git a/src/comment from make_change.js b/src/comment from make_change.js index d3a5028..4a20eaa 100644 --- a/src/comment from make_change.js +++ b/src/comment from make_change.js @@ -84,4 +84,45 @@ function makeChange({price, amountGiven}) { coins.pennies = difference return coins +} + +export default function makeChange({price, amountGiven}) { + + const coins = { + quarters: 0, + dimes: 0, + nickels: 0, + pennies: 0, + } + + let difference = amountGiven - price + + let quarterValue = 25, + let dimeValue = 10, + let pennyValue = 1, + let nickelValue = 5, + + + let numberOfNickels = Math.floor(difference / nickelValue ) +let numberOfPennies = Math.floor(difference / pennyValue ) +let numberOfDimes = Math.floor(difference / dimeValue ) +let numberOfQuarters = Math.floor(difference / quarterValue ) + + + + let numberOfPennies = difference + let numberOfNickels = difference + let numberOfDimes = difference + let numberOfQuarters = difference + + + coins.nickels = numberOfNickels + coins.pennies = numberOfPennies + coins.dimes = numberOfDimes + coins.quarters = numberOfQuarters + + + + return coins + } \ No newline at end of file diff --git a/test/collatzConjection_test.js b/test/collatzConjection_test.js new file mode 100644 index 0000000..839c4b2 --- /dev/null +++ b/test/collatzConjection_test.js @@ -0,0 +1,15 @@ +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('take a number a perform an evaluation based on what the current number is then returns an array', 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(12)).to.eql([ 7,22,11,34,17,52,26,13,40,20,10, + 5,16,8,4,2,1,12,6,3,10,5,16,8,4,2,1]) + }) +}) diff --git a/test/collatzConjecture_test.js b/test/collatzConjecture_test.js new file mode 100644 index 0000000..839c4b2 --- /dev/null +++ b/test/collatzConjecture_test.js @@ -0,0 +1,15 @@ +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('take a number a perform an evaluation based on what the current number is then returns an array', 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(12)).to.eql([ 7,22,11,34,17,52,26,13,40,20,10, + 5,16,8,4,2,1,12,6,3,10,5,16,8,4,2,1]) + }) +})