Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

My code should pass all the tests because all the tests are passing (when i try them manually) but i have no idea why it doesnt pass when i try to run the tests

Your code so far


const cash = document.getElementById('cash');
let price = document.getElementById('price');
const purchaseBtn = document.getElementById('purchase-btn');
const changeDueElement = document.getElementById('change-due');
const cidItems = document.querySelectorAll('.cid-item');
let cid = [
    ['PENNY', 1.01],
    ['NICKEL', 2.05],
    ['DIME', 3.1],
    ['QUARTER', 4.25],
    ['ONE', 90],
    ['FIVE', 55],
    ['TEN', 20],
    ['TWENTY', 60],
    ['ONE HUNDRED', 100],
];

const getTotalCid = () => {
    let totalCid = 0;
    for (let i = 0; i < cid.length; i++) {
        totalCid += cid[i][1];
    }
    return totalCid;
}

function combineCurrencies(changeArray) {
    const resultArray = [];
  
    // Iterate through each sub-array in changeArray
    changeArray.forEach(subArray => {
      const [currency, unitValue] = subArray;
  
      // Check if the currency already exists in resultArray
      const existingCurrency = resultArray.find(item => item[0] === currency);
  
      if (existingCurrency) {
        // If the currency exists, update its unit value
        existingCurrency[1] += unitValue;
      } else {
        // If the currency doesn't exist, add a new entry to resultArray
        resultArray.push([currency, unitValue]);
      }
    });
  
    return resultArray;
  }

const calculateChangeDue = () => {
    let result = '';
    let priceValue = parseFloat(price.textContent);
    let cashValue = parseFloat(cash.value);
    const currencyUnit = {
        'PENNY': 0.01,
        'NICKEL': 0.05,
        'DIME': 0.1,
        'QUARTER': 0.25,
        'ONE': 1,
        'FIVE': 5,
        'TEN': 10,
        'TWENTY': 20,
        'ONE HUNDRED': 100
    }
    let change = cashValue - priceValue;
    let totalCid = getTotalCid();

    if (cashValue < priceValue) {
        alert('Customer does not have enough money to purchase the item');
        return;
    } else if (cashValue === priceValue) {
        result = 'No change due - customer paid with exact cash'
    } else {
        let changeArray = [];
        for (let i = cid.length-1; i >= 0; i--) {
            const cidName = cid[i][0];
            let cidValue = cid[i][1];
            const unitValue = currencyUnit[cidName];

            while (parseFloat(change.toFixed(2)) >= unitValue && cidValue > 0) {
                change =  parseFloat((change - unitValue).toFixed(2));
                cidValue = parseFloat((cidValue - unitValue).toFixed(2));
                changeArray.push([cidName, unitValue]);
                totalCid =  parseFloat((totalCid - unitValue).toFixed(2));
                console.log(change);
            }
        }
        
        let finalArray = combineCurrencies(changeArray);
        if (parseFloat(change.toFixed(2)) > 0) {
            result = 'Status: INSUFFICIENT_FUNDS';
        } else if (change === 0 && totalCid === 0) {
            result = 'Status: CLOSED ' + finalArray.map(arr => `${arr[0]}: $${arr[1]}`).join(' ');
        } else if (change === 0 && totalCid > 0) {
            result = 'Status: OPEN ' + finalArray.map(arr => `${arr[0]}: $${arr[1]}`).join(' ');
        }
    }
    changeDueElement.textContent = result;
}

purchaseBtn.addEventListener('click', calculateChangeDue);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0

Challenge Information:

Build a Cash Register Project - Build a Cash Register

price is not (should not be) an element, it is just a top-level variable.

// let price = document.getElementById('price');
let price;

// let priceValue = parseFloat(price.textContent);
let priceValue = parseFloat(price);

The last test failing is likely a bug in the tests. It has been fixed but has not been pushed to production yet.

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