rob.m
rob.m

Reputation: 10571

How to get the total of given values in a loop?

I'm trying to read an external API which gives the following definitions for each historical date:

state - State or territory postal code abbreviation.
positive - Total cumulative positive test results.
positiveIncrease - Increase from the day before.
negative - Total cumulative negative test results.
negativeIncrease - Increase from the day before.
pending - Tests that have been submitted to a lab but no results have been reported yet.
totalTestResults - Calculated value (positive + negative) of total test results.
totalTestResultsIncrease - Increase from the day before.
hospitalized - Total cumulative number of people hospitalized.
hospitalizedIncrease - Increase from the day before.
death - Total cumulative number of people that have died.
deathIncrease - Increase from the day before.
dateChecked - ISO 8601 date of the time we saved visited their website
hospitalizedCurrently - Number of individuals currently hospitalized.
hospitalizedCumulative - Total number of individuals that have been hospitalized, including those that have been discharged.
inIcuCurrently - Number of individuals currently in an ICU.
inIcuCumulative - Total number of individuals that have been in the ICU.
onVentilatorCurrently - Number of individuals currently on a ventilator.
onVentilatorCumulative - Total number of individuals that have been on a ventilator.
recovered - Total number of individuals that have tested negative after a previous positive test.
total - DEPRECATED Will be removed in the future. (positive + negative + pending). Pending has been an unstable value and should not count in any totals.

Now I am looping all dates and I am trying to know the total for a given time period, so let's say we want to have the total from date 1 until the last date, I do:

function updateUI(filtererdData, dateStart, dateEnd, firstRunMap) {

    // new state
    deaths = 0;
    hospitalized = 0;
    recovered = 0;
    positive = 0;
    hospitalizedCumulative = 0;
    inIcuCumulative = 0;
    onVentilatorCumulative = 0;
    pending = 0;
    negative = 0;
    tests = 0;
    hospitalisedNow = 0;
    intensiveCareNow = 0;
    onVentilatorNow = 0;
    name = 0;
    cases = 0;
    var totalDeaths = 0;

    // filtererdData has now STATE data, and not only 1 state

    filtererdData.forEach(state => {

        var stateName = state.name;

        // "state.data" will have an array of all dates in between
        state.data.forEach(stateDataPerDay => {
            if(stateDataPerDay.death) deaths += stateDataPerDay.deathIncrease;
            if(stateDataPerDay.hospitalized) hospitalized += stateDataPerDay.hospitalizedIncrease;
            if(stateDataPerDay.recovered) recovered += stateDataPerDay.recovered;
            if(stateDataPerDay.positive) positive += stateDataPerDay.positiveIncrease;
            if(stateDataPerDay.negative) negative += stateDataPerDay.negativeIncrease;
            if(stateDataPerDay.totalTestResults) tests += stateDataPerDay.totalTestResultsIncrease;
            if(stateDataPerDay.hospitalizedCumulative) hospitalisedNow += stateDataPerDay.hospitalizedCurrently;
            if(stateDataPerDay.inIcuCumulative) intensiveCareNow += stateDataPerDay.inIcuCurrently;
            if(stateDataPerDay.onVentilatorCumulative) onVentilatorNow += stateDataPerDay.onVentilatorCurrently;
            cases += positive + negative;
            // WRONG
            if(stateDataPerDay.length - 1) {
                console.log(stateDataPerDay.death);
            }
        });

        // end each state data to save

        // let's append the total to each state as well
        // deaths & hospitalized are the sum of all selected days
        state.totalDeaths = deaths;
        state.totalHospitalized = hospitalized;
        state.totalRecovered = recovered;
        state.totalPositive = positive;

But I am doing this wrong, the totals I am having are just wrong. I am confused if to have the death totals I should be using .death or .deathIncrease and how this should be added within the loop to use that total value later on in the code. And do this for deaths, recovered, tests.

Another question in regards of this data is: How do I know the total cases? Is it positive + negative? And if so, how would I do the total for each state to use it later?

For some of the data I don't want to display its total as I will show it in a graph trend but for deaths, recovered, cases and tests I do.

Upvotes: 0

Views: 45

Answers (1)

Peter S
Peter S

Reputation: 897

Since you never reset the counter for each state, the totals will be off like you said. Move these variables inside the loop:

filteredData.forEach(state => {
    deaths = 0;
    hospitalized = 0;
    recovered = 0;
    positive = 0;
    pending = 0;
    negative = 0;
    tests = 0;
    hospitalisedNow = 0;
    intensiveCareNow = 0;
    onVentilatorNow = 0;
    name = 0;
    cases = 0;
    ...
}

Each state would need a seperate variable.

Upvotes: 1

Related Questions