Created
March 7, 2018 02:44
-
-
Save notsoluckycharm/9eb3012e51b3ba833f4bf4c5d4aa0df9 to your computer and use it in GitHub Desktop.
Given an input, return the least change possible
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
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