HubL: Output average number from array

SOLVE
Occasional Contributor

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

Reply
0 Upvotes
1 Accepted solution

Accepted Solutions
Highlighted
Regular Advisor | Gold Partner | HubSpot Certified Trainer

@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 Smiley Very Happy


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

 

Drop by and say Hi to me on slack.

2 Replies
Highlighted
Regular Advisor | Gold Partner | HubSpot Certified Trainer

@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 Smiley Very Happy


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

 

Drop by and say Hi to me on slack.

Occasional Contributor

Hi Tim,

 

This is perfect in every way. Thanks so much!

 

Rich