I'm currently working through the algorithm exercise on Free Code camp but have run into an issue with this particular exercise: https://www.freecodecamp.org/challenges/exact-change
The function takes several parameters and returns the correct values, almost.
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]]);
My code returns an array with the correct values but the decimals are parsed as strings.
[ [ 'PENNY', '0.04' ],
[ 'NICKEL', 0 ],
[ 'DIME', '0.20' ],
[ 'QUARTER', '0.50' ],
[ 'ONE', '1.00' ],
[ 'FIVE', '15.00' ],
[ 'TEN', '20.00' ],
[ 'TWENTY', '60.00' ],
[ 'ONE HUNDRED', 0 ] ]
Any idea where Im going wrong? , Ive tried debugging this for ages but cant find the flaw.
My Code:
function checkCashRegister(price, cash, cid) {
var moneyValues = [[0.01],[0.05],[0.10],[0.25],[1],[5],[10],[20],[100]];
var outPutChange = [["PENNY", 0], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0.00], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]];
var allCashInReg =0;
var final =[];
console.log(cid);
for(var a=0; a<cid.length;a++){
allCashInReg+= cid[a][1];
}
console.log("All Available Cash in register is: €"+allCashInReg.toFixed(2));
var changeDue = (Math.abs(price - cash)).toFixed(2);
console.log("Change due is: €"+changeDue);
if(allCashInReg==changeDue){
console.log("Closed");
return "Closed";
}
else if(allCashInReg<changeDue){
console.log("Insufficient funds");
return "Insufficient Funds";
}
else{
var k = moneyValues.length;
while(k>=0){
if((changeDue/moneyValues[k])>=0){
var denomination = moneyValues[k];
var cashRegVal = cid[k][1];
console.log("\nChecking: €"+ denomination+".....");
var divider = Math.floor(changeDue/moneyValues[k]);
if(divider!=0){
var divider2=0;
if(changeDue>cashRegVal){
divider2 = cashRegVal/denomination;
console.log("Divider value is:"+divider2);
}
else {
divider2 = Math.floor(changeDue/denomination);
console.log("Divider value is:"+divider2);
}
var valueToDeduct = parseFloat(denomination*divider2).toFixed(2);
console.log("Change due is: €"+changeDue);
console.log("Amount of "+cid[k][0]+"(\'s') in cash register: €"+cashRegVal);
console.log("Value to deduct: €"+valueToDeduct);
if(cashRegVal>=valueToDeduct){
cashRegVal=cashRegVal-valueToDeduct;
console.log("Valid transaction!\n");
console.log("New amount of "+cid[k][0]+"(\'s) in cash register: €"+cashRegVal);
changeDue = (changeDue - valueToDeduct).toFixed(2);
console.log(divider2+" "+cid[k][0]+'(\'s) have been deducted from the Cash Register');
console.log("Change due is: €"+changeDue);
outPutChange[k][1]=parseFloat(valueToDeduct).toFixed(2);
}
else{
console.log("Insufficient funds\n");
return ("Insufficient Funds");
}
}
else{
console.log("Too big to use\n");
}
}
k--;
}
}
if(changeDue>0){
console.log("Insufficient funds")
return "Insufficient Funds";
}
else{
console.log("\nTransaction Complete! \nHere is a list of your change:\n ");
console.log(outPutChange);
return outPutChange.reverse();
}
}
What I have tried:
Debugging he code,parsing the output values