Help me ! I didn't know the problem

Tell us what’s happening:

Your code so far


function checkCashRegister(price, cash, cid) {
let change = cash - price,
cashAvailable = countDrawer(cid),
notEnoughChange = change > cashAvailable,
closesDrawer = (change == cashAvailable),
exactChange = checkChange(change, cid, closesDrawer);

let status = (notEnoughChange || !exactChange.length)
? "INSUFFICIENT_FUNDS"
: closesDrawer
? "CLOSED"
:"OPEN"
return {statuts : '${statuts}', change: exactChange}
}
function checkChange(change, cid, closesDrawer) {
let changeDue = change * 100, 
units = [
  {"name": "ONE HUNDRED", "value": 10000},
  {"name": "TWENTY", "value": 2000},
  {"name": "TEN", "value": 1000},
  {"name": "FIVE", "value": 500},
  {"name": "ONE", "value": 100},
  {"name": "QUARTER", "value": 25},
  {"name": "DIME", "value": 10},
  {"anme": "NICKEL", "value": 5},
  {"name": "PENNY", "value": 1}
],
usableUnits = units.filter((unit, index) => {
  let unitAvailable = cid.reverse() [index][1] != 0
  return unitAvailable && unit ['value'] <= changeDue
  }),
  changeArr = []

  for (
    let i = 0;
    i < usableUnits.length && changeDue;
    i++
  ) {
    if (changeDue >= usableUnits[i]['value']) {
      let unitsDue = Math.floor(
        changeDue / usableUnits[i]['value']
      ),
      remainder = changeDue % usableUnits[i]['value'],
      totalUnitsDue = (usableUnits[i]['value'] * unitsDue) / 100,
      totalUnitsAvailable = cid.find((unit) => {
        return unit[0] === usableUnits[i]['name']
      })[1]

      if(totalUnitsDue <= totalUnitsAvailable) {
        changeArr.push([
          usableUnits[i]['name'],
          totalUnitsDue
        ])

        changeDue -= unitsDue * usableUnits[i]['value'];
      } else {
        changeArr.push([
          usableUnits[i]['name'],
          totalUnitsDue
        ])
        changeDue -= totalUnitsAvailable *100
      }
    }
  }
  return changeDue ? [] : changeArr
}

function countDrawer(cid) {
return cid.map(unit => unit[1] * 100)
.reduce((total, unitTotal) => {
  return (total+unitTotal);
}) / 100;
}

let result = checkCashRegister(3.26, 100, [["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/78.0.3904.97 Safari/537.36.

Challenge: Cash Register

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

good work!

A few minor notes:
Check the spelling and the use of/need for the literal ${...} here:

return {statuts : '${statuts}', change: exactChange}

Also a small typo in the units array :slight_smile:

Finally; the last test is expecting the full cid if ‘CLOSED’

{status: "CLOSED", change: [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]

1 Like

Hello, Thank you, :slight_smile: But this result :confused:

For me it passes 4 of the tests after those changes. There are 3 errors on line 13, and 1 on line 25 that I changed from your code above. I am using Chrome for my browser.

I checked the output for test 3 and the change is giving out more money than the drawer has.

And the final test is expecting the full drawer to be returned, even with the 0 values.

Hope that helps!

1 Like