Build A Cash Register Project

For the last test, I have the code solution and it should work in theory, but I have to use a hard copy of an array instead of a shallow copy of an array.

Okay, so trying to reproduce the last test

  • Adding at the bottom:
price = 19.5
cid = [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]
  • Typing 20 in the box and pressing button
  • Result is

Status: OPEN

QUARTER: $0.5

The last test PENNY is supposed to be 0.5

Sorry, I meant the second to last test (Test 9). I had an issue where I would pass the test but the coins were still being subtracted. Using a deep copy of an array instead of a shallow copy, I was able to fix this error.

Here is my final updated code. Everything works as intended, but somehow I am still failing some tests. I don’t know what to do anymore, and nobody seems to know why I’m not passing some tests when I get the desired results. Maybe it’s the tests themselves that needs fixing?

let price = 1.87;   //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]
];
 
//TEST CASE 6
//   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]
// ];

// TEST CASE 7 
//   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]
// ];
 
//TEST CASE 8 
// let price = 19.5;
//    let cid = [
//   ["PENNY", 0.01],
//   ["NICKEL", 0],
//   ["DIME", 0],
//   ["QUARTER", 0],
//   ["ONE", 0],
//   ["FIVE", 0],
//   ["TEN", 0],
//   ["TWENTY", 0],
//   ["ONE HUNDRED", 0]
// ];

//TEST CASE 9
// 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]
// ];

//TEST CASE 10
// 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 purchaseBtn = document.getElementById("purchase-btn");
const priceTotal = document.querySelector(".cr-total-rect-3");
const changeDue = document.getElementById("change-due");
const cashInput = document.getElementById("cash");
const crScreen = document.querySelector(".cr-screen");
let changeWorthArr = [100, 20, 10, 5, 1, .25, .1, .05, .01];
let checker = true;

priceTotal.textContent = `Total: $${price}`;

let cidReverse = structuredClone(cid);
cidReverse = cidReverse.reverse();
cidReverse.forEach((el)=>{
  el[1] = Math.ceil(el[1] * 100);
});
console.log("cidReverse output: ", cidReverse); //TEST
console.log("--------------------");
console.log("cid output: ",cid);   //TEST
console.log("--------------------");

const changeOutput = () =>{
  let dueChange = Math.round((parseFloat(cashInput.value) - price) * 100); 
  let cidTotal = cidReverse.reduce((acc,el)=>acc+el[1],0);
  let tempCidReverse = structuredClone(cidReverse);
  // console.log("tempCidReverse Before: ", tempCidReverse);//TEST
  console.log("CID Total: " , cidTotal); //TEST
  console.log("Due change: ", dueChange); //TEST
  changeDue.innerHTML = "";

  if(cidTotal < dueChange){
    changeDue.innerHTML = `<p>Status: INSUFFICIENT_FUNDS</p>`;
    return;
  }else if(cidTotal === dueChange){
    changeDue.innerHTML = `<p>Status: CLOSED</p>`;
  }else if(cidTotal > dueChange){
    changeDue.innerHTML = `<p>Status: OPEN</p>`;
  } 
    
  for(let i =0; i < cidReverse.length; i++){
    let count = 0;
    while(cidReverse[i][1] > 0){
      let tempChangeWorth = changeWorthArr[i] * 100;
      if(dueChange >= tempChangeWorth){
        dueChange = dueChange - tempChangeWorth;
        // console.log(`cidReverse[${i}][1] before: ` ,cidReverse[i][1]); //TEST
        cidReverse[i][1] = cidReverse[i][1] - tempChangeWorth;
        //console.log(`cidReverse[${i}][1] after: ` ,cidReverse[i][1]);  //TEST
        count = count + tempChangeWorth;
        // console.log("change: " + cidReverse[i][0] + " | element: " + i + " | change due:", count);
        // console.log(`cidReverse[${i}][1] after: ` ,cidReverse[i][1]);  //TEST
      }else{
        break;
      }
    }
    if(count > 0){
      changeDue.innerHTML += `<p>${cidReverse[i][0]}: $${count/100}</p>`;
    }
  }
  console.log("Due change: ",dueChange); //TEST
  if(dueChange > 0){
    changeDue.innerHTML = `<p>Status: INSUFFICIENT_FUNDS</p>`;
    console.log("Insufficient funds 2 check");
    // updateCID(cidReverse, cid);
    // console.log("tempCidReverse After: ", tempCidReverse); //TEST
    cidReverse = [...tempCidReverse];
    // console.log("cidReverse Updated: ", cidReverse); //TEST
  }
};

const checkCashInput = () =>{
  if(parseFloat(cashInput.value) < price){
    alert("Customer does not have enough money to purchase the item");
    checker = false;
  }else if(parseFloat(cashInput.value) === price){
    changeDue.textContent = "No change due - customer paid with exact cash";
    checker = false;
  }
};
  
const changeInDrawer = () =>{ 
  crScreen.innerHTML = `
  <p class="cid-p"><b>Change in drawer:</b></p>
  <p class="cid-p">Pennies: $${(cid[0][1]).toFixed(2)}</p>
  <p class="cid-p">Nickles: $${(cid[1][1]).toFixed(2)}</p>
  <p class="cid-p">Dimes: $${(cid[2][1]).toFixed(2)}</p>
  <p class="cid-p">Quarters: $${cid[3][1]}</p>
  <p class="cid-p">Ones: $${cid[4][1]}</p>
  <p class="cid-p">Fives: $${cid[5][1]}</p>
  <p class="cid-p">Tens: $${cid[6][1]}</p>
  <p class="cid-p">Twenties: $${cid[7][1]}</p>
  <p class="cid-p">Hundreds: $${cid[8][1]}</p>
  `;
};
changeInDrawer();

const updateCID = (updateArr,updatedArrValues) =>{
  let i = 0;
  let j = updatedArrValues.length - 1;
  while(i < updateArr.length && j >=0){
    // updateArr[i][1] = Math.round(updatedArrValues[j][1]/100);
    updateArr[i][1] = updatedArrValues[j][1]/100;
    // updateArr[i][1] = updatedArrValues[j][1];
    //cid[i][1] = cidReverse[i][1];
    i++;
    j--;
  }
  //  console.log("updateArr result: " , updateArr);
   changeInDrawer(); 
};
// changeInDrawer();
 
purchaseBtn.addEventListener("click", ()=>{
  checkCashInput();
  if(checker){
    changeOutput();
  }else{
    checker = true;
  }
  // updateCID();
  updateCID(cid, cidReverse);
  console.log("cid check: ",cid); //TEST
  console.log("reverseCid check: ", cidReverse);
});   

As in my last message I’m not getting expected result when trying these tests with your code.

For example, to correctly reproduce the last test:

  • Paste the test case at the bottom of code:
price = 19.5
cid = [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]
  • Input 20 to the field and press button.
  • Result returned by code is:

Status: OPEN

QUARTER: $0.5

1 Like

Oh okay, I see. Something is not updating correctly when the test code is pasted at the bottom, because it is still using the first original test case array values. This is a big clue.

Thank you, I was able to solve it. I just had to update my arrays in the addEventListener! I can now let out a big sigh of relief. :face_exhaling:

2 Likes