Created
August 3, 2018 21:28
-
-
Save mcw0933/7d9cbaed18fd9138be7d51c3d74595e3 to your computer and use it in GitHub Desktop.
Prompt #2 - Make Change
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
let currencyConfig = [{ "quarters": 25}, {"dimes": 10}, {"nickels": 5}, {"pennies": 1 }]; | |
function configure(config) { | |
currencyConfig = config; | |
} | |
function make_change(cents) { | |
let change = {}; | |
let remaining = cents; | |
for (var ix in currencyConfig) { | |
let coin = currencyConfig[ix]; | |
let coinName = Object.keys(coin)[0]; | |
let coinValue = coin[coinName]; | |
if (remaining >= coinValue) { | |
let numCoins = Math.floor(remaining / coinValue); | |
change[coinName] = numCoins; | |
remaining -= numCoins * coinValue; | |
} else { | |
delete change[coinName]; | |
} | |
} | |
return change; | |
} | |
module.exports = { | |
make_change | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var module = require("./index"); | |
test("it returns 1 penny for 1 cent", () => { | |
expect(module.make_change(1)).toEqual({ "pennies": 1 }); | |
}); | |
test("it returns 1 dime, 1 penny for 11 cents", () => { | |
expect(module.make_change(11)).toEqual({ "dimes": 1, "pennies": 1 }); | |
}); | |
test("it returns 1 quarter, 1 dime, 1 nickel, and 2 pennies for 42 cents", () => { | |
expect(module.make_change(42)).toEqual({ "quarters": 1, "dimes": 1, "nickels": 1, "pennies": 2 }); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment