JavaScript Algorithms and Data Structures Projects - Cash Register

Tell us what’s happening:
Describe your issue in detail here.

Your code so far

function checkCashRegister(price, cash, cid) {
  const unitMap = {
    PENNY:	0.01,
    NICKEL:	0.05,
    DIME:	0.1,
    QUARTER:	0.25,
    ONE: 1.0,
    FIVE:	5.0,
    TEN:	10.0,
    TWENTY:	20.0,
    "ONE HUNDRED": 100.0
  }
  let totalCID = 0
  for (let money of cid) {
    totalCID += money[1]
  }
  totalCID = totalCID.toFixed(2)
  let changeToGive = cash - price
  const changeArray = []

  if(changeToGive > totalCID) {
    return {status: "INSUFFICIENT_FUNDS",change: []}
  } else if (changeToGive.toFixed(2) === totalCID) {
    return {
      status: "CLOSED", 
      change: cid
    }
  } else {
    cid = cid.reverse()
    for(let money of cid) {
      const [unit, value] = money;
      const addition = [unit, 0]

      while(changeToGive >= unitMap[unit] && value > 0) {
        addition[1] += unitMap[unit]
        value -= unitMap[unit]
        changeToGive -= unitMap[unit]
        changeToGive = changeToGive.toFixed(2)
      }
      if(addition[1] > 0) {
        changeArray.push(addition)
      }
    }
  }

  if(changeToGive > 0) {
    return {status: "INSUFFICIENT_FUNDS", change: []}
  }

  return {
    status: "OPEN",
    change: changeArray
  }
}

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

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

Link to the challenge:

Please Tell us what’s happening in your own words.

Learning to describe problems is hard, but it is an important part of learning how to code.

Also, the more you say, the more we can help!

i see this.

TypeError: "value" is read-only

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