EDIT: added notes to the code
EDIT 2 : I MADE IT WORK!! cid needed reversing so it returns an array in the same oreder as requested in teh test
Ahoy!
So, after literally hours trying to solve that issue I am finally turning for some help.
I am stuck on a last test. Whatever what I do, it doesn’t seem to pass. I’ve tried in the console and the result seems ok.
Would anyone be able to help out?
Many thanks,
Tomek
function checkCashRegister(price, cash, cid) {
// result here
var final = { status: "CLOSED", change: [] }
var finalChange = [];
//change array will hold values of the iteration and will be returned in a result
var change = [["ONE HUNDRED", 0], ["TWENTY", 0], ["TEN", 0], ["FIVE", 0], ["ONE", 0], ["QUARTER", 0], ["DIME", 0], ["NICKEL", 0], ["PENNY", 0]];
//notValue, holds numerical value for each note
var noteValue = [["ONE HUNDRED", 100], ["TWENTY", 20], ["TEN", 10], ["FIVE", 5], ["ONE", 1], ["QUARTER", 0.25], ["DIME", 0.10], ["NICKEL", 0.05], ["PENNY", 0.01]];
// works out the numerical value of a change to be returned to the customer
var cashOwed = cash - price;
//totalCashInDrawer - returns total of numerical values in cid ( cash in drawer )
var totalCashInDrawer = Number(cid.flat().filter(item => !isNaN(item)).reduce((a,b)=>a+b).toFixed(2));
//reverse cid array so it matches change and noteValue variables pattern
var reversedCid = cid.reverse()
//Return {status: "CLOSED", change: [...]}with cash-in-drawer as the value for the key changeif it is equal to the change due.
if(totalCashInDrawer == cashOwed) {
// reverse added here!!
final.change = cid.reverse() ;
return final;
}
// Return {status: "INSUFFICIENT_FUNDS", change: []}if cash-in-drawer is less than the change due, or if you cannot return the exact change.
if(totalCashInDrawer < cashOwed) {
final.status = "INSUFFICIENT_FUNDS";
return final;
}
//iterate through cash in drawer -(reversedCid) and denominte its value accordning to the value of the note taken (noteValue), finally add that value to change variable - do for each element n the arr
if(totalCashInDrawer > cashOwed) {
for(var i = 0; i < reversedCid.length; i++) {
while((cashOwed >= noteValue[i][1]) && (reversedCid[i][1] >= noteValue[i][1])) {
cashOwed -= noteValue[i][1];
reversedCid[i][1] -= noteValue[i][1];
change[i][1] += noteValue[i][1];
cashOwed = cashOwed.toFixed(2)
}
//push the result of the iteration above into the finalChange array
if(change[i][1] > 0) {
finalChange.push(change[i])
}
}
//when cashOwed drops down to zero, return {status: "OPEN", change: [...]}, with the change due in coins and bills, sorted in highest to lowest order, as the value of the changekey.
if (cashOwed == 0) {
final.status="OPEN", final.change=finalChange;
return final;
//in case there's not enough coins of needed value to return
} else {
final.status="INSUFFICIENT_FUNDS", final.change=[];
return final;
}
}
};
checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])