Tell us what’s happening:
Alright I feel like this code is too entangled to work with. I have passed all but one test and I doubt I’ll be able to pass it with this code. Should I reset the project and start over or do you see hope in this code?
Your code so far
function checkCashRegister(price, cash, cid) {
// Global Usage
let statusArray = ["INSUFFICIENT_FUNDS", "CLOSED", "OPEN"];
let currency = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];
let change = (cash * 100) - (price * 100);
let unchangedChange = change;
let cashInRegister = [...cid];
let totalCash = 0;
for(let i = 0; i < cid.length; i++) {
totalCash += (cid[i][1]) * 100;
currency[i] *= 100;
cashInRegister[i][1] *= 100;
}
console.log(currency);
function pushChange() {
let localRegister = [...cashInRegister];
for(let i = 0; i < localRegister.length; i++) {
localRegister[i][1] /= 100;
}
receipt.change = [...localRegister];
}
let receipt = {
status: "",
change : []
};
if(change > totalCash) {
receipt.status = statusArray[0];
receipt.change = [];
} else if(change == totalCash) {
receipt.status = statusArray[1];
pushChange();
} else {
let matchesGreaterThanChange = [];
let currencyUsed = [];
let anotherArray = [];
for(let i = cashInRegister.length - 1; i >= 0; i--) {
if(change >= currency[i] && cashInRegister[i][1] != 0) {
console.log("Change in question: " + cashInRegister[i][1] / 100);
while(change >= currency[i] && cashInRegister[i][1] >= 0) {
if(cashInRegister[i][1] != 0) {
currencyUsed.push(cashInRegister[i][1] / 100);
}
pusherFunction(change, i);
console.log("Cash: " + cashInRegister[i][1] / 100);
console.log("Currency: " + currency[i] / 100)
console.log("Change: " + change / 100);
console.log();
change -= currency[i];
cashInRegister[i][1] -= currency[i];
if(cashInRegister[i][1] == 0) {
anotherArray.push(currencyUsed[0]);
currencyUsed = [];
} else {
}
matchesGreaterThanChange.push(cashInRegister[i][1]);
}
}
}
console.log(currencyUsed);
console.log(anotherArray);
function pusherFunction(changeInput, index) {
if(changeInput == currency[index] && currency[index] != currency[0]) {
let toBePushed = [];
toBePushed.push(cashInRegister[index][0]);
toBePushed.push(unchangedChange / 100);
receipt.change.push(toBePushed);
}
}
if(change > matchesGreaterThanChange[matchesGreaterThanChange.length - 1]) {
receipt.status = statusArray[0];
receipt.change = [];
} else {
receipt.status = statusArray[2];
}
}
console.log(totalCash);
console.log("Change: " + change);
console.log(receipt.status + " and " + receipt.change);
console.log(receipt);
// Here is your change, ma'am.
return receipt;
}
checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register/