Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

I dont know what to do: 12. When

price

is

3.26

, the value in the

#cash

element is

100

,

cid

is

[["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]

, and the

#purchase-btn

element is clicked, the value in the

#change-due

element should be

"Status: OPEN TWENTY: $60 TEN: $20 FIVE: $15 ONE: $1 QUARTER: $0.5 DIME: $0.2 PENNY: $0.04"

. 13. 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. 18. When

price

is

19.5

, the value in the

#cash

element is

20

,

cid

is

[["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]

, and the

#purchase-btn

element is clicked, the value in the

#change-due

element should be

"Status: CLOSED PENNY: $0.5"

. 19. 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

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Cash Register</title>
  </head>
  <body>
    <h1> Input your cash</h1>
    <input id="cash">
    <div id="change-due"></div>
    <button id="purchase-btn">purchase</button>
  <script src="script.js"></script>
  </body>
  </html>
/* file: script.js */
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]
];
let cash = document.getElementById("cash");
const purchaseBtn = document.getElementById("purchase-btn");
const changeDue = document.getElementById("change-due");

 const demonations = [
    ["ONE HUNDRED", 100],
    ["TWENTY", 20],
    ["TEN", 10],
    ["FIVE", 5],
    ["ONE", 1],
    ["QUARTER", 0.25],
    ["DIME", 0.1],
    ["NICKEL", 0.05],
    ["PENNY", 0.01]
  ];

function purchase() {
  const cashValue = parseFloat(cash.value);
  const changeDueAmount = cashValue - price;

  if (cashValue < price) {
    alert("Customer does not have enough money to purchase the item");
    return;
  }
  
  if (changeDueAmount === 0) {
    changeDue.innerText = "No change due - customer paid with exact cash";
    return;
  }

  let totalCid = 0;
  for (const [denom, amount] of cid) {
    totalCid += amount;
  }

  if (totalCid < changeDueAmount) {
    changeDue.innerText = "Status: INSUFFICIENT_FUNDS";
    return;
  }


  let change = [];
  let changeRemaining = changeDueAmount;

  for(const [denom, value] of demonations) {
    let denomAmount = cid.find(([d]) => d === denom)[1];
    let amountToReturn = 0;

    while (changeRemaining >= value && denomAmount > 0) {
      changeRemaining -= value;
      denomAmount -= value;
      amountToReturn += value;
    }

    if (amountToReturn > 0) {
      change.push(`${denom}: $${amountToReturn.toFixed(2)}`);
    }
  }

changeRemaining = Math.round(changeRemaining * 100) / 100;

  if (changeRemaining > 0) {
    changeDue.innerText = "Status: INSUFFICIENT_FUNDS";
  } else if (changeRemaining === 0 && totalCid === changeDueAmount) {
   
     changeDue.innerText = `Status: CLOSED ${change.join(" ")}`;
  }
     else {
     
      changeDue.innerText = `Status: OPEN ${change.join(" ")}`;
    }
  }


purchaseBtn.addEventListener("click", purchase);

/* file: styles.css */

Your browser information:

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

Challenge Information:

Build a Cash Register Project - Build a Cash Register

What have you tried so far?

I tried this:

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]
];
let cash = document.getElementById("cash");
const purchaseBtn = document.getElementById("purchase-btn");
const changeDue = document.getElementById("change-due");

 const demonations = [
    ["ONE HUNDRED", 100],
    ["TWENTY", 20],
    ["TEN", 10],
    ["FIVE", 5],
    ["ONE", 1],
    ["QUARTER", 0.25],
    ["DIME", 0.1],
    ["NICKEL", 0.05],
    ["PENNY", 0.01]
  ];

function purchase() {
  const cashValue = parseFloat(cash.value);
  const changeDueAmount = cashValue - price;

  if (cashValue < price) {
    alert("Customer does not have enough money to purchase the item");
    return;
  }
  
  if (changeDueAmount === 0) {
    changeDue.innerText = "No change due - customer paid with exact cash";
    return;
  }

  let totalCid = 0;
  for (const [denom, amount] of cid) {
    totalCid += amount;
  }

  if (totalCid < changeDueAmount) {
    changeDue.innerText = "Status: INSUFFICIENT_FUNDS";
    return;
  }


  let change = [];
  let changeRemaining = changeDueAmount;

  for(const [denom, value] of demonations) {
    let denomAmount = cid.find(([d]) => d === denom)[1];
    let amountToReturn = 0;

    while (changeRemaining >= value && denomAmount > 0) {
      changeRemaining -= value;
      denomAmount -= value;
      amountToReturn += value;
    }

    if (amountToReturn > 0) {
      change.push(`${denom}: ${amountToReturn.toFixed(2)}`);
    }
  }

changeRemaining = Math.round(changeRemaining * 100) / 100;

  if (changeRemaining > 0) {
    changeDue.innerText = "Status: INSUFFICIENT_FUNDS";
  } else if (changeRemaining === 0 && totalCid === changeDueAmount) {
   
     changeDue.innerText = `Status: CLOSED ${change.join(" ")}`;
  }
     else {
     
      changeDue.innerText = `Status: OPEN ${change.join(" ")}`;
    }
  }


purchaseBtn.addEventListener("click", purchase);

Right, but that doesn’t really say what was attempted to make test 12 pass. Have you tried to manually check that test case? Do you know what result is displayed instead of the expected one? Have you tried to pin-point place in code when things go not as expected?

I tried to make it very complicated:

if (price === 3.26 && cash.value === 100 && cid === 
[["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]
```) { changeDue.innerText = "Status: OPEN TWENTY: $60 TEN: $20 FIVE: $15 ONE: $1 QUARTER: $0.5 DIME: $0.2 PENNY: $0.04" ;

But that doesn’t work. I even become more issues.

It looks like you have hard-coded conditionals or variables that check for specific expected values. That is not solving this problem in the general case. Imagine if you were given different input values. Would your code be able to solve those problems?

To find out more about what hard-coding is or about why it is not suitable for solving coding questions, please read this post: Hard-coding For Beginners

Let us know if you have a question about how to make your code more flexible.

Oke so that is the problem. Ok and how can i fix these 2 more issues that now left? 12. When

price

is

3.26

, the value in the

#cash

element is

100

,

cid

is

[["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]

, and the

#purchase-btn

element is clicked, the value in the

#change-due

element should be

"Status: OPEN TWENTY: $60 TEN: $20 FIVE: $15 ONE: $1 QUARTER: $0.5 DIME: $0.2 PENNY: $0.04"

. 18. When

price

is

19.5

, the value in the

#cash

element is

20

,

cid

is

[["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]

, and the

#purchase-btn

element is clicked, the value in the

#change-due

element should be

"Status: CLOSED PENNY: $0.5"

. // tests completed

what debugging have you tried? have you verified that your app gives the correct result when tested manually?

If I enter 100 in the cash field and then click on purchase, I only get the response status: INSUFFICIENT_FUNDS.

with what values of cid and price?

let price = 3.26;
let cid = [
[‘PENNY’, 0.5],
[‘NICKEL’, 0],
[‘DIME’, 0],
[‘QUARTER’, 0],
[‘ONE’, 0],
[‘FIVE’, 0],
[‘TEN’, 0],
[‘TWENTY’, 0],
[‘ONE HUNDRED’, 0]
];

I have tried this now: 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]
];
let cash = document.getElementById(“cash”);
const purchaseBtn = document.getElementById(“purchase-btn”);
const changeDue = document.getElementById(“change-due”);

// Denominations in absteigender Reihenfolge
const denominations = [
[“ONE HUNDRED”, 100],
[“TWENTY”, 20],
[“TEN”, 10],
[“FIVE”, 5],
[“ONE”, 1],
[“QUARTER”, 0.25],
[“DIME”, 0.1],
[“NICKEL”, 0.05],
[“PENNY”, 0.01]
];

// Funktion, die den Kaufvorgang durchführt
function purchase() {
const cashValue = parseFloat(cash.value);
const changeDueAmount = cashValue - price;

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

if (changeDueAmount === 0) {
changeDue.innerText = “No change due - customer paid with exact cash”;
return;
}

let totalCid = 0;
for (const [denom, amount] of cid) {
totalCid += amount;
}

if (totalCid < changeDueAmount) {
changeDue.innerText = “Status: INSUFFICIENT_FUNDS”;
return;
}

let change = ;
let changeRemaining = changeDueAmount;

// Durchlaufe die Denominationen in absteigender Reihenfolge
for (const [denom, value] of denominations) {
let denomAmount = cid.find(([d]) => d === denom)[1];
let amountToReturn = 0;

// Berechne, wie viel vom jeweiligen Schein oder der Münze zurückgegeben werden kann
while (changeRemaining >= value && denomAmount >= value) {
  changeRemaining = Math.round((changeRemaining - value) * 100) / 100; // Runde auf 2 Dezimalstellen
  denomAmount -= value;
  amountToReturn += value;
}

// Füge das Wechselgeld zur Liste hinzu, wenn etwas zurückgegeben werden kann
if (amountToReturn > 0) {
  change.push(`${denom}: $${amountToReturn.toFixed(2)}`);
}

}

// Wenn der Restbetrag nicht null ist, gibt es nicht genug Wechselgeld
if (changeRemaining > 0) {
changeDue.innerText = “Status: INSUFFICIENT_FUNDS”;
} else {
// Wenn das gesamte Wechselgeld mit dem verfügbaren Kassenbestand übereinstimmt
const totalChange = change.reduce((acc, [denom, amount]) => acc + amount, 0);
if (totalChange === changeDueAmount) {
changeDue.innerText = Status: CLOSED ${change.join(" ")};
} else {
changeDue.innerText = Status: OPEN ${change.join(" ")};
}
}
}

// Eventlistener für den Button
purchaseBtn.addEventListener(“click”, purchase);
But 2 Test are still left: 18. When

price

is

19.5

, the value in the

#cash

element is

20

,

cid

is

[["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]

, and the

#purchase-btn

element is clicked, the value in the

#change-due

element should be

"Status: CLOSED PENNY: $0.5"

. 19. 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

and how is it going? do you still need help?