I’ve been working on this for a while and can’t seem to figure out the proper logic. I built the below code to help me visualize what to do next but it seems my code is growing with each step. Am I on the right track, or should I start over?
Link to Project:
Current Progress:
function checkCashRegister(price, cash, cid) {
var CidTextandAmounts = [];
function amounts (cid) {
var changeOne = [];
for (var i = 0; i < cid.length; i++ ) {
changeOne.push(cid[i][1]);
CidTextandAmounts.push(cid[i]);
} return changeOne;
}
function add(total,num) {
return total + num;
}
var CidAmounts = amounts(cid);
var totalcid = console.log("Total Cash in Draw: " + CidAmounts.reduce(add));
var changeDue = cash - price;
var values = [.01,.05,.1,.25,1,5,10,20,100];
function cidQuants (CidAmounts) {
var cidQuantity = [];
var cidParts=[];
var IdNote = [];// Note to multiply by
for (var i=0; i < values.length; i++ ) {
cidQuantity.push(CidAmounts[i] / values[i]);
cidParts.push(changeDue / values[i]);
IdNote.push( changeDue * cidParts[i] * values[i] );
}
var newIdNote = [...new Set(IdNote)];
var mutiplyby = changeDue / newIdNote;
console.log ("Number of Notes: " + cidQuantity);
console.log("Note to multiply: " + newIdNote);
console.log("Muliply by: " + mutiplyby);
return CidTextandAmounts;
}
console.log("Change Due: " + changeDue);
console.log("Notes: " + values);
console.log("Value of Notes: " + CidAmounts);
console.log(cidQuants(CidAmounts));
// Here is your change, ma'am.
//return change;
}
// Example cash-in-drawer array:
// [["PENNY", 1.01],
// ["NICKEL", 2.05],
// ["DIME", 3.1],
// ["QUARTER", 4.25],
// ["ONE", 90],
// ["FIVE", 55],
// ["TEN", 20],
// ["TWENTY", 60],
// ["ONE HUNDRED", 100]]
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]]);