• Resolved lowercas3

    (@lowercas3)


    Hello is there any way to accept a sequence of numbers in a field

    1, 2, 3, 4

    The sequence can be any length.

    and then calculate an average? (1+2+3+4)/4

    Thank you very much.

    • This topic was modified 2 years, 3 months ago by lowercas3.
Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author codepeople

    (@codepeople)

    Hello @lowercas3

    Thank you very much for using our plugin.

    You can insert a calculated field in the form and enter the following equation:

    AVERAGE(1,2,3,4);

    Best regards.

    Thread Starter lowercas3

    (@lowercas3)

    Sorry I should have clarified. How do I create a field to accept the user input as a sequence of numbers separated by commas?

    Plugin Author codepeople

    (@codepeople)

    Hello @lowercas3

    In this case, you should use a “Single Line Text” instead of a number field because the user will enter values that are not specific numbers. However, in this case, you should transform the information entered by the user into a numbers array.

    For example, assuming the “Single Line Text” is the fieldname123, the equation in the calculated field would be:

    AVERAGE(fieldname123|r.replace(/[^\.\d\,]/g,'').split(','))

    The field’s name with the |r modifier allows accessing the raw field’s value.

    Best regards.

    Thread Starter lowercas3

    (@lowercas3)

    Thank you. This works well. Let’s say I want to take the average of the square of each of the values. How would that work?

    (fieldname123|r.replace(/[^\.\d\,]/g,”).split(‘,’)) is the array of numbers – so I’d like to apply a square operation on each element of the array and then take the average.

    Plugin Author codepeople

    (@codepeople)

    Hello @lowercas3

    The SUM operation accepts a callback function as the last parameter. So, you can implement the equation as follows:

    (function(){
    var list = fieldname123|r.replace(/[^\.\d\,]/g,'').split(','),
        count = list.length;
    return SUM(list, function(x){return POW(x,2);})/count;
    })()

    Learn more about the available operations by visiting the following link:

    https://cff.dwbooster.com/documentation#mathematical-module

    Best regards.

    Thread Starter lowercas3

    (@lowercas3)

    Hello,

    I tried this code and it doesn’t appear to recognize negative numbers in the input list as being different from positive numbers. So 10, -10, 10 will give the same result as 10, 10, 10. Any idea what I might be doing wrong? thanks.

    (function(){
    var list = fieldname15|r.replace(/[^\.\d\,]/g,'').split(','),
        count = list.length;
    return SUM(list, function(x){return x;})/count;
    })()
    • This reply was modified 2 years, 3 months ago by lowercas3.
    Plugin Author codepeople

    (@codepeople)

    Hello @lowercas3

    Please, modify the regular expression as follows: /[^\-\.\d\,]/g

    Best regards.

    Thread Starter lowercas3

    (@lowercas3)

    Hello, thank you this works. The only issue is when I reduce the series to one number. It doesn’t provide a result. Any idea how we can accommodate this case?

    Plugin Author codepeople

    (@codepeople)

    Hello @lowercas3

    In this case, you must ensure the value is treated as a text and not a number before applying the replace method:

    (function(){
    var list = (fieldname15|r+'').replace(/[^\.\d\,]/g,'').split(','),
        count = list.length;
    return SUM(list)/count;
    })()

    Best regards.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Sum of a series of numbers’ is closed to new replies.