JS Cash Register Project Issue

Tell us what’s happening:
It passes every test except the last one. I did not realize that the last test calls for the change key sorted in reverse.

   **Your code so far**

function checkCashRegister(price, cash, cid) {
 var diff = cash - price;
 const ogdiff = cash - price;
 var changedue = {
   status: '',
   change: []
 };

let ref = [
 ["PENNY", 0.01],
 ["NICKEL", 0.05],
 ["DIME", 0.10],
 ["QUARTER", 0.25],
 ["ONE", 1],
 ["FIVE", 5],
 ["TEN", 10],
 ["TWENTY", 20],
 ["ONE HUNDRED", 100]
]

cid.reverse();
ref.reverse();

var drawersum = 0;
for (let i = 0; i < cid.length; i++){
 drawersum += cid[i][1];
}

console.log(drawersum);

var total = [...ref];

for (let i = 0; i < ref.length; i++) {
 let returnedcash = 0;
 let bill = cid[i][1]/ref[i][1]
 bill.toFixed(2);

   while (diff.toFixed(2) >= ref[i][1]
         && bill >= 1) {
         diff -= ref[i][1];
         returnedcash += ref[i][1];
         bill--; }

     if(returnedcash > 0) {
       if (returnedcash - Math.floor(returnedcash) !==0){
         total[i][1] = parseFloat(returnedcash);
         } else {

         total[i][1] = returnedcash;
       }
     } else {
       
       total[i][1] = returnedcash;
     }
}

console.log(total)

var sumtotal = 0;
for (let i = 0; i < total.length; i++) {
 sumtotal += total[i][1];
}
sumtotal = sumtotal.toFixed(2);
console.log(sumtotal);

if (sumtotal < ogdiff || drawersum < ogdiff ) {
 changedue.status = "INSUFFICIENT_FUNDS";
} else if (drawersum === ogdiff) {
 changedue.status = "CLOSED";
 changedue.change = cid;
} else {
 let totalafter = [];
 for (let x = 0; x < total.length; x++){
   if (total[x][1] !== 0) {
     totalafter.push(total[x]);
   }
 }
 changedue.status = "OPEN";
 changedue.change = totalafter;
}

console.log(changedue);
 return changedue;
}

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]]);

console.log(checkCashRegister(19.5, 20, [["PENNY", 0.00], ["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/98.0.4758.121 Safari/537.36

Challenge: Cash Register

Link to the challenge:

Hello there.

Do you have a question?

If so, please edit your post to include it in the Tell us what’s happening section.

Learning to describe problems is hard, but it is an important part of learning how to code.

Also, the more information you give us, the more likely we are to be able to help.

Thank you for pointing that out and reminding me to be more clear.
My question is, is the error due to my code being faulty or is it a bug in the test itself? I actually just solved it. I appreciate the swift response to help!

Good job figuring out what the issue was!

Yeah, the order is different when the drawer is open vs closed.

Yeah I didn’t realize that until just now. I’m learning quickly that attention to detail is crucial to learning code. Thanks again!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.