Tell us what’s happening:
Hello everyone,
I’m just finished the code but the third test seems to be wrong, when i compare the expected result with my output i see the same and i don’t know why the test doesn’t success.
My output vs expected result
I appreciate if you can help me.
Your code so far
let UNITS = [{name: "PENNY", value: 0.01},
{name: "NICKEL", value: 0.05},
{name: "DIME", value: 0.1},
{name: "QUARTER", value: 0.25},
{name: "ONE", value: 1},
{name: "FIVE", value: 5},
{name: "TEN", value: 10},
{name: "TWENTY", value: 20},
{name: "ONE HUNDRED", value: 100}];
function checkCashRegister(price, cash, cid) {
let change = cash - price;
let arrChange = [];
let newCid = [];
cid.forEach(x => newCid.push([...x]));
UNITS.reverse().forEach((unit, i) => {
if(unit.value < change && change > 0){
let res = extract(change, unit, newCid);
change = res.amount;
newCid = res.cid;
arrChange.push(res.change);
}
});
if(change === 0){
if(newCid.every(item => item[1] === 0)){
return {
status: "CLOSED",
change: cid
}
}else{
return {
status: "OPEN",
change: arrChange.sort((a, b) => b[1] - a[1])
}
}
}else{
return {
status: "INSUFFICIENT_FUNDS",
change: []
}
}
}
function extract(amount, currency, cid){
let max = cid.find(curr => curr[0] === currency.name);
let change = 0;
while(amount - currency.value >= 0 && currency.value <= max[1]){
amount = +(amount - currency.value).toFixed(12);
max[1] = +(max[1] - currency.value).toFixed(12);
change = +(change + currency.value).toFixed(12);
}
return {
amount,
cid,
change: [currency.name, Number(change.toFixed(2))]
}
}
console.log(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]]))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36
.