Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

i passed all the tests, except the last one, although i have the exact right outcome…
i’m really confused as i put a lot of time in this and finally got a working code, but it won’t let me pass…

Is there maybe a bug or some hidden feature the test is looking for?

i’ll post the code below. I know it’s ugly but it works…just the last test…

Your code so far

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

const cash = document.getElementById("cash");
const changeDue = document.getElementById("change-due");
const purchaseBtn = document.getElementById("purchase-btn");
let cashInDrawer = 0;

const checkCash = () => {

  if (cash.value < price) {
  alert("Customer does not have enough money to purchase the item")}
  else if (cash.value.toString() === price.toString()) {
    changeDue.textContent = "No change due - customer paid with exact cash";
    
  }
  else {
    const returnCash = cash.value - price;
    
    cid.forEach((el) => {
     cashInDrawer += el[1]*100
      })
    cashInDrawer = cashInDrawer / 100

    if (cashInDrawer < returnCash) {
      changeDue.textContent = "Status: INSUFFICIENT_FUNDS"
    } 

//berekenen hoeveel je normaal terug krijgt en of dat mogelijk is
  let change = [];
  let leftToReturn = returnCash
  console.log(leftToReturn)
    
    if (leftToReturn >= 100) {    
    const hundreds = Math.floor(leftToReturn/100)
      if (hundreds * 100 <= cid[8][1]) {
      change.push(["ONE HUNDRED", hundreds * 100]);
      leftToReturn = (leftToReturn*100 - hundreds*100*100)/100;
      cid[8][1] -= hundreds * 100 ;
      console.log(leftToReturn)
    }
   }
  
  if (leftToReturn >= 20) {
    const twentys = Math.floor(leftToReturn/20)
    if (twentys * 20 <= cid[7][1]) {
      change.push(["TWENTY", twentys * 20]);
      leftToReturn = (leftToReturn*100 - twentys*20*100)/100;
      cid[7][1] -= twentys * 20;
      console.log(leftToReturn)
      }
    else {
      change.push(["TWENTY", cid[7][1]]);
      leftToReturn = (leftToReturn*100 - cid[7][1]*100)/100;
      cid[7][1] = 0;
      console.log(leftToReturn)
    }

    }

  if (leftToReturn >= 10) {
    const tens = Math.floor(leftToReturn/10);
    if (tens * 10 <= cid[6][1]) {
      change.push(["TEN", tens * 10]);
      leftToReturn = (leftToReturn*100 - tens*10*100)/100;
      cid[6][1] -= tens * 10;
      console.log(leftToReturn)
    }
    else {
      change.push(["TEN", cid[6][1]]);
      leftToReturn = (leftToReturn*100 - cid[6][1]*100)/100;
      cid[6][1] = 0;
      console.log(leftToReturn)      
    }
  }
  if (leftToReturn >= 5) {
    const fives = Math.floor(leftToReturn/5);
    if (fives * 5 <= cid[5][1]) {
      change.push(["FIVE", fives * 5]);
      leftToReturn = (Math.round(leftToReturn*100) - fives*5*100)/100;
      cid[5][1] -= fives * 5;
      console.log(leftToReturn)
    }
  }
  if (leftToReturn >= 1) {
    const ones = Math.floor(leftToReturn/1);
    if (ones <= cid[4][1]) {
      change.push(["ONE", ones]);
      leftToReturn = (Math.round(leftToReturn*100) - ones*100)/100;
      cid[4][1] -= ones;
      console.log(leftToReturn)
    }
  }
  if (leftToReturn >= 0.25) {
    const quarters = Math.floor(leftToReturn/0.25);
    if (quarters <= cid[3][1]) {
      change.push(["QUARTER", quarters * 0.25]);
      leftToReturn = (Math.round(leftToReturn*100) - quarters*0.25*100)/100;
      cid[3][1] -= quarters * 0.25;
      console.log(leftToReturn)
    }
  } 
  if (leftToReturn >= 0.1) {
    const dimes = Math.floor(leftToReturn/0.1);
    if (dimes <= cid[2][1]) {
      change.push(["DIME", dimes * 0.1]);
      leftToReturn = (Math.round(leftToReturn*100) - dimes*0.1*100)/100;
      cid[2][1] -= dimes * 0.1;
      console.log(leftToReturn)
    }
  }
  if (leftToReturn >= 0.05) {
    const nickles = Math.floor(leftToReturn/0.05);
    if (nickles/100 <= cid[1][1]) {
      change.push(["NICKLE", nickles*0.05]);
      leftToReturn = (Math.round(leftToReturn*100) - nickles*0.05*100)/100;
      cid[1][1] -= nickles* 0.05;
      console.log(leftToReturn)
    }
  }   
    if (leftToReturn >= 0.01) {
    const pennys = Math.floor(leftToReturn/0.01);
    if (pennys/100 <= cid[0][1]) {
      change.push(["PENNY", pennys*0.01]);
      leftToReturn = (Math.round(leftToReturn*100) - pennys*0.01*100)/100;
      cid[0][1] -= pennys * 0.01;
      console.log(leftToReturn)
    }
  }
console.log(change);
console.log(cid);


let changestring = ""

for (let [key, value] of Object.entries(change)) {
    
    console.log(key, value);
    changestring += `${value[0]}: $${value[1]} `;
}

console.log(changestring)


if (leftToReturn > 0) {
  changeDue.textContent = "Status: INSUFFICIENT_FUNDS"
}
if (leftToReturn === 0) {
  changeDue.textContent = `Status: OPEN: ${changestring}`
}

let total = 0
change.forEach((el) => {
     total += el[1]
      })


if (cashInDrawer - total === 0) {
  changeDue.textContent = `Status: CLOSED ${changestring}`
}
}



}
purchaseBtn.addEventListener("click", checkCash)




Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36

Challenge Information:

Build a Cash Register Project - Build a Cash Register

I think we need your HTML too.

found a solution myself by changing the scope of a variable after reading about it in this thread

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