# Finaly JS exercise: Cash Register

My decision

function calcStatus(currChange, cidObj, change){

if(Math.ceil(currChange) === 0){
let count = 0;

for(let item in cidObj){
count += cidObj[item];

if(count ===change) return "CLOSED";

}
return "OPEN";

}
else{
return “INSUFFICIENT_FUNDS”;
}

}

function calculation(change, cidObj, count){
const exchange = {“ONE HUNDRED”: 100, TWENTY: 20,TEN: 10, FIVE: 5, ONE: 1, QUARTER: 0.25, DIME: 0.1, NICKEL: 0.05, PENNY: 0.01};

let currChange = change;
let status =“”;
const result ={status, change: [ ]};

for(let value in exchange){
const inStockValue = cidObj[value];
const nowValue= exchange[value];
const maxSubtraction = Math.floor(currChange/nowValue);

if(inStockValue !== 0 && maxSubtraction > 0){

if(inStockValue - nowValue * maxSubtraction >=0){
currChange = (currChange - nowValue * maxSubtraction).toFixed(2);
result.change.push([value, nowValue * maxSubtraction]);
}
else{
currChange = (currChange - inStockValue).toFixed(2);
result.change.push([value,inStockValue]);
}

}
else if(inStockValue == 0){
result.change.push([value, inStockValue])
}

}
result.change.reverse();
result.change.sort( (a,b) =>b[1]-a[1]);

status = calcStatus(currChange, cidObj, count);

if(status === “INSUFFICIENT_FUNDS”) {
return {status, change: [ ]};
}
return {…result, status: status};
}

function checkCashRegister(price, cash, cid) {
const cidObj = {};
cid.forEach(money => cidObj[money[0]] = money[1]);
const change = cash - price;

return calculation(change, cidObj, change);;
}

checkCashRegister(19.5, 20, [[“PENNY”, 0.5], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 0], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]]);

Can you please rate the code? Suggest what can be improved.