# Sum of numbers in array

**URL:** <https://community.glideapps.com/t/sum-of-numbers-in-array/58143>\
**Category:** Ask for Help\
**Created:** [February 19, 2023, 12:45am UTC](https://community.glideapps.com/t/sum-of-numbers-in-array/58143 "2023-02-19T00:45:04Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hassan\_Nadeem](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/hassan_nadeem/32/72956_2.png) [@Hassan\_Nadeem](https://community.glideapps.com/u/Hassan_Nadeem)\
**Post date:** [February 19, 2023, 12:45am UTC](https://community.glideapps.com/t/sum-of-numbers-in-array/58143/1 "2023-02-19T00:45:04Z")

</div>

Can I do a sum of numbers in an Array (split text) column? The roll up is counting items in array, not summing it. I found a almost 18 months old thread but couldn’t find the solution.

 ![Screenshot 2023-02-19 053842](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/9/e/9e07a65fd54c79879c9bd36bf0baa77a28eb822d.png)

---

<div class="post-metadata">

**Author:** ![ThinhDinh](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thinhdinh/32/49_2.png) [@ThinhDinh](https://community.glideapps.com/u/ThinhDinh)\
**Post date:** [February 19, 2023, 1:12am UTC](https://community.glideapps.com/t/sum-of-numbers-in-array/58143/2 "2023-02-19T01:12:28Z")

</div>

I assume the rollup column automatically reads the content of each array element from a split text column as text, so you wouldn’t be able to “sum” it.

How did you generate the “FirstNameScore” column?

---

<div class="post-metadata">

**Author:** ![Hassan\_Nadeem](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/hassan_nadeem/32/72956_2.png) [@Hassan\_Nadeem](https://community.glideapps.com/u/Hassan_Nadeem)\
**Post date:** [February 19, 2023, 1:17am UTC](https://community.glideapps.com/t/sum-of-numbers-in-array/58143/3 "2023-02-19T01:17:25Z")

</div>

> [@Need help with writing a complicated google sheet formula on Glide tables](https://community.glideapps.com/t/need-help-with-writing-a-complicated-google-sheet-formula-on-glide-tables/57918/11):
>
> sure… var x = [['A',1],['B',2],['C',3],['D',4],['E',5],['F',6],['G',7],['H',8],['I',9],['J',1],['K',2],['L',3],['M',4],['N',5],['O',6],['P',7],['Q',8],['R',9],['S',1],['T',2],['U',3],['V',4],['W',5],['X',6],['Y',7],['Z',8]]; var text = p1.toUpperCase(); var res=0; for(var j=0;j\<text.length;++j){ for(var i=0;i\<x.length;++i){if(text.slice(j,j+1)==x[i][0]){res=res+x[i][1]}}} return(res)

@ThinhDinh FirstnameScore column is computed using the JavaScript Uzo wrote

---

<div class="post-metadata">

**Author:** ![Uzo](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/uzo/32/22639_2.png) [@Uzo](https://community.glideapps.com/u/Uzo)\
**Post date:** [February 19, 2023, 1:33am UTC](https://community.glideapps.com/t/sum-of-numbers-in-array/58143/4 "2023-02-19T01:33:23Z")

</div>

Why do you need that score to be split?

---

<div class="post-metadata">

**Author:** ![Hassan\_Nadeem](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/hassan_nadeem/32/72956_2.png) [@Hassan\_Nadeem](https://community.glideapps.com/u/Hassan_Nadeem)\
**Post date:** [February 19, 2023, 1:35am UTC](https://community.glideapps.com/t/sum-of-numbers-in-array/58143/5 "2023-02-19T01:35:27Z")

</div>

It’s part of the logic behind my game. 😄

---

<div class="post-metadata">

**Author:** ![Uzo](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/uzo/32/22639_2.png) [@Uzo](https://community.glideapps.com/u/Uzo)\
**Post date:** [February 19, 2023, 1:36am UTC](https://community.glideapps.com/t/sum-of-numbers-in-array/58143/6 "2023-02-19T01:36:22Z")

</div>

divide by 10 and take floor

---

<div class="post-metadata">

**Author:** ![Hassan\_Nadeem](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/hassan_nadeem/32/72956_2.png) [@Hassan\_Nadeem](https://community.glideapps.com/u/Hassan_Nadeem)\
**Post date:** [February 19, 2023, 1:38am UTC](https://community.glideapps.com/t/sum-of-numbers-in-array/58143/7 "2023-02-19T01:38:34Z")

</div>

How do I write this in Math column? Is it going to give me the sum in array?

Just for the reference here is the original formula used in google spreadsheet to calculate this value. Can’t figure out how to rewrite this in Glide.

=ARRAY\_CONSTRAIN(ARRAYFORMULA(IF(IF(LEN(C5)\>1, SUM(IFERROR(MID(C5,ROW(INDIRECT(“1:”&LEN(C5))),1)+0)), C5) = C5, 0, IF(LEN(C5)\>1, SUM(IFERROR(MID(C5,ROW(INDIRECT(“1:”&LEN(C5))),1)+0)), C5))), 1, 1)

---

<div class="post-metadata">

**Author:** ![Uzo](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/uzo/32/22639_2.png) [@Uzo](https://community.glideapps.com/u/Uzo)\
**Post date:** [February 19, 2023, 1:39am UTC](https://community.glideapps.com/t/sum-of-numbers-in-array/58143/8 "2023-02-19T01:39:38Z")

</div>

tell me what you need…

---

<div class="post-metadata">

**Author:** ![ThinhDinh](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thinhdinh/32/49_2.png) [@ThinhDinh](https://community.glideapps.com/u/ThinhDinh)\
**Post date:** [February 19, 2023, 1:41am UTC](https://community.glideapps.com/t/sum-of-numbers-in-array/58143/9 "2023-02-19T01:41:09Z")

</div>

Circling back to the solution there first. I would say you can do it like this:

- Convert the original string to all lowercase
- Split text on the lowercase string
- Use a unique array column to get only unique letters
- Use a relation + rollup combo to get the total

---

<div class="post-metadata">

**Author:** ![Hassan\_Nadeem](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/hassan_nadeem/32/72956_2.png) [@Hassan\_Nadeem](https://community.glideapps.com/u/Hassan_Nadeem)\
**Post date:** [February 19, 2023, 1:41am UTC](https://community.glideapps.com/t/sum-of-numbers-in-array/58143/10 "2023-02-19T01:41:51Z")

</div>

Through your javascript I calculated the score using the letters.

Now the score for SanFrancisco came out to be 50 right?

My next logic is to add all digits of the score. In this case I have to do a sum of 5 + 0.

---

<div class="post-metadata">

**Author:** ![Uzo](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/uzo/32/22639_2.png) [@Uzo](https://community.glideapps.com/u/Uzo)\
**Post date:** [February 19, 2023, 1:42am UTC](https://community.glideapps.com/t/sum-of-numbers-in-array/58143/11 "2023-02-19T01:42:46Z")

</div>

what is the highest possible score?

---

<div class="post-metadata">

**Author:** ![Hassan\_Nadeem](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/hassan_nadeem/32/72956_2.png) [@Hassan\_Nadeem](https://community.glideapps.com/u/Hassan_Nadeem)\
**Post date:** [February 19, 2023, 1:43am UTC](https://community.glideapps.com/t/sum-of-numbers-in-array/58143/12 "2023-02-19T01:43:10Z")

</div>

Already tried that but it does not give me the total of letters that are used twice. For example if a word is apple then your method will give me a score of just “A” “P” “L” “E” and not the other “P”.

---

<div class="post-metadata">

**Author:** ![Hassan\_Nadeem](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/hassan_nadeem/32/72956_2.png) [@Hassan\_Nadeem](https://community.glideapps.com/u/Hassan_Nadeem)\
**Post date:** [February 19, 2023, 1:43am UTC](https://community.glideapps.com/t/sum-of-numbers-in-array/58143/13 "2023-02-19T01:43:38Z")

</div>

No limit I believe. You can refer the google sheet formula I attached. It works perfectly in the sheet.

 ![Screenshot 2023-02-19 064458](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/7/f/7f3d75193a32f3cb3943da676502441cf4d0c7dc.png)

Is there anyway I can join these columns like this and put it in a math column? Tried but didn’t work though.

---

<div class="post-metadata">

**Author:** ![Uzo](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/uzo/32/22639_2.png) [@Uzo](https://community.glideapps.com/u/Uzo)\
**Post date:** [February 19, 2023, 1:53am UTC](https://community.glideapps.com/t/sum-of-numbers-in-array/58143/14 "2023-02-19T01:53:08Z")

</div>

```auto
var n = p1, remainder, sumOfDigits = 0;
while(n)
{
    remainder = n % 10;
    sumOfDigits = sumOfDigits + remainder;
    n = Math.floor(n/10);
}
return (sumOfDigits)

```

put as p1 result from my first script

---

<div class="post-metadata">

**Author:** ![Hassan\_Nadeem](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/hassan_nadeem/32/72956_2.png) [@Hassan\_Nadeem](https://community.glideapps.com/u/Hassan_Nadeem)\
**Post date:** [February 19, 2023, 1:54am UTC](https://community.glideapps.com/t/sum-of-numbers-in-array/58143/15 "2023-02-19T01:54:12Z")

</div>

You are a genius. 🐐

---

<div class="post-metadata">

**Author:** ![Uzo](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/uzo/32/22639_2.png) [@Uzo](https://community.glideapps.com/u/Uzo)\
**Post date:** [February 19, 2023, 2:07am UTC](https://community.glideapps.com/t/sum-of-numbers-in-array/58143/16 "2023-02-19T02:07:23Z")

</div>

Next time explain your final goal for the problem. It is easier to give the right solution 😉

---

<div class="post-metadata">

**Author:** ![Hassan\_Nadeem](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/hassan_nadeem/32/72956_2.png) [@Hassan\_Nadeem](https://community.glideapps.com/u/Hassan_Nadeem)\
**Post date:** [February 19, 2023, 9:28pm UTC](https://community.glideapps.com/t/sum-of-numbers-in-array/58143/17 "2023-02-19T21:28:12Z")

</div>

> [@Uzo](#):
>
> ```auto
> var n = p1, remainder, sumOfDigits = 0;
> while(n)
> {
> remainder = n % 10;
> sumOfDigits = sumOfDigits + remainder;
> n = Math.floor(n/10);
> }
> return (sumOfDigits)
> 
> ```

@Uzo Can we change this JS in a way where if my number is a SINGLE DIGIT for example 9, then the answer should return as 0? I think we just need to add a IF ELSE statement on the script.

---

<div class="post-metadata">

**Author:** ![Uzo](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/uzo/32/22639_2.png) [@Uzo](https://community.glideapps.com/u/Uzo)\
**Post date:** [February 19, 2023, 9:35pm UTC](https://community.glideapps.com/t/sum-of-numbers-in-array/58143/18 "2023-02-19T21:35:45Z")

</div>

Try it. It is really easy to add that. I don’t think you need my help with that 😉

---

<div class="post-metadata">

**Author:** ![Hassan\_Nadeem](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/hassan_nadeem/32/72956_2.png) [@Hassan\_Nadeem](https://community.glideapps.com/u/Hassan_Nadeem)\
**Post date:** [February 19, 2023, 9:41pm UTC](https://community.glideapps.com/t/sum-of-numbers-in-array/58143/19 "2023-02-19T21:41:39Z")

</div>

Hahaha honestly I don’t. Without you, I will create Glides native IF ELSE column, and will end up creating so many of them.

One single add-on on your script will save my hours

---

<div class="post-metadata">

**Author:** ![Uzo](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/uzo/32/22639_2.png) [@Uzo](https://community.glideapps.com/u/Uzo)\
**Post date:** [February 19, 2023, 9:41pm UTC](https://community.glideapps.com/t/sum-of-numbers-in-array/58143/20 "2023-02-19T21:41:58Z")

</div>

if(p1\<10){return(0)};  
var n = p1, remainder, sumOfDigits = 0;  
while(n)  
{  
remainder = n % 10;  
sumOfDigits = sumOfDigits + remainder;  
n = Math.floor(n/10);  
}  
return (sumOfDigits)

[Next page](https://community.glideapps.com/t/sum-of-numbers-in-array/58143.md?page=2)
