Tell us what’s happening:
I’m one step away from my first certification, but somehow this register refuses to work, the console outputs
{ status: ‘OPEN’,
change:
[ [ ‘TWENTY’, 60 ],
[ ‘TEN’, 20 ],
[ ‘FIVE’, 15 ],
[ ‘ONE’, 1 ],
[ ‘QUARTER’, 0.5 ],
[ ‘DIME’, 0.20000000000000018 ],
[ ‘PENNY’, 0.030000000000000027 ] ] }
for the call at the end, while it should be 0.2 and 0.04.
I know that this code is VERY messy but please help, organise the code on your own, this is a very delicate and small mistake that i completley don’t understand so any experienced users’ help will be appreciated.
Thanks!
Your code so far
function checkCashRegister(price, cash, cid) {
var totalCash = 0;
function totalCashCalc(arr) {
for (let i = 0; i < arr.length; i++) {
totalCash += arr[i][1]}}
totalCashCalc(cid);
var result = [];
var vals = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];
var change = cash - price;
if (totalCash < change) {
return {status: "INSUFFICIENT_FUNDS", 'change': []}}
else if (totalCash == change) {
return {status: "CLOSED", 'change': cid}}
else {
for (let i = cid.length - 1; i >= 0; i--) {
var changeBefore = cid[i][1];
while (change >= vals[i] && cid[i][1] >= vals[i]) {
change -= vals[i]
cid[i][1] -= vals[i]};
result.push([cid[i][0], changeBefore - cid[i][1]])
}return {status: "OPEN", change: result.filter(function (a) {if (!a[1]) {return false}
else {return true}})}
}
}
console.log(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]]));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36
.
Challenge: Cash Register
Link to the challenge: