The cash register console keeps returning: “TypeError: Cannot read properties of undefined (reading ‘1’)”, I mean one as in the number one, not a variable called “1”. Can someone please help me?
We would be happy to help but we can’t see any code, so we have nothing to work with. Can you share your full code with us please?
function checkCashRegister(price, cash, cid) {
const cashAndCoinValues = [["PENNY", 0.01], ["NICKEL", 0.05], ["DIME", 0.10], ["QUARTER", 0.25], ["ONE", 1], ["FIVE", 5], ["TEN", 10], ["TWENTY", 20], ["ONE_HUNDRED", 100]];
let totalChangeGiven = 0;
let changeDue = cash - price;
let totalCashInDrawer = 0;
for (let i = 0; i < cid.length; i++) {
totalCashInDrawer += cid[i][1];
}
let returnableCashAndCoin = [];
let totalOfReturnableCashAndCoin = 0;
for (let j = 0; j < cashAndCoinValues.length; j++) {
if (cashAndCoinValues[j][1] < changeDue) {
if (cid[j][1] >= changeDue) {
returnableCashAndCoin.push(cashAndCoinValues[j]);
totalOfReturnableCashAndCoin += cashAndCoinValues[j][1];
}
}
}
let amountToReturnArr = [];
let typeToReturnArr = [];
for (let k = 0; k < returnableCashAndCoin.length; k++) {
amountToReturnArr.push(changeDue / returnableCashAndCoin[k][1]);
typeToReturnArr.push(returnableCashAndCoin[k][0]);
}
let lowestAmountToReturn = Math.min(...amountToReturnArr);
let returnObject = {status: "", change: []};
if (totalCashInDrawer === changeDue) {
returnObject.status = "CLOSED";
returnObject.change.push(cid);
}
if (totalCashInDrawer < changeDue) {
returnObject.status = "INSUFFICIENT_FUNDS";
}
if (totalCashInDrawer > changeDue) {
returnObject.status = "OPEN";
if (cid[amountToReturnArr.indexOf(lowestAmountToReturn)][1] === changeDue) {
returnObject.change.push([cid[amountToReturnArr.indexOf(lowestAmountToReturn)][0], changeDue]);
}
if (cid[amountToReturnArr.indexOf(lowestAmountToReturn)][1] < changeDue) {
returnObject.change.push([cid[amountToReturnArr.indexOf(lowestAmountToReturn)][0], cid[amountToReturnArr.indexOf(lowestAmountToReturn)][1]]);
totalChangeGiven += cid[amountToReturnArr.indexOf(lowestAmountToReturn)][1];
for (let l = 1; l < amountToReturnArr.indexOf(lowestAmountToReturn) + 1; l++) {
if (totalChangeGiven + cid[amountToReturnArr.indexOf(lowestAmountToReturn) - l][1] > changeDue) {
while (totalChangeGiven + cashAndCoinValues[amountToReturnArr.indexOf(lowestAmountToReturn) - l][1] <= changeDue) {
if (returnObject.change[returnObject.change.length - 1][0] === cashAndCoinValues[amountToReturnArr.indexOf(lowestAmountToReturn) - l][0]) {
returnObject.change[returnObject.change.length - 1][1] += cashAndCoinValues[amountToReturnArr.indexOf(lowestAmountToReturn) - l][1];
} else {
returnObject.change.push([cashAndCoinValues[amountToReturnArr.indexOf(lowestAmountToReturn) - l][0], cashAndCoinValues[amountToReturnArr.indexOf(lowestAmountToReturn) - l][1]]);
}
totalChangeGiven += cashAndCoinValues[amountToReturnArr.indexOf(lowestAmountToReturn) - l][1];
}
} else {
returnObject.change.push([cid[amountToReturnArr.indexOf(lowestAmountToReturn) - l][0], cid[amountToReturnArr.indexOf(lowestAmountToReturn) - l][1]]);
totalChangeGiven += cid[amountToReturnArr.indexOf(lowestAmountToReturn) - l][1];
}
}
}
}
return returnObject;
}
console.log(checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]));
probably this
cid[amountToReturnArr.indexOf(lowestAmountToReturn)][1]
amountToReturnArr
is an empty array, spreading an empty array into Math.min()
returns Infinity
. Passing Infinity
into .indexOf()
returns -1
console.log(amountToReturnArr); // []
let lowestAmountToReturn = Math.min(...amountToReturnArr);
console.log(lowestAmountToReturn); // Infinity
I’d suggest you add a bunch of console.log and check your assumptions about your values/conditions.
You can also run it through the debugger and step through the code (outside of the fCC editor).
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.