Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

Please tell me what sorting is required (numeric value or text). My output is the same as in the example, but the tests fail.

  1. When price is less than the value in the #cash element, total cash in drawer cid is greater than the change due, individual denomination amounts allows for returning change due, and the #purchase-btn element is clicked, the value in the #change-due element should be "Status: OPEN" with required change due in coins and bills sorted in highest to lowest order.

Your code so far

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

/* file: script.js */

/* file: styles.css */

Your browser information:

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

Challenge Information:

Build a Cash Register Project - Build a Cash Register

Hey there,

Please update the message to include your code. The code was too long to be automatically inserted by the help button.

When you enter a code, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

const cashRegister = document.getElementById("change");
const cashInput = document.getElementById("cash");
const allButton = document.querySelectorAll("#number");
const checkPaper = document.getElementById("change-due");
const gridButtons = document.getElementById("grid-buttons");
const purchaseBtn = document.getElementById("purchase-btn");
            
let cash = Number(cashInput.value);

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 result = []
let price = 3.26;
let money = {};
let charge = cash - price;
let count = 0;
let sort;

cid.forEach(element => {
  money[element[0]] = element[1];
});

let sumMoney; 

const bubbleSort = (arr) => {
  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < arr.length - 1 - i; j++) {
      if (arr[j][1] < arr[j + 1][1]) {
        [[arr[j][0], arr[j][1]], [arr[j + 1][0], arr[j + 1][1]]] = [[arr[j + 1][0], arr[j + 1][1]], [arr[j][0], arr[j][1]]]; // Меняем значения переменных
      }
    }
  }
return arr
}

const checkPaperDisplay=() =>{
 
  result.forEach(el => checkPaper.innerHTML += `${el[0]}$${el[1] }`)
}


const examinationPenny = (charge) => {
  count = 0;
  if(charge > 0.01) {
    
    while(money['PENNY'] > 0 && charge >= 0.01){
      money['PENNY'] -= 0.01;
      charge = charge.toFixed(2) - 0.01;
      console.log(charge)
      count++;
      console.log(count)
    }
    cid = Object.entries(money);
    cashRegister.innerHTML = '';
    
    if(count ) result.push(["PENNY: ", parseFloat(`${(count *= 0.01) }`)])
    checkPaperDisplay();
    
  }else{
    checkPaperDisplay();
  }
}

const examinationNickel = (charge) => {
  count = 0;
  if(charge > 0.05) {
    while(money['NICKEL'] > 0 && charge >= 0.05){
      money['NICKEL'] -= 0.05;
      charge -= 0.05;
      count++;
    }
    if(count) result.push(["NICKEL: ", parseFloat(`${count *= 0.05}`)])
    examinationPenny(charge);
  }else{
    examinationPenny(charge);
  }
}

const examinationDime = (charge) => {
  count = 0;
  if(charge > 0.1) {
    while(money['DIME'] > 0 && charge >= 0.1){
      money['DIME'] -= 0.1;
      charge -= 0.1;
      count++;
    }
    if(count) result.push(["DIME: ", parseFloat(`${count *= 0.1}`)])
    examinationNickel(charge);
  }else{
    examinationNickel(charge); 
  }
}

const examinationQuarter = (charge) => {
  count = 0;
  if(charge >= 0.25) {
    while(money['QUARTER'] > 0 && charge >= 0.25){
      money['QUARTER'] -= 0.25;
      charge -= 0.25;
      count++;
      
    }
    if(count > 0) {
      result.push([`QUARTER: `, parseFloat(`${count *= 0.25}`)]);
      examinationDime(charge);
    }
  }else{
    examinationDime(charge); 
  }
}

const examinationOne = (charge) => {
  count = 0;
  if(charge > 1) {
    
    while(money['ONE'] > 0 && charge >= 1){
      money['ONE'] -= 1;
      charge -= 1;
      count++;
    }
    if(count) result.push(["ONE: ", parseFloat(`${count *= 1}`)])
    examinationQuarter(charge);
  }else{
    examinationQuarter(charge);   
  }
}

const examinationFive = (charge) => {
  count = 0;
  if(charge > 5) {
    while(money['FIVE'] > 0 && charge >= 5){
      money['FIVE'] -= 5;
      charge -= 5;
      count++;
    }
    if(count) result.push(["FIVE: ", parseFloat(`${count *= 5}`)])
    examinationOne(charge);
  }else{
    examinationOne(charge); 
  }
}

const examinationTen = (charge) => {
  count = 0;
  if(charge > 10) {
    while(money['TEN'] > 0 && charge >= 10){
      money['TEN'] -= 10;
      charge -= 10;
      count++;
    }
    if(count) result.push(["TEN: ", parseFloat(`${count *= 10}`)])
    examinationFive(charge);
  }else{
    examinationFive(charge); 
  }
}

const examinationTwenty = (charge) => {
  count = 0;
  if(charge > 20) {
    while(money['TWENTY'] > 0 && charge >= 20){
      money['TWENTY'] -= 20;
      charge -= 20;
      count++;
    }
    if(count) result.push(["TWENTY: ", parseFloat(`${count *= 20}`) ])
    examinationTen(charge); 
  }else{
    examinationTen(charge);
  }
}

const examinationOneHundred = (charge) => {
  if(charge >= 100) {
    while(money['ONE HUNDRED'] > 0){
      money['ONE HUNDRED'] -= 100;
      charge -= 100;
      count++;
    }
    if(count) result.push(["HUNDRED: ", parseFloat(`${count *= 100}`)])
    examinationTwenty(charge);
  }else{
    examinationTwenty(charge);
  }
}
  

const calc = () => {
  sumMoney = Object.values(money).reduce((acc, el) => acc + el);
  cash = Number(cashInput.value);
  charge = cash - price;  
  clearDisplay()
  if (cash < price) return alert("Customer does not have enough money to purchase the item");
  if (cash === price) return checkPaper.innerHTML = "No change due - customer paid with exact cash";
  if (charge > sumMoney) return checkPaper.innerHTML = "Status: INSUFFICIENT_FUNDS";
  checkPaper.innerHTML = `<p>Status: OPEN</p>`
  examinationOneHundred(charge);
}


const clearDisplay = () => cashInput.value = "";
purchaseBtn.addEventListener("click", calc);



Later I want to modify this code to recursion so that it is called by one function.
but that’s it for now

I don’t understand whether it is necessary to sort by the amount of money issued, or by their value. hundred dollars are first on the list and pennies are last