Build a Cash Register Help

Hi, not sure if I’m making this in the right place. I’m working on the cash register, and when I manually input the values from all the tests, my outputs match theirs. However, when I run the tests, two of the tests fail. I’m not sure what’s going on, so any help would be appreciated. I put in the screenshot of the first test that fails, but it wouldn’t let me put in the second one.

<DOCTYPE html>
  <html lang="en">
    <head>
      <meta charset="UTF-8"/>
      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      <title>Numeral Converter></title>
      
      </head>
      <body>
       
        <main>
     <h1>Cash Register</h1>
     <h2>Price: $1.87</h2>     <input id="cash">Enter your cash here</input>
          <button id="purchase-btn">Purchase</button>
          
          <div id="change-due"></div>
          <p id="cash-in-drawer">Cash in Drawer:</p>
          
          </main>
          <script src="script.js"></script>
          </body>
          </html>
let price = 3.26;
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 penny=0;
let nickel=0;
let dime=0;
let quarter=0;
let one=0;
let five=0;
let ten=0;
let twenty=0;
let oneHun=0;
const cash=document.getElementById("cash");
const btn=document.getElementById("purchase-btn");
const output=document.getElementById("change-due");

const drawer=document.getElementById("cash-in-drawer");

updateDrawer();
btn.addEventListener("click",()=>{
  let input=parseFloat(cash.value);
if(input<price){
  alert("Customer does not have enough money to purchase the item");
} else if(input===price){
return output.innerText="No change due - customer paid with exact cash";
} else{
  let change=input-price;
  
 
  if(checkTotal(cid)===change){
    count(change);
    output.innerText=`Status: CLOSED ${finalChange(penny, nickel, dime, quarter, one, five, ten, twenty, oneHun )}`;
  }else if(checkTotal(cid)<change||isChange(change,cid)!==0){
    output.innerText=`Status: INSUFFICIENT_FUNDS`;
  
  }else if(isChange(change,cid)===0){
  
count(change);
  output.innerText=`Status: OPEN ${finalChange(penny, nickel, dime, quarter, one, five, ten, twenty, oneHun )}`
}
}
});
function updateDrawer(change){
  console.log(cid[0][1])
  drawer.innerText=`
  Change in Drawer:
  Pennies: $${cid[0][1]}
  Nickles: $${cid[1][1]}
  Dimes: $${cid[2][1]}
  Quarters: $${cid[3][1]}
  Dollars: $${cid[4][1]}
  Five Dollars: $${cid[5][1]}
  Ten Dollars: $${cid[6][1]}
  Twenty Dollars: $${cid[7][1]}
  One Hundred Dollars: $${cid[8][1]}`
}

const checkTotal=(cid)=>{
  let sum=0;
for (let i = 0; i < cid.length; i++) {
   sum+=cid[i][1];
   
}

return sum;
}
const isChange=(change, cid)=>{
  let adaptableChange=change;
  let possible=true;
  let og=change;
const valueMeanings={
"PENNY":0.01,
"NICKEL":0.05,
"DIME":0.1,
"QUARTER":0.25,
"ONE":1,
"FIVE":5,
"TEN": 10,
"TWENTY":20,
"ONE HUNDRED":100
};
for(let i=0;i<cid.length;i++){
  let typeValue=valueMeanings[cid[i][0]];
  let availableValue=cid[i][1];
  let maxChange = Math.floor(availableValue / typeValue)
  let usage=Math.min(adaptableChange,maxChange);
  
  adaptableChange-=usage*typeValue;
        
}
if(adaptableChange>0){
  return og
} else{
  return adaptableChange;
}
}

function finalChange(penny, nickel, dime, quarter, one, five, ten, twenty, oneHun ){
let caps="";
if(oneHun!==0){
  caps+=`ONE HUNDRED: $${oneHun}`;
}
if(twenty!==0){
  caps+=`\nTWENTY: $${twenty}`;
}
if(ten!==0){
  caps+=`\nTEN: $${ten}`;
}
if(five!==0){
  caps+=`\nFIVE: $${five}`;
}
if(one!==0){
  caps+=`\nONE: $${one}`;
}
if(quarter!==0){
  caps+=`\nQUARTER: $${quarter}`;
}
if(dime!==0){
  caps+=`\nDIME: $${dime}`;
}
if(nickel!==0){
  caps+=`\nNICKEL: $${nickel}`;
}

if(penny!==0){
  caps+=`\nPENNY: $${penny}`;
}
return caps;
}
function count(change){
  while(change>=100&&cid[8][1]>=100){
    change-=100;
    cid[8][1]-=100;
oneHun+=100;
    updateDrawer();
  }
  while(change>=20&&cid[7][1]>=20){
    change-=20;
    cid[7][1]-=20;
    twenty+=20;
    updateDrawer();
  }
  while(change>=10&&cid[6][1]>=10){
    change-=10;
    cid[6][1]-=10;
    ten+=10;
    updateDrawer();
  }
  while(change>=5&&cid[5][1]>=5){
    change-=5;
    cid[5][1]-=5;
    five+=5;
    updateDrawer();
  }
  while(change>=1&&cid[4][1]>=1){
    change-=1;
    cid[4][1]-=1;
    one+=1;
    updateDrawer();
  }
  while(change>=0.25&&cid[3][1]>=0.25){
    change-=0.25;
    cid[3][1]-=0.25;
    quarter+=0.25;
    updateDrawer();
  }
  while(change>=.100&&cid[2][1]>=.1){
    change-=.100;
    cid[2][1]-=.100;
    dime+=.1;
    updateDrawer();
  }
  while(change>=.05&&cid[1][1]>=.05){
    change-=.05;
    cid[1][1]-=.05;
    nickel+=.05;
    updateDrawer();
  }
  
  while(change>=0.01&&cid[0][1]>=0.01){
     change = Math.round((change - 0.01) * 100) / 100;
  cid[0][1] = Math.round((cid[0][1] - 0.01) * 100) / 100;
    
    penny=Math.round((penny+0.01)*100)/100;
    
    updateDrawer();
    console.log(change);
  console.log(cid[0][1]);
  }
}

Welcome to the forum!

Please put your code between 2 lines of 3 backticks (`) like this:

```
# your code here
```

or use the Preformatted Text button ( the one with </> symbol):

to make your code preformatted.

And also include the link to the challenge for better assistance.

sorry just went ahead and did it!

Changing the values with which price and cid are declared is not exactly reproducing the state during test. More accurate is reassigning cid and price at the end of code (while keeping initial declaration).

Code should be able to still get the expected result in such case.

copying this code and just placing it at the bottom of the rest of my code? that did not work for me. or am I not understanding correctly

price = 3.26;
 cid = [
  ["PENNY", 1.01],
  ["NICKEL", 2.05],
  ["DIME", 3.1],
  ["QUARTER", 4.25],
  ["ONE", 90],
  ["FIVE", 55],
  ["TEN", 20],
  ["TWENTY", 60],
  ["ONE HUNDRED",100]
];

Okay I was able to fix the first test that was wrong by resetting the money values inside of the event listener. I’m now having a problem with this test:
Screenshot 2024-06-10 235149
My output is showing as Status: OPEN
PENNY: $0.01
Which is just wrong. This is the only test that’s failing and I thought it was working earlier, so I’m a bit confused.

This is answer to the previous version of your post.

That was too much moved to the end of code, reassign just cid and price. Tests will not reassign these other values or run updateDrawer() automatically.

What you found now is the problem that’s harder to show. What’s happening now is the following:

  • Values are reassigned at the end of code. Drawer is updated.
  • 20 is typed to the box and button is pressed, result is displayed. Result appear to be correct.
  • 100 is typed to the box and button is pressed, result is displayed. Result appear to not be correct.

Question is now - at what point between two presses of the button, values are reset?


Please share updated code.

Hi sorry, I saw that you were typing as I fixed the first problem and went to change my response to you. Here’s the updated code:

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]
];

let penny=0;
let nickel=0;
let dime=0;
let quarter=0;
let one=0;
let five=0;
let ten=0;
let twenty=0;
let oneHun=0;

const cash=document.getElementById("cash");
const btn=document.getElementById("purchase-btn");
const output=document.getElementById("change-due");

const drawer=document.getElementById("cash-in-drawer");

updateDrawer();
btn.addEventListener("click",()=>{
  
  let input=parseFloat(cash.value);
if(input<price){
  alert("Customer does not have enough money to purchase the item");
} else if(input===price){
return output.innerText="No change due - customer paid with exact cash";
} else{
  let change=input-price;
  
 
  if(checkTotal(cid)===change){
    count(change);
    output.innerText=`Status: CLOSED ${finalChange(penny, nickel, dime, quarter, one, five, ten, twenty, oneHun )}`;
  }else if(checkTotal(cid)<change||isChange(change,cid)!==0){
    output.innerText=`Status: INSUFFICIENT_FUNDS`;
  
  }else if(isChange(change,cid)===0){
  console.log(isChange(change,cid)+"k");
count(change);
  output.innerText=`Status: OPEN ${finalChange(penny, nickel, dime, quarter, one, five, ten, twenty, oneHun )}`
}
}
penny=0;
 nickel=0;
 dime=0;
 quarter=0;
 one=0;
 five=0;
 ten=0;
 twenty=0;
 oneHun=0;
});
function updateDrawer(change){
  console.log(cid[0][1])
  drawer.innerText=`
  Change in Drawer:
  Pennies: $${cid[0][1]}
  Nickles: $${cid[1][1]}
  Dimes: $${cid[2][1]}
  Quarters: $${cid[3][1]}
  Dollars: $${cid[4][1]}
  Five Dollars: $${cid[5][1]}
  Ten Dollars: $${cid[6][1]}
  Twenty Dollars: $${cid[7][1]}
  One Hundred Dollars: $${cid[8][1]}`
}

const checkTotal=(cid)=>{
  let sum=0;
for (let i = 0; i < cid.length; i++) {
   sum+=cid[i][1];
   
}

return sum;
}
const isChange=(change, cid)=>{
  let adaptableChange=change;
  let possible=true;
  let og=change;
const valueMeanings={
"PENNY":0.01,
"NICKEL":0.05,
"DIME":0.1,
"QUARTER":0.25,
"ONE":1,
"FIVE":5,
"TEN": 10,
"TWENTY":20,
"ONE HUNDRED":100
};
for(let i=0;i<cid.length;i++){
  let typeValue=valueMeanings[cid[i][0]];
  let availableValue=cid[i][1];
  let maxChange = Math.floor(availableValue / typeValue)
  let usage=Math.min(adaptableChange,maxChange);
  
  adaptableChange-=usage*typeValue;
        
}
if(adaptableChange>0){
  return og
} else{
  return adaptableChange;
}
}

function finalChange(penny, nickel, dime, quarter, one, five, ten, twenty, oneHun ){
let caps="";
if(oneHun!==0){
  caps+=`ONE HUNDRED: $${oneHun}`;
}
if(twenty!==0){
  caps+=`\nTWENTY: $${twenty}`;
}
if(ten!==0){
  caps+=`\nTEN: $${ten}`;
}
if(five!==0){
  caps+=`\nFIVE: $${five}`;
}
if(one!==0){
  caps+=`\nONE: $${one}`;
}
if(quarter!==0){
  caps+=`\nQUARTER: $${quarter}`;
}
if(dime!==0){
  caps+=`\nDIME: $${dime}`;
}
if(nickel!==0){
  caps+=`\nNICKEL: $${nickel}`;
}

if(penny!==0){
  caps+=`\nPENNY: $${penny}`;
}
return caps;
}
function count(change){
  while(change>=100&&cid[8][1]>=100){
    change-=100;
    cid[8][1]-=100;
oneHun+=100;
    updateDrawer();
  }
  while(change>=20&&cid[7][1]>=20){
    change-=20;
    cid[7][1]-=20;
    twenty+=20;
    updateDrawer();
  }
  while(change>=10&&cid[6][1]>=10){
    change-=10;
    cid[6][1]-=10;
    ten+=10;
    updateDrawer();
  }
  while(change>=5&&cid[5][1]>=5){
    change-=5;
    cid[5][1]-=5;
    five+=5;
    updateDrawer();
  }
  while(change>=1&&cid[4][1]>=1){
    change-=1;
    cid[4][1]-=1;
    one+=1;
    updateDrawer();
  }
  while(change>=0.25&&cid[3][1]>=0.25){
    change-=0.25;
    cid[3][1]-=0.25;
    quarter+=0.25;
    updateDrawer();
  }
  while(change>=.100&&cid[2][1]>=.1){
    change-=.100;
    cid[2][1]-=.100;
    dime+=.1;
    updateDrawer();
  }
  while(change>=.05&&cid[1][1]>=.05){
    change-=.05;
    cid[1][1]-=.05;
    nickel+=.05;
    updateDrawer();
  }
  
  while(change>=0.01&&cid[0][1]>=0.01){
     change = Math.round((change - 0.01) * 100) / 100;
  cid[0][1] = Math.round((cid[0][1] - 0.01) * 100) / 100;
    
    penny=Math.round((penny+0.01)*100)/100;
    
    updateDrawer();
    
  }
}

 price = 3.26;
 cid = [
  ["PENNY", 1.01],
  ["NICKEL", 2.05],
  ["DIME", 3.1],
  ["QUARTER", 4.25],
  ["ONE", 90],
  ["FIVE", 55],
  ["TEN", 20],
  ["TWENTY", 60],
  ["ONE HUNDRED",100]
];
updateDrawer();
 price = 19.5;
 cid = [
  ["PENNY", 0.01],
  ["NICKEL", 0],
  ["DIME", 0],
  ["QUARTER", 0],
  ["ONE", 1],
  ["FIVE", 0],
  ["TEN", 0],
  ["TWENTY", 0],
  ["ONE HUNDRED",0]
];
updateDrawer();

I think that the problem is that isChange(change,cid) is returning 0 when it should be returning 0.5, but I’m not sure where within the function lies the issue, unless you think the issue is that the values still aren’t resetting properly.

Hmm, isChange is certainly the suspect. Since usage is multiplied by denomination value, shouldn’t it always be integer?

Keep in mind this could mean there are still some imprecisions that might be revealing themselves while dealing with isChange.

Are you able to give me a more specific suggestion on how to fix it? I spent a while trying to work on it, but I haven’t been able to get all of the tests to run. Either the sixth and seventh tests don’t run or the ninth test doesn’t run, but nothing I do has been able to fix all of them.

Well, not exactly suggestion how to fix isChange. Take a look at the count and isChange functions. There are some parts that are overlapping. While calculating denominations, count is doing basically the same as isChange plus some more.

My hint would be to try to leave just count function. Changes that would be needed in it, appear to be very minor, compared to making sure isChange is doing as good job.

Okay I took out isChange and am just using count. When I run the sixth test, the price is 19.5 and I input 20. The change should be 0.5 in quarters, but when I run it, it’s 1. I was able to figure out using console logs that the quarter while loop is running four times instead of two, but I’m not sure why because both conditions aren’t met for it to run.

/*let price = 3.26;
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 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]
];
/*
let price = 19.5;
 let cid = [
  ["PENNY", 0.01],
  ["NICKEL", 0],
  ["DIME", 0],
  ["QUARTER", 0],
  ["ONE", 1],
  ["FIVE", 0],
  ["TEN", 0],
  ["TWENTY", 0],
  ["ONE HUNDRED",0]
];
*/
let penny=0;
let nickel=0;
let dime=0;
let quarter=0;
let one=0;
let five=0;
let ten=0;
let twenty=0;
let oneHun=0;

const cash=document.getElementById("cash");
const btn=document.getElementById("purchase-btn");
const output=document.getElementById("change-due");

const drawer=document.getElementById("cash-in-drawer");

updateDrawer();
btn.addEventListener("click",()=>{
  
  let input=parseFloat(cash.value);
if(input<price){
  alert("Customer does not have enough money to purchase the item");
} else if(input===price){
return output.innerText="No change due - customer paid with exact cash";
} else{
  let change=input-price;
  
 
  if(checkTotal(cid)===change){
    count(change);
    output.innerText=`Status: CLOSED ${finalChange(penny, nickel, dime, quarter, one, five, ten, twenty, oneHun )}`;
    
  }else if(checkTotal(cid)<change||count(change)!==0){
   
    output.innerText=`Status: INSUFFICIENT_FUNDS`;
  
  }else if(count(change)===0){
  

  output.innerText=`Status: OPEN ${finalChange(penny, nickel, dime, quarter, one, five, ten, twenty, oneHun )}`
}
}
penny=0;
 nickel=0;
 dime=0;
 quarter=0;
 one=0;
 five=0;
 ten=0;
 twenty=0;
 oneHun=0;
});
function updateDrawer(){
  console.log(cid[0][1])
  drawer.innerText=`
  Change in Drawer:
  Pennies: $${cid[0][1]}
  Nickles: $${cid[1][1]}
  Dimes: $${cid[2][1]}
  Quarters: $${cid[3][1]}
  Dollars: $${cid[4][1]}
  Five Dollars: $${cid[5][1]}
  Ten Dollars: $${cid[6][1]}
  Twenty Dollars: $${cid[7][1]}
  One Hundred Dollars: $${cid[8][1]}`
}

const checkTotal=(cid)=>{
  let sum=0;
for (let i = 0; i < cid.length; i++) {
   sum+=cid[i][1];
   
}

return sum;
}


function finalChange(penny, nickel, dime, quarter, one, five, ten, twenty, oneHun ){
let caps="";
if(oneHun!==0){
  caps+=`ONE HUNDRED: $${oneHun}`;
}
if(twenty!==0){
  caps+=`\nTWENTY: $${twenty}`;
}
if(ten!==0){
  caps+=`\nTEN: $${ten}`;
}
if(five!==0){
  caps+=`\nFIVE: $${five}`;
}
if(one!==0){
  caps+=`\nONE: $${one}`;
}
if(quarter!==0){
  caps+=`\nQUARTER: $${quarter}`;
}
if(dime!==0){
  caps+=`\nDIME: $${dime}`;
}
if(nickel!==0){
  caps+=`\nNICKEL: $${nickel}`;
}

if(penny!==0){
  caps+=`\nPENNY: $${penny}`;
}
return caps;
}
function count(change){
  while(change>=100&&cid[8][1]>=100){
    change-=100;
    cid[8][1]-=100;
oneHun+=100;
    updateDrawer();
  }
  while(change>=20&&cid[7][1]>=20){
    change-=20;
    cid[7][1]-=20;
    twenty+=20;
    updateDrawer();
    
  }
  while(change>=10&&cid[6][1]>=10){
    change-=10;
    cid[6][1]-=10;
    ten+=10;
    updateDrawer();
    console.log(cid[6][1]);
    console.log(change+"k");
    console.log(ten);
  }
  while(change>=5&&cid[5][1]>=5){
    change-=5;
    cid[5][1]-=5;
    five+=5;
    updateDrawer();
  }
  while(change>=1&&cid[4][1]>=1){
    change-=1;
    cid[4][1]-=1;
    one+=1;
    updateDrawer();
  }
  while(change>=0.25&&cid[3][1]>=0.25){
     console.log(cid[3][1]);
    console.log(change+"k");
    change-=0.25;
    cid[3][1]-=0.25;
    quarter+=0.25;
    change = Math.round(change * 100) / 100;
  quarter = Math.round(quarter * 100) / 100;
   console.log(change+"l");
    updateDrawer();
    
    
  }
  while(change>=.100&&cid[2][1]>=.1){
    change-=.100;
    cid[2][1]-=.100;
    dime+=.1;
    updateDrawer();
  }
  while(change>=.05&&cid[1][1]>=.05){
    change-=.05;
    cid[1][1]-=.05;
    nickel+=.05;
    updateDrawer();
  }
  
  while(change>=0.01&&cid[0][1]>=0.01){
     change = Math.round((change - 0.01) * 100) / 100;
   
  cid[0][1] = Math.round((cid[0][1] - 0.01) * 100) / 100;
    
    penny=Math.round((penny+0.01)*100)/100;
    
    updateDrawer();
    
  }
  return change;
}
/*
 price = 3.26;
 cid = [
  ["PENNY", 1.01],
  ["NICKEL", 2.05],
  ["DIME", 3.1],
  ["QUARTER", 4.25],
  ["ONE", 90],
  ["FIVE", 55],
  ["TEN", 20],
  ["TWENTY", 60],
  ["ONE HUNDRED",100]
];
updateDrawer();
 price = 19.5;
 cid = [
  ["PENNY", 0.01],
  ["NICKEL", 0],
  ["DIME", 0],
  ["QUARTER", 0],
  ["ONE", 1],
  ["FIVE", 0],
  ["TEN", 0],
  ["TWENTY", 0],
  ["ONE HUNDRED",0]
];
updateDrawer();*/

Okay I’m stuck again with the same problem as before: when I input 20 and the price is 19.5, it says OPEN with 0.1 instead of insufficient funds.

/*let price = 3.26;
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 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]
];*/

let price = 19.5;
 let cid = [
  ["PENNY", 0.01],
  ["NICKEL", 0],
  ["DIME", 0],
  ["QUARTER", 0],
  ["ONE", 1],
  ["FIVE", 0],
  ["TEN", 0],
  ["TWENTY", 0],
  ["ONE HUNDRED",0]
];

let penny=0;
let nickel=0;
let dime=0;
let quarter=0;
let one=0;
let five=0;
let ten=0;
let twenty=0;
let oneHun=0;

const cash=document.getElementById("cash");
const btn=document.getElementById("purchase-btn");
const output=document.getElementById("change-due");

const drawer=document.getElementById("cash-in-drawer");

updateDrawer();
btn.addEventListener("click",()=>{
  
  let input=parseFloat(cash.value);
if(input<price){
  alert("Customer does not have enough money to purchase the item");
} else if(input===price){
return output.innerText="No change due - customer paid with exact cash";
} else{
  let change=input-price;
  
 
  if(checkTotal(cid)>change){
    count(change);
    output.innerText=`Status: OPEN ${finalChange(penny, nickel, dime, quarter, one, five, ten, twenty, oneHun )}`;
    
  }else if(checkTotal(cid)===change){
    count(change);
    output.innerText=`Status: CLOSED ${finalChange(penny, nickel, dime, quarter, one, five, ten, twenty, oneHun )}`;
  }
  else if(checkTotal(cid)<change||count(change)!==0){
   
    output.innerText=`Status: INSUFFICIENT_FUNDS`;
  
}
}
penny=0;
 nickel=0;
 dime=0;
 quarter=0;
 one=0;
 five=0;
 ten=0;
 twenty=0;
 oneHun=0;
});
function updateDrawer(){
  console.log(cid[0][1])
  drawer.innerText=`
  Change in Drawer:
  Pennies: $${cid[0][1]}
  Nickles: $${cid[1][1]}
  Dimes: $${cid[2][1]}
  Quarters: $${cid[3][1]}
  Dollars: $${cid[4][1]}
  Five Dollars: $${cid[5][1]}
  Ten Dollars: $${cid[6][1]}
  Twenty Dollars: $${cid[7][1]}
  One Hundred Dollars: $${cid[8][1]}`
}

const checkTotal=(cid)=>{
  let sum=0;
for (let i = 0; i < cid.length; i++) {
   sum+=cid[i][1];
   
}

return sum;
}


function finalChange(penny, nickel, dime, quarter, one, five, ten, twenty, oneHun ){
let caps="";
if(oneHun!==0){
  caps+=`ONE HUNDRED: $${oneHun}`;
}
if(twenty!==0){
  caps+=`\nTWENTY: $${twenty}`;
}
if(ten!==0){
  caps+=`\nTEN: $${ten}`;
}
if(five!==0){
  caps+=`\nFIVE: $${five}`;
}
if(one!==0){
  caps+=`\nONE: $${one}`;
}
if(quarter!==0){
  caps+=`\nQUARTER: $${quarter}`;
}
if(dime!==0){
  caps+=`\nDIME: $${dime}`;
}
if(nickel!==0){
  caps+=`\nNICKEL: $${nickel}`;
}

if(penny!==0){
  caps+=`\nPENNY: $${penny}`;
}
return caps;
}
function count(change){
  while(change>=100&&cid[8][1]>=100){
    change-=100;
    cid[8][1]-=100;
oneHun+=100;
    updateDrawer();
  }
  while(change>=20&&cid[7][1]>=20){
    console.log(change+"twent");
    change-=20;
    
    cid[7][1]-=20;
    twenty+=20;
    updateDrawer();
    
  }
  while(change>=10&&cid[6][1]>=10){
    console.log(change+"ten");
    change-=10;
    cid[6][1]-=10;
    ten+=10;
    updateDrawer();
    console.log(cid[6][1]);
    console.log(change+"k");
    console.log(ten);
  }
  while(change>=5&&cid[5][1]>=5){
    console.log("Change before FIVE loop:", change); // Log the value of change before entering the FIVE loop
    console.log("cid[5][1] before FIVE loop:", cid[5][1]); // Log the value of cid[5][1] before entering the FIVE loop
    change-=5;
    cid[5][1]-=5;
    five+=5;
    updateDrawer();
    console.log("Change after FIVE loop:", change); // Log the value of change after processing FIVE
    console.log("cid[5][1] after FIVE loop:", cid[5][1]); // Log the value of cid[5][1] 
  }

  while(change>=1&&cid[4][1]>=1){
    change-=1;
    cid[4][1]-=1;
    one+=1;
    updateDrawer();
  }
  while(change>=0.25&&cid[3][1]>=0.25){
     
      change-=0.25;
    cid[3][1]-=0.25;
    quarter+=0.25;
   
   updateDrawer();
  
    
  }
  while(change>=.100&&cid[2][1]>=.1){
    change-=.100;
    cid[2][1]-=.100;
    dime+=.1;
    updateDrawer();
  }
  while(change>=.05&&cid[1][1]>=.05){
    change-=.05;
    cid[1][1]-=.05;
    nickel+=.05;
    updateDrawer();
  }
  
  while(change>=0.01&&cid[0][1]>=0.01){
     change = Math.round((change - 0.01) * 100) / 100;
   
  cid[0][1] = Math.round((cid[0][1] - 0.01) * 100) / 100;
    
    penny=Math.round((penny+0.01)*100)/100;
    
    updateDrawer();
    
  }
  return change;
}
/*
 price = 3.26;
 cid = [
  ["PENNY", 1.01],
  ["NICKEL", 2.05],
  ["DIME", 3.1],
  ["QUARTER", 4.25],
  ["ONE", 90],
  ["FIVE", 55],
  ["TEN", 20],
  ["TWENTY", 60],
  ["ONE HUNDRED",100]
];
updateDrawer();
 price = 19.5;
 cid = [
  ["PENNY", 0.01],
  ["NICKEL", 0],
  ["DIME", 0],
  ["QUARTER", 0],
  ["ONE", 1],
  ["FIVE", 0],
  ["TEN", 0],
  ["TWENTY", 0],
  ["ONE HUNDRED",0]
];
updateDrawer();*/

To be honest I can’t believe I’ve spent this much time and effort and changed the code so much to be stuck with the same problem!

I would use absolutely zero global variables like this.

If I were to change that, could I just put that in a function and then declare the function? I don’t think that’s the issue though, the issue is that if the price is 19.5 and I give 20 but there’s a dollar and .01 cents in the register, the output is OPEN: PENNY: 0.01 instead of insufficient funds. How would changing where the variables are declared fix that?

I’d first tidy up you global space pollution so it’s easier to trace what’s happening.

I’d also look for ways to simplify your logic.

And I’d swap to using cents instead of dollars inside of your functions. Your code is vulnerable to floating point rounding errors.

Declaring them within a function and then just calling the function in that spot where they were before caused another test to fail that hadn’t before. What do you mean by global space pollution?

Also, could you be more specific as to which functions should use cents instead of dollars? And if I were to convert to cents, I would have to convert back to dollars, so I don’t understand how that would fix issues with floating point rounding errors.

Do you think that the floating point error is causing the test to fail? I thought it was more of a logic issue, that the function is calling count(change) when it should just be outputting insufficient funds.

Variables should live in the absolutely smallest scope they need to. And very little should generally need to live in a global space. Functions have inputs and outputs so you don’t have to hide data outside of the function scope. It’s difficult to trace what each function does when it touches a bunch of values outside of its scope.

Wrt cents, I would think about how you could possibly do your calculations on integer numbers of cents instead of decimal numbers of dollars, so 100 cents instead of 1 dollar. Floating point rounding definitely can give you the wrong results.

Hi, I ended up looking at a video on youtube that helped to explain some of the topics for this project. My code passes now, but it looks completely different than before and doesn’t have the updateDrawer() function as it seemed to just break it. Thanks for your tips!

1 Like

I wouldn’t submit code you copied from a YouTube video. That violates the honestly policy.

1 Like