I’ve been struggling with this one and got 3/5, but can’t pass the two ones that are supposed to return ‘open’ with an array of the bills and coins you used to complete the change.
This is my code so far, the console.log() of the array is giving me everything perfect, but gives me 0.03 PENNYS instead of 0.04.
And in the other check i’m supposed to return {status: "OPEN", change: [["QUARTER", 0.5]]}
but i am returning nothing.
plss help :C
function checkCashRegister(price, cash, cid) {
let change = cash - price;
let sum = 0;
let arr = [];
let objcid = {};
let objvalue = {
'ONE HUNDRED': 100,
TWENTY: 20,
TEN: 10,
FIVE: 5,
ONE: 1,
QUARTER: 0.25,
DIME: 0.1,
NICKEL: 0.05,
PENNY: 0.01
}
cid.forEach((v) => {
// Extract the key and the value
let key = v[0];
let value = v[1];
// Add the key and value to the object
objcid[key] = value;
});
for (let prop in objcid)
{
sum += objcid[prop];
}
if (change === sum)
{
return {status: "CLOSED", change: cid};
}
else
{
for (let prop in objvalue)
{
let amount = 0
while (objvalue[prop] <= change && objcid[prop] > 0)
{
amount += objvalue[prop];
change -= objvalue[prop];
objcid[prop] -= objvalue[prop];
sum -= objvalue[prop];
}
if (change !== 0 && amount !== 0)
{
if (prop === 'PENNY')
{
amount += 0.01;
}
arr.push([prop, amount]);
}
}
console.log(arr)
if (change !== 0)
{
return {status: "INSUFFICIENT_FUNDS", change: []};
}
else if (change === 0 && sum > 0)
{
return {status: "OPEN", change: arr}
}
}
};
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; rv:98.0) Gecko/20100101 Firefox/98.0
Challenge: Cash Register
Link to the challenge: