JavaScript Algorithms and Data Structures Projects: Cash Register/ Help

JavaScript Algorithms and Data Structures Projects: Cash Register/ Help
0

#1

Tell us what’s happening:
I can only pass the first test and I don’t know why

Your code so far


const REGISTER_STATUS = {insufficientFunds: "INSUFFICIENT_FUNDS", closed: 
"CLOSED", open: "OPEN"};

function checkCashRegister(price, cash, cid) {
  let cashRegister = {status: '', cash: cid};
  const changeNeeded = parseFloat(cash-price).toFixed(2);
  const changeAvailable = getTotalCashRegisterChange(cid); 
  cashRegister.status = getTotalCashRegisterStatus(changeNeeded, changeAvailable);

  if(cashRegister.status === REGISTER_STATUS.insufficientFunds) {
    cashRegister.change = [];
    return cashRegister;
  }

  cashRegister.change = getCustomersChange(changeNeeded, cid);

  if(changeNeeded > getTotalCashRegisterChange(cashRegister.change)) {
    cashRegister.status = REGISTER_STATUS.insufficientFunds;
    cashRegister.change = [];
  }

  if(cashRegister.status === REGISTER_STATUS.closed) {
    cashRegister.change = [...cid];
  }
  
  return cashRegister;
}

function getCustomersChange(changeNeeded, changeInDrawer) {
  const change = [];
  const currencyDictionary = {
    "PENNY": 0.01,
    "NICKEL": 0.05,
    "DIME": 0.10,
    "QUARTER": 0.25,
    "ONE": 1.00,
    "FIVE": 5.00,
    "TEN": 10.00,
    "TWENTY": 20.00,
    "ONE HUNDRED": 100.00
  };

  for(let i = changeInDrawer.length-1; i>=0; i--) {
    const coinName = changeInDrawer[i][0];
    const coinTotal = changeInDrawer[i][1];
    const coinValue = currencyDictionary[coinName];
    let coinAmount = (coinTotal / coinValue).toFixed(2);
    let coinsToReturn = 0;

    while(changeNeeded >= coinValue && coinAmount > 0) {
      changeNeeded -= coinValue;
      changeNeeded = changeNeeded.toFixed(2);
      coinAmount--;
      coinsToReturn++;
    }

    if (coinsToReturn > 0) {
      change.push([coinName, coinsToReturn * coinValue]);
    }

  }

  return change;
}

function getTotalCashRegisterChange(changeInDrawer) {
  let total = 0;
  for(let change in changeInDrawer) {
    let changeValue = change[1];
    total += changeValue;
  }
  return total.toFixed(2);
}

function getTotalCashRegisterStatus(changeNeeded, changeAvailable) {
  if(Number(changeNeeded)> Number(changeAvailable)) {
    return REGISTER_STATUS.insufficientFunds;
  } else if (Number(changeNeeded) < Number(changeAvailable)) {
    return REGISTER_STATUS.open;
  } else {
    return REGISTER_STATUS.closed;
  }
}

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 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register/


#2

It might help you to have console.log() line somewhere you think would be useful. The result I get from the first test is closed. It doesn’t match what the answer is.

Since that is the case, the problem lies either with how you’re handling the cases, or the way your calculating the tender used, or even both.


#3

Thanks for your help! I figured it out