Hi,
The issue I am facing is that I have multiple values of accumulated currencies in 2d lists in the array of statObj
than having just one value name and accumulating currency value.
Can someone please suggest me how to remove value and keep the arrays of currency with highest value.
Hereby is my code:
function checkCashRegister(price, cash, cid) {
const originalCid = cid.map((el) => [...el]);
let currency = [
["PENNY", 0.01],
["NICKEL", 0.05],
["DIME", 0.1],
["QUARTER", 0.25],
["ONE", 1],
["FIVE", 5],
["TEN", 10],
["TWENTY", 20],
["ONE HUNDRED", 100]
];
let change = cash - price;
let statObj = {
status: "",
change: []
};
let totalSum = 0;
let countIndex = 0;
function updateChange() {
debugger;
let usableCurrencyArr = [];
let closestVal;
for (let i = 0; i < currency.length; i++) {
if (currency[i][1] <= change && cid[i][1] > 0) {
totalSum += cid[i][1];
usableCurrencyArr.push(currency[i][1]);
closestVal = Math.max(...usableCurrencyArr);
}
}
for (let i = 0; i < currency.length; i++) {
console.log(closestVal);
if (closestVal === currency[i][1]) {
console.log(i);
return i;
}
}
}
console.log("index:", countIndex);
while (change > 0) {
let matchedIndex = updateChange();
if (totalSum < change) {
console.log(totalSum < change);
statObj.status = "INSUFFICIENT_FUNDS";
console.log(statObj);
return statObj;
}
if (change === cid[matchedIndex][1]) {
cid[matchedIndex][1] = cid[matchedIndex][1] - change;
console.log(cid[matchedIndex][1], originalCid[matchedIndex][1]);
if (cid.every((el) => el[1] === 0)) {
statObj.status = "CLOSED";
statObj.change = originalCid;
console.log(statObj);
return statObj;
}
}
if (currency[matchedIndex][0] === cid[matchedIndex][0]) {
console.log(currency[matchedIndex][0] === cid[matchedIndex][0]);
cid[matchedIndex][1] = parseFloat(
(cid[matchedIndex][1] - currency[matchedIndex][1]).toFixed(2)
);
change = parseFloat((change - currency[matchedIndex][1]).toFixed(2));
console.log(change);
statObj.status = "OPEN";
statObj.change.push([
cid[matchedIndex][0],
parseFloat(
(originalCid[matchedIndex][1] - cid[matchedIndex][1]).toFixed(2)
)
]);
console.log(statObj);
updateChange();
}
//change = change - currency[matchedIndex][1]
}
return statObj;
}
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]
]);
Following is the link to the project problem - https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register
Thanks!!!