Tell us what’s happening:
Hi, I have written some code to finish the Cash Register Task, but with the code below i can not pass the Test Case 3 even the printing result is same as the answer.
Also i am curious that why 96.74 - 60.00 is = 36.37999
Anyone knows? Thanks
Your code so far
function checkCashRegister(price, cash, cid) {
var change= [];
const unit = {
"PENNY": 0.01,
"NICKEL": 0.05,
"Dime": 0.1,
"QUARTER": 0.25,
"ONE": 1,
"FIVE": 5,
"TEN": 10,
"TWENTY": 20,
"ONE HUNDRED": 100
};
var index = cid.length;
let change_value = cash - price;
var status = "emtpy";
for (let i = index - 1; i >= 0; i--) {
let CurrencyUnit = cid[i][0];
let cash_in_drawer = 0;
if (cid[i][1] != 0 && unit[CurrencyUnit] < change_value) {
let token_cash;
if(change_value > cid[i][1]){
token_cash = cid[i][1];
}else{
token_cash = unit[CurrencyUnit] * Math.floor(change_value / unit[CurrencyUnit]);
}
cash_in_drawer += cid[i][1];
cash_in_drawer -= token_cash;
change.push([cid[i][0], token_cash]);
change_value = change_value - token_cash;
change_value = Math.round(change_value * 100) / 100;
if(change_value == 0 && cash_in_drawer != 0){
status = "OPEN";
}else{
status = "CLOSED";
change = cid
}
}
}
if(change_value != 0){
status = "INSUFFICIENT_FUNDS";
change = [];
}
// Here is your change, ma'am.
return {
"status": status,
"change": change
};
}
// 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", 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/73.0.3683.86 Safari/537.36
.
Link to the challenge:
For security reasons, you can’t include links in your posts yet. Use the freeCodeCamp forum a bit more to unlock this ability.