Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

i keep failing the last to test but when I change my cid and price to match the condition, they pass.

i’ve used let for my variables and I pass all other tests

Your code so far

<!-- file: index.html -->

/* file: script.js */

/* 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/138.0.0.0 Safari/537.36

Challenge Information:

Build a Cash Register Project - Build a Cash Register

Hey there,

Please update the message to include your code. The code was too long to be automatically inserted by the help button.

When you enter a code, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

here is my HTML

<!DOCTYPE html>
<html lang="en">
  <header>
    <meta charset="utf-8" />
    <meta name="viewport" content="width= device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="styles.css" />
    <title>Moogle's Cash Register</title>
  </header>
  <body>
    <h1>Moogle's Cash Register</h1>
    <section id="afterHero">
      <div id="change-due" aria-label="change due"></div>
      <label for="cash" name="cash">Enter cash from customer:</label>
      <input type="number" id="cash" name="cash" />
      <button id="purchase-btn">Purchase</button>
    </section>
    <p class="price">Total: $</p>
    <div id="drawerConnection"></div>
    <section class="drawerBody">
      <div id="drawerButtons">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
      </div>
      <div id="changeWrapper">
        <p>Change in drawer:</p>
        <p>Pennies: $<span></span></p>
        <p>Nickels: $<span></span></p>
        <p>Dimes: $<span></span></p>
        <p>Quarters: $<span></span></p>
        <p>Ones: $<span></span></p>
        <p>Fives: $<span></span></p>
        <p>Tens: $<span></span></p>
        <p>Twenties: $<span></span></p>
        <p>Hundreds: $<span></span></p>
      </div>
    </section>
    <div id="drawerStand"><div></div></div>
    <script src="script.js"></script>
  </body>
</html>

here is my javascript:

let cash = document.getElementById("cash");
let changeDue = document.getElementById("change-due");
const purchaseBtn = document.getElementById("purchase-btn");
const priceTotal = document.querySelector(".price");
const changeWrapper = document.getElementById("changeWrapper");
let price = 1;
priceTotal.innerText += price;
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],
];
let cid2 = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];
let cashOut = {};
let number = 0;

for (let num in cid) {
  number += cid[num][1];
}
number = Math.ceil(number * 100) / 100;
console.log(number);
let index = 0;
function checkInput(cash) {
  cash = parseFloat(cash.value);
  if (cash < price) {
    alert("Customer does not have enough money to purchase the item");
    return
  } else if (cash < 1) {
    cash -= price;
    cash = Math.floor(cash * 100) / 100;
    console.log("cash less than 1: ".cash);
  } else {
    cash -= price;
    cash = Math.round(cash * 100) / 100;
  }

  console.log("cash after being inputed: ", cash);
  return cash;
}
function runRegister(newCash) {
  let enoughCash = false;

  if (newCash === 0) {
    changeDue.innerHTML = "No change due - customer paid with exact cash";
    return;
  } else {
    for (let i = cid.length - 1; i >= 0; i--) {
      if (newCash >= cid2[i] && cid[i][1] > 0) {
        index = i;
        enoughCash = true;
        console.log(i);
        break;
      }
    }
    if (!enoughCash) {
      changeDue.innerHTML = "Status: INSUFFICIENT_FUNDS";
      return null;
    } else if (cid2[index] >= 0) {
      cashOut[cid[index][0]] = (cashOut[cid[index][0]] ?? 0) + cid2[index];
      console.log(cashOut);
    }
    cid[index][1] -= cid2[index];
    cid[index][1] = Math.round(cid[index][1] * 100) / 100;
    newCash -= cid2[index];
    console.log(newCash);
    console.log(Math.round(newCash * 100) / 100);
    return Math.round(newCash * 100) / 100;
  }
}
purchaseBtn.addEventListener("click", () => {
  changeDue.innerHTML = "";
  cashOut = {};

  let newCash = checkInput(cash);
  if (newCash === null) {
    cash.value = "";
  } else if (newCash === 0) {
    changeDue.innerHTML = "No change due - customer paid with exact cash";
    return;
  } else if (newCash > number) {
    changeDue.innerHTML = "Status: INSUFFICIENT_FUNDS";
    console.log(number);
  } else if (newCash === number) {
    changeDue.innerHTML += `<p>Status: CLOSED</p>`;
    cashRegister(newCash);
  } else {
    changeDue.innerHTML += `<p>Status: OPEN</p>`;
    cashRegister(newCash);
  }
  cash.value = "";
});

function cashRegister(newCash) {
  newCash = runRegister(newCash);

  if (newCash === null) {
    return;
  } else if (newCash <= 0) {
    console.log("made it to change due output: ", newCash);
    for (let key in cashOut) {
      cashOut[key] = Math.floor(cashOut[key] * 100) / 100;
      changeDue.innerHTML += `<p>${key}: $${cashOut[key]}</p>`;
    }
    changeWrapper.innerHTML = `<p>Change in drawer:</p>
        <p>Pennies: $<span>${cid[0][1]}</span></p>
        <p>Nickels: $<span>${cid[1][1]}</span></p>
        <p>Dimes: $<span>${cid[2][1]}</span></p>
        <p>Quarters: $<span>${cid[3][1]}</span></p>
        <p>Ones: $<span>${cid[4][1]}</span></p>
        <p>Fives: $<span>${cid[5][1]}</span></p>
        <p>Tens: $<span>${cid[6][1]}</span></p>
        <p>Twenties: $<span>${cid[7][1]}</span></p>
        <p>Hundreds: $<span>${cid[8][1]}</span></p>`;
    return;
  } else {
    cashRegister(newCash);
  }
}

It looks like you have too many global variables. The tests call your function multiple times in a row so none of the global variables will be reset (Except price and cid).

Maybe you can test multiple function calls in a row and see if the result is still correct.

1 Like

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