Code works great, except one file

So, I have this code (courtesy of @jmooring):

{{ $prevFirstDoseTotal := 0 }}
        {{ $prevSecondDoseTotal := 0 }}
        {{ $prevDosesByIsland := "" }}
        {{ with .NextInSection }}
          {{ $prevDosesByIsland = .Params.update.doses.byIsland }}
          {{ range $i, $e := .Params.update.doses.byIsland }}
          {{ $prevFirstDose := 0 }}
          {{ $prevSecondDose := 0 }}
          {{ $first := .island.first }}
          {{ $second := .island.second }}
          {{ $newFirstDose := 0 }}
          {{ $newSecondDose := 0 }}
          {{ $islandName := }}
          {{ with $prevDosesByIsland }}
            {{ $prevFirstDose = (index (where . "" $islandName) 0).island.first }}
            {{ $prevSecondDose = (index (where . "" $islandName) 0).island.second }}
            {{ $newFirstDose = add $newFirstDose ( sub $first $prevFirstDose ) }}
            {{ $newSecondDose = add $newSecondDose (sub $second $prevSecondDose ) }}
            {{ $prevSecondDoseTotal = add $prevSecondDoseTotal $newSecondDose }}
            {{ $prevFirstDoseTotal = add $prevFirstDoseTotal $newFirstDose }}
            <th class="[ column--head text--left ]">{{ }}</th>
            <!-- First -->
            <td class="[ text--center ]">{{if ne $newFirstDose 0}}{{ $newFirstDose }}{{else}}—{{end}}</td>
            <td class="[ text--center ]">{{ .island.first }}</td>
            <!-- Second -->
            <td class="[ text--center ]">{{ if ne $newSecondDose 0 }}{{ $newSecondDose }}{{else}}—{{end}}</td>
            <td class="[ text--center ]">{{if ne .island.second 0 }}{{ .island.second }}{{else}}—{{end}}</td>
            <!-- Total -->
            <td class="[ text--center ]">{{ add .island.first .island.second }}</td>
          {{ $firstTotal = add $firstTotal .island.first }}
          {{ $secondTotal = add $secondTotal .island.second }}
          {{ $byIsalndTotal := 0}}
          {{ $byIslandTotal = add $byIslandTotal ( add .island.first .island.second ) }}
          {{ end }}

Basically, it allows for calculations between the current variables and variables from .NextInSection and works great… except for ONE instance for $newFirstDose!!! Here’s the results: COVID-19 Vaccination Update 210910 | COVID-19 API | Bahamas

You can check the update before and after, they’re great… just this ONE!!! Why is it doing this? I thought it was the content file, but seems fine, it doesn’t seem to account for the large number.

The codebase is here: covid-api-bahamas/byIsland-full.html at main · jsphpndr/covid-api-bahamas · GitHub

It seems to be pulling the right figures if you get the listing from $prevFirstDoes, but the calculations are incorrect. If the listing is correct, then it should follow, your calculations should render correctly.

I can’t figure out where the large number is coming from either, because it’s not a result of the numbers, either by subtraction or addition.

Really, weird.


Which number on updates/210910/ is wrong?

Hey @jmooring,

The First > New or $newFirstDose.

If you look at the update before and after you will see the calculations work out fine, just not this one file.

83644(current update) - 79744(previous update) = 3900, but I’m getting a return of 49210. The error persists throughout the range.

What is odd, is that figures in the update after and before are correct, but not this one. I thought it might be the content file, but nothing seems amiss.

I also adjust the calculations and got a semi-right answer, but that offset all the others.

The issue before was some content files were missing the weight, so that caused errors, but now that it’s sorted, I can’t account for this weird occurrence.

Fun Fact: The figures for fully vaccinated > new or $newFullyDose are correct, IN the SAME file.

Ok. Got this solved. This was actually, just a calculation error on my part.

I couldn’t account for the figures because all the other calculations were correct and I had no point of reference. Took a break and then came back at the code… and boom — clear as day. :man_facepalming:t4:

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.