Build A Cash Register Project - Build A Cash Register

Hello

I am running into an issue that I can’t quite understand. I have been running the tests on my code constantly to troubleshoot specific issues, however, whenever I change the value of a specific array in cid, some tests that were passing before suddenly fail, while the tests that I took the cid from to directly troubleshoot the issues suddenly passed.

This perplexes me, because I am pretty sure the tests are looking to reassign each array to match the cid of the specific test, but if that is the case then why do some checks suddenly fail when I manually change it myself? Below, I include my HTML and CSS, and then two different JS snippets, one with cid matching one test, and another matching a different cid. Any ideas?

Apologies in advance for the messy notes and code.

<!DOCTYPE html>
<html lang="en">
  <head>
    <link rel="stylesheet" href="styles.css">    
  </head>
  <body>
    <main>
      <h2>Cash Register</h2>
      <div id="change-due"></div>
      <input id="cash" placeholder="Enter Cash" type="number"></input>
      <button id="purchase-btn">Purchase</button>   
    </main>
    <script src="script.js"></script>
  </body>
</html>
body {
  font-family: sans-serif;
  color: #eeeeee;
  background-color: #222222;
}
// We need to take change, convert it into individual bills
// to give to the customer, and deduce that amount from
// each array in cid 

let cashInput = document.getElementById("cash");
let price = 19.5;
let purchaseBtn = document.getElementById("purchase-btn");
let changeDue = document.getElementById("change-due")

// something about changing the values here is causing an issue with the checks
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 cashReference = [
  ["PENNY", 0.01],
  ["NICKEL", 0.05],
  ["DIME", 0.1],
  ["QUARTER", 0.25],
  ["ONE", 1],
  ["FIVE", 5],
  ["TEN", 10],
  ["TWENTY", 20],
  ["ONE HUNDRED", 100]
];

const calculate = () => {

  // Change needs to be compared to each different value of
  // bill, take out the appropriate number of bills, 
  // subtract that total from each array in the cid,
  // and display the amount on screen

  let change = cashInput.value - price;
  let i = 8;
  changeDue.innerHTML = ""

  if (!determineChange() || !determineStatus()) {
    return
  }

  while (i >= 0) {

    if (change >= cashReference[i][1]) {
      console.log(change)
      let numberOfBills = Math.floor(change / cashReference[i][1])
      let amountOfBills = numberOfBills * cashReference[i][1];
      if (amountOfBills > cid[i][1]) {
        let restInDrawer = cid[i][1]
        change -= restInDrawer
        changeDue.textContent += `${cid[i][0]}: \$${restInDrawer} `
        cid[i][1] -= cid[i][1]
      } else {
        cid[i][1] -= amountOfBills;
        change -= amountOfBills;
        changeDue.textContent += `${cid[i][0]}: \$${amountOfBills} `  
      }
      change = change.toFixed(2) 
    }
    i--;
  }
}

// Deduces the total of all the cash currently in cid

let cidTotal = 0;

for (let i = 0; i <= 8; i++) {
  cidTotal += cid[i][1];
};

let roundedCidTotal = cidTotal.toFixed(2);
console.log(roundedCidTotal)

// Determines if cash register is functional

const determineStatus = () => {
  
  let change = cashInput.value - price;

  if (roundedCidTotal < change) {
    changeDue.innerHTML = `<p>Status: INSUFFICIENT_FUNDS </p>`;
    return false 
  } else if (roundedCidTotal === change) {
    changeDue.innerHTML = `<p>Status: CLOSED </p>`;
    return false
  } else if (roundedCidTotal > change) {
    changeDue.innerHTML = `<p>Status: OPEN </p>`;
    return true
    // might throw error over if change can be returned, come back to this later
  }
}

// Checking if cash is less than or equal to price to
// give change-due message

const determineChange = () => {

  if (cashInput.value < price) {
    alert("Customer does not have enough money to purchase the item")
    return false
  } else if (cashInput.value == price) {
    changeDue.innerHTML = `<p>No change due - customer paid with exact cash</p>`
    return false
  } else {return true}
}

purchaseBtn.addEventListener("click", calculate);
// We need to take change, convert it into individual bills
// to give to the customer, and deduce that amount from
// each array in cid 

let cashInput = document.getElementById("cash");
let price = 19.5;
let purchaseBtn = document.getElementById("purchase-btn");
let changeDue = document.getElementById("change-due")

// something about changing the values here is causing an issue with the checks
let cid = [
  ["PENNY", 0.1],
  ["NICKEL", 0],
  ["DIME", 0],
  ["QUARTER", 0],
  ["ONE", 0],
  ["FIVE", 0],
  ["TEN", 0],
  ["TWENTY", 0],
  ["ONE HUNDRED", 0]
];

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

const calculate = () => {

  // Change needs to be compared to each different value of
  // bill, take out the appropriate number of bills, 
  // subtract that total from each array in the cid,
  // and display the amount on screen

  let change = cashInput.value - price;
  let i = 8;
  changeDue.innerHTML = ""

  if (!determineChange() || !determineStatus()) {
    return
  }

  while (i >= 0) {

    if (change >= cashReference[i][1]) {
      console.log(change)
      let numberOfBills = Math.floor(change / cashReference[i][1])
      let amountOfBills = numberOfBills * cashReference[i][1];
      if (amountOfBills > cid[i][1]) {
        let restInDrawer = cid[i][1]
        change -= restInDrawer
        changeDue.textContent += `${cid[i][0]}: \$${restInDrawer} `
        cid[i][1] -= cid[i][1]
      } else {
        cid[i][1] -= amountOfBills;
        change -= amountOfBills;
        changeDue.textContent += `${cid[i][0]}: \$${amountOfBills} `  
      }
      change = change.toFixed(2) 
    }
    i--;
  }
}

// Deduces the total of all the cash currently in cid

let cidTotal = 0;

for (let i = 0; i <= 8; i++) {
  cidTotal += cid[i][1];
};

let roundedCidTotal = cidTotal.toFixed(2);
console.log(roundedCidTotal)

// Determines if cash register is functional

const determineStatus = () => {
  
  let change = cashInput.value - price;

  if (roundedCidTotal < change) {
    changeDue.innerHTML = `<p>Status: INSUFFICIENT_FUNDS </p>`;
    return false 
  } else if (roundedCidTotal === change) {
    changeDue.innerHTML = `<p>Status: CLOSED </p>`;
    return false
  } else if (roundedCidTotal > change) {
    changeDue.innerHTML = `<p>Status: OPEN </p>`;
    return true
    // might throw error over if change can be returned, come back to this later
  }
}

// Checking if cash is less than or equal to price to
// give change-due message

const determineChange = () => {

  if (cashInput.value < price) {
    alert("Customer does not have enough money to purchase the item")
    return false
  } else if (cashInput.value == price) {
    changeDue.innerHTML = `<p>No change due - customer paid with exact cash</p>`
    return false
  } else {return true}
}

purchaseBtn.addEventListener("click", calculate);

Are you making any calculations in the global scope? When code is modified in editor, the preview is reloaded effectively executing all code again. However during the tests that will not happen.

1 Like

Would the cidTotal calculation be global? I am not certain how that would be an issue necessarily but nothing else is really being calculated in the global scope besides taking money out of cid and putting money back in

Yes. This calculation happens only once - when preview window is loaded. When tests are changing cid, the value from the initial calculations stays unchanged.

Hello

I am running into an issue where despite the output being correct for the particular check I am trying to pass, it counts it as failed. Can anyone provide an explanation or help?

The Check:

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

My Output:

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

My Code

HTML:

<!DOCTYPE html>
<html lang="en">
  <head>
    <link rel="stylesheet" href="styles.css">    
  </head>
  <body>
    <main>
      <h2>Cash Register</h2>
      <div id="change-due"></div>
      <input id="cash" placeholder="Enter Cash" type="number"></input>
      <button id="purchase-btn">Purchase</button>   
    </main>
    <script src="script.js"></script>
  </body>
</html>

CSS:

body {
  font-family: sans-serif;
  color: #eeeeee;
  background-color: #222222;
}

Javascript:

let cashInput = document.getElementById("cash");
let price = 3.26;
let purchaseBtn = document.getElementById("purchase-btn");
let changeDue = document.getElementById("change-due");

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 cashReference = [
  ["PENNY", 0.01],
  ["NICKEL", 0.05],
  ["DIME", 0.1],
  ["QUARTER", 0.25],
  ["ONE", 1],
  ["FIVE", 5],
  ["TEN", 10],
  ["TWENTY", 20],
  ["ONE HUNDRED", 100]
];

let changeArray = [
  ["PENNY: ", 0],
  ["NICKEL: ", 0],
  ["DIME: ", 0],
  ["QUARTER: ", 0],
  ["ONE: ", 0],
  ["FIVE: ", 0],
  ["TEN: ", 0],
  ["TWENTY: ", 0],
  ["ONE HUNDRED: ", 0]
]

const calculate = () => {

  // Check if type of bills/coins is 0, if not, take
  // one bill out and put it into total change. If so, check
  // next type down the list. 

  let change = cashInput.value - price;
  change = change.toFixed(2)
  const initialCidTotal = cidTotalFunction();
  changeDue.innerHTML = ""

  // console.log("Change")
  // console.log(determineChangeAvailability())
  // console.log()
  // console.log("Insufficient")
  // console.log(determineInsufficient())
  // console.log()
  // console.log("Status")
  // console.log(determineStatus())

  if (!determineChangeAvailability()) {
    return
  }
  
  if (determineInsufficient()) {
    return
  }

  determineStatus()

  let i = 8

  while (i >= 0) {

    let totalChange = 0
    console.log(cashReference[i][1])
    console.log(cid[i][1])
    console.log(change)
    // console.log(change / cashReference[i][1])
    console.log()

    if (cid[i][1] > 0 &&  
    change > cashReference[i][1] || 
    Number.isInteger(change / cashReference[i][1])) {
      change -= cashReference[i][1]
      change = change.toFixed(2)
      cid[i][1] -= cashReference[i][1];
      totalChange += cashReference[i][1];
      changeArray[i][1] += cashReference[i][1];

      console.log(totalChange)
      console.log(change)
      console.log()

      if (totalChange > change) {
        totalChange -= cashReference[i][1];
        i--
        continue
      }
    } else {
      i--
    }
  }
  updateChangeDueDiv()
}


// Display array on screen

const updateChangeDueDiv = () => {

  for (let i = 8; i >= 0; i--) {
    if (changeArray[i][1] !== 0) {
      changeDue.textContent += `${changeArray[i][0]} \$${changeArray[i][1]} `
    }
  }
}

// Deduces the total of all the cash currently in cid

let cidTotalFunction = () => {

  let calculation = 0;

  for (let i = 0; i <= 8; i++) {
    calculation += cid[i][1];
  };

  let roundedCidTotal = calculation.toFixed(2);
  return roundedCidTotal
}

// Determine if funds are insufficient

const determineInsufficient = () => {

  let change = cashInput.value - price;

  if (cidTotalFunction() < change) {
    changeDue.innerHTML = `<p>Status: INSUFFICIENT_FUNDS</p>`
    return true
  }
  return false
}

// Determines if cash register is open or closed

const determineStatus = () => {
  
  let change = cashInput.value - price;

  if (cidTotalFunction() === change) {
    changeDue.innerHTML = `<p>Status: CLOSED </p>`;
    return true
  } else if (cidTotalFunction() > change) {
    changeDue.innerHTML = `<p>Status: OPEN </p>`;
    return true
  } else {
    console.log("Error!")
    return false
  }
}

// Checking if cash is less than or equal to price to
// give change-due message

const determineChangeAvailability = () => {

  if (cashInput.value < price) {
    alert("Customer does not have enough money to purchase the item")
    return false
  } else if (cashInput.value == price) {
    changeDue.innerHTML = `<p>No change due - customer paid with exact cash</p>`
    return false
  } else {
    return true
  }
}

purchaseBtn.addEventListener("click", calculate);
2 Likes

Hi @Sprigazalea

There is an error message in the console.

Happy coding

2 Likes

How did you get this error? For me, this error doesn’t appear.

Hi @Sprigazalea

At the top right of the screen, press the Console button.
image

Happy coding

When do you get that error message? I use the console regularly and have not seen this error.

Hi @Sprigazalea

You need to add an amount, then run the app.

Try removing the console.log as the error message is the last line in the console.

Happy coding

I commented-out the console.log, but still no error message.

Press the 'Purchase' button multiple times.

First image is after I press the button again, the second image is once I press the purchase button enough for it to give an INSUFFICIENT_FUNDS message. Still no error.

Did you update your code after posting?

Restart the computer, and run the tests again.

Also, test the app on another browser.

Happy coding

I got it to display on Chromium, when I went back to test it on Firefox, I was able to recreate it there as well. Thank you for the help.

1 Like

Hello

Some of the checks were updated while I was working on this project, and suddenly these checks will not pass despite the correct output being given.

Any help would be appreciated.

When the value in the #cash element is less than price, an alert should appear with the text "Customer does not have enough money to purchase the item"
When the value in the #cash element is equal to price, the value in the #change-due element should be "No change due - customer paid with exact cash"

My HTML:

<!DOCTYPE html>
<html lang="en">
  <head>
    <link rel="stylesheet" href="styles.css">    
  </head>
  <body>
    <main>
      <h2>Cash Register</h2>
      <div id="change-due"></div>
      <input id="cash" placeholder="Enter Cash" type="number"></input>
      <button id="purchase-btn">Purchase</button>
      <div id="cid">
        <p id="one-hundred"></p>
        <p id="twenty"></p>
        <p id="ten"></p>
        <p id="five"></p>
        <p id="one"></p>
        <p id="quarter"></p>
        <p id="dime"></p>
        <p id="nickel"></p>
        <p id="penny"></p>  
      </div>   
    </main>
    <script src="script.js"></script>
  </body>
</html>

CSS:

body {
  font-family: sans-serif;
  color: #eeeeee;
  background-color: #222222;
}

Javascript:

let cashInput = document.getElementById("cash");
let price = 3.26;
let purchaseBtn = document.getElementById("purchase-btn");
let changeDue = document.getElementById("change-due");

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 cashReference = [
  ["PENNY", 0.01],
  ["NICKEL", 0.05],
  ["DIME", 0.1],
  ["QUARTER", 0.25],
  ["ONE", 1],
  ["FIVE", 5],
  ["TEN", 10],
  ["TWENTY", 20],
  ["ONE HUNDRED", 100]
];

let changeArray = [
  ["PENNY: ", 0],
  ["NICKEL: ", 0],
  ["DIME: ", 0],
  ["QUARTER: ", 0],
  ["ONE: ", 0],
  ["FIVE: ", 0],
  ["TEN: ", 0],
  ["TWENTY: ", 0],
  ["ONE HUNDRED: ", 0]
]

const calculate = () => {

  // Check if type of bills/coins is 0, if not, take
  // one bill out and put it into total change. If so, check
  // next type down the list. 

  const initialChange = cashInput.value - price;
  let change = cashInput.value - price;
  change = change.toFixed(2)
  const initialCidTotal = cidTotalFunction();
  changeDue.innerHTML = ""

  // console.log("Change")
  // console.log(determineChangeAvailability())
  // console.log()
  // console.log("Insufficient")
  // console.log(determineInsufficient())
  // console.log()
  // console.log("Status")
  // console.log(determineStatus())

  if (!determineChangeAvailability()) {
    return
  }
  
  if (determineInsufficient()) {
    return
  }

  determineStatus()

  let i = 8

  let totalChange = 0

  while (i >= 0) {

    
    console.log(cashReference[i][1])
    console.log(cid[i][1])
    console.log(change)
    console.log(change / cashReference[i][1])
    console.log()

    if (cid[i][1] > 0 &&  
    change > cashReference[i][1] || 
    Number.isInteger(change / cashReference[i][1])) {
      change -= cashReference[i][1]
      change = change.toFixed(2)
      cid[i][1] -= cashReference[i][1];
      totalChange += cashReference[i][1];
      changeArray[i][1] += cashReference[i][1];

      console.log(totalChange)
      console.log(change)
      console.log()

      if (change == 0) {
        break
      }

      if (totalChange > initialChange) {
        change -= cashReference[i][1];
        change = change.toFixed(2)
        cid[i][1] += cashReference[i][1];
        totalChange -= cashReference[i][1];
        changeArray[i][1] -= cashReference[i][1];
        i--
        continue
      }
    } else {
      i--
    }
  }
  updateChangeDueDiv()
  displayCid()
}


// Display array on screen

const updateChangeDueDiv = () => {

  for (let i = 8; i >= 0; i--) {
    if (changeArray[i][1] !== 0) {
      changeDue.textContent += `${changeArray[i][0]} \$${changeArray[i][1]} `
    }
  }
  changeArray = [
    ["PENNY: ", 0],
    ["NICKEL: ", 0],
    ["DIME: ", 0],
    ["QUARTER: ", 0],
    ["ONE: ", 0],
    ["FIVE: ", 0],
    ["TEN: ", 0],
    ["TWENTY: ", 0],
    ["ONE HUNDRED: ", 0]
  ]
}

// Deduces the total of all the cash currently in cid

let cidTotalFunction = () => {

  let calculation = 0;

  for (let i = 0; i <= 8; i++) {
    calculation += cid[i][1];
  };

  let roundedCidTotal = calculation.toFixed(2);
  return roundedCidTotal
}

// Determine if funds are insufficient

const determineInsufficient = () => {

  let change = cashInput.value - price;

  if (cidTotalFunction() < change) {
    changeDue.innerHTML = `<p>Status: INSUFFICIENT_FUNDS</p>`
    return true
  }
  return false
}

// Determines if cash register is open or closed

const determineStatus = () => {
  
  let change = cashInput.value - price;

  if (cidTotalFunction() === change) {
    changeDue.innerHTML = `<p>Status: CLOSED </p>`;
    return true
  } else if (cidTotalFunction() > change) {
    changeDue.innerHTML = `<p>Status: OPEN </p>`;
    return true
  } else {
    console.log("Error!")
    return false
  }
}

// Checking if cash is less than or equal to price to
// give change-due message

const determineChangeAvailability = () => {

  if (cashInput.value < price) {
    alert("Customer does not have enough money to purchase the item")
    return false
  } else if (cashInput.value == price) {
    changeDue.innerHTML = `<p>No change due - customer paid with exact cash</p>`
    return false
  } else {
    return true
  }
}

const displayCid = () => {

  document.getElementById("one-hundred").textContent = `${cid[8][0]}: \$${cid[8][1]}`
  document.getElementById("twenty").textContent = `${cid[7][0]}: \$${cid[7][1]}`
  document.getElementById("ten").textContent = `${cid[6][0]}: \$${cid[6][1]}`
  document.getElementById("five").textContent = `${cid[5][0]}: \$${cid[5][1]}`
  document.getElementById("one").textContent = `${cid[4][0]}: \$${cid[4][1]}`
  document.getElementById("quarter").textContent = `${cid[3][0]}: \$${cid[3][1]}`
  document.getElementById("dime").textContent = `${cid[2][0]}: \$${cid[2][1]}`
  document.getElementById("nickel").textContent = `${cid[1][0]}: \$${cid[1][1]}`
  document.getElementById("penny").textContent = `${cid[0][0]}: \$${cid[0][1]}`

}

displayCid()

purchaseBtn.addEventListener("click", calculate);

This should not be a global variable

The issue above is different from the issue this thread was originally created to address, is a merge still necessary?

As long as it is for the same challenge, please keep it in this thread.

1 Like

Could you explain how that affects the ability for the two checks to pass that I am having issue with? I don’t understand how that is relevant