JavaScript Algorithms and Data Structures Projects - Cash Register

Tell us what’s happening:
Console.log shows the correct return value, but the return function is not passing any of the tests save the “insufficient funds” tests. Not sure why it would show the correct result but not pass the test?

Your code so far


```javascript
  //set base cash register response//
  let state = '';
  let change = [];
  //set the currency units for mapping//
  const currencyUnit = {
    "PENNY": 1, 
    "NICKEL": 5, 
    "DIME": 10, 
    "QUARTER": 25, 
    "ONE": 100, 
    "FIVE": 500, 
    "TEN": 1000, 
    "TWENTY": 2000, 
    "ONE HUNDRED": 10000
    };

function checkCashRegister(price, cash, cid) {
  //Calculate change needed//
  let changeNeeded = (cash - price) * 100;
  let changeCheck = (cash - price) * 100;
  //get total cash in drawer//
  let totalCid = cid.reduce((accumulator, currentValue) => accumulator + currentValue[1], 0) * 100;
    
  //set change value//
  let highLowCid = cid.reverse();

  highLowCid.forEach(elem => {
    let denom = elem[0];
    let denomValue = elem[1] * 100;
    let returnedChange = 0;
    while (changeNeeded >= currencyUnit[denom] && denomValue > 0) {
      returnedChange += currencyUnit[denom];
      changeNeeded -= currencyUnit[denom];
      denomValue -= currencyUnit[denom]
    }
    //console.log(changeNeeded)
  if (returnedChange !== 0) {
    change.push([denom, returnedChange * .01]);
  }

  });
  //set register status//
  if (changeNeeded > 0) {
    state = "INSUFFICIENT_FUNDS";
    change = [];
  } else
  if (changeNeeded === 0 && changeCheck == totalCid) {
    state = "CLOSED";
    change = change;
  } else
  {
    state = "OPEN";
    change = change
    }

 /* console.log(changeNeeded)
  console.log(state);
  console.log(change)*/
  console.log({"status": state, "change": change})
  return { "status": state, "change": 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 (Windows NT 10.0; Win64; x64) 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:

why are you making these global variables?
If I move these inside your function, everything passes except the last test case.

1 Like

ah. just the way it processed through in my head. didn’t think to move em down.

thank you!

passed after moving those into the function and updating the “closed” return array. much appreciated!

1 Like