Hello, I have seem to be having trouble with my check funds function.
My code is not working properly when it reaches the penny if statement. For the result I get this [[“TWENTY”, 60.00], [“TEN”, 20.00], [“FIVE”, 15.00], [“ONE”, 1.00], [“QUARTER”, 0.50], [“DIME”, 0.20], ["PENNY", 0.03]]. Instead of the correct answer which is [[“TWENTY”, 60.00], [“TEN”, 20.00], [“FIVE”, 15.00], [“ONE”, 1.00], [“QUARTER”, 0.50], [“DIME”, 0.20], [“PENNY”, 0.04]].
I have been stuck on this for some time now. Any help will be appreciated.
//Used to compare total cid to change needed
function totalFunds(cid){
var total = 0;
for(var i=0; i <cid.length; i++){
total += cid[i][1];
}
return total.toFixed(2);
}
//checks to make sure their are sufficient funds
function checkFunds(faceValue, change, cid){
var result = 0;
while(cid >= faceValue && change >= 0.00 && change >= faceValue){
result += faceValue;
change -= faceValue;
cid -= faceValue;
}
return result;
}
// Used to calculate change
function subtractThis(faceValue, amount, cid){
var counter = 0;
var result = 0;
while(cid >= faceValue && amount > 0.00 && amount >= faceValue){
amount -= faceValue;
result += faceValue;
cid -= faceValue;
counter++;
}
console.log(counter);
return faceValue * counter;
}
function removeZeros(temp){
var result = [];
for(var i=0; i < temp.length; i++){
if(temp[i][1] !== 0){
result.unshift(temp[i]);
}
}
return result;
}
function checkCashRegister(price, cash, cid) {
var change = (cash - price); // amount due
var total = totalFunds(cid);
if(change > total){
return "Insufficient Funds";
}
if(change.toFixed(2) === total){
return "Closed";
}
//P, N, D, Q, 1, 5, 10, 20, 100
var result = [["PENNY", 0], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]];
if (change >= 100){
result[8][1] += checkFunds(100, change, cid[8][1]);
change -= subtractThis(100, change, cid[8][1]);
}
if (change >= 20){
result[7][1] += checkFunds(20, change, cid[7][1]);
change -= Math.round(subtractThis(20, change, cid[7][1]));
change = change.toFixed(2);
}
if (change >= 10){
result[6][1] += checkFunds(10, change, cid[6][1]);
change -= subtractThis(10, change, cid[6][1]);
change = change.toFixed(2);
}
if (change >= 5){
result[5][1] += checkFunds(5, change, cid[5][1]);
change -= subtractThis(5, change, cid[5][1]);
change = change.toFixed(2);
}
if (change >= 1){
result[4][1] += checkFunds(1, change, cid[4][1]);
change -= subtractThis(1, change, cid[4][1]);
change = change.toFixed(2);
}
if (change >= 0.25){
result[3][1] += checkFunds(0.25, change, cid[3][1]);
change -= subtractThis(0.25, change, cid[3][1]);
change = change.toFixed(2);
}
if (change >= 0.10){
result[2][1] += checkFunds(0.10, change, cid[2][1]);
change -= subtractThis(0.10, change, cid[2][1]);
change = change.toFixed(2);
}
if (change >= 0.05){
result[1][1] += checkFunds(0.05, change, cid[1][1]);
change -= subtractThis(0.05, change, cid[1][1]);
change = change.toFixed(2);
}
if (change >= 0.01){
result[0][1] += checkFunds(0.01, change, cid[0][1]);
change -= subtractThis(0.01, change, cid[0][1]);
change = change.toFixed(2);
}
result = removeZeros(result);
/*if(change > 0){
return "Insufficient Funds";
}*/
// Here is your change, ma'am.
return result;
}
checkCashRegister(3.26, 100.00, [["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]]);