Tell us what’s happening:
My code produces the value of penny as 0.03 but it should do 0.04 instead, I have no idea where did I go wrong also this isn’t finished code.
Your code so far
function checkCashRegister(price, cash, cid) {
// Store the currency unit and amount for ease
let currencyUnit = {
"PENNY": 0.01,
"NICKEL": 0.05,
"DIME": 0.1,
"QUARTER": 0.25,
"ONE": 1,
"FIVE": 5,
"TEN": 10,
"TWENTY": 20,
"ONE HUNDRED": 100,
}
let result = {}
let changes = [];
let change = cash - price;
let sufficient = false; // Sum of value of changes equal to change
for(let i = cid.length - 1; i >= 0; i--) {
if(currencyUnit[cid[i][0]] <= change && !sufficient) {
// If the change in drawer can be equally divided
if(change % currencyUnit[cid[i][0]] == 0) {
let divide = change / currencyUnit[cid[i][0]];
let sub = cid[i][1] - currencyUnit[cid[i][0]]*divide
if (sub >= 0) {
changes.push([cid[i][0], change])
}
} else {
let divide = Math.floor(change / currencyUnit[cid[i][0]]);
let unitChange = currencyUnit[cid[i][0]]*divide;
let sub = cid[i][1] - unitChange;
if (sub < 0) {
changes.push([cid[i][0], cid[i][1]]);
change -= cid[i][1];
} else {
changes.push([cid[i][0],unitChange])
change -= unitChange;
}
}
}
let sum = 0;
for (let i in changes) {
sum += changes[i][1];
}
if(sum == change) {
sufficient = true;
}
}
if (changes.length != 0) {
console.log({
status:"OPEN",
change:changes,
});
return {
status:"OPEN",
change:changes,
}
}
return result;
}
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 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36
.
Challenge: Cash Register
Link to the challenge: