JavaScript Algorithms and Data Structures Projects - Cash Register

Tell us what’s happening:
Describe your issue in detail here.
I’ve been stuck on this for 2 days …never made a help post before but i am getting hopeless on this one …plz help

``````function checkCashRegister(price, cash, cid) {
let newCid =[
["PENNY", 0.01],
["NICKEL", 0.05],
["DIME", 0.1],
["QUARTER", 0.25],
["ONE", 1],
["FIVE", 5],
["TEN", 10],
["TWENTY", 20],
["ONE HUNDRED", 100]
]
let changeReturn = (cash - price)
let change = []

////////////// INSUFFICIENT_FUNDS SCENARIO

let sumOfAll = 0
for(let elem of cid){
sumOfAll += elem[1]
}

if(sumOfAll < changeReturn){
return {status:"INSUFFICIENT_FUNDS", 'change': []}
}

////////////////////////////////////////////

let a = cid.length - 1
while (a >= 0){
while(changeReturn >= newCid[a][1] && cid[a][1] !== 0){
cid[a][1] -= newCid[a][1]
changeReturn -= newCid[a][1]

}
if(!change.includes(newCid[a][0])){
}
a--
}

if(change[change.length - 1][0] === newCid[0][0] &&
change[change.length -1][1] !== 0){
change[change.length -1][1] += newCid[0][1]
if(cid[0][0] < 0.01){
cid[0][0] = 0
}
}

////////////// CLOSED SCENARIO

let sum = 0
for(let elem of cid){
sum += elem[1]
}

if(sum < 0){
return {status:"CLOSED", 'change': change}
}

//////////////

change = change.filter(elem => elem[1] !== 0)
return {status:"OPEN", 'change': change}
}

console.log(
//  checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]),
checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])
)

``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/112.0`

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

Welcome to the forum. Dont be afraid to ask for help. Can you describe what exactly the problem you got?

Return `{status: "INSUFFICIENT_FUNDS", change: []}` if cash-in-drawer is less than the change due, or if you cannot return the exact change.

Are you checking for the exact change?

Last test:

``````{ status: 'OPEN', change: [ [ 'PENNY', 0.5000000000000002 ] ] }
``````

If this was a physical cash exchange, how would you return this?

i tried using .toFixed but was unable to change its output to 0.5

Post the code otherwise we can’t help.