# Change calculator

Hello, Im just trying to validate my code, once I compare the results with those provided by FCC are the same, but don’t pass the check. What I am doing wrong? Thanks in advance.

``````  **Your code so far**
``````
``````
const changeType = [
["PENNY", 0.01],
["NICKEL", 0.05],
["DIME", 0.1],
["QUARTER", 0.25],
["ONE", 1],
["FIVE", 5],
["TEN", 10],
["TWENTY", 20],
["ONE HUNDRED", 100]
];

var change = [["PENNY", 0]
,["NICKEL", 0],
["DIME", 0],
["QUARTER", 0],
["ONE", 0],
["FIVE", 0],
["TEN", 0],
["TWENTY", 0],
["ONE HUNDRED", 0]
];

function sort(array){

var resultArray = [];

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

resultArray.push(array[i]);
}

return resultArray;

}

function sumChange (obj) {

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

return Number(total.toFixed(2));

}

function changeCalc(diff,cid) {

for( let i=8 ; i>=0 ; i--){

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

return sort(change.filter( (item) => item[1]!=0));

}

const checkCashRegister = (price, cash , cid) => {
var diff=cash-price;
return sumChange(cid)==diff ? {resultArraystatus: "CLOSED", change: cid} :
sumChange(cid)<diff ? {status: "INSUFFICIENT_FUNDS", change: []} :
{status: "OPEN", change: changeCalc(diff,cid)};

}

checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);
``````
``````  **Your browser information:**
``````

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

Challenge: Cash Register

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests 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
``````

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.