Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

Hi, I’ve tried to find solutions elsewhere but none are up to date or help me solve this one.

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="styles.css">
  </head>
  <body>
    <main>

        <input id="cash"></input>
        <div id="change-due"></div>
        <button id="purchase-btn"></button>
    </main>
	<script src="script.js"></script>
  </body>
</html>
/* file: styles.css */

/* file: script.js */
let cashElement = document.querySelector('#cash');
let changeDueElement = document.querySelector('#change-due');
let purchaseBtnElement = document.querySelector('#purchase-btn');


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

  let cash = parseFloat(cashElement.value);

array
  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]]; // replace this with your actual cid


  let changeDue = cash - price;


  if (cash < price) {
    alert("Customer does not have enough money to purchase the item");
  } else if (cash === price) {
    changeDueElement.textContent = "No change due - customer paid with exact cash";
  } else {


    let changeDueStr = cid.reduce((str, [unit, amount]) => {
      let unitCount = Math.floor(changeDue / amount);
      changeDue -= unitCount * amount;
      return str + (unitCount > 0 ? ` ${unit.toUpperCase()}: $${(unitCount * amount).toFixed(2)}` : '');
    }, "Status: OPEN").trim();
    changeDueElement.textContent = changeDueStr;
  }
});

Your browser information:

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

Challenge Information:

Build a Cash Register Project - Build a Cash Register

this was another solution, but doesn’t quite work with the updates so I don’t know how to make it work correctly

function checkCashRegister(price, cash, cid){
let currencyUnit = {
“PENNY”: .01,
“NICKEL”: .05,
“DIME”: .1,
“QUARTER”: .25,
“ONE”: 1,
“FIVE”: 5,
“TEN”: 10,
“TWENTY”: 20,
“ONE HUNDRED”: 100
}
let changeDue = cash - price;
let totalCID = Number(cid.reduce((sum, element) => sum + element[1], 0).toFixed(2));

if(totalCID < changeDue) {
return {Status: “INSUFFICIENT_FUNDS”, change: };
}
else if(totalCID == changeDue) {
return {Status: “CLOSED”, change: cid};
}
else{
let changeArr = ;
for(let i = cid.length -1; i >= 0; i–){
let currencyUnitName = cid[i][0];
let ccurrencyUnitValueTotal = cid[i][1];
let currencyUnitValue = currencyUnit[currencyUnitName];
let currencyUnitAmount = Number((currencyUnitValueTotal / currencyUnitValue).toFixed(0));
let currencyUnitsToReturn = 0;

while(changeDue >= currencyUnitValue && currencyUnitAmount > 0){
changeDue -= currencyUnitValue;
changeDue = Number(changeDue.toFixed(2));
currencyUnitAmount–;
currencyUnitsToReturn++;
}
if(currencyUnitsToReturn > 0){
changeArr.push([currencyUnitName, currencyUnitsToReturn * currencyUnitValue])
}
}
if(changeDue > 0){
return {Status: “INSUFFICIENT_FUNDS”, change: };
}
return {Status: “OPEN”, change: changeArr};
}
}

console.log(checkCashRegister(19.5, 20, [[“PENNY”, 1.01], [“NICKEL”, 2.05], [“DIME”, 3.1], [“QUARTER”, 4.25], [“ONE”, 90], [“FIVE”, 55], [“TEN”, 20], [“TWENTY”, 60], [“ONE HUNDRED”, 100]]));

Looking at other people’s full solutions before you solve the certification projects yourself isn’t really keeping with the spirit of the academic honesty policy.

You need to remove any code that you did not write yourself.

We are of course happy to help you with the code you wrote yourself for this project.

I didn’t just copy and paste I watched videos on how people solved it at the time.

this is the code I know how it works:
let cash = document.querySelector(‘#cash’);//get cash ID input
let changeDueElement = document.querySelector(‘#change-due’); // getdiv change ID
let purchase = document.querySelector(‘#purchase-btn’);// get purchase button

purchaseBtnElement.addEventListener(‘click’, () => {
let cid = {
“PENNY”: .01,
“NICKEL”: .05,
“DIME”: .1,
“QUARTER”: .25,
“ONE”: 1,
“FIVE”: 5,
“TEN”: 10,
“TWENTY”: 20,
“ONE HUNDRED”: 100
}//currency given to us by FCC
});
let price = 19.5; // what code starts with

let changeDue = cash - price;//simple maths equasion

if (cash < price) {
alert(“Customer does not have enough money to purchase the item”);//if cash less than price output item
} else if (cash === price) {
changeDueElement.textContent = “No change due - customer paid with exact cash”;// if cash given is strictly equal to price output text content of change due element
}

I know I need an else statement, but don’t understand the last parts other than tofixed method fixes digets based on the arguement

the last part ultimitely needs to be something like this:
else {
to calculate the exact change due
let changeDueStr = cid.reduce((str, [unit, amount]) => {
let unitCount = Math.floor(changeDue / amount);
changeDue -= unitCount * amount;
return str + (unitCount > 0 ? ${unit.toUpperCase()}: $${(unitCount * amount).toFixed(2)} : ‘’);
}, “Status: OPEN”).trim();
changeDueElement.textContent = changeDueStr;
}
}); // as is the more up to date text needing to be output

instead of this:
else{
let changeArr = ;
for(let i = cid.length -1; i >= 0; i–){
let currencyUnitName = cid[i][0];
let ccurrencyUnitValueTotal = cid[i][1];
let currencyUnitValue = currencyUnit[currencyUnitName];
let currencyUnitAmount = Number((currencyUnitValueTotal / currencyUnitValue).toFixed(0));
let currencyUnitsToReturn = 0;

while(changeDue >= currencyUnitValue && currencyUnitAmount > 0){
changeDue -= currencyUnitValue;
changeDue = Number(changeDue.toFixed(2));
currencyUnitAmount–;
currencyUnitsToReturn++;
}
if(currencyUnitsToReturn > 0){
changeArr.push([currencyUnitName, currencyUnitsToReturn * currencyUnitValue])
}
}
if(changeDue > 0){
return {Status: “INSUFFICIENT_FUNDS”, change: };
}
return {Status: “OPEN”, change: changeArr};
}
}

console.log(checkCashRegister(19.5, 20, [[“PENNY”, 1.01], [“NICKEL”, 2.05], [“DIME”, 3.1], [“QUARTER”, 4.25], [“ONE”, 90], [“FIVE”, 55], [“TEN”, 20], [“TWENTY”, 60], [“ONE HUNDRED”,
// as this is out of date and only outputs “INSUFFICIENT_FUNDS”, “OPEN”, or “CLOSED”, which is the first part of the original task but I don’t know how to make it up to date as there is nothing online helping me with this and the other post mentioned on this forum was never resolved so it looks like they are in the same situation.

We are here to learn, I don’t just copy and paste I learn by doing, but if by searching for answers none are found how am I supposed to progress and learn further?

because you should not copy what others have done. Please try to solve it yourself.

It is absolutely not OK to look at other solutions before you solve the project. You need to work on the projects without looking at other peoples solutions or videos.

Its ok to get stuck, but you should ask for help instead of looking at other people’s solution.

Again, I would remove all code that you wrote while looking at other people’s solutions first, and then ask us questions about how you’re stuck.