Hey there, i have an issue with the Cash Register Challenge.
I marked the part in the code with a comment.
In the forEach loop i am substracting number values until claim is zero or the cidObject runs out of coins. The substracted values are stored in the change object.
It works with every test except the last one.
In the last one instead of the value 50 for Pennys i get 0.5000000000000002.
If you run the script youll see it in the console.
I hardcoded the solution–not that elegant.
Can someone help me out ? I was/am stuck on this for quiet some time.
Here is the code:
const money ={
"100.00": "ONE HUNDRED",
"20.00": "TWENTY",
"10.00": "TEN",
"5.00": "FIVE",
"1.00": "ONE",
"0.25": "QUARTER",
"0.10": "DIME",
"0.05": "NICKEL",
"0.01": "PENNY"
}
function checkCashRegister(price, cash, cid) {
// claim
let claim = cash-price;
//create a currency object (change) and the cid Total
let change = {};
let cidTotal = 0
cid.forEach(n=>{
let key = n[0];
let value = n[1];
change[key]= 0
cidTotal+=n[1]
})
// create an object to substract the value from
let cidObject ={};
cid.forEach(n=>{
let key = n[0];
let value = n[1];
cidObject[key] = value;
})
// get amount of curreny units
let numbers = Object.keys(money);
//This is the problematic part:
// I am substracting from claim and cidObject, until their values are 0
// and i am storing those substracted values in the change object
numbers.forEach(value =>{
while (value<= claim && value <=cidObject[money[value]]){
claim= claim.toFixed(2)-value;
cidObject[money[value]]=cidObject[money[value]].toFixed(2)-value;
change[money[value]]+=1*value;
}
})
console.log(change)
//hardcoded solution: to fix the float: PENNY 0.5000000000000002 => PENNY: 0.5
change.PENNY = parseFloat(change.PENNY.toFixed(2))
console.log(change)
}
// checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90 ], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]])
// checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]])
// checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])
// checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])
checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])
Thanks !