the array object is being passed in.
It’s subtraticting 20 each pass
But I can’t find the dot notation or bracket index on line 14
to log the property for that particular denomination
function everybodiesGoodEnoughForSomeChange (changeDue, ...cid) {
let romanString = ''; // let while loop equal funtion call
while (changeDue > 0){
switch (true) {
// hundreds
case (changeDue > 99):
console.log(cid);
romanString = romanString.concat('ONE HUNDRED');
changeDue = changeDue -100;
break;
// twenties
case (changeDue > 19):
console.log('change due ', changeDue);
console.log('line 14 dot or Bracket notation for denomination ', cid[TWENTY]);
console.log(cid);
romanString = romanString.concat('TWENTY');
changeDue = changeDue -20;
break;
// tens
case (changeDue > 9):
console.log('change due ', changeDue);
romanString = romanString.concat('TEN');
changeDue = changeDue -10;
break;
// fives
case (changeDue > 4):
console.log('change due ', changeDue);
romanString = romanString.concat('FIVE');
changeDue = changeDue -5;
break;
// ones
case (changeDue > .99):
console.log('change due ', changeDue);
romanString = romanString.concat('ONE');
changeDue = changeDue -1;
break;
// quarters
case (changeDue > .24):
console.log('change due ', changeDue);
romanString = romanString.concat('QUARTER');
changeDue = changeDue -.25;
break;
// dimes
case (changeDue > .09):
console.log('change due ', changeDue);
romanString = romanString.concat('DIME');
changeDue = changeDue -.10;
break;
// nickles
case (changeDue > .04):
console.log('change due ', changeDue);
romanString = romanString.concat('NICKLE');
changeDue = changeDue -.05;
break;
// pennies
case (changeDue > 0):
console.log('change due ', changeDue);
romanString = romanString.concat('PENNY');
changeDue = changeDue -.01;
break;
default:
console.log('51 changeDue = ',changeDue);
}//end of switch
}//end of while..
console.log('55 romanString = ',romanString);
return romanString; //refactor while loop as anounoumous function?
} //end of HELPER function everybodiesGoodEnoughForSomeChange
function checkCashRegister(price, cash, cid) {
let total = cid.map(currency => currency[1]*100).reduce((total, amount) => total + amount);
let totalINdrawer = total/100; //use reduce method to sum the 2nd property of an array, do not use a loop
let changeDue = cash - price ;
totalINdrawer = total / 100;
let change = '';//set default return
if (totalINdrawer == changeDue ) {
change = {status: "CLOSED", change: cid};
}//end of if (totalINdrawer == changeDue )
else if (totalINdrawer < changeDue) {
change = {status: "INSUFFICIENT_FUNDS", change: []};
}//end of// else if (totalINdrawer < changeDue)
else {// (totalINdrawer > changeDue) ..so calculate change
//calculate change object here
let romanString = everybodiesGoodEnoughForSomeChange(changeDue, cid);
if (totalINdrawer < changeDue) {//and an and or an OR to the conditional to cover odd denomininations
//denominations not suffeicent, do not compute, as in 2 quarters in drawer, to convert to a dime, nickle & pennies)
change = {status: "INSUFFICIENT_FUNDS", change: []};
}
else { //just give his money bruh
change = {status: "OPEN", change: changeDue};//calculate denominations of change..
}// end of else, give him money bruh
}//end of else, give change as per calculation
console.log('L92 change = ', change);
return change;} // Here is your change, ma'am.//end of function checkCashRegister(price, cash, cid)
// console.log('3.26, 100');
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]]);
// {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}.
// console.log('19.5, 20');
// 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]]);
// should return {status: "OPEN", change: [["QUARTER", 0.5]]}.
// checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);
// should return {status: "INSUFFICIENT_FUNDS", change: []}.
// console.log('19.5, 20');
// checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);
// should return {status: "INSUFFICIENT_FUNDS", 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]]);
// should return {status: "CLOSED", change: [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]
fork the CodePen to see it live
cid[7][1]
works in most cases, but the array is not always passed in the same order
also noting that it continues to count denominations into the negative…
I’ll need to look at that a bit more…