You are viewing limited content. For full access, please sign in.

Question

Question

Averages and Sums in javascript

asked on March 21, 2016 Show version history

Trying to calculate the average of 2 fields using javascript.

I want it to be able to output the value on another field.

$('.inv input').on('change', function(){
  var sum = 0, n = 0;
  $('.inv input').each(function(){
    if ($(this).val() != ''){
      sum += parseFloat($(this).val());
      n += 1;
    }
  });
  $('.invave input').val(sum/n);
});

The class of the two values being 'inv'  and the output class on the field with average is 'invave'

 

Subsequently I wanted to see what I need to setup for summing multiple fields,  some sums need to be negative.

IDs:  q4, -q5, q6,-q7,q8

 

0 0

Answer

SELECTED ANSWER
replied on March 21, 2016 Show version history

Did you mean both #q58 an #q59 are in class 'inv'?

Here is the setup of my sample form in its entirety, with the CSS details displayed. How does yours differ?

EDIT: Changed how image was attached for readability.

2016-03-21_1536.png
1 0

Replies

replied on March 21, 2016

Hi Chase, If using Forms 10, you can do this without Javascript.

1 0
replied on March 21, 2016

I am actually trying to divide two values, would the follow not work to simple divide two values and output to the last field listed?

 

$('#inv input').val().change(function () {
   x=parseFloat($('#q60 input').val());
   y=parseFloat($('#q61 input').val());
  $('#q62 input').val((x/y).toFixed(2));
})

 

replied on March 21, 2016

I don't see any option with the new calculator to float the values.  I cannot have a QUOTIENT return 1 when I divide 3/2. 

I need the true value.

0 0
replied on March 21, 2016

Hi Chase, in Forms 10 Calculations, there should also be an AVERAGE function which should suit your needs. (I confess I haven't looked too closely at the posted JavaScript yet.)

0 0
replied on March 21, 2016

Let me post the most recent one that works for me. 

Average wont work when dividing two values for me though. 

 

$(document).ready(function () {
    $('.inv').on('blur', 'input', sumtotal);
	function sumtotal() {
	    var s = 0;
        var z = 0;
		$('.inv input').each(function () {
           s = parseFloat($('#q60 input').val());
           z = parseFloat($('#q61 input').val());         
          
		});
		$('#q62 input').val((z/s).toFixed(2));
	}
	function parseNumber(n) {
	    var f = parseFloat(n); //Convert to float number.
		return isNaN(f) ? 0 : f; //treat invalid input as 0;
	}
});

This works to divide two values.  but the value returned is not accurate. 
For example if I did values:   1452 / 11060 = 0.1312 on a calculator.

The Form returned 0.09 for the result

0 0
replied on March 21, 2016

Oh, so you do not need the average, just the quotient? In which case you don't need a function, the / division operator should be able to be used directly for Forms 10 Calculations.

With regards to the JavaScript, how exactly is your Form set up? When I created a sample form with three fields, #q8 and #q9 given the class .inv and having the JavaScript output to field #q10, the JavaScript you posted indeed returned 0.13 as expected.

0 0
replied on March 21, 2016

#q60, #q60 are both in class 'inv' 

 

 

0 0
SELECTED ANSWER
replied on March 21, 2016 Show version history

Did you mean both #q58 an #q59 are in class 'inv'?

Here is the setup of my sample form in its entirety, with the CSS details displayed. How does yours differ?

EDIT: Changed how image was attached for readability.

2016-03-21_1536.png
1 0
replied on March 24, 2016

I used  $('.inv input').on('blur', sumtotal);   where sumtotal is just the name given to the function correct?  It could be any name?

0 0
replied on March 24, 2016

Hi Chris, correct; it can be any name. I was wondering whether that had been adapted from an existing function.

0 0
replied on March 24, 2016

No I used the sumtotal function as well from examples found.  I'm not sure why the code was returning 0.09.  But after I retyped it, it is returning 0.13.

The joys of programming I guess.  Thanks for the validation on the script though.  Helped a lot.

0 0
replied on March 24, 2016

You're welcome, Chris. Glad it's working now!

0 0
replied on March 24, 2016

Thanks Robert.

2 0
replied on March 21, 2016

Forms 10 Calculator

Average Field    =AVG(Number1,Number2)

Division Field     =Number1/Number2   (Field is set to 2 decimal Places)

Quotient will only return the Integer value, which is why you were getting 1 for the answer.

 

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.