# Squaring Each Number In An Array With Dynamically Added Inputs

## 16 January 2016 - 1 answer

I'm trying to calculate the standard deviation of a set of data entered by a user into a form with dynamically added inputs. Thus far I have been able to calculate the sum of the elements in the array, but I cannot figure out how to square each element of the array. I have searched this forum, but trying suggestions from the only applicable result (Square each number in an array in javascript) did not seem to work. Here is a snippet of my code:

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

Where `.calcSD` is the button the user clicks to perform the calculation. Moreover, the length of the array is given by `var number = arr.sort().filter(Boolean).length;` as the script is intended to filter out any inputs that are left blank.

Also, if it make a difference, inputs are dynamically added to the array via:

``````\$('.multi-field-wrapper').each(function() {
var \$wrapper = \$('.multi-fields', this);
\$('.multi-field:first-child', \$wrapper).clone(true).appendTo(\$wrapper).find('input').val('').focus();
});
``````

So I ask: how would go about determining the square of each element in the resulting array?

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

`||` in `parseInt` will use base as `0` if it 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;
});
``````<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>