# Exact Change "Closed" difficulty

Exact Change "Closed" difficulty
0

#1

I am returning the correct answers in the browser but it wont accept “Closed” as a correct answer. It accepts everything else as correct but not “Closed” and I am very confused as to why.

``````var changeDue;
var drawerTotal = 0;

function modularWhile(drawVal, curTar, curCoin){
var counter = 0;
var arrBuild = [];

while(changeDue >= curCoin && drawVal > 0){

if(counter === 0){
arrBuild.push([curTar, curCoin]);
counter = 1;
drawerTotal -= curCoin;

}
else{
arrBuild[0][1] += curCoin;
}
drawVal -= curCoin;
changeDue -= curCoin;
drawerTotal -= curCoin;
}

if(arrBuild.length > 0 && arrBuild[0][0] == "PENNY"){
arrBuild[0][1] += curCoin;
changeDue -= curCoin;
drawerTotal -= curCoin;
}
return arrBuild;
}

function checkCashRegister(price, cash, cid) {
changeDue = cash - price;
var dollarVal = [0.01, 0.05, 0.1, 0.25, 1.00, 5.00, 10.00, 20.00];
var change = [];
// Here is your change, ma'am.

for(var i = 7; i > -1; i--){
drawerTotal += cid[i][1];
var tempArr = modularWhile(cid[i][1], cid[i][0], dollarVal[i]);
if(tempArr.length > 0){
var flat = tempArr.reduce(function(a, b) {
return a.concat(b);
}, []);
change.push(flat);
}
}

if(changeDue > 0.01){
return "Insufficient Funds";
}
else if(drawerTotal < 0.01){
return "Closed";
}
return change;
}

// Example cash-in-drawer array:
// [["PENNY", 1.01],
// ["NICKEL", 2.05],
// ["DIME", 3.10],
// ["QUARTER", 4.25],
// ["ONE", 90.00],
// ["FIVE", 55.00],
// ["TEN", 20.00],
// ["TWENTY", 60.00],
// ["ONE HUNDRED", 100.00]]

checkCashRegister(19.50, 20.00, [["PENNY", 0.50], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);
``````

#2

You need to use triple backticks to post code to the forum.
See this post for details.

I also blurred the code to prevent spoilers.

#3

My gut tells me that the problem lies with this code

`````` if(changeDue > 0.01){
return "Insufficient Funds";
}
else if(drawerTotal < 0.01){
return "Closed";
}
``````

Unless those variables are dependent on each other, there’s a possibility they could both be true, and while you expect the second return, you get the first instead.

#4

Thank you for helping with my post. It looks like my code is returning the correct answer and freecodecamp isn’t accepting that answer.