Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

I can’t figure out what is wrong. I tried everything but still it doesn’t pass. Please someone help me to fix this. It works but I still not pass yet.

Your code so far

<!-- file: index.html -->

/* file: styles.css */

/* file: script.js */
const cash = document.getElementById("cash");
const changeDue = document.getElementById("change-due");
const purchaseBtn = document.getElementById("purchase-btn");
const boxInside = document.getElementById("box-inside");

const 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],
];
const denominationsText = [
  "Pennies",
  "Nickels",
  "Dimes",
  "Quarters",
  "Ones",
  "Fives",
  "Tens",
  "Twenties",
  "Hundreds",
];

const values = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];

const valueRe = values.slice().reverse();
const cidRe = cid.slice().reverse();
const denoRe = denominationsText.slice().reverse();

const outputFun = () => {

  let total = 0;
  let change = 0;
  let cashValue = Number(cash.value);

  if(price > cashValue) {
    alert("Customer does not have enough money to purchase the item");
    return;
  } else if(price === cashValue) {
    changeDue.innerHTML = `<p>No change due - customer paid with exact cash</p>`;
    return;
  }

  for (let i = 0; i < cid.length; i++) {
    total += cid[i][1];
  }

  total = Number(total.toFixed(2));
  change = cashValue - price;
  change = Number(change.toFixed(2));

  if (change > total) {
    changeDue.innerHTML = `<p>Status: INSUFFICIENT_FUNDS</p>`;
    return;
  }

  let changeArr = [];
  let changeCount = 0;
  let changeName = [];

  for(let i = 0; i< cidRe.length; i++) {
    changeCount = 0;

    while(change >= valueRe[i] && cidRe[i][1] > 0) {
      change -= valueRe[i];
      cidRe[i][1] -= valueRe[i];
      change = Number(change.toFixed(2));
      changeCount++
    }

    if(changeCount > 0) {
      let returnChange = changeCount * valueRe[i];
      returnChange = Number(returnChange.toFixed(2));
      changeArr.push(returnChange);
      changeName.push(cidRe[i][0]);
    }
  }

  cid = cidRe.slice().reverse();

  boxChg(cid);
  
  changeDue.innerHTML = "";

if (price < cashValue && total > change) {
  let content = document.createElement("p");
  content.textContent = "Status: OPEN";
  changeDue.appendChild(content);
  for (let i = 0; i < changeArr.length; i++) {
    let content = document.createElement("p");
    content.textContent = `${changeName[i]}: $${changeArr[i]}`;
    changeDue.appendChild(content);
  }
  return;
} else {
  let content = document.createElement("p");
  content.textContent = "Status: CLOSED";
  changeDue.appendChild(content);
  for (let i = 0; i < changeArr.length; i++) {
    let content = document.createElement("p");
    content.textContent = `${changeName[i]}: $${changeArr[i]}`;
    changeDue.appendChild(content);
  }
  return;
}

}

const boxChg = (arr) => {
  boxInside.innerHTML = "<h2>Change in drawer</h2>";
  for (let i = 0; i < cid.length; i++) {
    const boxText = document.createElement("p");
    boxText.textContent = `${denominationsText[i]}: ${Number(
      arr[i][1].toFixed(2)
    )}`;
    boxInside.appendChild(boxText);
  }
};


purchaseBtn.addEventListener('click', () => {
  outputFun();
  cash.value = "";
})

boxChg(cid);

Your browser information:

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

Challenge Information:

Build a Cash Register Project - Build a Cash Register
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures-v8/build-a-cash-register-project/build-a-cash-register`Preformatted text`

What error message are you getting or what test are you failing?

You might get more information from the browser console (F12)

These global variables are probably a problem

When price is less than the value in the #cash element, total cash in drawer cid is greater than change due, individual denomination amounts make impossible to return needed change, and the #purchase-btn element is clicked, the value in the #change-due element should be "Status: INSUFFICIENT_FUNDS"

I am stuck at here, sir. I don’t understand what it means.it is look like same condition as Status: OPEN.

Thanks! It was a problem. But I still have these two instructions. I really don’t understand how to pass these two it is seem wrong to me. Can you please help me?

here is remaining instructions -

  • When price is less than the value in the #cash element, total cash in drawer cid is greater than change due, individual denomination amounts make impossible to return needed change, and the #purchase-btn element is clicked, the value in the #change-due element should be "Status: INSUFFICIENT_FUNDS"

  • Failed:When price is 19.5, the value in the #cash element is 20, cid is [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]], and the #purchase-btn element is clicked, the value in the #change-due element should be "Status: INSUFFICIENT_FUNDS"

Here is my code so far.

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

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],
];
const denominationsText = [
  "Pennies",
  "Nickels",
  "Dimes",
  "Quarters",
  "Ones",
  "Fives",
  "Tens",
  "Twenties",
  "Hundreds",
];

const values = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];

const outputFun = () => {

  let total = 0;
  let change = 0;
  let cashValue = Number(cash.value);
  

  if(price > cashValue) {
    alert("Customer does not have enough money to purchase the item");
    return;
  } else if(price === cashValue) {
    changeDue.innerHTML = `<p>No change due - customer paid with exact cash</p>`;
    return;
  }

  for (let i = 0; i < cid.length; i++) {
    total += cid[i][1];
  }
  if(total> cashValue) {
    total -= cashValue;
  }
  total = Number(total.toFixed(2));
  change = cashValue - price;
  change = Number(change.toFixed(2));
  let remainChange = change;

  if (price < cashValue && change > total) {
    changeDue.innerHTML = `<p>Status: INSUFFICIENT_FUNDS</p>`;
    return;
  }

  let changeArr = [];
  let changeCount = 0;
  let changeName = [];

  for(let i = cid.length - 1; i>=0; i--) {
    changeCount = 0;

    while(change >= values[i] && cid[i][1] > 0) {
      change -= values[i];
      cid[i][1] -= values[i];
      change = Number(change.toFixed(2));
      changeCount++;
    }

    if(changeCount > 0) {
      let returnChange = changeCount * values[i];
      returnChange = Number(returnChange.toFixed(2));
      changeArr.push(returnChange);
      changeName.push(cid[i][0]);
    }
  }

  console.log(cid);

  boxChg(cid);

  console.log(total);
  console.log(remainChange);
  
  changeDue.innerHTML = "";

if (price < cashValue && total > remainChange) {
  let content = document.createElement("p");
  content.textContent = "Status: OPEN";
  changeDue.appendChild(content);
  for (let i = 0; i < changeArr.length; i++) {
    let content = document.createElement("p");
    content.textContent = `${changeName[i]}: $${changeArr[i]}`;
    changeDue.appendChild(content);
  }
  return;
}

if (price < cashValue && remainChange > total) {
  changeDue.innerHTML = `<p>Status: INSUFFICIENT_FUNDS</p>`;
  return;
}

if(price < cashValue && total === remainChange) {

  let content = document.createElement("p");
  content.textContent = "Status: CLOSED";
  changeDue.appendChild(content);
  for (let i = 0; i < changeArr.length; i++) {
    let content = document.createElement("p");
    content.textContent = `${changeName[i]}: $${changeArr[i]}`;
    changeDue.appendChild(content);
  }
  return;
}

}

const boxChg = (arr) => {
  boxInside.innerHTML = "<h2>Change in drawer</h2>";
  for (let i = 0; i < cid.length; i++) {
    const boxText = document.createElement("p");
    boxText.textContent = `${denominationsText[i]}: ${Number(
      arr[i][1].toFixed(2)
    )}`;
    boxInside.appendChild(boxText);
  }
};


purchaseBtn.addEventListener('click', () => {
  outputFun();
  cash.value = "";
})

boxChg(cid);

Have you tried to test with the values given?