Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

I have personally tested my code and it is working perfectly but it keeps failing the test and I don’t know what to do anymore.

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/133.0.0.0 Safari/537.36 Edg/133.0.0.0

Challenge Information:

Build a Cash Register Project - Build a Cash Register

Welcome to the forum :wave:

I found the problem, you don’t have any code

3 Likes

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 (').

yeah, how to delete a post cause i can’t figure out how to edit it. i’ll just give up at this point.

Please don’t give up. We will help. We just need to see your code. You should see a pencil icon on your original post. Click that to edit and add your code.

there’s no pencil icon on the post, i’ve tried to search for it. can’t i just write it here on the comments?

Yes you can just write it here in a new message :+1:

Please include both JS and HTML

Yes, of course. Just refer to @ILM 's post on how to do that.

So, you don’t see this in your original post (link icon, pencil icon, ellipsis, and Reply)?
image

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link ref="stylesheet" href="styles.css">
</head>
<body>
  <!--Heading-->
    <h1 id="title">Cash Register Project</h1>

    <!--Change-->
    <div id="change-due">change due</div>

    <!--Input-->
    <section id="input">
      <p>Enter Cash From Customer</p>
      <input type="number" id="cash">
      <button id="purchase-btn">Purchase</button>
    </section>

    <!--Register-->
    <section id="register">
      <div id="total">total</div>
      <div id="cash-in-drawer"></div>
    </section>
    <script src="script.js"></script>
</body>
</html>
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', 60],
  ['ONE HUNDRED', 100]
];


//creating money objects
const money = [
  {
    
    name : "PENNY",
    value : 0.01,
    amount : cid[0][1]
    
  },
  {
    
    name : "NICKEL",
    value : 0.05,
    amount : cid[1][1],
    
  },
  {
    
    name : "DIME",
    value : 0.1,
    amount : cid[2][1],
    
  },
  {
    
    name : "QUARTER",
    value : 0.25,
    amount : cid[3][1],
    
  },
  {
    
    name : "ONE",
    value : 1,
    amount : cid[4][1],
    
  },
  {
    
    name : "FIVE",
    value : 5,
    amount : cid[5][1],
  
  },
  {
    
    name : "TEN",
    value : 10,
    amount : cid[6][1],
    
  },
  {
    
    name : "TWENTY",
    value : 20,
    amount : cid[7][1],
    
  },
  {
    
    name : "ONE HUNDRED",
    value : 100,
    amount : cid[8][1],
    
  }
]


const string = () =>{
  let diff
  let string = "Status: OPEN"
 for (let i=8;i>=0;i--){
   diff = cid[i][1] - money[i].amount
   if (diff > 0){
     string += " "+cid[i][0]+": $"+parseFloat(diff.toFixed(2))
   }
  }
  return string
}
const stringC = () =>{
  let diff
  let string = "Status: CLOSED"
 for (let i=8;i>=0;i--){
   diff = cid[i][1] - money[i].amount
   if (diff > 0){
     string += " "+cid[i][0]+": $"+parseFloat(diff.toFixed(2))
   }
  }
  return string
}


const update = () =>{
  for (let i=0;i<=8;i++){
    cid[i][1] = money[i].amount
  }
}

// declaring html elements
let purchaseBtn = document.getElementById("purchase-btn")
let changeTxt = document.getElementById("change-due")
let input = document.getElementById("cash")
let register = document.getElementById("cash-in-drawer")
let total = document.getElementById("total")

//setting the price
total.textContent = "Total: $"+price


//button click
purchaseBtn.addEventListener('click',()=>{
  let index=-1
  let value=0
  let cash = parseFloat(input.value)
  let changeDue = parseFloat((cash - price).toFixed(2))
  let text = ""

  //checking if change can be added up
  const sum = () => {
    let add=0
    for (let i=8;i>=0;i -= 1){
      if (index < 0){
        if (money[i].value < changeDue){
          index = i
        }
      }
    }

    for (let i=index;i>=0;i--){
      add += money[i].amount
    }
    return add
  }



  //if user inserted an iput value
  if (cash){

    if (cash < price){
      alert("Customer does not have enough money to purchase the item")
    }else if(cash === price){
      changeTxt.textContent = "No change due - customer paid with exact cash"
    }else{
      
      update()
      if(sum() >= changeDue){
        if (sum() === changeDue){
          while (value < changeDue){
            for (let i=index;i>=0;i--){
              let avail = 0
              let difference = parseFloat((changeDue - value).toFixed(2))
              while (money[i].value <= difference && difference > 0 && money[i].amount > 0){
                
                money[i].amount -= money[i].value
                value += money[i].value
                avail += money[i].value
                difference = parseFloat((changeDue - value).toFixed(2))
              
              

              }
              if (avail > 0){
                changeTxt.textContent += "\n"+money[i].name+": $"+avail
                
              }
              if (money[0].amount === 0){
                value = changeDue + 1
              }
            }
          }
          changeTxt.textContent = stringC()
        }else{


          changeTxt.textContent = "Status: OPEN "
          while (value < changeDue){
            for (let i=index;i>=0;i--){
              let avail = 0
              let difference = parseFloat((changeDue - value).toFixed(2))
              while (money[i].value <= difference && difference > 0 && money[i].amount > 0){
                
                money[i].amount -= money[i].value
                value += money[i].value
                avail += money[i].value
                difference = parseFloat((changeDue - value).toFixed(2))
              
              

              }
              if (avail > 0){
                changeTxt.textContent += "\n"+money[i].name+": $"+avail
                
              }
              if (money[0].amount === 0){
                value = changeDue + 1
              }
            }
          }
          
          if (parseFloat((value).toFixed(2)) === parseFloat((changeDue).toFixed(2))){
          changeTxt.textContent = string()


        }else{
          changeTxt.textContent = "Status: INSUFFICIENT_FUNDS"
        }
        }
        
      }else{
        changeTxt.textContent = "Status: INSUFFICIENT_FUNDS"
      }
    }

  //if user did not insert an input value
  }else{
    alert("Please insert a cash amount")
  }
})

i apologise for the messy code, it’s cause i have been trying to make it pass the test so i messed it up in the process

I’ve edited your code for readability. 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. What tests are you failing right now?

What editor are you using? I recommend VS Code. Currently, you have some curly double quotes and curly single quotes rather than straight double quotes and straight single quotes. And in a couple of your for loops it looks like you have an en-dash character rather than two hyphens where you’re decrementing. Please get that cleaned up first, then we can move on to any other issues.

This can happen when pasting into the forum without proper formatting.

Since I reformatted it they should be ok, did you copy it before I formatted it maybe?

Thanks. I didn’t know that. I did copy before your formatted it!

1 Like

so it’s alternating depending on the cid values, so basically it fails from test 11 upwards. the tests that have the same cid as my code pass the other ones fail basically

Ah, then it must be your global variables. The tests will change the value of your cid and price globals. Other globals dependent on cid will not get updated, so the tests will fail you. Try moving all globals other than cid and price into your event listener and see how that works for you.

thank you so much. i just did that and it passed the test . i finally got my certification

1 Like