let ActualStatus = function(status, change) {
this.status = status;
this.change = change;
};
let currency = [['PENNY', 0.01],['NICKEL', 0.05],['DIME', 0.1],['QUARTER', 0.25],['ONE', 1],['FIVE', 5],['TEN', 10],['TWENTY', 20],['ONE HUNDRED', 100]];
function checkCashRegister(price, cash, cid) {
let result
let change = +(cash - price).toFixed(2);
let changeCopy = +(cash - price).toFixed(2);
let div = 0;
let sumOfCash = 0;
let moneyChange = [];
let partialChange = [];
currency.reverse();
cid.reverse();
for (let i = 0; i < currency.length; i++) {
sumOfCash = +(sumOfCash + cid[i][1]).toFixed(2)
if(change > 0) {
div = +(Math.floor(change / currency[i][1])).toFixed(2)
if(div * change > cid[i][1]) {
partialChange = cid[i][1];
} else {
partialChange = +(div * currency[i][1].toFixed(2));
}
if(partialChange > 0) {
moneyChange.push([cid[i][0], partialChange])
}
}
change = +(change - partialChange).toFixed(2)
}
if(change > 0) {
result = new ActualStatus("INSUFFICIENT_FUNDS", []);
}
else if(changeCopy < sumOfCash ) {
result = new ActualStatus("OPEN", moneyChange);
}
else if(changeCopy === sumOfCash) {
result = new ActualStatus("CLOSED", cid.reverse());
}
return result
}
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]]));
After you run the test, it shows you what the expected return is. Compare it closely to the output of your console.log and you should spot the issue.
Sorry, but I made everithing to find the issue, but I still not find it. I see that the output of my console.log match the expected return. So, I will reset my code and do it from zero using other ways to solve it if it is necessary. I have made it whit other tests, and resulted efectively.
Your return does not match the expected return.
Expected return…
{status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}
Your return…
{ status: 'OPEN', change: 'TWENTY,60TEN,20FIVE,15ONE,1QUARTER,0.5DIME,0.2PENNY,0.04' }
I sended the wrong code whit my question and after I changed it . Before it you has downloaded the wrong code. If you download my code now will give you another output. Please, sorry for the mistake and let me know.
Your “let currency” is defined outside your function, so it never gets reset. You call .reverse() on it, which mutates the array. The second time the function is called, it reverses it again back to the original order, which is not what you want and will produce the wrong result.
Thank you very much for your hint, the code passed the test now. Great
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.