I’m writing a cash register function that breaks down change into smallest possible denominations by comparing it to the denominations array. So 8.55
becomes array
[8, 0.5, 0.05]
which breaks down into [5, 3, 0.5, 0.05]
but as soon as I go above 10 the editor crashes. Even though 14.55
for example should simply be broken down as [10, 4, 0.5, 0.05]
. What am I missing?
let change = [8, 0.9, 0.06]
let denom = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100]
for (let x of change) {
for (let y of denom) {
if (x / y > 1 && x / y < 4) {
let mlt = Math.floor(x / y)
largestFit = y * mlt
let idx = change.indexOf(x)
change.splice(idx, 1, largestFit)
change.splice(idx + 1, 0, x - largestFit)
}
}
}
change = change.map(a => Number(a.toFixed(2))).filter(a => a > 0)
console.log(change)