# Exact Change decimal issue

Exact Change decimal issue
0

#1

Hello everyone,
I’m trying to solve this and I came up with the below.

It’s long and ugly and verbose but try not to judge that still learning.

The problem I have is with the validation of this code when it comes to

should return [[“QUARTER”, 0.50]]

It returns [[“QUARTER”, “0.50”]] - i cant seem to find a way to return a NUMBER that carries 2 decilmals so instead of 0.5 to display 0.50 .

Help?

``````function checkCashRegister(price, cash, cid) {
var change = []
const args = Array.from(arguments);
const moneyArray = args[2]
var penny = moneyArray[0][0],
pennyValue = moneyArray[0][1]
var nickel = moneyArray[1][0],
nickelValue = moneyArray[1][1]
var dime = moneyArray[2][0],
dimeValue = moneyArray[2][1]
var quarter = moneyArray[3][0],
quarterValue = moneyArray[3][1]
var one = moneyArray[4][0],
oneValue = moneyArray[4][1]
var five = moneyArray[5][0],
fiveValue = moneyArray[5][1]
var ten = moneyArray[6][0],
tenValue = moneyArray[6][1]
var twenty = moneyArray[7][0],
twentyValue = moneyArray[7][1]
var oneHundred = moneyArray[8][0],
oneHundredValue = moneyArray[8][1]
console.log(moneyArray)

var changeDue = (cash - price).toFixed(2)

var totalMoneyInDrawer = parseFloat(
moneyArray.reduce(function (acc, item) {
acc = acc + item[1]
return acc;
}, 0)
).toFixed(2)

console.log('ChangeDue is a: ' + typeof(changeDue))
console.log('TotalMoney is a: ' + typeof(totalMoneyInDrawer))
console.log(parseFloat(totalMoneyInDrawer) > parseFloat(changeDue))

// trebuie sa fac o functie care sa ia un numar si sa-l imparta la fiecare 'fel de ban din casa'.
//daca modulo % de numar/bani === 0 atunci ii dam omului rest din 'felul de ban de casa': bani - numar
//ce ramane, se ia din nou si urmeaza sa fie impartit la bani din casa pana reulta 0

console.log('Change due: ' + changeDue)
console.log('Money in drawer: ' + totalMoneyInDrawer)
console.log(totalMoneyInDrawer + ' is a: ' + typeof(totalMoneyInDrawer))

if (parseFloat(totalMoneyInDrawer) < parseFloat(changeDue)) {
console.log('Insufficient Funds')
return 'Insufficient Funds'
} else if (totalMoneyInDrawer === changeDue) {
console.log('Closed')
return 'Closed'
}
var numberOfLoops = 0;

for (var item in moneyArray) {
if (changeDue < 1) {
if (changeDue > 0.25 && quarterValue >= 0.25) { // Daca restul este 0.ceva si avem monezi de 0.25
console.log('Paying with QUARTERS .25')
console.log('We have now: ' + quarterValue + ' quarters.')
change.push(quarter) // am dat rest din .25
while (quarterValue !== 0 && changeDue > 0) {// Daca avem monezi de .25    || QUARTERS: 1.25 ; Change 0.50
numberOfLoops += 1
changeDue = (changeDue - 0.25).toFixed(2)
quarterValue = quarterValue - 0.25
console.log('Just paid .25 quarters')
console.log('Quarter value is now: ' + quarterValue)
console.log('Left to pay: ' + changeDue)
console.log('Quarters left: ' + quarterValue)
}
var xxx = parseFloat(Math.round((numberOfLoops * 0.25) * 100) / 100).toPrecision(2)
var yyy = Number(Math.round((numberOfLoops * 0.25)+'e2')+'e-2')
console.log(xxx + ' este: ' + typeof(xxx))
console.log(yyy + ' este: ' + typeof(yyy))
change.push(xxx)
}
if (changeDue > 0.10 && dimeValue >= 0.10) { // Daca avem monezi de .10 (Dimes)
console.log('Paying with DIMES .10')
change.push(dime) // am dat rest din .10
while (dimeValue !== 0 && changeDue >= 0.10) { // Nu stiu de ce mai mare decat 0.10!!!
numberOfLoops += 1
changeDue = (changeDue - 0.10).toFixed(2)
dimeValue = dimeValue - 0.10
console.log('Dime value is now: ' + dimeValue.toFixed(2))
console.log('Change Due: ' + changeDue)
console.log('Dimes left: ' + dimeValue.toFixed(2))
}
change.push((numberOfLoops * 0.10).toFixed(2))
}
numberOfLoops = 0
if (changeDue >= 0.05 && nickelValue >= 0.05) { // Daca avem monezi de .05 (Nickels)
console.log('Paying with NICKELS .05')
change.push(nickel) // am dat rest din .05
while (nickelValue !== 0 && changeDue >= 0.05) { // Inca avem nickels?
numberOfLoops += 1
changeDue = (changeDue - 0.05).toFixed(2)
nickelValue = nickelValue - 0.05
console.log('Dime value is now: ' + nickelValue.toFixed(2))
console.log('Change Due: ' + changeDue)
console.log('Dimes left: ' + nickelValue.toFixed(2))
}
change.push((numberOfLoops * 0.05).toFixed(2))
}
numberOfLoops = 0
if (changeDue >= 0.01 && pennyValue >= 0.01) { // Daca avem monezi de .01 (Pennies)
console.log('Paying with PENNIES .01')
change.push(penny) // am dat rest din .05
while (pennyValue !== 0 && changeDue >= 0.01) { // Inca avem pennies?
numberOfLoops += 1
changeDue = (changeDue - 0.01).toFixed(2)
pennyValue = pennyValue - 0.01
console.log('Dime value is now: ' + pennyValue.toFixed(2))
console.log('Change Due: ' + changeDue)
console.log('Dimes left: ' + pennyValue.toFixed(2))
}
change.push((numberOfLoops * 0.01).toFixed(2))
}
} else if (changeDue > 1) {
// Make the logic here for when the change is > 1
if (changeDue > 100 && oneHundredValue >= 100) { // Daca restul este 1.ceva si avem bani de 100
console.log('Paying with HUNDRED Bills 100')
console.log('We have now: ' + oneHundredValue + ' 100 \$\$ bills')
change.push(oneHundred) // am dat rest din .25
while (oneHundredValue !== 0 && changeDue > 0) {// Daca avem hartii de 100
numberOfLoops += 1
changeDue = (changeDue - 100).toFixed(2)
oneHundredValue = oneHundredValue - 100
console.log('Just paid \$100')
console.log('\$100 value is now: ' + oneHundredValue)
console.log('Left to pay: ' + changeDue)
console.log('\$100 bills left: ' + oneHundredValue)
}
var xxx = parseFloat(Math.round((numberOfLoops * 100) * 100) / 100)
var yyy = Number(Math.round((numberOfLoops * 0.25)+'e2')+'e-2')
console.log(xxx + ' este: ' + typeof(xxx))
console.log(yyy + ' este: ' + typeof(yyy))
change.push(xxx)
}
numberOfLoops = 0
if (changeDue > 20 && twentyValue >= 20) { // Daca restul este 1.ceva si avem bani de 20
console.log('Paying with TWENTY Bills 20')
console.log('We have now: ' + twentyValue + ' 20 \$\$ bills')
change.push(twenty) // am dat rest din \$20
while (twentyValue !== 0 && changeDue > 20) { // Daca avem hartii de 20
numberOfLoops += 1
changeDue = (changeDue - 20).toFixed(2)
twentyValue = twentyValue - 20
console.log('Just paid \$20')
console.log('\$20 value is now: ' + twentyValue)
console.log('Left to pay: ' + changeDue)
console.log('\$20 bills left: ' + twentyValue)
}
var xxx = parseFloat(Math.round((numberOfLoops * 20) * 100) / 100)
change.push(xxx)
}
numberOfLoops = 0
if (changeDue > 10 && tenValue >= 10) { // Daca restul este 1.ceva si avem bani de 10
console.log('Paying with TEN Bills 10')
console.log('We have now: ' + tenValue + ' 10 \$\$ bills')
change.push(ten) // am dat rest din \$10
while (tenValue !== 0 && changeDue > 10) { // Daca avem hartii de 10
numberOfLoops += 1
changeDue = (changeDue - 10).toFixed(2)
tenValue = tenValue - 10
console.log('Just paid \$10')
console.log('\$10 value is now: ' + tenValue)
console.log('Left to pay: ' + changeDue)
console.log('\$10 bills left: ' + tenValue)
}
var xxx = parseFloat(Math.round((numberOfLoops * 10) * 100) / 100)
change.push(xxx)
}
numberOfLoops = 0
if (changeDue > 5 && fiveValue >= 5) { // Daca restul este 1.ceva si avem bani de 5
console.log('Paying with TEN Bills 5')
console.log('We have now: ' + fiveValue + ' 5 \$\$ bills')
change.push(five) // am dat rest din \$5
while (fiveValue !== 0 && changeDue > 5) { // Daca avem hartii de 5
numberOfLoops += 1
changeDue = (changeDue - 5).toFixed(2)
fiveValue = fiveValue - 5
console.log('Just paid \$5')
console.log('\$5 value is now: ' + fiveValue)
console.log('Left to pay: ' + changeDue)
console.log('\$5 bills left: ' + fiveValue)
}
var xxx = parseFloat(Math.round((numberOfLoops * 5) * 100) / 100)
change.push(xxx)
}
numberOfLoops = 0
if (changeDue > 1 && oneValue >= 1) { // Daca restul este 1.ceva si avem bani de 1
console.log('Paying with TEN Bills 1')
console.log('We have now: ' + oneValue + ' 1 \$\$ bills')
change.push(one) // am dat rest din \$1
while (oneValue !== 0 && changeDue > 1) { // Daca avem hartii de 1
numberOfLoops += 1
changeDue = (changeDue - 1).toFixed(2)
oneValue = oneValue - 1
console.log('Just paid \$1')
console.log('\$1 value is now: ' + oneValue)
console.log('Left to pay: ' + changeDue)
console.log('\$1 bills left: ' + oneValue)
}
var xxx = parseFloat(Math.round((numberOfLoops * 1) * 100) / 100)
change.push(xxx)
}
numberOfLoops = 0
if (changeDue > 0.25 && quarterValue >= 0.25) { // Daca restul este 0.ceva si avem monezi de 0.25
console.log('Paying with QUARTERS .25')
console.log('We have now: ' + quarterValue + ' quarters.')
change.push(quarter) // am dat rest din .25
while (quarterValue !== 0 && changeDue > 0.25) {// Daca avem monezi de .25    || QUARTERS: 1.25 ; Change 0.50
numberOfLoops += 1
changeDue = (changeDue - 0.25).toFixed(2)
quarterValue = quarterValue - 0.25
console.log('Just paid .25 quarters')
console.log('Quarter value is now: ' + quarterValue)
console.log('Left to pay: ' + changeDue)
console.log('Quarters left: ' + quarterValue)
}
var xxx = parseFloat(Math.round((numberOfLoops * 0.25) * 100) / 100).toPrecision(2)
var yyy = Number(Math.round((numberOfLoops * 0.25)+'e2')+'e-2')
console.log(xxx + ' este: ' + typeof(xxx))
console.log(yyy + ' este: ' + typeof(yyy))
change.push(xxx)
}
numberOfLoops = 0
if (changeDue > 0.10 && dimeValue >= 0.10) { // Daca avem monezi de .10 (Dimes)
console.log('Paying with DIMES .10')
change.push(dime) // am dat rest din .10
while (dimeValue !== 0 && changeDue >= 0.10) { // mai avem Dimes?
numberOfLoops += 1
console.log('Dime value is now: ' + dimeValue.toFixed(2))
console.log('Change Due: ' + changeDue)
console.log('Dimes left: ' + dimeValue.toFixed(2))
changeDue = (changeDue - 0.10).toFixed(2)
dimeValue = dimeValue - 0.10
}
//change.push((numberOfLoops * 0.10).toFixed(2))
change.push(parseFloat(Math.round(numberOfLoops * 0.10 * 100) / 100))
}
numberOfLoops = 0
if (changeDue >= 0.05 && nickelValue >= 0.05) { // Daca avem monezi de .05 (Nickels)
console.log('Paying with NICKELS .05')
change.push(nickel) // am dat rest din .05
while (nickelValue !== 0 && changeDue >= 0.05) { // Inca avem nickels?
numberOfLoops += 1
changeDue = (changeDue - 0.05).toFixed(2)
nickelValue = nickelValue - 0.05
console.log('Dime value is now: ' + nickelValue.toFixed(2))
console.log('Change Due: ' + changeDue)
console.log('Dimes left: ' + nickelValue.toFixed(2))
}
var num = numberOfLoops * 0.05
function showAsFloat(num, n){
return !isNaN(+num) ? (+num).toFixed(n || 2) : num;
}
change.push(showAsFloat(num))
}
numberOfLoops = 0
if (changeDue >= 0.01 && pennyValue >= 0.01) { // Daca avem monezi de .01 (Pennies)
console.log('Paying with PENNIES .01')
change.push(penny) // am dat rest din .05
while (pennyValue !== 0 && changeDue >= 0.01) { // Inca avem pennies?
numberOfLoops += 1
console.log('Penny value is now: ' + pennyValue.toFixed(2))
console.log('Change Due: ' + changeDue)
console.log('Pennies left: ' + pennyValue.toFixed(2))
changeDue = (changeDue - 0.01).toFixed(2)
pennyValue = pennyValue - 0.01
}
change.push(parseFloat(Math.round(numberOfLoops * 0.01 * 100) / 100))
}
}

if (changeDue > 0) {
console.log('Insufficient Funds')
return 'Insufficient Funds'
}

// Here is your change, ma'am.
console.log('Change due: ' + changeDue)
console.log(change)
return change;
}
}
checkCashRegister(19.50, 20.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]])
``````

#2

Use integers for calculations. Multiply all decimals * 100.
In the final step, divide result / 100