Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

I cannot pass tests 11,12,13,18 and 19 even though I get the output the test is asking for. At the same time, my code passes through other similar tests which are similar to tests 11,12,13,18 and 19. As a result, I am stuck and cannot move forward. Please help. :folded_hands:

Your code so far

let price = 19.5;
let cid = [
  ['PENNY', 1.01],
  ['NICKEL', 2.05],
  ['DIME', 3.1],
  ['QUARTER', 4.25],
  ['ONE', 90],
  ['FIVE', 55],
  ['TEN', 20],
  ['TWENTY',6 0],
  ['ONE HUNDRED', 100],
];

const displayScreen = document.getElementById("display-screen");
displayScreen.innerText = `$${price}`;

const cash = document.getElementById("cash");
const purchaseBtn = document.getElementById("purchase-btn");
const changeDueElement = document.getElementById("change-due");
const pennies = document.getElementById("pennies");
const nickel = document.getElementById("nickel");
const dime = document.getElementById("dime");
const quarter = document.getElementById("quarter");
const one = document.getElementById("one");
const five = document.getElementById("five");
const ten = document.getElementById("ten");
const twenty = document.getElementById("twenty");
const hundred = document.getElementById("hundred");

pennies.innerText = `Pennies: $${cid[0][1]}`;
nickel.innerText = `Nickels: $${cid[1][1]}`;
dime.innerText = `Dimes: $${cid[2][1]}`;
quarter.innerText = `Quarters: $${cid[3][1]}`;
one.innerText = `Ones: $${cid[4][1]}`;
five.innerText = `Fives: $${cid[5][1]}`;
ten.innerText = `Tens: $${cid[6][1]}`;
twenty.innerText = `Twentys: $${cid[7][1]}`;
hundred.innerText = `Hundreds: ${cid[8][1]}`;

let ref = [
    ['ONE HUNDRED', 10000, Math.round(cid[8][1] * 100)],
    ['TWENTY', 2000, Math.round(cid[7][1] * 100)],
    ['TEN', 1000, Math.round(cid[6][1] * 100)],
    ['FIVE', 500, Math.round(cid[5][1] * 100)],
    ['ONE', 100, Math.round(cid[4][1] * 100)],
    ['QUARTER', 25, Math.round(cid[3][1] * 100)],
    ['DIME', 10, Math.round(cid[2][1] * 100)],
    ['NICKEL', 5, Math.round(cid[1][1] * 100)],
    ['PENNY', 1, Math.round(cid[0][1] * 100)],
    
  ];

const changeDueCalculator = (num) => {

  let sum = ref.reduce((accumulator,arr) => accumulator + arr[2], 0);
  

const countChangeUsed = {};
if (num < 0){
  alert("Customer does not have enough money to purchase the item")
  
}
else if (num === 0){
  changeDueElement.innerHTML = `<p>No change due - customer paid with exact cash</p>`;
  changeDueElement.classList.remove("hidden");
}
else if (num > sum){
  changeDueElement.innerHTML = `<p>STATUS: INSUFFICIENT_FUNDS</p>`;
  changeDueElement.classList.remove("hidden");
}

else if (num === sum) {
  
  ref.forEach((array) => {
    while (num >= array[1] && array[2] > 0){

      countChangeUsed[array[0]] = (countChangeUsed[array[0]] || 0) + array[1];

      num = num - array[1];
      array[2] = array[2] - array[1];
    }
  
   });
    
    console.log(ref);
    console.log(countChangeUsed);
    const keys = Object.keys(countChangeUsed);
    const values = Object.values(countChangeUsed);
    console.log(keys);
    console.log(values);

    let output = "Status: CLOSED ";
    for (let i = 0; i < keys.length; i++){
      output += `${keys[i]}: $${values[i]/100} `
    }
    
      changeDueElement.innerHTML = `<p>${output}</p>`;
    
    changeDueElement.classList.remove("hidden");

  pennies.innerText = `Pennies: $${ref[8][2]/100}`;
  nickel.innerText = `Nickels: $${ref[7][2]/100}`;
  dime.innerText = `Dimes: $${ref[6][2]/100}`;
  quarter.innerText = `Quarters: $${ref[5][2]/100}`;
  one.innerText = `Ones: $${ref[4][2]/100}`;
  five.innerText = `Fives: $${ref[3][2]/100}`;
  ten.innerText = `Tens: $${ref[2][2]/100}`;
  twenty.innerText = `Twentys: $${ref[1][2]/100}`;
  hundred.innerText = `Hundreds: $${ref[0][2]/100}`;
  }


else {
  
  ref.forEach((array) => {
    while (num >= array[1] && array[2] > 0){

      countChangeUsed[array[0]] = (countChangeUsed[array[0]] || 0) + array[1];

      num = num - array[1];

      array[2] = array[2] - array[1];
    }

   }); 
    console.log(num);
    num = Math.round(Number(cash.value * 100)) - Math.round(price * 100);
    console.log(num);
    console.log(countChangeUsed);
    const keys = Object.keys(countChangeUsed);
    const values = Object.values(countChangeUsed);
    console.log(keys);
    console.log(values);

    let sumOfObject = 0;

    for(let j = 0; j < values.length; j++){
      sumOfObject = sumOfObject + values[j];
    }
    console.log(sumOfObject);

    if (sumOfObject === num) {

    let output = "Status: OPEN ";
    for (let i = 0; i < keys.length; i++){
      output += `${keys[i]}: $${values[i]/100} `
    }
    
      changeDueElement.innerHTML = `<p>${output}</p>`;
    
    changeDueElement.classList.remove("hidden");

  pennies.innerText = `Pennies: $${ref[8][2]/100}`;
  nickel.innerText = `Nickels: $${ref[7][2]/100}`;
  dime.innerText = `Dimes: $${ref[6][2]/100}`;
  quarter.innerText = `Quarters: $${ref[5][2]/100}`;
  one.innerText = `Ones: $${ref[4][2]/100}`;
  five.innerText = `Fives: $${ref[3][2]/100}`;
  ten.innerText = `Tens: $${ref[2][2]/100}`;
  twenty.innerText = `Twentys: $${ref[1][2]/100}`;
  hundred.innerText = `Hundreds: $${ref[0][2]/100}`;
    }

    else  {

      changeDueElement.innerHTML = `<p>STATUS: INSUFFICIENT_FUNDS</p>`;
    
    changeDueElement.classList.remove("hidden");

    }

  }
  
  return ref;
}


purchaseBtn.addEventListener("click", () => {
  let cashValue = cash.value;
  changeDueCalculator(Math.round(Number(cashValue * 100)) - Math.round(price * 100));
  
})
cash.addEventListener("keydown", e => {
  let cashValue = cash.value;
  if (e.key === "Enter"){
    changeDueCalculator(Math.round(Number(cashValue * 100)) - Math.round(price * 100));
  }
})
<!-- 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/136.0.0.0 Safari/537.36

Challenge Information:

Build a Cash Register Project - Build a Cash Register

please share also your html

I’ve edited your post to improve the readability of the code. When you enter a code block into a forum post, 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 (').

Thank you for improving my post. :folded_hands: Here is my HTML code:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Cash Register</title>
    <link rel = "stylesheet" href = "./styles.css">
    <meta charset = "utf-8">
    <meta name ="viewport" content = "width=device-width, initial-scale = 1.0">
  </head>
  <body>
    <header>
      <h1>Cash Register Project</h1>
    </header>
    <main>
      <div id = "change-due" class = "hidden">
        </div>
      <section id="customer-cash">
        <label for = "input">Enter cash from customer:</label><br>
        <input id = "cash"><br>
        <button id = "purchase-btn">Purchase</button>
      </section>
      <section id ="cash-register">
        <div id = "screen-panel">
          <div id = "display-screen">
            Total: $1.87
          </div>
        </div>
        <div id = "connector-part"></div>
        <div id = "main-frame">
        <div id = "number-pad">
          <div class = "number-key"></div>
          <div class = "number-key"></div>
          <div class = "number-key"></div>
          <div class = "number-key"></div>
          <div class = "number-key"></div>
          <div class = "number-key"></div>
          <div class = "number-key"></div>
          <div class = "number-key"></div>
          <div class = "number-key"></div>
        </div>
        <div id = "change-in-drawer">
          <p id = "cid-line">Change in drawer:</p>
          
            <p id = "pennies">PENNY: $1.01<p>
            <p id = "nickel">NICKEL: $2.05<p>
            <p id = "dime">DIME: $3.1</p>
            <p id = "quarter">QUARTER: $4.25</p>
            <p id = "one">ONE: $90</p>
            <p id = "five">FIVE: $55</p>
            <p id = "ten">TEN: $20</p>
            <p id = "twenty">TWENTY: $60</p>
            <p id = "hundred">HUNDRED: $100</p>
          
        </div>
        <div id = "bottom-part">
          <div id = "the-circle"></div>
        </div>
        </div>
      </section>
    </main>
    <script src = "script.js"></script>
  </body>
</html>

I get a syntax error from this line, do you also get a syntax error there?

There is a space between 6 and zero above, but that is not the case in my code. It must have crept in when I was copying my code and checking my code for different values. Apologies for that.
Moreover, my code produces the desired output, error-free, but, still, I am unable to pass the tests.

add this code below yours in the js file: cash register test suite.js · GitHub

the code does pretty much what the tests do. Your code start not getting the right output after a couple of tests

I am sorry but, I don’t follow. What is adding this block of code below my code going to achieve? Also, why is my code giving wrong results after a few times?

Also. I tried my code for all the tests where the expected result is different from the result my code gets and multiple times for each value. I got the expected result each time. I am not sure what the problem is and I have been at it for 5 days now.

It’s going to run tests similar to the automated FCC tests.

The tests call your function directly. If you have global variables they will not be reset between function calls because they are not in the function.

Thank you for trying to help me out.
I have been running my code over and over again. I get the desired output every time. I wonder if my code would be able to give the correct results if the case were as you mentioned.

I moved my ref variable inside the function and my code passes the test now. But, the thing is that the code is not working as shown in the example code. All values in the “Change in drawer:” are reset every time and start from initial values as opposed to their updated values. For example, if the change due is a quarter, the change due, next time when cash is provided, should be calculated taking quarters to be 4.25 - 0.25 = 4. But, it starts from 4.25 itself.