Cash Register Challange

Tell us what’s happening:
I edited the code because I have been able to sort a couple of issue. Evreything is fine but 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]]}

  **Your code so far**

function checkCashRegister(price, cash, cid) {
let changes = cash-price;
let totCid = Math.round(cid.reduce((acc, el)=> acc+el[1],0)*100)/100;
let newChange = [];

//creation of currencyUnit object
let currencyUnit = {
"ONE HUNDRED" : 100,
"TWENTY" : 20,
"TEN" : 10,
"FIVE" : 5,
"DOLLAR" : 1,
"QUARTER" : 0.25,
"DIME" : 0.1,
"NICKEL": 0.05,
"PENNY": 0.1,
}

// first test, if the total cash in the register equals the changes
if (changes == totCid){
return {
  status: "CLOSED",
  change: cid
}
};

// second test, if the changes is grater than the total fund in the register
if (changes > totCid){
return {
  status: "INSUFFICIENT_FUNDS",
  change: []
}
};

// if the first two test conditions don't occur then iterate the cid, and check if I have the right currency unit the give back the exact change. While loop is used to map the cid with the currency Unit object.
for(let i = cid.length - 1; i>=0; i--){
let value = 0;
while(currencyUnit[cid[i][0]]<=changes  && cid[i][1]>changes){
value+=currencyUnit[cid[i][0]]
// update the changes for the next while loop iteration
changes = changes-value;
}
// add in the newArry the currency unit used to return the change
if (value>0){
newChange.push([cid[i][0],value])
}
}

// final condition: if changes>0 means I don't have in the cid the right units the return the exact amount...
if (changes>0){
return {
  status: "INSUFFICIENT_FUNDS",
  change: []
}
//...otherwise the change should be zero, and I return the following object
} else {
return {
  status: "OPEN",
  change: newChange
}
}
}

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/90.0.4430.212 Safari/537.36.

Challenge: Cash Register

Link to the challenge:

value+=currencyUnit[cid[i][0]]
// update the changes for the next while loop iteration
changes = changes-value;

Add a console.log(changes) and look what you get - you might be surprised.

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