Skip to main content
Known Participant
Oct 5, 2024 3:30 PM UTC
Answered

count if a letter that you find in all months of the year counts

Help! I have to count (A) how many times it occurs in the months (therefore in all the days of the year)
I add a code:
var total = 0;
for (var i=1; i<=12; i++) {
for (var j=1; j<=31; j++) {
var f = this.getField("Day"+i+"_"+j);
if (f.valueAsString =="A") total++;

}
}
event.value = total;

This topic is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
Oct 5, 2024 7:28 PM UTC

If I want to color text A in red, what can I do?


Change this:

if (f.valueAsString =="A") total++;

To:

if (f.valueAsString =="A") {
	total++;
	f.textColor = color.red;
} else f.textColor = color.black;

9 replies

try67
Community Expert
Community Expert
Oct 5, 2024 3:34 PM UTC

Since there aren't 31 days in each month, you should probably include code that skips missing fields (unless you have created those fields and they are always empty). Beside that, the code seems fine. What happens when you use it?

Translate
Known Participant
Oct 5, 2024 3:55 PM UTC

the months ending on the 30th are left blank.. the problem is that

for (var i=1; i<=12; i++)

doesn't count for me, by checking month by month it reaches July and then it no longer counts as never? Thank you

Translate
try67
Community Expert
Community Expert
Oct 5, 2024 4:07 PM UTC

Check the JS Console for error messages.

Translate