So, I have this code (courtesy of @jmooring):
{{ $prevFirstDoseTotal := 0 }}
{{ $prevSecondDoseTotal := 0 }}
{{ $prevDosesByIsland := "" }}
{{ with .NextInSection }}
{{ $prevDosesByIsland = .Params.update.doses.byIsland }}
{{end}}
<tbody>
{{ range $i, $e := .Params.update.doses.byIsland }}
{{ $prevFirstDose := 0 }}
{{ $prevSecondDose := 0 }}
{{ $first := .island.first }}
{{ $second := .island.second }}
{{ $newFirstDose := 0 }}
{{ $newSecondDose := 0 }}
{{ $islandName := .island.name }}
{{ with $prevDosesByIsland }}
{{ $prevFirstDose = (index (where . "island.name" $islandName) 0).island.first }}
{{ $prevSecondDose = (index (where . "island.name" $islandName) 0).island.second }}
{{ $newFirstDose = add $newFirstDose ( sub $first $prevFirstDose ) }}
{{ $newSecondDose = add $newSecondDose (sub $second $prevSecondDose ) }}
{{ $prevSecondDoseTotal = add $prevSecondDoseTotal $newSecondDose }}
{{ $prevFirstDoseTotal = add $prevFirstDoseTotal $newFirstDose }}
{{end}}
<tr>
<th class="[ column--head text--left ]">{{ .island.name }}</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>
</tr>
{{ $firstTotal = add $firstTotal .island.first }}
{{ $secondTotal = add $secondTotal .island.second }}
{{ $byIsalndTotal := 0}}
{{ $byIslandTotal = add $byIslandTotal ( add .island.first .island.second ) }}
{{ end }}
</tbody>
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.