Tell us what’s happening:
Hi everyone,
this is my code for the JS challenge " Cash Register".
I can pass other tests except for this one:
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]])
should return {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}
.
Can somebody helps me debug ? Thanks a lot.
Your code so far
function checkCashRegister(price, cash, cid) {
let change=cash-price;
let sum=0;
for (let i=0;i<cid.length;i++){
sum+=cid[i][1];
}
if(change === sum){
return {status: "CLOSED", change: cid}
} else if (change > sum){
return {status: "INSUFFICIENT_FUNDS", change: []}
} else {
let output=[];
let table=[100,20,10,5,1,0.25,0.1,0.05,0.01];
let newCid=cid.reverse();
for (let i=0;i<newCid.length;i++){
if (change<=newCid[i][1]){
output.push(parseInt(change/table[i])*table[i]);
change -= parseInt(change/table[i])*table[i];
} else {
output.push(newCid[i][1]);
change -= newCid[i][1];
}
}
if (change===0){
let result=[];
let map=[
["ONE HUNDRED", output[0]],
["TWENTY", output[1]],
["TEN", output[2]],
["FIVE", output[3]],
["ONE", output[4]],
["QUARTER", output[5]],
["DIME", output[6]],
["NICKEL", output[7]],
["PENNY", output[8]]
];
for (let i=0;i<map.length;i++){
if (map[i][1]!==0){
result.push(map[i]);
}
}
return {status: "OPEN", change: result }
}else {return {status: "INSUFFICIENT_FUNDS", 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]]);
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36
Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register
Link to the challenge: