javascript - Squaring each number in an array with dynamically added inputs -


i'm trying calculate standard deviation of set of data entered user form dynamically added inputs. far have been able calculate sum of elements in array, cannot figure out how square each element of array. have searched forum, trying suggestions applicable result (square each number in array in javascript) did not seem work. here snippet of code:

$(".calcsd").click(function() {        $("input[type=text]").each(function() {         arr.push($(this).val().trim() || 0);         sum += parseint($(this).val().trim() || 0);         }); 

where .calcsd button user clicks perform calculation. moreover, length of array given var number = arr.sort().filter(boolean).length; script intended filter out inputs left blank.

also, if make difference, inputs dynamically added array via:

$('.multi-field-wrapper').each(function() {   var $wrapper = $('.multi-fields', this);   $(".add-field", $(this)).unbind('click').click(function(e) {     $('.multi-field:first-child', $wrapper).clone(true).appendto($wrapper).find('input').val('').focus();   }); 

so ask: how go determining square of each element in resulting array?

the pow() method returns value of x power of y (x^y)

|| in parseint use base 0 if returns falsey value

$(".calcsd").click(function() {    var sum = 0;    var arr=[];    $("input[type=text]").each(function() {      var squared = math.pow(parseint(this.value)||0, 2);      arr.push(squared);      sum += squared;    });    alert(sum);  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>  <input type="text">  <input type="text">  <button class="calcsd">calculate</button>


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -