javascript - How do I find the sum value of an array of objects in a loop? -


i'm not sure if wording of question accurate based on description of problem please edit if can more accurate.

i trying build stamp duty calculator improve js, have array of objects different "bands" , "percents" using calculate tax based on users input. have attached image better understanding

enter image description here

i displaying amount of tax each band in table , trying find total tax finding sum of values in "tax" column.

currently displaying highest value.

i have tried can thing off , nothing has worked, how can fix this?

here code,

 $(function (jquery) {       (function stampdutycalculator() {       var taxbands = [          {              min: 0,              max: 125000,              percent: 0          },          {              min: 125000,              max: 250000,              percent: 0.02          },          {              min: 250000,              max: 925000,              percent: 0.05          },          {              min: 925000,              max: 1500000,              percent: 0.1          },          {              min: 1500000,              max: null,              percent: 0.12          }      ];       var secondtaxbands = [          {              min: 0,              max: 125000,              percent: 0.03          },          {              min: 125000,              max: 250000,              percent: 0.05          },          {              min: 250000,              max: 925000,              percent: 0.08          },          {              min: 925000,              max: 1500000,              percent: 0.13          },          {              min: 1500000,              max: null,              percent: 0.15          }      ];       var tablerow = "<tr><td>{taxband}</td><td>{percent}</td><td>{taxable}</td><td class='tax'>{tax}</td></tr>",           table = $("#explained-table"),          results = $("#results"),          effectiverate = $("#effective-rate");           $('#calculate').on('click', function calculatebutton() {              if ($("#input-value").val() !== '') {                   calculatestampduty();              }          });           function calculatestampduty() {              var bands = taxbands,                         userinput = parseint($("#input-value").val(), 10),                  row;              if ($('#second-home').is(':checked')) {                  bands = secondtaxbands;             }              if (table.length) {                   table.find("tr:gt(0)").remove();              }              var taxablesum = function (x, y) {                  var maxband = (x !== null) ? math.min(x, userinput) : maxband = userinput;                  return maxband - y;             },                 tax = function (taxablesum, x) {                  return (taxablesum * x).tofixed(2);             },                 effectiverate = function(tax) {                     return math.round(tax / userinput * 100).tofixed(1);             },                   numberwithcommas = function (x) {                 var parts = x.tostring().split(".");                 parts[0] = parts[0].replace(/\b(?=(\d{3})+(?!\d))/g, ",");                 return parts.join(".");             };                          (var = 0; < bands.length; i++) { //for loop loop through array of objects              var min = bands[i].min, //variables used arguments in functions above, not best practice declare functions in loop                 max = bands[i].max,                 pct = bands[i].percent,                 taxablesum = taxablesum(max, min),                 tax = tax(taxablesum, pct),                 erate = effectiverate(tax);              if (max !== null) { //replaces template tags min, max , percent values in object                 row = tablerow.replace("{taxband}", "£" + min + " - " + "£" + max).replace("{percent}", (pct * 100) + "%");             } else {                 row = tablerow.replace("{taxband}", "£" + min + "+").replace("{percent}", (pct * 100) + "%"); //used last taxband             }               if (taxablesum < 0) {                 row = row.replace("{taxable}", "£" + 0 + ".00").replace("{tax}", "£" + 0 + ".00");             }   else if (userinput > 1500000) {                     row = row.replace("{taxable}", "£" + numberwithcommas(taxablesum)).replace("{tax}", "£" + numberwithcommas(tax));                     results.text("£" + numberwithcommas(tax));                     effectiverate.text(erate + "%");             }   else if (userinput > 925000) {                     row = row.replace("{taxable}", "£" + numberwithcommas(taxablesum)).replace("{tax}", "£" + numberwithcommas(tax));                     results.text("£" + numberwithcommas(tax));                     effectiverate.text(erate + "%");             }   else if (userinput > 250000) {                     row = row.replace("{taxable}", "£" + numberwithcommas(taxablesum)).replace("{tax}", "£" + numberwithcommas(tax));                     results.text("£" + numberwithcommas(tax));                     effectiverate.text(erate + "%");             }   else if (userinput > 125000) {                     row = row.replace("{taxable}", "£" + numberwithcommas(taxablesum)).replace("{tax}", "£" + numberwithcommas(tax));                     results.text("£" + numberwithcommas(tax));                     effectiverate.text(erate + "%");             }   else {                     row = row.replace("{taxable}", "£" + userinput).replace("{tax}", "£" + numberwithcommas(tax));                     results.text("£" + (numberwithcommas(tax) * 0));                     effectiverate.text(erate * 0 + "%");             }              table.append(row);              console.log(number(tax) );         }          }       }());  }); 

edit here fiddle https://jsfiddle.net/p6c1w5r3/

some of buttons functionality not completed yet, wanted calculations correct first

check fiddle. https://jsfiddle.net/p6c1w5r3/7/

basically create variable outside loop called totaltax. inside loop add tax value variable.

finally, should not set label text every time, instead set once @ end.

oh btw, tax calculation returns weird values when amount less 20000.


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -