// 'use strict'; var calcNPS = function () { var positiveCnt = 0, neutralCnt = 0, negativeCnt = 0; $('input[data-ajco-nps]').each(function () { var elem = $(this); var val = parseInt(elem.val()); if (isNaN(val)) { $(this).val(''); val = 0; } switch (elem.data('ajco-nps')) { case 'positive': positiveCnt += val; break; case 'neutral': neutralCnt += val; break; case 'negative': negativeCnt += val; break; } }); $('#count-negative').text(negativeCnt.toString()); $('#count-positive').text(positiveCnt.toString()); $('#count-neutral').text(neutralCnt.toString()); var totalCnt = negativeCnt + positiveCnt + neutralCnt; var sumNegative = Math.round(negativeCnt * 100 / totalCnt); var sumPositive = Math.round(positiveCnt * 100 / totalCnt); $('#sum-negative').text(sumNegative + '%'); $('#sum-positive').text(sumPositive + '%'); $('#total-nps').text(Math.round(sumPositive - sumNegative)); } $(document).ready(function () { $('input[data-ajco-nps]').change(function () { calcNPS(); }).keyup(function (e) { calcNPS(); }); $('#ajco-nps-restart').click(function(){ $('input[data-ajco-nps]').each(function () { $(this).val(''); }); $('#count-negative').text('0'); $('#count-positive').text('0'); $('#count-neutral').text('0'); $('#sum-negative').text('0%'); $('#sum-positive').text('0%'); $('#total-nps').text('0'); return false; }); });