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
Your code so far
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){
let add = 0
while(changeReturn >= newCid[a][1] && cid[a][1] !== 0){
cid[a][1] -= newCid[a][1]
changeReturn -= newCid[a][1]
add += newCid[a][1]
}
if(!change.includes(newCid[a][0])){
change.push([newCid[a][0],add])
}
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]])
)
Your browser information:
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
Link to the challenge: