JavaScript Algorithms and Data Structures Projects - Cash Register

Tell us what’s happening:
Describe your issue in detail here.
For the test case
checkCashRegister(19.5, 20, [[“PENNY”, 0.5], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 0], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]])

The output I am getting is { status: ‘CLOSED’, change: [ [ ‘PENNY’, 0.5 ] ] } and it is expecting {status: “CLOSED”, change: [[“PENNY”, 0.5], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 0], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]]}.

Your code so far

const units = {
  "ONE HUNDRED" : 100,
  "TWENTY" : 20,
  "TEN" : 10,
  "FIVE" : 5,
  "ONE" : 1,
  "QUARTER" : 0.25,
  "DIME" : 0.1,
  "NICKEL" : 0.05,
  "PENNY" : 0.01
}

function checkCashRegister(price, cash, cid) {
  let change = {};
  let rem = cash-price;
  let exact_change = [];
  let unit = Object.keys(units);
  let type_of_coins = unit.length;
  let unit_empty_count = 0;
  unit.forEach((item) => {
    if(item === "PENNY") {
      rem = Math.ceil(100*rem)/100;
    }
    var x = rem/units[item];
    if(cid[--type_of_coins][1] > 0 && x >= 1) {
      var y = Math.floor(x)*units[item];
      if(cid[type_of_coins][1] > y) {
        exact_change.push([item, y])
        rem -= y;
        cid[type_of_coins][1] -= y;
      } else {
        exact_change.push([item, cid[type_of_coins][1]])
        rem -= cid[type_of_coins][1];
        unit_empty_count++;
        cid[type_of_coins][1] = 0;
      }
    } else if(cid[type_of_coins][1] === 0) {
        unit_empty_count++;
    }
  });

  if(rem !== 0) {
    change.status = "INSUFFICIENT_FUNDS"
    change.change = []
  } else {
    if(unit_empty_count === unit.length) {
      change.status = "CLOSED"
    }
    else {
      change.status = "OPEN"
    }
    change.change = exact_change
  }
  console.log(change)
  return 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]]);

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

Link to the challenge:

You need to push the zero-value arrays as well.

One option would be to just push it inside the cid[type_of_coins][1] === 0 condition and then reverse the array inside the unit_empty_count === unit.length condition. Although that doesn’t really seem like the correct thing to do, it will however work without any other changes to the logic.

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