arrays - Trouble with javascript function to generate exact change -
i'm working on challenge online coding community. object write function generate exact change highest lowestusing denominations shown in cid array (cash in drawer). function takes 3 arguments, price, cash, cid. cid cash in drawer.
i'm trying compare cash amounts of denominations in cid against cash amounts of exact change computation.
for example, if change = 96, exact change highest lowest
['twenty', 80], ['ten', 1], ['five', 1], ['one', 1]
. however, if cid shows ['twenty', 60], ['ten', 20], ["five, 55] code must compute exact change using amount in cid. in case, result be,
['twenty', 60], ['ten', 20], ['five', 15], ['one', 1]
in code below, display() function includes conditional statement intended make comparison , switch in cid value if necessary. however, causing duplicate value ["twenty"] appear. questions ar causing , how fix it.
the input function produces incorrect result of:
[ [ 'twenty', 60 ], [ 'twenty', 20 ], [ 'ten', 10 ], [ 'five', 5 ], [ 'one', 1 ], [ 'quarter', 0.5 ], [ 'dime', 0.2 ], [ 'penny', 0.04 ] ]
i have included code below, have model set up] @ http://repl.it/baku/2.
function drawer(price, cash, cid){ var change = cash - price; change = change.tofixed(2); change = +change; var combine = []; var cidmap = [ cid[0], cid[1], cid[2], cid[3], cid[4], cid[5], cid[6], cid[7], cid[8] ]; var denommap = [ 100, 'one hundred', 20, 'twenty', 10, 'ten', 5, 'five', 1, 'one', 0.25, 'quarter', 0.1, 'dime', 0.05, 'nickel', 0.01, "penny" ]; function total(cid){ var cashamts = []; cid.foreach(function(v){ cashamts.push(v[1]); },0); var sum = cashamts.reduce(function(a, b){return + b; }).tofixed(2); return +sum; } if(cash < price){ return price; } else if(total(cid) < change){ return "insufficient funds"; } else if(total(cid) === change){ return "closed"; } else if(total(cid) > change){ return display(change, cid); } function display(change) { var temp; num = change; var num = change; var value = ''; (var = 0; change > 0 && < denommap.length; += 2) { while (change >= denommap[i]) { change += denommap[i + 1]; change = change.replace(/\d+/g, ""); change = change.replace(/\./g, ""); value += denommap[i]; var div = math.floor(num/+value) * denommap[i]; if(change === cidmap[0][0] && div > cidmap[0][1]){ div = cidmap[0][1]; } else if(change === cidmap[1][0] && div > cidmap[1][1]){ div = cidmap[1][1]; } else if(change === cidmap[2][0] && div > cidmap[2][1]){ div = cidmap[2][1]; } else if(change === cidmap[3][0] && div > cidmap[3][1]){ div = cidmap[3][1]; } else if(change === cidmap[4][0] && div > cidmap[4][1]){ div = cidmap[4][1]; } else if(change === cidmap[5][0] && div > cidmap[5][1]){ div = cidmap[5][1]; } else if(change === cidmap[6][0] && div > cidmap[6][1]){ div = cidmap[6][1]; } else if(change === cidmap[7][0] && div > cidmap[7][1]){ div = cidmap[7][1]; } else if(change === cidmap[8][0] && div > cidmap[8][1]){ div = cidmap[8][1]; } temp = [change, div]; combine.push(temp); num = (num - div).tofixed(2); num = +num; display(num); } } return combine; } } drawer(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]]);
what causing this?
you taking of 3 'twenty'
notes out of drawer not updating drawer reflect there no 'twenty'
notes left. on next loop iteration, drawer incorrectly thinks still have 'twenty'
note give.
how fix it?
update cash in drawer after making withdrawal , include check remaining cash. here 1 way minimal modifications original code give insight.
take note of changeavailable
function , called. take note of updates drawer (e.g 3 twenty
notes: cidmap[7][1] = 0;
).
http://jsbin.com/vapobu/edit?js,console
(function () { "use strict"; function drawer(price, cash, cid){ var change = cash - price; change = change.tofixed(2); change = +change; var combine = []; var cidmap = [ cid[0], cid[1], cid[2], cid[3], cid[4], cid[5], cid[6], cid[7], cid[8] ]; var denommap = [ 100, 'one hundred', 20, 'twenty', 10, 'ten', 5, 'five', 1, 'one', 0.25, 'quarter', 0.1, 'dime', 0.05, 'nickel', 0.01, "penny" ]; function display(change) { var temp; //num = change;//use global variables sparingly, preferably within namespace, suspect type-o var num = change; var value = '', changeavailable = function (nameofcurrency) { var querycash = cid.filter(function (currentcurrency) { return currentcurrency[0] === nameofcurrency; }), cashremaining = querycash[0][1]; return (cashremaining !== 0); }; (var = 0; change > 0 && < denommap.length; += 2) { while (change >= denommap[i] && changeavailable(denommap[i + 1])) { change += denommap[i + 1]; change = change.replace(/\d+/g, ""); change = change.replace(/\./g, ""); value += denommap[i]; var div; div = math.floor(num/+value) * denommap[i]; if(change === cidmap[0][0] && div > cidmap[0][1]){ div = cidmap[0][1]; cidmap[0][1] = 0; } else if(change === cidmap[1][0] && div > cidmap[1][1]){ div = cidmap[1][1]; cidmap[1][1] = 0; } else if(change === cidmap[2][0] && div > cidmap[2][1]){ div = cidmap[2][1]; cidmap[2][1] = 0; } else if(change === cidmap[3][0] && div > cidmap[3][1]){ div = cidmap[3][1]; cidmap[3][1] = 0; } else if(change === cidmap[4][0] && div > cidmap[4][1]){ div = cidmap[4][1]; cidmap[4][1] = 0; } else if(change === cidmap[5][0] && div > cidmap[5][1]){ div = cidmap[5][1]; cidmap[5][1] = 0; } else if(change === cidmap[6][0] && div > cidmap[6][1]){ div = cidmap[6][1]; cidmap[6][1] = 0; } else if(change === cidmap[7][0] && div > cidmap[7][1]){ div = cidmap[7][1]; cidmap[7][1] = 0; //temp = [change, div];//duplicated code } else if(change === cidmap[8][0] && div > cidmap[8][1]){ div = cidmap[8][1]; cidmap[8][1] = 0; } temp = [change, div]; combine.push(temp); num = (num - div).tofixed(2); num = +num; display(num); } } return combine; } function total(cid){ var cashamts = []; cid.foreach(function(v){ cashamts.push(v[1]); },0); var sum = cashamts.reduce(function(a, b){return + b; }).tofixed(2); return +sum; } if(cash < price){ return price; } else if(total(cid) < change){ return "insufficient funds"; } else if(total(cid) === change){ return "closed"; } else if(total(cid) > change){ return display(change, cid); } } console.log(drawer(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]])); }());
Comments
Post a Comment