Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

Hello

I have tested the code against all given scenarios by changing the cid, price, and cash amount, and it passes. However, when I run the tests, I only pass the tests that match the current cid array.

Is there something I’m missing? Any help would be greatly appreciated.

Thank you

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", 60],
  ["ONE HUNDRED", 100]
];
console.log(cid)
let cashInDrawer = cid.reduce((total, [currency, amount]) => total + amount, 0);
console.log(`Total cash in drawer: $${cashInDrawer.toFixed(2)}`);


let changeArr = [
  ["PENNY"],
  ["NICKEL"],
  ["DIME"],
  ["QUARTER"],
  ["ONE"],
  ["FIVE"],
  ["TEN"],
  ["TWENTY"],
  ["ONE HUNDRED"]
];

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

const purchase = () => {
  let cashAmount = parseFloat(cash.value)
  let change;
  change = calcChange(cashAmount);
  console.log("Change PENNYS $" + change)
  if (cashAmount < price) {
    alert("Customer does not have enough money to purchase the item")
  } else if (cashAmount === price) {
    changeDue.innerHTML = "No change due - customer paid with exact cash"
  } else if (cashInDrawer < change / 100) {
    changeDue.innerHTML = "Status: INSUFFICIENT_FUNDS"
  } else if (cashInDrawer === change / 100) {
    changeArr.forEach(arr => arr.length = 1);
    splitChange(change)
    console.log(changeArr);
    const nonZeroChangeArr = changeArr.filter(([currency, amount]) => amount > 0);
    changeDue.innerHTML = "Status: CLOSED " + nonZeroChangeArr.map(([currency, amount]) => `${currency}: $${amount}`).join(", ");
  } else {
    changeArr.forEach(arr => arr.length = 1);
    splitChange(change)
    console.log(changeArr)
    const nonZeroChangeArr = changeArr.filter(([currency, amount]) => amount > 0);
    changeDue.innerHTML = "Status: OPEN " + nonZeroChangeArr.map(([currency, amount]) => `${currency}: $${amount}`).join(", ");
 
  }
}

let calcChange = (cashAmount) => {
  let changeDollars = cashAmount - price;
  let changePennies = Math.round(changeDollars * 100);
  return changePennies;
}



    let cid7 = Math.round(cid[7][1] * 100); // Convert TWENTY to pennies (rounded)
    console.log("cid7 TWENTYS in PENNYS " + cid7);
    let numOf2000s = Math.floor(cid7 / 2000); // Calculate num of 2000s
    let array2000s = new Array(numOf2000s).fill(2000);
    console.log("Array of 2000s: ", array2000s);
    
    let cid6 = Math.round(cid[6][1] * 100); // Convert TEN to pennies (rounded)
    console.log("cid6 TENS in PENNYS " + cid6);
    let numOf1000s = Math.floor(cid6 / 1000); // Calculate num of 1000s
    let array1000s = new Array(numOf1000s).fill(1000);
    console.log("Array of 1000s: ", array1000s);
    
    let cid5 = Math.round(cid[5][1] * 100); // Convert FIVE to pennies (rounded)
    console.log("cid5 FIVES in PENNYS " + cid5);
    let numOf500s = Math.floor(cid5 / 500); // Calculate num of 500s
    let array500s = new Array(numOf500s).fill(500);
    console.log("Array of 500s: ", array500s);
    
    let cid4 = Math.round(cid[4][1] * 100); // Convert ONE to pennies (rounded)
    console.log("cid4 ONES in PENNYS " + cid4);
    let numOf100s = Math.floor(cid4 / 100); // Calculate num of 100s
    let array100s = new Array(numOf100s).fill(100);
    console.log("Array of 100s: ", array100s);
    
    let cid3 = Math.round(cid[3][1] * 100); // Convert QUARTER to pennies (rounded)
    console.log("cid3 QUARTERS in PENNYS " + cid3);
    let numOf25s = Math.floor(cid3 / 25); // Calculate num of 25s
    let array25s = new Array(numOf25s).fill(25);
    console.log("Array of 25s: ", array25s);
    
    let cid2 = Math.round(cid[2][1] * 100); // Convert DIME to pennies (rounded)
    console.log("cid2 DIMES in PENNYS " + cid2);
    let numOf10s = Math.floor(cid2 / 10); // Calculate num of 10s
    let array10s = new Array(numOf10s).fill(10);
    console.log("Array of 10s: ", array10s);
    
    let cid1 = Math.round(cid[1][1] * 100); // Convert NICKEL to pennies (rounded)
    console.log("cid1 NICKELS in PENNYS " + cid1);
    let numOf5s = Math.floor(cid1 / 5); // Calculate num of 5s
    let array5s = new Array(numOf5s).fill(5);
    console.log("Array of 5s: ", array5s);
    
    let cid0 = Math.round(cid[0][1] * 100); // Convert PENNY to pennies (rounded)
    console.log("cid0 PENNYS in PENNYS " + cid0);
    let numOf1s = Math.floor(cid0 / 1); // Calculate num of 1s
    let array1s = new Array(numOf1s).fill(1);
    console.log("Array of 1s: ", array1s);


  let splitChange = (change) => {

    let remainingChange = change;

    let twentyCount = 0;
    for (let i = 0; i < array2000s.length; i++) {
      if (remainingChange >= 2000) {
        remainingChange -= 2000;
        twentyCount++;
      }
    }
    changeArr[7].push(twentyCount * 20);
    console.log(`$20 x ${twentyCount}`);
    console.log(remainingChange) // 3674

    let tenCount = 0;
    for (let i = 0; i < array1000s.length; i++) {
      if (remainingChange >= 1000) {
        remainingChange -= 1000;
        tenCount++;
      }
    }
    changeArr[6].push(tenCount * 10);
    console.log(`$10 x ${tenCount}`);
    console.log(remainingChange) // 1674

    let fivesCount = 0;
    for (let i = 0; i < array500s.length; i++) {
      if (remainingChange >= 500) {
        remainingChange -= 500;
        fivesCount++;
      }
    }
    changeArr[5].push(fivesCount * 5);
    console.log(`$5 x ${fivesCount}`);
    console.log(remainingChange)

    let dollarsCount = 0;
    for (let i = 0; i < array100s.length; i++) {
      if (remainingChange >= 100) {
        remainingChange -= 100;
        dollarsCount++;
      }
    }
    changeArr[4].push(dollarsCount * 1);
    console.log(`$1 x ${dollarsCount}`);
    console.log(remainingChange)

    let quarterCount = 0;
    for (let i = 0; i < array25s.length; i++) {
      if (remainingChange >= 25) {
        remainingChange -= 25;
        quarterCount++;
      }
    }
    changeArr[3].push(quarterCount * 0.25);
    console.log(`$0.25 x ${quarterCount}`);
    console.log(remainingChange)

    let dimeCount = 0;
    for (let i = 0; i < array10s.length; i++) {
      if (remainingChange >= 10) {
        remainingChange -= 10;
        dimeCount++;
      }
    }
    changeArr[2].push(dimeCount * 0.10);
    console.log(`$0.10 x ${dimeCount}`);
    console.log(remainingChange)

    let nickelCount = 0;
    for (let i = 0; i < array5s.length; i++) {
      if (remainingChange >= 5) {
        remainingChange -= 5;
        nickelCount++;
      }
    }
    changeArr[1].push(nickelCount * 0.05);
    console.log(`$0.05 x ${nickelCount}`);
    console.log(remainingChange)

    let pennyCount = 0;
    for (let i = 0; i < array1s.length; i++) {
      if (remainingChange >= 1) {
        remainingChange -= 1;
        pennyCount++;
      }
    }
    changeArr[0].push(pennyCount * 0.01);
    console.log(`$0.01 x ${pennyCount}`);
    console.log(remainingChange)

    
  }

 
purchaseBtn.addEventListener("click",
  purchase)
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Cash Register</title>
    <link rel="stylesheet" href="./styles.css" />
  </head>
  <body>
    <header>
      <h1 class="title">Cash Register</h1>
    </header>
    <div id="cash-app">
      <input id="cash" type="number" placeholder="cash input">
      <button id="purchase-btn" type="buttton">Purchase</button>
      <div id="change-due"></div>
    </div>
    <script src="./script.js"></script>
  </body>
</html>
body {
  background-color: rgb(38, 38, 61);
  color: white;
  font-family: sans-serif;
}

Your browser information:

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

Challenge Information:

Build a Cash Register Project - Build a Cash Register

Leaving this variable out in the global space spoils the re-usability of your functions. The tests change the cid variable between runs.

Thank you

I’ve implemented the change you recommended by moving the cashInDrawer variable inside the purchase function, so it’s now scoped locally and passed one more test but I’m still facing issues with the last two tests despite this adjustment.

There might be other flaws in my code…

Could you share 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]
];
console.log(cid)



let changeArr = [
  ["PENNY"],
  ["NICKEL"],
  ["DIME"],
  ["QUARTER"],
  ["ONE"],
  ["FIVE"],
  ["TEN"],
  ["TWENTY"],
  ["ONE HUNDRED"]
];

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

const purchase = () => {
  let cashInDrawer = cid.reduce((total, [currency, amount]) => total + amount, 0);
  console.log(`Total cash in drawer: $${cashInDrawer.toFixed(2)}`);
  let cashAmount = parseFloat(cash.value)
  let change;
  change = calcChange(cashAmount);
  console.log("Change PENNYS $" + change)
  if (cashAmount < price) {
    alert("Customer does not have enough money to purchase the item")
  } else if (cashAmount === price) {
    changeDue.innerHTML = "No change due - customer paid with exact cash"
  } else if (cashInDrawer < change / 100) {
    changeDue.innerHTML = "Status: INSUFFICIENT_FUNDS"
  } else if (cashInDrawer === change / 100) {
    changeArr.forEach(arr => arr.length = 1);
    splitChange(change)
    console.log(changeArr);
    const nonZeroChangeArr = changeArr.filter(([currency, amount]) => amount > 0);
    changeDue.innerHTML = "Status: CLOSED " + nonZeroChangeArr.map(([currency, amount]) => `${currency}: $${amount}`).join(", ");
  } else {
    changeArr.forEach(arr => arr.length = 1);
    splitChange(change)
    console.log(changeArr)
    const nonZeroChangeArr = changeArr.filter(([currency, amount]) => amount > 0);
    changeDue.innerHTML = "Status: OPEN " + nonZeroChangeArr.map(([currency, amount]) => `${currency}: $${amount}`).join(", ");
 
  }
}

let calcChange = (cashAmount) => {
  let changeDollars = cashAmount - price;
  let changePennies = Math.round(changeDollars * 100);
  return changePennies;
}



    let cid7 = Math.round(cid[7][1] * 100); // Convert TWENTY to pennies (rounded)
    console.log("cid7 TWENTYS in PENNYS " + cid7);
    let numOf2000s = Math.floor(cid7 / 2000); // Calculate num of 2000s
    let array2000s = new Array(numOf2000s).fill(2000);
    console.log("Array of 2000s: ", array2000s);
    
    let cid6 = Math.round(cid[6][1] * 100); // Convert TEN to pennies (rounded)
    console.log("cid6 TENS in PENNYS " + cid6);
    let numOf1000s = Math.floor(cid6 / 1000); // Calculate num of 1000s
    let array1000s = new Array(numOf1000s).fill(1000);
    console.log("Array of 1000s: ", array1000s);
    
    let cid5 = Math.round(cid[5][1] * 100); // Convert FIVE to pennies (rounded)
    console.log("cid5 FIVES in PENNYS " + cid5);
    let numOf500s = Math.floor(cid5 / 500); // Calculate num of 500s
    let array500s = new Array(numOf500s).fill(500);
    console.log("Array of 500s: ", array500s);
    
    let cid4 = Math.round(cid[4][1] * 100); // Convert ONE to pennies (rounded)
    console.log("cid4 ONES in PENNYS " + cid4);
    let numOf100s = Math.floor(cid4 / 100); // Calculate num of 100s
    let array100s = new Array(numOf100s).fill(100);
    console.log("Array of 100s: ", array100s);
    
    let cid3 = Math.round(cid[3][1] * 100); // Convert QUARTER to pennies (rounded)
    console.log("cid3 QUARTERS in PENNYS " + cid3);
    let numOf25s = Math.floor(cid3 / 25); // Calculate num of 25s
    let array25s = new Array(numOf25s).fill(25);
    console.log("Array of 25s: ", array25s);
    
    let cid2 = Math.round(cid[2][1] * 100); // Convert DIME to pennies (rounded)
    console.log("cid2 DIMES in PENNYS " + cid2);
    let numOf10s = Math.floor(cid2 / 10); // Calculate num of 10s
    let array10s = new Array(numOf10s).fill(10);
    console.log("Array of 10s: ", array10s);
    
    let cid1 = Math.round(cid[1][1] * 100); // Convert NICKEL to pennies (rounded)
    console.log("cid1 NICKELS in PENNYS " + cid1);
    let numOf5s = Math.floor(cid1 / 5); // Calculate num of 5s
    let array5s = new Array(numOf5s).fill(5);
    console.log("Array of 5s: ", array5s);
    
    let cid0 = Math.round(cid[0][1] * 100); // Convert PENNY to pennies (rounded)
    console.log("cid0 PENNYS in PENNYS " + cid0);
    let numOf1s = Math.floor(cid0 / 1); // Calculate num of 1s
    let array1s = new Array(numOf1s).fill(1);
    console.log("Array of 1s: ", array1s);


  let splitChange = (change) => {

    let remainingChange = change;

    let twentyCount = 0;
    for (let i = 0; i < array2000s.length; i++) {
      if (remainingChange >= 2000) {
        remainingChange -= 2000;
        twentyCount++;
      }
    }
    changeArr[7].push(twentyCount * 20);
    console.log(`$20 x ${twentyCount}`);
    console.log(remainingChange) // 3674

    let tenCount = 0;
    for (let i = 0; i < array1000s.length; i++) {
      if (remainingChange >= 1000) {
        remainingChange -= 1000;
        tenCount++;
      }
    }
    changeArr[6].push(tenCount * 10);
    console.log(`$10 x ${tenCount}`);
    console.log(remainingChange) // 1674

    let fivesCount = 0;
    for (let i = 0; i < array500s.length; i++) {
      if (remainingChange >= 500) {
        remainingChange -= 500;
        fivesCount++;
      }
    }
    changeArr[5].push(fivesCount * 5);
    console.log(`$5 x ${fivesCount}`);
    console.log(remainingChange)

    let dollarsCount = 0;
    for (let i = 0; i < array100s.length; i++) {
      if (remainingChange >= 100) {
        remainingChange -= 100;
        dollarsCount++;
      }
    }
    changeArr[4].push(dollarsCount * 1);
    console.log(`$1 x ${dollarsCount}`);
    console.log(remainingChange)

    let quarterCount = 0;
    for (let i = 0; i < array25s.length; i++) {
      if (remainingChange >= 25) {
        remainingChange -= 25;
        quarterCount++;
      }
    }
    changeArr[3].push(quarterCount * 0.25);
    console.log(`$0.25 x ${quarterCount}`);
    console.log(remainingChange)

    let dimeCount = 0;
    for (let i = 0; i < array10s.length; i++) {
      if (remainingChange >= 10) {
        remainingChange -= 10;
        dimeCount++;
      }
    }
    changeArr[2].push(dimeCount * 0.10);
    console.log(`$0.10 x ${dimeCount}`);
    console.log(remainingChange)

    let nickelCount = 0;
    for (let i = 0; i < array5s.length; i++) {
      if (remainingChange >= 5) {
        remainingChange -= 5;
        nickelCount++;
      }
    }
    changeArr[1].push(nickelCount * 0.05);
    console.log(`$0.05 x ${nickelCount}`);
    console.log(remainingChange)

    let pennyCount = 0;
    for (let i = 0; i < array1s.length; i++) {
      if (remainingChange >= 1) {
        remainingChange -= 1;
        pennyCount++;
      }
    }
    changeArr[0].push(pennyCount * 0.01);
    console.log(`$0.01 x ${pennyCount}`);
    console.log(remainingChange)

    
  }

 
purchaseBtn.addEventListener("click",
  purchase)

There seem to be more global variables.