Skip to content

Instantly share code, notes, and snippets.

@notsoluckycharm
Created March 7, 2018 02:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save notsoluckycharm/9eb3012e51b3ba833f4bf4c5d4aa0df9 to your computer and use it in GitHub Desktop.
Save notsoluckycharm/9eb3012e51b3ba833f4bf4c5d4aa0df9 to your computer and use it in GitHub Desktop.
Given an input, return the least change possible
const denomizations = [ .01, .05, .1, .25, .5, 1.0 ];
const input = .75;
function returnLeastChange(input = 0, change = {}, denomizations = []){
let pointer = 0;
const toReturn = {};
let newInput = input;
while(pointer < denomizations.length - 1){
const x = denomizations[pointer];
if(x <= newInput ){
toReturn[x] = Math.floor(newInput/x);
newInput = newInput % x;
}
pointer += 1;
}
return toReturn;
}
console.log(
returnLeastChange(input, {}, denomizations.reverse(0))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment