Tell us what’s happening:
is it too hard or am i dumb
I’m trying to switch to the next currency when the register runs out of that money, but it doesn’t work and i don’t why. And the first one is float couldn’t fix that either.
every kind of help would be much appreciated, thanks in advance
Your code so far
function checkCashRegister(price, cash, cid) {
let change = cash - price;
let status;
let currency = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];
let arr = [];
let money = 0;
for(let i=0; i < cid.length; i++){
money += cid[i][1];
}
if(change > money) return {status: "INSUFFICIENT_FUNDS", change: []};
for(let i = currency.length - 1; i >= 0; i--){
let result = 0;
let counter = 0;
while(currency[i] <= change){ // something is wrong here, i think
change -= currency[i];
change = change.toFixed(2);
cid[i][1] -= currency[i];
if(cid[i][1] < 0){
continue;
} else {
result += currency[i];
}
if(status === undefined) status = "OPEN";
if(counter === 0){
arr.push(cid[i][0], result);
} else {
arr[1] += currency[i];
}
counter++;
}
}
let final = {status: status, change: [arr]}
console.log(arr)
return final;
}
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 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36
Challenge: Cash Register
Link to the challenge: