JavaScript Algorithms and Data Structures Projects - Cash Register

Tell us what’s happening:
My code works for all but the 3rd and 5th checks for this problem.
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]]}
When I run my code, the number of pennies only accumulates to 0.03 and I am not sure why.

checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) should return {status: "INSUFFICIENT_FUNDS", change: []}
I discovered that this fails due to the fact that my code does not have a condition that checks if exact change is available. Again, I am not sure what my approach should be for that.

Your code so far

function checkCashRegister(price, cash, cid) {
  //Register Tallies
let pennyAvail = cid[0][1];
let nickelAvail = cid[1][1];
let dimeAvail = cid[2][1];
let quarterAvail = cid[3][1];
let oneAvail = cid[4][1];
let fiveAvail = cid[5][1];
let tenAvail = cid[6][1];
let twentyAvail = cid[7][1];  
let hundredAvail = cid[8][1];
//Cash in Drawer Total
let totalAvail = pennyAvail + nickelAvail + dimeAvail + quarterAvail + 
oneAvail + fiveAvail + tenAvail + twentyAvail + hundredAvail;
//Change Due Tallies
let pennyDue = 0;
let nickelDue = 0;
let dimeDue = 0;
let quarterDue = 0;
let oneDue = 0;
let fiveDue = 0;
let tenDue = 0;
let twentyDue = 0;  
let hundredDue = 0;
//Change due
let changeDue = cash - price;
let changeGiven = {
  status: "OPEN",
  change: []
};
//CONDITIONAL STATMENTS
/*If the conditions of the 1st and 2nd if statements are not met
A for-loop is used to decrement the total change due while also keeping a
tally of which coins or bills are to be returned.

Once i reaches 0, we exit the loop*/
if (changeDue > totalAvail) {
  changeGiven.status = "INSUFFICIENT_FUNDS";
  return changeGiven;
} else if (changeDue == totalAvail) {
  changeGiven.status = "CLOSED";
  changeGiven.change.push(...cid);
  return changeGiven;
} else {
  for (let i = changeDue; i > 0;) {
    if (i >= 100 && hundredAvail > 0) {
      i -= 100;
      hundredAvail -= 100;
      hundredDue += 100;
    } else if (i >= 20 && twentyAvail > 0) {
      i -= 20;
      twentyAvail -= 20;
      twentyDue += 20;
    } else if (i >= 10 && tenAvail > 0) {
      i -= 10;
      tenAvail -= 10;
      tenDue += 10;
    } else if (i >= 5 && fiveAvail > 0) {
      i -= 5;
      fiveAvail -= 5;
      fiveDue += 5;
    } else if (i >= 1 && oneAvail > 0) {
      i -= 1;
      oneAvail -= 1;
      oneDue += 1;
    } else if (i >= 0.25 && quarterAvail > 0) {
      i -= 0.25;
      quarterAvail -= 0.25;
      quarterDue += 0.25;
    } else if (i >= 0.1 && dimeAvail > 0) {
      i -= 0.1;
      dimeAvail -= 0.1;
      dimeDue += 0.1;
    } else if (i >= 0.05 && nickelAvail > 0) {
      i -= 0.05;
      nickelAvail -= 0.05;
      nickelDue += 0.05;
    } else if (i >= 0.01 && pennyAvail > 0) {
      i -= 0.01;
      pennyAvail -= 0.01;
      pennyDue += 0.01;
    }
  }
}
/*After exiting the loop, all tallies that were accumulated are pushed
onto the change property within the changeGiven object*/
if (hundredDue > 0) {
  changeGiven.change.push(["ONE HUNDRED", hundredDue]);
}if (twentyDue > 0) {
  changeGiven.change.push(["TWENTY", twentyDue]);
}if (tenDue > 0) {
  changeGiven.change.push(["TEN", tenDue]);
}if (fiveDue > 0) {
  changeGiven.change.push(["FIVE", fiveDue]);
}if (oneDue > 0) {
  changeGiven.change.push(["ONE", oneDue]);
}if (quarterDue > 0) {
  changeGiven.change.push(["QUARTER", quarterDue]);
}if (dimeDue > 0) {
  changeGiven.change.push(["DIME", dimeDue]);
}if (nickelDue > 0) {
  changeGiven.change.push(["NICKEL", nickelDue]);
}if (pennyDue > 0) {
  changeGiven.change.push(["PENNY", pennyDue]);
} return changeGiven;
}

console.log(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/113.0.0.0 Safari/537.36

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

Link to the challenge:

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