Hubspot Form Calculators Values

by Dhruvang Gajjar

HTML

<label><input type="checkbox" name="hello" value="3" />3</label>
<label><input type="checkbox" name="hello" value="2" />2</label>
<label><input type="checkbox" name="hello" value="30" />3</label>
<label><input type="checkbox" name="hello" value="300" />3</label>
<label><input type="checkbox" name="hello" value="20" />2</label>

JavaScript

var getAvgSum = function(el) {
  var sum, avg = 0;
  var $arr = [];
  el.each(function() {
    $arr.push(parseInt($(this).val()[0]));
  });

  if ($arr.length) {
    sum = $arr.reduce(function(a, b) {
      return a + b;
    });
    avg = sum / $arr.length;
  }

  return {
    sum: sum,
    avg: avg
  }
}

$("body").on('change', 'input[name=hello]', function() {
  var aa = getAvgSum($("input[name=hello]:checked"));
  console.log(aa.sum, aa.avg);
})