Tell us what’s happening:
Hello guys I have a problem that I dont understand. run the code and tell me from where comes 0.000000000 numbers.
i have [ ‘PENNY’, 0.5000000000000002 ] instead of 0.5
I tried to fix this with toFixed(2) but it doesnt work on arr[i][1] in the loop.
P.S. I know that my code looks horrible, but i first trying to do the challenge by myself without any hints.
Your code so far
function checkCashRegister(price, cash, cid) {
let result = {
status: '',
change: []
}
let arr1 = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];
let arr = [["PENNY", 0], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0],
["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]];
let change = cash - price;
for (let i = arr1.length - 1; i >= 0; i--) {
while (change > 0 && cid[i][1] > 0) {
if (change < arr1[i]) {
break;
} else {
arr[i][1] = arr[i][1] + arr1[i]
cid[i][1] = cid[i][1].toFixed(2) - arr1[i]
change = change.toFixed(2) - arr1[i];
console.log('cid = ' + cid[i], ' - ' + arr1[i], 'arr = ' + arr[i], 'change = ' + change)
}
}
}
console.log((cid.map(e=>e[1]).reduce((a,b)=>a+b)))
if(change != 0){
result.status = 'INSUFFICIENT_FUNDS';
return result;
}else if((cid.map(e=>e[1]).reduce((a,b)=>a+b))==0){
result.status = "CLOSED"
result.change = arr;
return result
}else{
result.status = "OPEN"
result.change = arr.filter(el => el[1] != 0).reverse();
return result;
}
console.log('arr = ' + arr)
console.log('cid = ' + cid)
}
// Example cash-in-drawer array:
// [["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.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36
.
Link to the challenge: