Calculating exact change with JavaScript -


i'm attempting solve algorithm challenge on @ freecodecamp.

here prompt problem:

design cash register drawer function checkcashregister() accepts purchase price first argument (price), payment second argument (cash), , cash-in-drawer (cid) third argument.

cid 2d array listing available currency.

return string "insufficient funds" if cash-in-drawer less change due. return string "closed" if cash-in-drawer equal change due.

otherwise, return change in coin , bills, sorted in highest lowest order.

my solution works of parameters, except following:

checkcashregister(19.50, 20.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]]) 

should return: [["quarter", 0.50]]

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]]) 

should return: [["twenty", 60.00], ["ten", 20.00], ["five", 15.00], ["one", 1.00], ["quarter", 0.50], ["dime", 0.20], ["penny", 0.04]]

function checkcashregister(price, cash, cid) {   var change = 100 * (cash - price);   var availablefunds = 0;    var moneyvalues = [1, 5, 10, 25, 100, 500, 1000, 2000, 10000];   var amttoreturn = [];    (var = cid.length - 1; >= 0; i--){     var amt = 0;     while (moneyvalues[i] <= change && cid[i][1] > 0 && change > 0){       console.log("subtracting " + moneyvalues[i]);       cid[i][1] -= moneyvalues[i]/100; // reduce amount in cid       change -= moneyvalues[i]; // reduce amount change       amt += moneyvalues[i]/100; // keep track of how money taken out of cid     }     if (amt !== 0){       // adds record of amount taken out of cid       amttoreturn.push([cid[i][0], amt.tofixed(2)]);     }   }     // if there still change left on   if (change !== 0){     console.log("broke");     console.log(change);     return "insufficient funds";   }    // if there money left in cid, returns amttoreturn   (var j = 0; j < cid.length; j++){     if (cid[j][1] > 0){       console.log(amttoreturn);       return amttoreturn;     }   }    // if register empty   console.log("closed");   return "closed";  }  // example cash-in-drawer array: // [["penny", 1.01], 0 // ["nickel", 2.05], 1 // ["dime", 3.10],   2 // ["quarter", 4.25],3 // ["one", 90.00],   4 // ["five", 55.00],  5 // ["ten", 20.00],   6 // ["twenty", 60.00],7 // ["one hundred", 100.00]]8  checkcashregister(19.50, 20.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]]); 

i used value of money in cents avoid floating point precision errors. feel there's tiny bug somewhere preventing me finishing problem.

here fiddle: https://jsfiddle.net/odyjcmfj/

they're apparently expecting numbers second entry of ["bill", value] arrays.

just replace:

  amttoreturn.push([cid[i][0], amt.tofixed(2)]); 

with:

  amttoreturn.push([cid[i][0], amt]); 

and should set go.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -