currently I am stuck while testing for when I have a $1 and a penny in my cash register and the expected change is a quarter. I can’t seem to get the correct test right. Other test seems to be working quite alright unlike the one I mentioned just now. Here is my code:
function checkCashRegister(price, cash, cid) {
const currencyUnit = [
{ 'name': 'ONE HUNDRED', 'value': 100 },
{ 'name': 'TWENTY', 'value': 20 },
{ 'name': 'TEN', 'value': 10 },
{ 'name': 'FIVE', 'value': 5 },
{ 'name': 'ONE', 'value': 1 },
{ 'name': 'QUARTER', 'value': 0.25 },
{ 'name': 'DIME', 'value': 0.1 },
{ 'name': 'NICKEL', 'value': 0.05 },
{ 'name': 'PENNY', 'value': 0.01 }
];
//console.log(cid);
let copyDrawer = [];
// const denom = [100, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01];
for (let arr of cid) {
copyDrawer.push(arr);
}
// console.log(copyDrawer);
let expectedChange = cash - price;
let totalInRegister = function() {
return copyDrawer.map((arr) => {
return arr[1];
}).reduce((acc, curr) => {
return acc + curr;
}, 0).toFixed(2);
};
// Note 'change' is not the same as 'change()';
let sortedDrawer = copyDrawer.reverse(); //cid.reverse();
// console.log(sortedDrawer);
let misc;
let drawer = [];
let billAndCoins = [];
let initialChange = [];
let finale = {};
// get an array of the number of currency in the cash drawer by dividing each element in sortedDrawer
// by the currencyUnit object values and store this in variable frequency.
let frequency = sortedDrawer.map((inx, i) => { return Math.round(inx[1] / currencyUnit[i].value); });
//set up a while loop inside a for loop deducting expected change from currencyUnit value
function test() {
for (let idx = 0; idx < currencyUnit.length; idx++) {
let iterator = frequency[idx];
let cashi = 0;
while (expectedChange >= currencyUnit[idx].value && iterator > 0) {
misc = Number((expectedChange - currencyUnit[idx].value).toFixed(2));
if (misc !== 0.00 && currencyUnit[idx].name === 'PENNY') {
console.log({
status: "INSUFFICIENT_FUNDS",
change: []
});
}
expectedChange = misc;
iterator--;
cashi += currencyUnit[idx].value;
}
drawer.push(Math.round(cashi * 100) / 100);
}
}
test();
function too() {
// map over currencyUnit and variable drawer to access name of currency and the total
// amount of bills used during the change/bill sorting and push into billAndCoins.
// splice these values in pairs using splice() method and save in variable splitToValue
// push the result of the splice() and save in variable initialChange.
currencyUnit.map((val, idx) => {
billAndCoins.push(val.name, drawer[idx]);
let splitToValue = billAndCoins.splice(0, 2);
initialChange.push(splitToValue);
});
//Filter out bills that was used during the change sorting
// these are where you have values greater than 0 in the array
let usedBillsAndCoins = initialChange.filter((val) => {
return val[1] > 0;
});
finale.status = 'OPEN';
finale.change = usedBillsAndCoins;
console.log(finale);
// return finale;
}
too();
}
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: []}