Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

My code is producing the expected results but it’s still saying I’m failing most of the tests. I put in the exact values for price and cid that the tests ask for and it’s coming out with the exact right text, but it still says I’m not passing the tests. (Also I know I have a really messy way of handling the assignment, it’s just the only way it made sense in my head haha.)

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">
    <title>Cash Register Project</title>
    <link rel="stylesheet" href="styles.css">
  </head>
  <body>
    <input id="cash" type="number">
    <button id="purchase-btn">Purchase</button>
    <p id="change-due"></p>
  </body>
  <script src="script.js"></script>
</html>
/* file: script.js */
let price = 1.87;
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 cash = document.getElementById("cash");
const changeDue = document.getElementById("change-due");
const purchaseBtn = document.getElementById("purchase-btn");

const cashValue = (arr) => {
  let total = 0;
  for (let i = 0; i < arr.length; i++) {
    total += arr[i][1];
  }
  return total.toFixed(2);
}
const cashDrawer = cashValue(cid);

let hundredCounter = 0;
let twentyCounter = 0;
let tenCounter = 0;
let fiveCounter = 0;
let oneCounter = 0;
let quarterCounter = 0;
let dimeCounter = 0;
let nickelCounter = 0;
let pennyCounter = 0;

let hundredGiven = false;
let twentyGiven = false;
let tenGiven = false;
let fiveGiven = false;
let oneGiven = false;
let quarterGiven = false;
let dimeGiven = false;
let nickelGiven = false;
let pennyGiven = false;

purchaseBtn.addEventListener("click", purchase);

function changeCalculator(change) {
  if (change > cashDrawer) {
    changeDue.textContent = "Status: INSUFFICIENT_FUNDS";
  } else {
    changeDue.innerHTML = "Status: OPEN <br>";
    console.log(change);
    while (change >= 100 && cid[8][1] > 0) {
      change = (change - 100).toFixed(2);
      cid[8][1] = (cid[8][1] - 100).toFixed(2);
      hundredGiven = true;
      hundredCounter++;
    } 
    console.log(change);
    while (change >= 20 && cid[7][1] > 0) {
      change = (change - 20).toFixed(2);
      cid[7][1] = (cid[7][1] - 20).toFixed(2);
      twentyGiven = true;
      twentyCounter++;
    }
    console.log(change);
    while (change >= 10 && cid[6][1] > 0) {
      change = (change - 10).toFixed(2);
      cid[6][1] = (cid[6][1] - 10).toFixed(2);
      tenGiven = true;
      tenCounter++;
    } 
    console.log(change);
    while (change >= 5 && cid[5][1] > 0) {
      change = (change - 5).toFixed(2);
      cid[5][1] = (cid[5][1] - 5).toFixed(2);
      fiveGiven = true;
      fiveCounter++;
    }
    console.log(change);
    while (change >= 1 && cid[4][1] > 0) {
      change = (change - 1).toFixed(2);
      cid[4][1] = (cid[4][1] - 1).toFixed(2);
      oneGiven = true;
      oneCounter++;
    }
    console.log(change);
    while (change >= 0.25 && cid[3][1] > 0) {
      change = (change - 0.25).toFixed(2);
      cid[3][1] = (cid[3][1] - 0.25).toFixed(2);
      quarterGiven = true;
      quarterCounter++;
    }
    console.log(change);
    while (change >= 0.1 && cid[2][1] > 0) {
      change = (change - 0.1).toFixed(2);
      cid[2][1] = (cid[2][1] - 0.1).toFixed(2);
      dimeGiven = true;
      dimeCounter++;
    }
    console.log(change);
    while (change >= 0.05 && cid[1][1] > 0) {
      change = (change - 0.05).toFixed(2);
      cid[1][1] = (cid[1][1] - 0.05).toFixed(2);
      nickelGiven = true;
      nickelCounter++;
    }
    console.log(change);
    while (change >= 0.01 && cid[0][1] > 0) {
      change = (change - 0.01).toFixed(2);
      cid[0][1] = (cid[0][1] - 0.01).toFixed(2);
      pennyGiven = true; 
      pennyCounter++;
    }
    console.log(change);
  }
  cashWriter();
}

function cashWriter() {
  changeDue.innerHTML += hundredGiven ? `ONE HUNDRED: $${hundredCounter * 100} <br>` : "";
  changeDue.innerHTML += twentyGiven ? `TWENTY: $${twentyCounter * 20} <br>` : "";
  changeDue.innerHTML += tenGiven ? `TEN: $${tenCounter * 10} <br>` : "";
  changeDue.innerHTML += fiveGiven ? `FIVE: $${fiveCounter * 5} <br>` : "";
  changeDue.innerHTML += oneGiven ? `ONE: $${oneCounter} <br>` : "";
  changeDue.innerHTML += quarterGiven ? `QUARTER: $${(quarterCounter * 0.25).toFixed(2)} <br>` : "";
  changeDue.innerHTML += dimeGiven ? `DIME: $${(dimeCounter * 0.1).toFixed(2)} <br>` : "";
  changeDue.innerHTML += nickelGiven ? `NICKEL: $${(nickelCounter * 0.05).toFixed(2)} <br>` : "";
  changeDue.innerHTML += pennyGiven ? `PENNY: $${(pennyCounter * 0.01).toFixed(2)}` : "";
}

function purchase() {
  const floatCash = parseFloat(cash.value);
  if (floatCash < price) {
    alert("Customer does not have enough money to purchase the item");
  }
  if (floatCash === price) {
    changeDue.textContent = "No change due - customer paid with exact cash";
  }
  if (floatCash > price) {
    let change = floatCash - price;
    changeCalculator(change);
  }
}
/* file: styles.css */

Your browser information:

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

Challenge Information:

Build a Cash Register Project - Build a Cash Register

Also, I know I need to add the “Status: CLOSED” part of the project still, I’m just trying to make the basic math part work before I do that.

you should not have global variables, or you get issues like this:

If you test your code with two subsequent tests, adding the following at the bottom of your editor

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]]

document.querySelector('#purchase-btn').click();

cid = [ [ 'PENNY', 0.86 ],
     [ 'NICKEL', 0.45 ],
     [ 'DIME', 1.4 ],
     [ 'QUARTER', 2.25 ],
     [ 'ONE', 7 ],
     [ 'FIVE', 75 ],
     [ 'TEN', 100 ],
     [ 'TWENTY', 200 ],
     [ 'ONE HUNDRED', 1400 ] ],
  price = 86.9,
  cash.value =  '100' 

document.querySelector('#purchase-btn').click();

the output for the second one is

Status: OPEN
TWENTY: $60
TEN: $30
FIVE: $15
ONE: $4
QUARTER: $0.50
DIME: $0.30
PENNY: $0.04

which is quite too high

Oh, I see! I didn’t catch that, thanks for pointing it out. :slight_smile: