Finaly JS exercise: Cash Register

Link to exercise.

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.

Please Tell us what’s happening in your own words.

Learning to describe problems is hard, but it is an important part of learning how to code.

Also, the more you say, the more we can help!

I just wanted to be pointed out the obvious flaws in my code. Although it works, I think it can still be improved.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.