Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

I’m not passing last test and i don’t know why. It looks the same as example, if someone could check it out i would be very thankful. No rush, i just want to move on to the next project. I’m really grateful for all the work you guys are doing and i hope in future i can help people to learn code as well.

Your code so far

const cash = document.getElementById(‘cash’);
const purchaseBtn = document.getElementById(‘purchase-btn’);
const changeDue = document.getElementById(‘change-due’);
const totalAmount = document.querySelector(‘.total’);
const changeDisplay = document.querySelectorAll(‘.changes span’);
let price = 19.5;
let cid = [[“PENNY”, 0.5], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 0], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]];

// currencyInformation are amounts from smallest to biggest - penny equal to 0.01 and one hundred is equal 100
const currencyInformation = [0.01,0.05,0.1,0.25,1,5,10,20,100];
// 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
// }
//
// Displaying different stages depending on cashier situation with cash register
const stateManaging = [“OPEN”, “CLOSED”,“INSUFFICIENT_FUNDS”];

purchaseBtn.addEventListener(‘click’,() => {
const money = cash.value;
mainCalculation(money);
});
//IFEE functions to display correct amount on a website for first time
(function displayTotal(total){
totalAmount.textContent = price;
})();
function displayChange(cid){
for(let i = 0; i < changeDisplay.length; i++) {
changeDisplay[i].textContent = $${cid[i][1]}
}
};
displayChange(cid);
// Main logic for displaying correct output for our cashier
const mainCalculation = (userValue) => {
changeDue.textContent = ‘’;
userValue = parseFloat(userValue);
if(userValue < price) {
alert(“Customer does not have enough money to purchase the item”);
return;
}
if(Math.abs(userValue - price) < 0.01) {
changeDue.textContent += “No change due - customer paid with exact cash”;
return;
}
if(userValue > price) {
let result = givingRestMoney(userValue, cid);
let newParagraph = document.createElement(‘p’);
if(result.isInsufficient) {
newParagraph.appendChild(document.createTextNode(Status: ${stateManaging[2]}));
changeDue.appendChild(newParagraph);
} else {
let statusText = parseFloat(sumRegister(cid)) < 0.005 ? Status: ${stateManaging[1]} : Status: ${stateManaging[0]}; // CLOSED or OPEN
newParagraph.appendChild(document.createTextNode(statusText));
changeDue.appendChild(newParagraph);
let quarterParagraph = document.createElement(‘p’);
quarterParagraph.textContent = “QUARTER: $0”;
changeDue.appendChild(quarterParagraph);
let dimeParagraph = document.createElement(‘p’);
dimeParagraph.textContent = “DIME: $0”;
changeDue.appendChild(dimeParagraph);
let nickelParagraph = document.createElement(‘p’);
nickelParagraph.textContent = “NICKEL: $0”;
changeDue.appendChild(nickelParagraph);
for (const key in result.changes) {
if (result.changes.hasOwnProperty(key)) {
let changeText = document.createElement(‘p’);
if(result.changes[key].toFixed(2) == 0.50) {
changeText.textContent = ${key}: $${result.changes[key].toFixed(1)};
changeDue.appendChild(changeText);
} else {
changeText.textContent = ${key}: $${result.changes[key].toFixed(2)};
changeDue.appendChild(changeText);
displayChange(cid);
}
}
}
}
}
};
//Algorithm counting exact amount to give back to customer. It returns an object with key=value as penny=amount of how many. It also change cid so when giving back we also decrement amount in drawer
const givingRestMoney = (userInput, cid) => {
let difference = parseFloat((userInput - price).toFixed(2));
let changes = {};
let i = currencyInformation.length - 1;

while (difference > 0.005 && i >= 0) { // Use a small tolerance for floating-point comparisons
let key = cid[i][0];
let coinValue = currencyInformation[i];
let coinAmount = cid[i][1];

if (difference >= coinValue && coinAmount >= coinValue) {
  let howManyTimes = Math.min(Math.floor(difference / coinValue), Math.floor(coinAmount / coinValue));
  let howMuch = coinValue * howManyTimes;

  difference -= howMuch;
  difference = parseFloat(difference.toFixed(2)); // Round difference to maintain precision

  cid[i][1] -= howMuch;
  changes[key] = howMuch;
}
i--;

}
const isInsufficient = difference > 0.005;
sumRegister(cid);
return {
changes: changes,
isInsufficient: difference > 0.005
};
};
const sumRegister = (array) => {
let sum = 0;
for(const num of array) {
sum += num[1];
}
return sum.toFixed(2);
}

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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36

Challenge Information:

Build a Cash Register Project - Build a Cash Register

I didn’t test your code but the last test is bugged. It has been fixed but is not in production yet. I would suggest you wait a few days or a week and try again.

2 Likes

how long can i stay bugged before someone fixes it? cuase ive been stuggling with the same issue for a week

I don’t know, I assume because so many fixes are being made we are holding off on pushing to production.

But it would be nice to get this fix into production seeing as it is a certificate project.

1 Like

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