JavaScript Algorithms and Data Structures Projects - Cash Register

Tell us what’s happening:
WHY DOES THE VARIABLE newChangeNew still pushes even thou I made this condition
if(newChange !== []){ newChange.push(newChangeNew); }

can someone help how to capture an empty array

Your code so far

function checkCashRegister(price, cash, cid) {
  const registerMoney = cid;
  const piecesOfMoney = registerMoney.map(x => [x[0], pieces(x[0], x[1])] );
  const surplus = registerMoney.reverse().map(x => [x[0], ((x[1] * 1000/ pieces(x[0], x[1]))/1000) , pieces(x[0], x[1])] );
  // console.log(surplus)
  var cashInRegister = 0;
  const response = {status: "INSUFFICIENT_FUNDS", change: []}
  const dueChange = cash - price;
  //function for change when register has a surplus
  function surplusChange(change, lookupChange){ 
      var captureChange = change;
      var captureLookUp = lookupChange;
      const newChange = [];
        for(let i in captureLookUp){
          var newChangeNew = [];
          var amountPerPiece = 0;
           while(captureChange >= captureLookUp[i][1] && captureLookUp[i][2] !== 0){
            captureChange -= captureLookUp[i][1];
            amountPerPiece += captureLookUp[i][1];
            if(captureChange < captureLookUp[i][1] || captureLookUp[i][2] === 1) {
              newChangeNew.push(captureLookUp[i][0])
              newChangeNew.push(amountPerPiece)
            }
            captureLookUp[i][2]--
          }
          if(newChange !== []){
          newChange.push(newChangeNew);
          }
        }
        return newChange
  }
  //function to know pieces of unique money
  function pieces(piece, value){
    if(piece === "PENNY"){
      return value * 100 / 1;
    }
    if(piece === "NICKEL"){
      return (value * 1000 / 5) / 10;
    }
    if(piece === "DIME"){
      return value * 100 / 10
    }
    if(piece === "QUARTER"){
      return value * 100 / 25
    }
    if(piece === "ONE"){
      return value / 1
    }
    if(piece === "FIVE"){
      return value / 5
    }
    if(piece === "TEN"){
      return value / 10
    }
    if(piece === "TWENTY"){
      return value / 20
    }
    if(piece === "ONE HUNDRED"){
      return value /100
    }
  }
  //calculating money in register
  for(let i of registerMoney){
    cashInRegister += i[1] * 100
  }
  cashInRegister /= 100;
  // response
  if(cashInRegister < dueChange) {
    return response
  }
  else if(cashInRegister == dueChange){
    response.status = "CLOSED";
    response.change = piecesOfMoney;
    return response
  }
  else{
    response.status = "OPEN";
    response.change = surplusChange(dueChange, surplus);;
    return response
  }
}

console.log(checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

Link to the challenge:

Try this line in the console:

[] === []
// false

Here is a FCC article about comparing arrays for more details, but in your case where you just want to check if the array is empty, you could consider something like:

if (newChange.length !== 0) {

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