Hi,
Eventhough it seems that my out is correct, I can’t get the test pass. Can someone tell me what is happening here?
let currency={
"PENNY":0.01,
"NICKEL":0.05,
"DIME":0.1,
"QUARTER":0.25,
"DOLLAR":1,
"ONE":1,
"FIVE": 5,
"TEN":10,
"TWENTY":20,
"ONE HUNDRED":100
};
//Function which return the total value in the CID.
function totalInCashRegister(cid){
return Math.round(cid.reduce((acc,x)=>{return acc+x[1]},0)*100)/100;
}
//function that giving a cahnge valie and an element of a CID return how ,uch from that notebank you can give to the customer and the left change
function emptyDrawer(change,array){
let valueNote=currency[array[0]];
let numberCoins=array[1]/valueNote;
let returnChange=0;
while(numberCoins>0 && change>=valueNote){
change=Math.round((change-valueNote)*100)/100;
numberCoins=Math.round((numberCoins-1)*100)/100;
returnChange=returnChange+valueNote;
}
if(returnChange>0){
return [[[array[0],Math.round(returnChange*100)/100]],change];
}
else{
return [[null],change ]
}
}
//function taking the whole CID and return the cash and the left change.
function returnCashToCustomer(change,cid){
return cid.reverse().reduce((acc,x)=>{
let emptying=emptyDrawer(acc[1],x);
if(emptying[1]!=acc[1]){
acc[1]=emptying[1];
acc[0].push([emptying[0]]);
}
return acc;
},[[],change])
}
function checkCashRegister(price, cash, cid) {
var change=cash-price;
var output = { status: null, change: [] };
// Handle exact change
if (totalInCashRegister(cid) === change) {
output.status = 'CLOSED';
output.change = cid;
return output;
}
// Handle insufficient funds
if (totalInCashRegister(cid) < change) {
output.status = 'INSUFFICIENT_FUNDS';
output.change = [];
return output;
}
//handling other case
let cashToReturn=returnCashToCustomer(change,cid);
//console.log(cashToReturn);
if (cashToReturn[1]>0){
output.status = 'INSUFFICIENT_FUNDS';
output.change = [];
return output;
}
//we can return all the cash to the custumer
output.status="OPEN";
output.change=cashToReturn[0].slice(0);
return output;
}
// 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]]
let test1=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]]);
console.log("-----------------------------")
console.log(test1.status);
console.log(test1.change);
console.log(test1.change.length);
console.log("-----------------------------")
let test2=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]]) ;
console.log("-----------------------------")
console.log(test2.status);
console.log(test2.change);
console.log(test2.change.length);
console.log("-----------------------------")
Best
Raphael