Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

My algorithm is working and my answers match the answers requested by the questions, but when I run the test, questions after 12 don’t get checked, is there a trick or some rule that should be followed?

Your code so far

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

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]]
let changeLog={};
const currencyUnit = [
["Penny",	0.01 ],
["Nickel",	0.05 ],
["Dime",	0.1 ],
["Quarter",	0.25 ],
["Dollar",	1 ],
["Five Dollars",	5 ],
["Ten Dollars",	10 ],
["Twenty Dollars",	20 ],
["One Hundred Dollars",	100],
]


let cash = document.getElementById('cash');
const purchaseBtn = document.querySelector('#purchase-btn');
const changeDue = document.querySelector('#change-due');
const cashDrawer = document.querySelector('#cash-drawer');
const priceP = document.querySelector("#price")
let cashInDrawer = totalDrawerMoney();

window.onload = () => {
    updateDrawer();
    updatePrice()
}
purchaseBtn.addEventListener('click', () => {
    changeDue.innerHTML = ""
   if (cash.value===""){
    alert("Please enter the cash amount");
   } else if(cash.value < price){
    alert("Customer does not have enough money to purchase the item");
   }else if (parseFloat(cash.value) === parseFloat(price)){
    statusUpdate("No change due - customer paid with exact cash") 
   }else calcChange();
})

function calcChange(){
    cash = parseFloat(cash.value) ;
    price =  parseFloat(price.toFixed(2));
    let change = cash - price;
    if (cash > price + cashInDrawer){
        statusUpdate("Status: INSUFFICIENT_FUNDS");
    }
    else{
        currencyUnit.slice().reverse().forEach(
            ([key , value],index)=>{
                value = parseFloat(value.toFixed(2))
                let flag = true
                while(flag){
                    if (change >= value  && cid[cid.length-index-1][1]!=0){
                        key = cid[cid.length-index-1][0]
                        change = parseFloat((change-value).toFixed(2))
                        changeLog[key] ? changeLog[key]+=value : changeLog[key]=value
                        cid[cid.length-index-1][1]-=value
                        cashInDrawer -=value
                    }
                    else if(change<value || cid[cid.length-index-1][1]==0 ){
                        flag = false
                    }
                }
            }
        )
        if (cashInDrawer==0){
            statusUpdate("Status: CLOSED ")
        } else if (change!=0){
            statusUpdate("Status: Not enough change ")
        }else{
            statusUpdate("Status: OPEN ")
            changeLog = Object.entries(changeLog)
            changeLog.forEach(([k,v])=>{
                changeDue.innerHTML +=`<p>${k}: $${v}<\p>`
            updateDrawer()            
            })
        }
    }
}

function totalDrawerMoney (){
    let arr = []
    for (const [key,value] of cid) {
         arr.push(value);
    }
    return parseFloat(arr.reduce((a, b) => a + b, 0).toFixed(2));
}


function updateDrawer(){
    cashDrawer.innerHTML = `Change in drawer: `
    for (const [key,value] of cid){
        let string = key.toLowerCase()
        string = string[0].toUpperCase() + string.slice(1)
        cashDrawer.innerHTML += `${string} : $${value}\n`
    }
}
function updatePrice(){
    priceP.textContent = `Total: $${price}`
}

function statusUpdate(str){
    changeDue.innerHTML += `${str}`
}

/* 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/131.0.0.0 Safari/537.36

Challenge Information:

Build a Cash Register Project - Build a Cash Register

Welcome to the forum @h.namazi91

Please also post your html.

Happy coding

1 Like

Your code contains global variables that are changed each time the function is run. This means that after each function call completes, subsequent function calls start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2
1 Like

I found a very tiny problem with my condition and got the program to work correctly

I have this problem, too.
Could you provide for me what that problem was?

hi @kholid_shofiev , please create your own topic to ask for help

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Get Help > Ask for Help button located on the challenge.

The Ask for Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

1 Like