Ok, what line is supposed to add the text about the pennies
this line:
const formattedChangeStr = changeArray.map(([unit, amount]) => `${unit}: $${amount.toFixed(2)}`).join(' ')
But I have all of that logic in the else statement that checks (cidTotal > change)
if (cashInputVal > price) {
if(cidTotal < change){
status = "INSUFFICIENT_FUNDS";
changeDue.innerText =`Status: ${status}`;
}
else if(cidTotal === change){
status = "CLOSED";
changeDue.innerText =`Status: ${status}`;
}
//if(cidTotal > change)
else{
status = "OPEN";
// IRL I would start with the largest bills and remove them from the register by subtracting,
// like if I had $17.34 I would see how many tens I need, then how many fives, then ones, then quarters and dimes etc.
// and subtracting my change from each unit/denomination and updating the array
const unitValues = {
"PENNY": 0.01,
"NICKEL": 0.05,
"DIME": 0.10,
"QUARTER": 0.25,
"ONE": 1.00,
"FIVE": 5.00,
"TEN": 10.00,
"TWENTY": 20.00,
"ONE HUNDRED": 100.00
};
// loop backwards thru the 2d array cid array big numbers/bills are first
for (let i = cid.length - 1; i >= 0; i--){
// destructure inner array to break it up into units/denominations and cash
let [unit, amount] = cid[i];
let unitValue = unitValues[unit];
let amountToGive = 0;
// while money of that denomination is available in draw
// and still change to be givrn back
while (amount >= unitValue && unitValue <= change) {
//update change
change = parseFloat((change - unitValue).toFixed(2));
//update emount of cash in drawer
amount = parseFloat((amount - unitValue).toFixed(2));
//update amount to give
amountToGive = parseFloat((amountToGive + unitValue).toFixed(2));
console.log('end of while loop?');
}
//populate the change array
if (amountToGive > 0) {
changeArray.push([unit, amountToGive]);
}
}
if (change > 0) {
console.log(`change is: ${change}`)
// couldn't give back full change
status = "INSUFFICIENT_FUNDS";
changeDue.innerText = `Status: ${status}`;
return;
}
if (cidTotal === calculateChange(cashInputVal, price)) {
status = "CLOSED";
changeArray = [...cid];
}
else {
status = "OPEN";
}
const formattedChangeStr = changeArray.map(([unit, amount]) => `${unit}: $${amount.toFixed(2)}`).join(' ')
changeDue.innerText = `Status: ${status} ${formattedChangeStr}`;
}
}
})
So are the contents of changeArray correct?
I believe so since the first 17 tests are passing.
You don’t need to believe. You should check for this specific test case.
So a return is being triggered before that point probably?
I thinks o but the only return I have is here:
if (change > 0) {
console.log(`change is: ${change}`)
// couldn't give back full change
status = "INSUFFICIENT_FUNDS";
changeDue.innerText = `Status: ${status}`;
return;
}
And if I remove that return test cases 16 and 17 fail.
-
- When
priceis19.5, the value in the#cashelement is20,cidis[["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]], and the#purchase-btnelement is clicked, the value in the#change-dueelement should be"Status: INSUFFICIENT_FUNDS".
- When
-
Failed:17. When
priceis less than the value in the#cashelement, total cash in drawercidis greater than change due, but the individual denomination amounts make it impossible to return needed change, when the#purchase-btnelement is clicked, the value in the#change-dueelement should be"Status: INSUFFICIENT_FUNDS"
So why is that check falsely triggering?
I’m not really sure why.
You should investigate that. Understanding that is likely key. I would guess it might be a floating point issue.
Ah I see I’ll take a closer look at that later. Thank you.
Ok so the issue was I was completely ignored the creation of a new array/results string with the expected change in the case where the money in the draw was equal to the change due
else if(cidTotal === change){
status = "CLOSED";
// make changeArray new array that includes only the denominations from cid that have amount of money > 0
// _ makes it so you ignore the unit name, looking only at the amount
changeArray = cid.filter(([_, amount]) => amount > 0);
//fix format
const formattedChangeStr = changeArray.map(([unit, amount]) => `${unit}: $${amount.toFixed(2)}`).join(' ');
changeDue.innerText = `Status: ${status} ${formattedChangeStr}`;
return;}
After adding this logic my code passes all the tests and I have completed this project!!
