Tell us what’s happening:
Asking for someone to point out the problem.
Not getting the expected output of { status: OPEN, change: [[“QUARTER”, 0.5]] }
Your code so far
function checkCashRegister(price, cash, cid) {
const change = cash - price;
//changeDue is the amount of money to be given to the customer
let changeDue = [["PENNY", 0], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]];
//cashReg is cid and the representation of the cash register
let cashReg = [...cid];
//moneyUnit is the denomination of each bill contained in the cash register. It is arranged in accordance of order in the cash register
let moneyUnit = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];
//Declare two variables denoting status of cash register and changeDue respectively
let _status, _change;
//Create an IIFE that will check the amount of the cash register to decide whether to call another function or not
( function() {
//Check the amount of the cash register
let cashRegContent = cashReg.reduce( ((total,entry) => total + entry[1]),0);
cashRegContent = cashRegContent.toFixed(2);
cashRegContent = parseFloat(cashRegContent);
//When the total amount of the cash register is equal to the chnange
if ( cashRegContent === change ) {_status = "CLOSED"; _change = cid}
//When the total amount of the cash register is less than the change
else if ( cashRegContent < change ) {_status = "INSUFFICIENT FUNDS"; _change = []}
//Call a function when the total amount of the cash register is more than the change
else if ( cashRegContent > change) { processChange(); }
}) ();
// Function processChange will check if the given denomination of bill can provide for the change
function processChange() {
//For each denomination, bal is the amount of the preceeding denominations in the ChangeDue
//Iteration starts from the end of the array
for (let j = cashReg.length - 1 ; j<0 ; j--) {
let bal = changeDue.slice(j+1).reduce( ((total,entry) => total + entry[1]),0);
bal = bal.toFixed(2);
bal = parseFloat(bal);
//partial is the amount to take from cashReg before putting it to the ChangeDue
let partial = 0;
while ( (bal + partial + moneyUnit[j]) <= change && cashReg[j][1] > 0) {
partial += moneyUnit[j];
cashReg[j][1] -= moneyUnit[j];
}
changeDue[j][1] = partial;
}
}
let balance = changeDue.reduce( ((total,entry) => total + entry[1]),0);
balance = balance.toFixed(2);
balance = parseFloat(balance);
//console.log(balance);
//console.log(changeDue);
//Depending on the value of balance, call a function and set the corresponding values to variables _status and _change
if (balance === change) { trim(); _status = "OPEN" ; _change = changeDue}
else if (balance < change) {_status = "INSUFFICIENT FUNDS"; _change = [] }
function trim() { changeDue = changeDue.filter(entry => {return entry[1] !== 0} )}
let regStat = {status: _status, change: _change};
return regStat;
}
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: <code>Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:105.0) Gecko/20100101 Firefox/105.0</code>
**Challenge:** JavaScript Algorithms and Data Structures Projects - Cash Register
**Link to the challenge:**
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register