JavaScript Algorithms and Data Structures Projects - Cash Register

Tell us what’s happening:
Can someone please help me with this last penny? I’m already tried to use toFixed and there is no result.

Your code so far

function checkCashRegister(price, cash, cid) {

  let result = {
    status: "",
    change: []
  }

  let allCid = function (cid) {
    let sum = 0;
    for (let i = 0; i < cid.length; i++) {
      sum = sum + cid[i][1]
    }
    return sum
  }

  let gettingChange = cash - price;
  if (allCid(cid) === gettingChange) {
    result.status = "CLOSED"
    result.change = cid
  }

  if (allCid(cid) < gettingChange) {
    result.status = "INSUFFICIENT_FUNDS"
    result.change = []
  }

  const cashAmount = [.01 , .05 , .1 , .25 , 1, 5, 10, 20, 100]
  let startOfChange = function(gettingChange) {
    for (let i = cashAmount.length; i > 0; i--) {
      if (cashAmount[i] <= gettingChange) {
      return cashAmount.indexOf(cashAmount[i])
      }
    }
  }

  let rounded = function(number){
    return +number.toFixed(2);
}



  if (allCid(cid) > gettingChange) {
    let changeArray = []
    let sumOfChange = 0
    
    for (let i = startOfChange(gettingChange); i >= 0; i--) {
      if (sumOfChange + cid[i][1] <= gettingChange) {
        sumOfChange = sumOfChange + cid[i][1]
        changeArray.push(cid[i])
        } else if (sumOfChange + cashAmount[i] <= gettingChange) { 
        let a = 0;
        do {
          sumOfChange = sumOfChange + cashAmount[i]
          a++ 
        }
        while (sumOfChange + cashAmount[i] <= gettingChange)
        cid[i][1] = cashAmount[i] * a
        changeArray.push(cid[i])
        console.log(sumOfChange, gettingChange, changeArray)
        }
    }
    result.status = "OPEN"
    result.change = changeArray
  }
  //console.log(result)
  return result;
}

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/106.0.0.0 Safari/537.36

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

Link to the challenge:

I would do all of the internal calculations in an integer number of cents.

Thank you. I will try this.

1 Like

Thank you so much - it works!!!

1 Like

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