Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

0: Array [ “PENNY”, 0.58 ]

1: Array [ “NICKEL”, 2.05 ]

2: Array [ “DIME”, 2.3 ]

3: Array [ “QUARTER”, 0 ]

4: Array [ “ONE”, 0 ]

5: Array [ “FIVE”, 0 ]

6: Array [ “TEN”, 0 ]

7: Array [ “TWENTY”, 0 ]

8: Array [ “ONE HUNDRED”, 0 ]is logged to console
all cid must be 0 when i enter cash higher than it, this is considered as 0, i think problem in modulo in while loop but i tried to fix but nothing happens,please check my code

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; rv:128.0) Gecko/20100101 Firefox/128.0

Challenge Information:

Build a Cash Register Project - Build a Cash Register

Hi there. You have not shared any code with us?

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]
];
cid.forEach(arr => drawer.innerHTML += `<br>${arr[0]}: $${arr[1]}`)
let reversedCid = cid.slice().reverse();
const purchaseBtn = document.getElementById('purchase-btn');
//to log anything to console easier using log();
const log = arg => console.log(arg);
const priceDiv = document.getElementById('price-div');
let changeDue = document.getElementById('change-due');
const fix = arg => parseFloat((arg).toFixed(2).replace('.00','').replace('.10',.1));
priceDiv.textContent = `price : $${price}`
purchaseBtn.addEventListener('click',()=>{
  let cash = document.getElementById('cash').value;
  if(cash < price){
alert("Customer does not have enough money to purchase the item");
  }else if(cash == price){
  changeDue.textContent = "No change due - customer paid with exact cash";    
  }else{ 
   let x = reversedCid;
   let y = cid;
    const amount =[100,20,10,5,1,0.25,0.1,0.05,0.01]; 
   
    let fixedCash = fix(parseFloat(cash -= price));
    let due = [ ["ONE HUNDRED", 0],
                ["TWENTY", 0],
                ["TEN", 0],
                ["FIVE", 0],
                ["ONE", 0],
                ["QUARTER", 0],
                ["DIME", 0],
                ["NICKEL", 0],
                ["PENNY", 0]
    ]; 
amount.forEach((num,index)=>{
while(fixedCash - num >= 0  && (reversedCid[index][1] * 100) % (num * 100) === 0 && reversedCid[index][1] - num >= 0){ 
fixedCash = fix(fixedCash - num);
due[index][1] = fix(due[index][1] + num);
reversedCid[index][1] = fix(reversedCid[index][1] - num);
}
})
if(fixedCash !== 0){
log(cid)
cid = y;
reversedCid = x;
fixedCash = fix(parseFloat(cash -= price));
changeDue.textContent = "Status: INSUFFICIENT_FUNDS";
}else{
  cid = [["PENNY", reversedCid[8][1]],
                ["NICKEL", reversedCid[7][1]],
                ["DIME", reversedCid[6][1]],
                ["QUARTER", reversedCid[5][1]],
                ["ONE", reversedCid[4][1]],
                ["FIVE", reversedCid[3][1]],
                ["TEN", reversedCid[2][1]],
                ["TWENTY", reversedCid[1][1]],
                ["ONE HUNDRED", reversedCid[0][1]
  ]]             
  drawer.innerHTML = '<b>changes in cid:';
  cid.forEach(arr => drawer.innerHTML += `<br>${arr[0]}: $${arr[1]}`)
  due = due.filter(num => num[1] !== 0)
  changeDue.innerHTML = "Status: OPEN";
  due.forEach(arr => changeDue.innerHTML += `<br>${arr[0]}: $${arr[1]}`)  
}
  }
})

Hi mate, glad to see you!
Remember that you can use the preformated text to add your code in a ordered way, it’s easy for us to understand the code!

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

all what i copied here is only script

The problem in while loop, modulo(%), since programming languages not very accurate at math, it returns wrong value

so i need to fix this problem, idk how cid assigned to reversed cid without assigning, however i think all the problem in while loop that nested in forEach loop

If I can suggest: when posting on the forum you should format your code so it is readable. Part of that is making the code properly indented and even adding comment and logs to show us what you have done and why. It takes effort for someone to check your code and if you make it easy for them to read the code and understand what you have done, this will encourage more participation from the community.

1 Like

ok i posted another post please read it