Build A Cash Project

I get exactly the needed answer but they will not let me go pass the challenge

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]
];
let amounts = [];
[...cid].forEach((num)=> amounts.push(num[1]) )
amounts = amounts.reverse()
console.log("page started")
const input = document.getElementById("cash");
const div = document.getElementById("change-due")
const purchase = document.getElementById("purchase-btn")

const display = (status,payment = [])=>{

    let displayer = `Status: ${status} `
    payment.forEach((pay)=>{
        displayer+=`${pay.join(": $")} `
    })
    div.textContent = displayer

}


const ordering=(status,cashes,moneys)=>{
    console.log("Ordering....")
    const payment = []
    cashes.forEach((x,index)=>x[1]=x[1]*moneys[index])
    cashes.forEach((c)=>{ if(c[1] !== 0){payment.push(c)}})  
    console.log(payment)
    display(status,payment)
}

const getBalanceFromMoney = (status,balance) =>{
    const moneys = [100,20,10,5,1,0.25,0.1,0.05,0.01]
    const cashes = [
        ["ONE HUNDRED",0],
        ["TWENTY",0],
        ["TEN",0],
        ["FIVE",0],
        ["ONE",0],
        ["QUATER",0],
        ["DIME",0],
        ["NICKEL",0],
        ["PENNY",0]
    ]

    console.log(cashes)
    
    let tempBalance = balance
    
    moneys.forEach((money,index)=>{
        
        while(tempBalance.toFixed(2) >= money && amounts[index] !== 0){
            tempBalance -= money
            amounts[index] -= money
            cashes[index][1] += 1
            
        }

    })
    console.log(cashes)
    ordering(status,cashes,moneys)
}



const checkStatus = (balance) =>{
    let status = "";
    const balanced_2 = balance.toFixed(2)
    const reduced = [...amounts].reduce((a,b)=>a+b)
    if(reduced == balanced_2){
        status = "CLOSED"
        getBalanceFromMoney(status,balance)
    }
    if(reduced < balanced_2){
        status = "INSUFFICIENT_FUNDS"
        display(status)
    }
    if(reduced > balanced_2){
        status = "OPEN"
        getBalanceFromMoney(status,balance)
    }
}


const checkBalance = (cash) =>{
    if(cash===price){
        div.textContent = "No change due - customer paid with exact cash"
    }else{
        checkStatus(parseFloat(cash - price))
    }
}

const isCashValid = () => {
    console.log("checking if cash is valid.....")
    const cash = input.value
    cash < price? alert("Customer does not have enough money to purchase the item"): checkBalance(parseFloat(cash))
}

purchase.addEventListener("click",isCashValid)```

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

Thanks, Yeh I know its a issue but for this challenge the answer comes 100% as expected but they are not letting me pass

Right… because the global variables break your code

Okay, I’ll try changing them

Sir, Can you give me some hints about the variables that I need to change

All this cannot be in the global scope.

Thanks for the help :slight_smile: