Hey! I’m doing the last exercise from Javascript Algorithms and Data Structure Projects.
Probably I should take a walk, rest and find out whats happening with my code but I’m kinda anxious to end this code so I’m asking for help
Blockquote
function checkCashRegister(price, cash, cid) {
function roundHalf(num) {
return Math.round(num*2)/2;
}
const map = {
"ONE HUNDRED": 100,
"TWENTY": 20,
"TEN": 10,
"FIVE": 5,
"ONE": 1,
"QUARTER": 0.25,
"DIME": 0.1,
"NICKEL": 0.05,
"PENNY": 0.01
}
let totalMoney=0;
let change = cash - price;
for(let index = 0;index<[...cid].length;index++){
totalMoney+= cid[index][1];
}
if (totalMoney < change){
return {status: "INSUFFICIENT_FUNDS", change: []};
}
if (change == totalMoney){
return {status: "CLOSED", change: cid};
}
let changeArr = [];
let value,coinNumber=0,coinTotal;
for(let index = 0; index < Object.keys(map).length;index++){
if(change > Object.values(map)[index]){
if(cid[Object.values(map).length - index -1][1] != 0){
coinTotal = cid[Object.keys(map).length -1-index][1]/Object.values(map)[index];
while(change >= Object.values(map)[index] && coinTotal > 0){
change -= Object.values(map)[index];
coinTotal--;
coinNumber++;
}
value = coinNumber * Object.values(map)[index];
changeArr.push([Object.keys(map)[index],value]);
}
}
}
if(change>0){
return {status: "INSUFFICIENT_FUNDS", change: []};
}
const obj = {
status: "OPEN",
change: changeArr,
}
return obj;
}
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]]));