Calculating the number of comments

There are several JavaScript examples to count comments in the Community. You can start with those and then improve.

In any case, please read the documentation on how to define and validate the Javascript code before using it (those would be the first two paragraphs): JavaScript calculated custom fields.

There are Community posts on similar use cases:

Best,
Zane / support@eazyBI.com