Good morning! I’ve searched through a whole bunch of other posts related to this but I can’t seem to pinpoint my issue. For test case #6, I am getting the correct answer in my Chrome DevTools console. Also at one point I had test cases #1, 2, 4 working and I am just so lost right now I don’t even know what I’m looking at. So if anyone could give me a few pointers or a hint at an area where I’ve gone wrong, I’d appreciate it.
This is what I’m seeing in my console for #6.
-
{status: “CLOSED”, change: Array(9)}
-
change: (9) [Array(2), Array(2), Array(2), Array(2), Array(2), Array(2), Array(2), Array(2), Array(2)]
-
status: “CLOSED”
-
proto: Object
-
change: Array(9)
-
0: (2) [“PENNY”, 0.5]
-
1: (2) [“NICKEL”, 0]
-
2: (2) [“DIME”, 0]
-
3: (2) [“QUARTER”, 0]
-
4: (2) [“ONE”, 0]
-
5: (2) [“FIVE”, 0]
-
6: (2) [“TEN”, 0]
-
7: (2) [“TWENTY”, 0]
-
8: (2) [“ONE HUNDRED”, 0]
let cashRegisterStatus = { status: null, change: null};
let valueOnly = [];
function checkCashRegister(price, cash, cashInDrawer) {
let total = getCashInDrawer(cashInDrawer);
console.log("The total amount in the drawer is $" + total);
let custChange = parseFloat(cash-price).toFixed(2);
console.log("Customer needs $" + custChange + " in change");
let giveThisToCust = getCustChange(custChange, cashInDrawer);
//calling getCustChange function to be used in checkCashRegister function
if(Number(giveThisToCust) < 0 && Number(custChange) > 0) {
cashRegisterStatus.status = "INSUFFICIENT_FUNDS";
cashRegisterStatus.change = [];
console.log(cashRegisterStatus);
return cashRegisterStatus;
}
if (Number(custChange) > Number(total)) {
cashRegisterStatus.status = "INSUFFICIENT_FUNDS";
cashRegisterStatus.change = [];
console.log(cashRegisterStatus);
return cashRegisterStatus;
}
if (Number(custChange) === Number(total)) {
console.log(custChange);
console.log(total);
cashRegisterStatus.status = "CLOSED";
cashRegisterStatus.change=[...cashInDrawer];
console.log(cashRegisterStatus);
return cashRegisterStatus;
}
else {
cashRegisterStatus.status = "OPEN";
cashRegisterStatus.change = [...giveThisToCust];
console.log(cashRegisterStatus);
return cashRegisterStatus;
}
} //end checkCashRegister function
function getCashInDrawer (cashInDrawer) {
let available = 0;
for (let i = 0; i < cashInDrawer.length; i++) {
valueOnly.push(cashInDrawer[i][1]);
available = valueOnly.reduce((total, amount) => total + amount).toFixed(2);
}
return available;
}
function getCustChange(custChange, cashInDrawer){
let change = [];
const denomDict = { //holding all values for each money type
"PENNY": .01,
"NICKEL": .05,
"DIME": .10,
"QUARTER": .25,
"ONE": 1.00,
"FIVE": 5.00,
"TEN": 10.00,
"TWENTY": 20.00,
"ONE HUNDRED": 100.00
};
for( let i = cashInDrawer.length - 1; i >= 0; i--){ //looping backwards through the denomDict
const coinName = cashInDrawer[i][0]; //[i] is looped element, 0 is name of money type
console.log("This is name of coin " + coinName);
const coinTotal = cashInDrawer[i][1]; // [i] is looped element, 1 is value of money type
console.log("How much of all coin type?" + coinTotal);
const coinValue = denomDict[coinName];//using value of coinName (name of money) and matching to name in denomDict
console.log("This is the value of the coin " + coinValue);
let coinAmount = (coinTotal / coinValue ).toFixed(2);
console.log("How many coins?" + coinAmount);
let returnedCoins = 0;
while (custChange >= coinValue && coinAmount > 0) { //while we still need change and coinAmount is greater than 0
custChange -= coinValue;
custChange = custChange.toFixed(2);
coinAmount --; //amount of coins left in the drawer
returnedCoins ++; //add to amount return, add 1
}
if(returnedCoins > 0) {
change.push([coinName, returnedCoins * coinValue]);
;// pushing name of the coin "PENNY", plus the amount of change in pennies (value of coin * quantity of coin)
}
}
console.log(change);
return change;
}
checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);