Build a Cash Register Project - Build a Cash Register Test 9

Tell us what’s happening:

Hello, my code is not passing test number 9. In my local run environment and in the preview on freeCodeCamp, both display ‘Status: INSUFFICIENT_FUNDS’.

Your code so far

const cashInput = document.getElementById('cash');
const purchaseBtn = document.getElementById('purchase-btn')
const changeDueEle = document.getElementById('change-due')

let price = 0;
let cid = []

let cidDen = [
  ["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]
]
let cidA = [
  ["PENNY", 1.01],
  ["NICKEL", 2.05],
  ["DIME", 3.1],
  ["QUARTER", 4.25],
  ["ONE", 90],
  ["FIVE", 55],
  ["TEN", 20],
  ["TWENTY", 60],
  ["ONE HUNDRED", 100]
];
let cidB = [
  ["PENNY", 0.01],
  ["NICKEL", 0],
  ["DIME", 0],
  ["QUARTER", 0],
  ["ONE", 0],
  ["FIVE", 0],
  ["TEN", 0],
  ["TWENTY", 0],
  ["ONE HUNDRED", 0],
];
let cidC = [
  ["PENNY", 0.01],
  ["NICKEL", 0],
  ["DIME", 0],
  ["QUARTER", 0],
  ["ONE", 1],
  ["FIVE", 0],
  ["TEN", 0],
  ["TWENTY", 0],
  ["ONE HUNDRED", 0],
];
let cidD = [
  ["PENNY", 0.5],
  ["NICKEL", 0],
  ["DIME", 0],
  ["QUARTER", 0],
  ["ONE", 0],
  ["FIVE", 0],
  ["TEN", 0],
  ["TWENTY", 0],
  ["ONE HUNDRED", 0],
];
let cash = Number(cashInput.value)

let display = []
// console.log(cid[1][1])
let pageDisplay = []

const cashValues = (changeDue, cid) => {
  let newChangeAmount = changeDue
  let totalCid = 0
  // console.log(...currentCid)
  for (let i = 8; i >= 0; i--) {
    totalCid += cid[i][1]
    let totalDen = 0

    let numOfUnit = Math.floor(newChangeAmount / cidDen[i][1]);
    let numOfUnitCid = (cid[i][1] / cidDen[i][1]);

    // console.log(cidDen[i][0], '-', 'NOU:', numOfUnit, 'NOUID:', numOfUnitCid, 'NCA:', newChangeAmount)

    if (numOfUnit < numOfUnitCid && numOfUnit > 0 && numOfUnitCid > 0 && newChangeAmount > 0) {
      for (let j = 0; j < numOfUnit; j++) {
        // console.log(cidDen[i][0])
        // console.log(display[display.length-2],cidDen[i][0])

        totalDen += cidDen[i][1]
        if (display[display.length - 1] != `${cidDen[i][0]}:`) {
          display.push(`${cidDen[i][0]}:`)
        }
        newChangeAmount = Math.round(newChangeAmount * 100) / 100 - cidDen[i][1];
        numOfUnitCid--
      }

      display.push(`$${totalDen}`)

    } else if (numOfUnit > numOfUnitCid && numOfUnitCid > 0) {
      for (let j = 0; j < numOfUnitCid; j++) {

        totalDen += cidDen[i][1]
        if (display[display.length - 1] != `${cidDen[i][0]}:`) {
          display.push(`${cidDen[i][0]}:`)
        }
        newChangeAmount = (Math.round(newChangeAmount * 100) / 100) - cidDen[i][1];
      }
      display.push(`$${totalDen}`)
    } else if (numOfUnit == numOfUnitCid && numOfUnitCid > 0) {
      for (let j = 0; j < numOfUnit; j++) {

        totalDen += cidDen[i][1]
        if (display[display.length - 1] != `${cidDen[i][0]}:`) {
          display.push(`${cidDen[i][0]}:`)
        }

        newChangeAmount = Math.round(newChangeAmount * 100) / 100 - cidDen[i][1];
        numOfUnitCid--

        // console.log(totalCid,cidDen[i][1])
        if (totalCid > 0) {
          totalCid = Math.round(totalCid * 100) / 100 - cidDen[i][1]
        }
        // console.log(cidDen[i][0],totalCid)
      }
      totalDen = Math.round(totalDen * 100) / 100
      // console.log(totalDen)
      display.push(`$${totalDen}`)
    }
    // console.log(cidDen[i][0], '-', 'NOU:', numOfUnit, 'NOUID:', numOfUnitCid, 'NCA:', newChangeAmount)
  }
  // console.log(newChangeAmount, totalCid,display.join(' '))
  if (totalCid < 0.01) {
    changeDueEle.textContent += `Status: CLOSED ${display.join(' ')}`
  } else if (newChangeAmount > 0) {
    changeDueEle.textContent += `Status: INSUFFICIENT_FUNDS`
  } else if (totalCid > 0.01) {
    changeDueEle.textContent += `Status: OPEN ${display.join(' ')}`

  }
}

const registerTests = (price, cash, cid) => {
  if (cash === price) {
    console.log('No change due')
    changeDueEle.textContent = "No change due - customer paid with exact cash"
  } else if (cash < price) {
    console.log('Not enough money')
    alert('Customer does not have enough money to purchase the item')
  } else if (cash > price) {
    let changeDue = cash - price
    let totalCid = 0
    for (let i = 0; i < 8; i++) {
      totalCid += cid[i][1]
    }
    if (changeDue > totalCid) {
      changeDueEle.textContent = 'Status: INSUFFICIENT_FUNDS'
    } else {
      cashValues(changeDue, cid)
      // console.log('Change due:', changeDue, 'CID:', totalCid)
    }
  }
}

purchaseBtn.addEventListener('click', () => { registerTests(price, Number(cashInput.value), cid) })

WARNING

The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.

You will need to take an additional step here so the code you wrote presents in an easy to read format.

Please copy/paste all the editor code showing in the challenge from where you just linked.

Replace these two sentences with your copied code.
Please leave the ``` line above and the ``` line below,
because they allow your code to properly format in the post.

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36

Challenge Information:

Build a Cash Register Project - Build a Cash Register

the tests will populate and change this, are you sure you are accounting for that?

If I declare
let cid
test 9 still fails.

can you provide your html? it is needed for debugging

Sure.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Cash Register</title>
  <link rel="stylesheet" href="./styles.css">
</head>
<body>
  <h1>Cash Register Project</h1>
  <input id="cash">
  <button id="purchase-btn">Purchase</button>
  <div id="change-due"></div>
  <script src="./script.js"></script>
</body>
</html>

I found the code that was not passing.
changeDueEle.textContent += 'Status: INSUFFICIENT_FUNDS'
However the following code did pass.
changeDueEle.innerText = 'Status: INSUFFICIENT_FUNDS'

Thank you ilenia for taking time to look at my code.

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