Been trying to complete the cash register problem for a week, and am stuck on printing all the values been deducted, maybe someone can notify me where necessary…
function checkCashRegister(price, cash, cid) {
var productPrice = price;
var payment = cash;
var currentCash = [...cid];
var remain = payment - productPrice;
var filterCashFlow = [];
var ChangeToGive = [];
var currency = [['PENNY',0.01],
['NICKEL',0.05],
['DIME',0.1],
['QUARTER',0.25],
['ONE',1],
['FIVE',5],
['TEN',10],
['TWENTY',20],
['HUNDRED',100]
];
//it will save current cash thats greater than the currency
for(let i=0; i<currency.length; i++){
if(remain>currency[i][1]){
filterCashFlow.push(currency[i])
}
}
filterCashFlow.reverse();
for(let i=0;i<currency.length;i++){//this is where is giving me headeach
if(remain>currency[i][1]){
if(currentCash.indexOf(currency[i])) ChangeToGive.push(currentCash[i])
}
}
ChangeToGive.reverse();
var TotalCash = ChangeToGive.reduce((x,y)=>{return x+y[1]},0)
var status = ()=>{
return(remain > TotalCash)?"INSUFFICIENT_FUNDS":
(remain == TotalCash)?"CLOSED":
(remain < TotalCash)?"OPEN":false;
}
/*var f = currency.reduce((x,m)=>{
let fin = 0;
while(ChangeToGive[m][1] >0 && remain >= m[1]){
remain -= m[1];
console.log(ChangeToGive[m][1])
ChangeToGive[m][1] -= m[1];
m[1] += m[1];
fin += m[1]
}
if(fin>0){
x.push(m[1])
}
return x;
},[])*/
var f = ()=>{
let fin = 0;
let x = [];
if(fin ==remain){
return x;
}
for(let i=0; i <filterCashFlow.length; i++){
while(ChangeToGive[i][1] >=0){
x.push(filterCashFlow[i])
ChangeToGive[i][1] -= remain;
remain -= filterCashFlow[i][1];
filterCashFlow[i][1] += filterCashFlow[i][1];
fin += filterCashFlow[i][1]
console.log(ChangeToGive[i][1])
remain = Math.round(remain * 100) / 100;
}
}
return x;
}
function checker(){
let check;
if(status() == "INSUFFICIENT_FUNDS"){
return check = [];
}
if(status() == "CLOSED"){
return check = currentCash;
}
if(status() == "OPEN"){
return check = f();
}
return check;
}
// Here is your change, ma'am.
return ({status:status(),change:checker()})
}
console.log(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]]))