Hi!
After a whole day trying to figure out why decimals in my calcule got insane, now something else it’s happening.
I tryed 3 ways to copy cid
: Array.from()
, copy = [...cid]
, copy = cid.slice()
, but every time both cid
and copyCID
have the same value at the end.
How can I fix it? All tests are passed, but the last one… this one.
Your code so far
function checkCashRegister(price, cash, cid) {
let money = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];
let rest = cash - price;
let change = [];
let copyCID = [...cid]; //trying to copy 'cid'
let sumCID = 0;
let sum = 0;
// console.log(copyCID)
for (let i = copyCID.length-1; i >= 0; i--) {
let j = 0;
sumCID = parseFloat((sumCID + copyCID[i][1]).toFixed(2));
while (rest >= money[i] && copyCID[i][1] > 0) {
rest = parseFloat((rest - money[i]).toFixed(2));
copyCID[i][1] = parseFloat((copyCID[i][1] - money[i]).toFixed(2));;
j++;
if (rest < money[i] || copyCID[i][1] === 0)
{change.push([copyCID[i][0],money[i]*j])}
}
}
console.log(cid) /* Both have same answer...
console.log(copyCID) with [['PENNY', 0], ...] */
for (let i = 0; i < change.length; i++) {
sum = parseFloat((sum + change[i][1]).toFixed(2))
}
if (sumCID < rest || sum < rest) return {status: "INSUFFICIENT_FUNDS", change: []}
else if (sumCID == sum) return {status: "CLOSED", change: copyCID}
else return {status: "OPEN", change: change}
}
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 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36
.
Challenge: Cash Register
Link to the challenge: