I’m stuck on third test. I print my answer and the answer provided. They look the same to me…
here are screenshots and code below. (not completed, but for test 3 it should work?)
function checkCashRegister(price, cash, cid) {
var remainder = cash - price; //to be paid
//console.log(remainder);
let faceValue = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];
let result = {};
//Check sum of cash in cashier
let sum = cid.reduce(function (total, coin) {
return total + coin[1];
}, 0);
if (sum < remainder) {
result.status = "INSUFFICIENT_FUNDS";
result.change = [];
return result;
} else if (sum == remainder) {
result.status = "CLOSED";
result.change = [];
return result;
} else {
//if any leftover
result.status = "OPEN";
result.change = [];
for (let i = faceValue.length-1; i >= 0; i--) {
if (remainder < faceValue[i]){
continue;
}else {
if (cid[i][1] <= remainder) {
remainder -= cid[i][1];
result.change.push([cid[i][0], cid[i][1]]);
} else {
if (remainder % faceValue[i] == 0){
result.change.push([cid[i][0], remainder]);
remainder = 0;
} else {
let thisLevelChange = Math.floor(remainder/faceValue[i])*faceValue[i];
result.change.push([cid[i][0], thisLevelChange]);
remainder -= thisLevelChange;
remainder = remainder.toFixed(2);
//console.log(cid[i][0], thisLevelChange, remainder);
}
}
}
}
if (remainder > 0) {
return {status: "INSUFFICIENT_FUNDS", change: []}; //enough money but not enough changes
}
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]]));
let x = 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]]);
let y = {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}
console.log(x);
console.log(y);
Redundancy is obvious but this is the updated code… and it passed all tests except the third one.
function checkCashRegister(price, cash, cid) {
var remainder = cash - price; //to be paid
//console.log(remainder);
let faceValue = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];
let result = {};
//Check sum of cash in cashier
let sum = cid.reduce(function (total, coin) {
return total + coin[1];
}, 0);
if (sum < remainder) {
result.status = "INSUFFICIENT_FUNDS";
result.change = [];
return result;
} else if (sum == remainder) {
result.status = "CLOSED";
result.change = [];
for (let i = faceValue.length-1; i >= 0; i--) {
if (remainder < faceValue[i]){
result.change.push([cid[i][0], cid[i][1]]);
}else {
if (cid[i][1] <= remainder) {
remainder -= cid[i][1];
result.change.push([cid[i][0], cid[i][1]]);
} else {
if (remainder % faceValue[i] == 0){
result.change.push([cid[i][0], remainder]);
remainder = 0;
} else {
let thisLevelChange = Math.floor(remainder/faceValue[i])*faceValue[i];
result.change.push([cid[i][0], thisLevelChange]);
remainder -= thisLevelChange;
remainder = remainder.toFixed(2);
//console.log(cid[i][0], thisLevelChange, remainder);
}
}
}
}
result.change = result.change.reverse();
return result;
} else {
//if any leftover
result.status = "OPEN";
result.change = [];
for (let i = faceValue.length-1; i >= 0; i--) {
if (remainder < faceValue[i]){
continue;
}else {
if (cid[i][1] <= remainder) {
remainder -= cid[i][1];
result.change.push([cid[i][0], cid[i][1]]);
} else {
if (remainder % faceValue[i] == 0){
result.change.push([cid[i][0], remainder]);
remainder = 0;
} else {
let thisLevelChange = Math.floor(remainder/faceValue[i])*faceValue[i];
result.change.push([cid[i][0], thisLevelChange]);
remainder -= thisLevelChange;
remainder = remainder.toFixed(2);
//console.log(cid[i][0], thisLevelChange, remainder);
}
}
}
}
if (remainder > 0) {
return {status: "INSUFFICIENT_FUNDS", change: []}; //enough money but not enough changes
}
return result;
}
}
Take look at the PENNY
in function’s result, for some reason it has value in a string.
1 Like
remainder.toFixed(2);
toFixed returns a string. You can just wrap it in a Number()
call.
1 Like
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.