I am getting this result when running tests on this challenge:
// tests completed
// console output
{ status: ‘OPEN’,
change:
[ [ ‘TWENTY’, 60 ],
[ ‘TEN’, 20 ],
[ ‘FIVE’, 15 ],
[ ‘ONE’, 1 ],
[ ‘QUARTER’, 0.5 ],
[ ‘DIME’, 0.2 ],
[ ‘PENNY’, 0.04 ] ] }
which matches the expected output, but doesn’t pass the test?
Any help would be greatly appreciated, as well as any other suggestions on how to improve my code.
Thanks.
var currency = [["PENNY", 0.01], ["NICKEL", 0.05], ["DIME", 0.1], ["QUARTER", 0.25], ["ONE", 1], ["FIVE", 5], ["TEN", 10], ["TWENTY", 20], ["ONE HUNDRED", 100]];
var changeArr = [["PENNY", 0], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]];
function checkCashRegister(price, cash, cid) {
var output = {status: null, change: []};
var change = cash - price;
var tillTotal = 0;
for (var i = 0; i < cid.length; i++) {
tillTotal += cid[i][1];
}
tillTotal = Math.round(tillTotal * 100) / 100;
if (tillTotal < change) {
output.status = "INSUFFICIENT_FUNDS";
return output;
}
if (tillTotal === change) {
output.status = "CLOSED";
output.change = cid;
return output;
}
if (tillTotal > change) {
for (var k = (cid.length - 1); k >= 0; k--) {
while (currency[k][1] <= change && cid[k][1] >= currency[k][1]) {
changeArr[k][1] += currency[k][1]
cid[k][1] -= currency[k][1]
change -= currency[k][1]
change = Math.round(change * 100) / 100;
}
}
var newArr = [];
for (var q = 0; q < changeArr.length; q++) {
if (changeArr[q][1] > 0) {
newArr.unshift(changeArr[q])
}
}
output.status = "OPEN";
output.change = newArr;
return output;
}
}
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]]));
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36
.
Challenge: Cash Register
Link to the challenge: