I’m trying to understand why the order of the array changed or needed to change in the last validation checks. I want to write my own code rather then copy or emulate the hint but this order of the output array makes this difficult. Any help with this would be appreciated.
Your code so far
function checkCashRegister(price, cash, cid) {
var money = [
{name: "ONE HUNDRED", value: 100.00},
{name: "TWENTY", value: 20.00},
{name: "TEN", value: 10.00},
{name: "FIVE", value: 5.00},
{name: "ONE", value: 1.00},
{name: "QUARTER", value: 0.25},
{name: "DIME", value: 0.10},
{name: "NICKEL", value: 0.05},
{name: "PENNY", value: 0.01}
];
let tc = 0;
let aArr = [];
let cntcid = 0;
let cL = cid.length-1;
let change = cash - price;
for (let i=0;i<cid.length;i++) {
cntcid = cntcid + cid[cL-i][1];
money[i].total = cid[cL-i][1];
}
function Change() {
this.status = "";
this.change = [];
}
let newC = new Change();
console.log(change);
console.log(cntcid);
if (cntcid < change) {
newC.status = "INSUFFICIENT_FUNDS";
return newC;
} else if (cntcid === change) {
newC.status = "CLOSED";
} else {
newC.status = "OPEN";
}
for (let j=0;j<money.length;j++) {
var a = money[j];
aArr=[];
if (change/a.value >= 1) {
tc = change/a.value - (change/a.value)%1;
if (tc*a.value<=a.total) {
change = +(change - tc*a.value).toFixed(2);
aArr.push(a.name);
aArr[1] = +(tc*a.value).toFixed(2);
} else {
change = +(change - a.total).toFixed(2);
aArr.push(a.name);
aArr[1] = a.total;
}
newC.change.push(aArr);
}
}
console.log(change);
if (change!=0.00) {
newC.status = "INSUFFICIENT FUNDS";
newC.change = [];
}
return newC;
}
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 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register