I had spent a few days trying to figure out why my solution wasn’t working, running into various strange results and contradictions in the passed tests depending on my syntax.
However, on the third day of my tests, I decided to run the various check conditions on repl.it and found all of my code worked.
Am I perhaps overlooking something about the interface FCC uses?
Otherwise, I’m unsure of how to complete the challenge without coming up with a completely new solution.
Here is my code:
function checkCashRegister(price, cash, cid) {
// SET UP CHANGE VARIABLE
let changeDue = cash - price;
// SET VALUES OF CASH TYPES:
let cashValues = [
{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.10},
{name: "NICKEL", value: 0.05},
{name: "PENNY", value: 0.01}
]
// DETERMINE AMOUNTS OF CASH IN DRAWER:
let totalPennies = (cid[0][1] / cashValues[8].value);
let unroundedNickels = (cid[1][1] / cashValues[7].value);
let totalNickels = Number((unroundedNickels).toFixed(2));
let totalDimes = (cid[2][1] / cashValues[6].value);
let totalQuarters = (cid[3][1] / cashValues[5].value);
let totalOnes = (cid[4][1] / cashValues[4].value);
let totalFives = (cid[5][1] / cashValues[3].value);
let totalTens = (cid[6][1] / cashValues[2].value);
let totalTwenties = (cid[7][1] / cashValues[1].value);
let totalHundreds = (cid[8][1] / cashValues[0].value);
let cashAmounts = [
{name: "HUNDREDS", amount: totalHundreds},
{name: 'TWENTIES', amount: totalTwenties},
{name: 'TENS', amount: totalTens},
{name: 'FIVES', amount: totalFives},
{name: 'ONES', amount: totalOnes},
{name: 'QUARTERS', amount: totalQuarters},
{name: 'DIMES', amount: totalDimes},
{name: 'NICKLES', amount: totalNickels},
{name: 'PENNIES', amount: totalPennies}
];
// TOTAL AMOUNT OF CASH IN DRAWER:
// ADD VALUES IN DRAWER
let cidTotal = (cid[0][1] + cid[1][1] + cid[2][1] + cid[3][1] + cid[4][1] + cid[5][1] + cid[6][1] + cid[7][1] + cid[8][1]);
// LET THAT TOTAL EQUAL THE TRUE USED TOTAL
let trueTotal = Number((cidTotal).toFixed(2));
// REVERSE THE CID TO WORK BETTER IN LOOPS
let usefulCID = cid.reverse();
// SET UP REGISTER OBJECT
let Register = {total: trueTotal, amounts: cashAmounts, values: cashValues, inDrawer: cid}
// SET UP RESULT OBJECT
// I.E. WHAT WILL BE RETURNED
let Result = {status: "", change: []}
let changeArr = [];
let totVal = 0;
let updatedChange = 0;
let num = 0;
let cashObj = [];
// ============================================================================
// ()()()()()()()()()()()()()()[ MAIN STUFF ]()()()()()()()()()()()()()()()()()
// ============================================================================
if (changeDue === Register.total) {
Result.status = "CLOSED"
Result.change = cid;
console.log("REGISTER TOTAL: " + Register.total)
console.log("Change Due: " + changeDue)
console.log("CLOSED RESULT STATUS: " + Result.status)
console.log("CLOSED RESULT CHANGE: " + Result.change)
return Result;
}
if (Register.total < changeDue) {
Result.status = "INSUFFICIENT_FUNDS"
return Result;
}
for (let i = 0; i < cashValues.length; i++) {
let amt = Register.amounts[i].amount;
while (Register.values[i].value <= changeDue && amt > 0) {
num++
console.log("CHANGE DUE: " + changeDue);
console.log("Less than Change Due: " + Register.values[i].value)
updatedChange = (changeDue - Number((Register.values[i].value).toFixed(2)))
changeDue = Number((updatedChange).toFixed(2));
amt = (Register.amounts[i].amount - num)
console.log("Amount of " + Register.values[i].name + " left: " + amt)
totVal += Register.values[i].value
console.log("TOTAL VAL: " + totVal)
if (Register.values[i].value > changeDue) {
console.log("Max of " + Register.values[i].name + "'s Needed Reached: " + num)
cashObj.push(Register.values[i].name)
cashObj.push(totVal)
console.log("Cash OBJ: " + cashObj)
break;
}
else if (amt === 0) {
console.log("zero " + Register.values[i].name + " left")
cashObj.push(Register.values[i].name)
cashObj.push(totVal)
console.log("Cash OBJ: " + cashObj)
break;
}
else if (totVal > 0 && changeDue === 0) {
console.log("FINAL TOTAL VAL: " + totVal)
cashObj.push(Register.values[i].name)
cashObj.push(totVal)
console.log("Cash Obj: " + cashObj)
}
else if (Register.values[i].value < changeDue && amt === 0 && changeDue > 0) {
Result.status = "INSUFFICIENT_FUNDS"
return Result;
}
}
console.log("===================================")
num = 0;
totVal = 0;
}
if (Result.change < changeDue) {
Result.status = "INSUFFICIENT_FUNDS";
return Result;
}
Result.status = "OPEN"
Result.change.push(cashObj)
return Result;
// ===========================================================================
// ()()()()()()()()()()()()()()[ RETURN RESULT ]()()()()()()()()()()()()()()()
// ===========================================================================
}
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]]);