This topic is locked
[SOLVED]

 Calculate average on add page

1/4/2021 1:32:06 PM
PHPRunner General questions
L
luchoadmin author

Hello dear, I am writing to you this time, since I do not know how to calculate the average in the add page in phprunner. for now I solve it as follows, but not calculate the average if you don't fill in all the fields. or it gives me wrong.

I hope you can help me..
var ctrlPrice = Runner.getControl(pageid, 'Prueba');

var ctrlPrice1 = Runner.getControl(pageid, 'Prueba1');

var ctrlPrice2 = Runner.getControl(pageid, 'Prueba2');

var ctrlPrice3 = Runner.getControl(pageid, 'Prueba3');

var ctrlPrice4 = Runner.getControl(pageid, 'Prueba4');

;

var ctrlTotal = Runner.getControl(pageid, 'Promedio');
function func() {

ctrlTotal.setValue((Number(ctrlPrice.getValue()) + Number(ctrlPrice1.getValue()) + Number(ctrlPrice2.getValue()) + Number(ctrlPrice3.getValue()) + Number(ctrlPrice4.getValue()) )/5) ;

};
ctrlPrice.on('onchange', func);

ctrlPrice1.on('onchange', func);

ctrlPrice2.on('onchange', func);

ctrlPrice3.on('onchange', func);

ctrlPrice4.on('onchange', func);

ctrlTotal.on('onchange', func);
Thank You

H
Hd_Mersintarim 1/4/2021

on edit as properties

make 0 for all your calculate fields value

L
luchoadmin author 1/5/2021



on edit as properties

make 0 for all your calculate fields value


Yes, I understand but keep dividing by 5 and not by the number of loaded numbers

Thnk you

Sergey Kornilov admin 1/5/2021

In your code you always delete the amount by 5 so it is not surprising. I would suggest implementing your logic on paper first, it will be easier to understand the logic this.
I think you need to count the number of elements that are not 0 and divide it by this number. So if your have five numbers like n1, n2, n3, n4 and n5 here is the sample code:

var avg = (n1 + n2 + n3 + n4 + n5) / ( (n1==0? 0:1) + (n2==0? 0:1) + (n3==0? 0:1) + (n4==0? 0:1) + (n5==0? 0:1));


We are making the use of ternary operator here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

L
luchoadmin author 1/8/2021



In your code you always delete the amount by 5 so it is not surprising. I would suggest implementing your logic on paper first, it will be easier to understand the logic this.
I think you need to count the number of elements that are not 0 and divide it by this number. So if your have five numbers like n1, n2, n3, n4 and n5 here is the sample code:

var avg = (n1 + n2 + n3 + n4 + n5) / ( (n1==0? 0:1) + (n2==0? 0:1) + (n3==0? 0:1) + (n4==0? 0:1) + (n5==0? 0:1));


We are making the use of ternary operator here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator


Thank You!

var ctrlPrice = Runner.getControl(pageid, 'Prueba');

var ctrlPrice1 = Runner.getControl(pageid, 'Prueba1');

var ctrlPrice2 = Runner.getControl(pageid, 'Prueba2');

var ctrlPrice3 = Runner.getControl(pageid, 'Prueba3');

var ctrlPrice4 = Runner.getControl(pageid, 'Prueba4');
var ctrlTotal = Runner.getControl(pageid, 'Prueba6');

var ctrlTotal1 = Runner.getControl(pageid, 'Prueba7');
var ctrlTotal2 = Runner.getControl(pageid, 'Prueba5');
function func() {

ctrlTotal.setValue(Number(ctrlPrice.getValue()) + Number(ctrlPrice1.getValue()) + Number(ctrlPrice2.getValue()) + Number(ctrlPrice3.getValue()) + Number(ctrlPrice4.getValue()));
ctrlTotal1.setValue((Number(ctrlPrice.getValue()==0? 0:1)) + (Number(ctrlPrice1.getValue()==0? 0:1)) + (Number(ctrlPrice2.getValue()==0? 0:1)) + (Number(ctrlPrice3.getValue()==0? 0:1)) + (Number(ctrlPrice4.getValue()==0? 0:1))) ;
ctrlTotal2.setValue(Number(ctrlTotal.getValue()) / Number(ctrlTotal1.getValue()));