Jul 2024 Cash Register Bug on Tests? | Certification Project

Hi there,

I am finishing the certification project for the cash register and have noticed a potential issue with the tests. Specifically, there seems to be a contradiction between the tests with actual numbers and the generic tests without numbers.

  • Observation: Every test with specified numbers passes.
  • Issue: When the same logic is applied to generic tests (without specified numbers), they fail.

I have added the 5 generic tests that fail under the ‘Failed Tests’ section, along with their corresponding tests with specified numbers that pass when the same variables are used.

Could you please review this discrepancy? Or tell me if something with the code is buggy?

Also there is a weird Refrence Error about _randomNumber is not defined which I do not have any variable like that. I add logs at the end.

Failed Tests

As a new user I can only upload one picture, I hope it is easy to read. but in order from top to bottom there are the following failed cases

Failed #1 Cash < Price

Failed #2 Cash === Price

Failed #3 ‘Status: OPEN’

  • The price of an item is less than the total cash available in the drawer.
  • The drawer has enough funds to provide the required change.
  • The purchase button is clicked to initiate the transaction.

Failed # 4 & #5 ‘Status: INSUFFICIENT_FUNDS’

Click to see code for reference

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

let price = 19.5;
let cid = [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]];

const currencyUnits = [
    ['PENNY', .01],
    ['NICKEL', .05],
    ['DIME', .10],
    ['QUARTER', .25],
    ['ONE', 1],
    ['FIVE', 5],
    ['TEN', 10],
    ['TWENTY', 20],
    ['ONE HUNDRED', 100]
];


purchaseBtn.addEventListener('click', ()=>{

  const cashValue = parseFloat(cash.value)
  const changeDue = cashValue - price

  if (cashValue < price) {
    alert('Customer does not have enough money to purchase the item')
    return
  }

  if (cashValue === price) {
    change.innerText = 'No change due - customer paid with exact cash'
    return
  }

  const changeResult = getChange(changeDue, cid)

  if (changeResult.status === 'INSUFFICIENT_FUNDS') {
    change.innerText = `Status: ${changeResult.status}`
  } else if ( changeResult.status === 'CLOSED'){
     change.innerText = `Status: ${changeResult.status} ${formatChange(changeResult.change)}`
  } else {
    let changeText = `Status: OPEN ${formatChange(changeResult.change)}`
    change.innerText = changeText.trim()
  }

})

function getChange(changeDue, cid){
  let totalCid = parseFloat(cid.reduce((sum, [_, amount])=> sum + amount, 0).toFixed(2))
  if (totalCid < changeDue ){
    return {status: 'INSUFFICIENT_FUNDS', change:[]}
  }

  let changeArray = [];
  let remainingChange = changeDue;

  for (let i = currencyUnits.length - 1; i >= 0; i--) {
    let unit = currencyUnits[i][0];
    let unitValue = currencyUnits[i][1];
    let unitInDrawer = cid[i][1];
    
    if (unitValue <= remainingChange && unitInDrawer > 0){
       let amountFromUnit = 0;
       while (remainingChange >= unitValue && unitInDrawer>0){
        remainingChange = (remainingChange -unitValue).toFixed(2)
        unitInDrawer -= unitValue
        amountFromUnit += unitValue
       }
       if (amountFromUnit > 0){
         changeArray.push([unit, amountFromUnit])
       }
    } //end if(unitValue <= remainingChange && unitInDrawer > 0)
  }//end for loop

  if (remainingChange > 0){
    return  {status: 'INSUFFICIENT_FUNDS', change:[]}
  } 

  if (changeDue === totalCid){
    return  {status: 'CLOSED', change:cid}
  }

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

} 


const formatChange = changeArray => changeArray.map(([unit, amount])=> {
  if (amount > 0){
    return `${unit}: $${amount.toFixed(2)}`
  } else {
    return
  }
}).join(' ')

Logs when Submitting Code

"
// running tests
When the value in the #cash element is less than price, an alert should appear with the text “Customer does not have enough money to purchase the item”.
When the value in the #cash element is equal to price, the value in the #change-due element should be “No change due - customer paid with exact cash”.
When price is less than the value in the #cash element, total cash in drawer cid is greater than the change due, individual denomination amounts allows for returning change due, and the #purchase-btn element is clicked, the value in the #change-due element should be “Status: OPEN” with required change due in coins and bills sorted in highest to lowest order.
When price is less than the value in the #cash element, total cash in drawer cid is greater than change due, individual denomination amounts make impossible to return needed change, and the #purchase-btn element is clicked, the value in the #change-due element should be “Status: INSUFFICIENT_FUNDS”
When price is less than the value in the #cash element, total cash in drawer cid is less than the change due, and the #purchase-btn element is clicked, the value in the #change-due element should be “Status: INSUFFICIENT_FUNDS”.
When price is less than the value in the #cash element, total cash in drawer cid is equal to change due, and the #purchase-btn element is clicked, the value in the #change-due element should be “Status: CLOSED” with change due in coins and bills sorted in highest to lowest order.
// tests completed
// console output
[ReferenceError: _randomNumber is not defined]
[ReferenceError: _randomNumber is not defined]
[ReferenceError: _randomNumber is not defined]
[ReferenceError: _randomNumber is not defined]
[ReferenceError: _randomNumber is not defined]
[ReferenceError: _randomNumber is not defined]
"

It’s a bug:

It has been fixed, now we just need to wait for the deployment of the fix

1 Like

Thank you @ILM ! I see that it is fixed. I still had to make minor changes but today I was able to pass the certification!

Thanks to the freeCodeCamp team <3

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