CMS Development

Rich_Bibblio
Contributor

HubL: Output average number from array

SOLVE

Hi all,

A HubL puzzle:
Take a long integer, put each digit into an array, output the average.
E.g. input is 6314095485.
Array becomes [6, 3, 1, 4, 0, 9, 5, 4, 8, 5]
Average = 4.5

How is this written in HubL?

Many thanks!
Rich

0 Upvotes
1 Accepted solution
tjoyce
Solution
Recognized Expert | Elite Partner
Recognized Expert | Elite Partner

HubL: Output average number from array

SOLVE

@Rich_Bibblio

//starting number
{% set num = 6314095485 %}
//convert it to string {% set numAsString = num+"" %}
//convert the string to a list and add each number {% set numSum = numAsString|list|sum %}
//sanity check; get the length of the list {% set numCount = numAsString|list|length %}
//division; use the float filter to keep from rounding up/down to integer {{(numSum|float / numCount)}}

If this answer helped, please, mark as solved 😄


tim@belch.io | forms.belch.io | Design your own Beautiful HubSpot Forms; No coding necessary.

 

Drop by and say Hi to me on slack.

View solution in original post

2 Replies 2
tjoyce
Solution
Recognized Expert | Elite Partner
Recognized Expert | Elite Partner

HubL: Output average number from array

SOLVE

@Rich_Bibblio

//starting number
{% set num = 6314095485 %}
//convert it to string {% set numAsString = num+"" %}
//convert the string to a list and add each number {% set numSum = numAsString|list|sum %}
//sanity check; get the length of the list {% set numCount = numAsString|list|length %}
//division; use the float filter to keep from rounding up/down to integer {{(numSum|float / numCount)}}

If this answer helped, please, mark as solved 😄


tim@belch.io | forms.belch.io | Design your own Beautiful HubSpot Forms; No coding necessary.

 

Drop by and say Hi to me on slack.

Rich_Bibblio
Contributor

HubL: Output average number from array

SOLVE

Hi Tim,

 

This is perfect in every way. Thanks so much!

 

Rich