It is programmed to individually increase each time it encounters a matching record

for(var i= 0; i < foundRecords.length ; i++){
  var MaleChildNew=0,
      MaleNew=0, 
      femaleChildNew=0, 
      femaleNew=0, 
      policeMaleChildNew = 0, 
      policefemaleChildNew=0, 
      policeMaleNew=0, 
      policefemaleNew=0, 
      npoliceMaleChildNew=0, 
      npoliceMaleNew=0, 
      npolicefemaleChildNew=0, 
      npolicefemaleNew=0;
      if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
      policeMaleChildNew++;
      }else if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
      policefemaleChildNew++;
      }else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
      policeMaleNew++;
      }else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
      policefemaleNew++;
      } if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
                    npoliceMaleChildNew++;
                }else if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
                    npolicefemaleChildNew++;
                }else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
                    npoliceMaleNew++;
                }else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
                    npolicefemaleNew++;
                } if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
                    MaleChildNew++;
                }else if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
                    femaleChildNew++;
                }else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
                    MaleNew++;
                }else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
                    femaleNew++;
                }
            }

The issue here is that it increments the value only for the category found at the end of the database and returns 1. What I need is for it to increase the value for every matching entry encountered and store the total count. However, this code returns 0 for all other variables except one which matches the last DB entry.

Answer №1

To ensure that your variables are not re-initialized to 0 for each item in the foundRecords array, it's advised to declare them outside of the for loop:

var totalMaleChild=0,
    totalMale=0, 
    totalFemaleChild=0, 
    totalFemale=0, 
    policeTotalMaleChild = 0, 
    policeTotalFemaleChild=0, 
    policeTotalMale=0, 
    policeTotalFemale=0, 
    npoliceTotalMaleChild=0, 
    npoliceTotalMale=0, 
    npoliceTotalFemaleChild=0, 
    npoliceTotalFemale=0;

for(var i= 0; i < foundRecords.length ; i++){
      if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
      policeTotalMaleChild++;
      }else if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
      policeTotalFemaleChild++;
      }else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
      policeTotalMale++;
      }else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
      policeTotalFemale++;
      } if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
          npoliceTotalMaleChild++;
      }else if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
          npoliceTotalFemaleChild++;
      }else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
          npoliceTotalMale++;
      }else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
          npoliceTotalFemale++;
      } if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
          totalMaleChild++;
      }else if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
          totalFemaleChild++;
      }else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
          totalMale++;
      }else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
          totalFemale++;
      }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Adjust the user interface in response to a modification in a directive attribute within AngularJs

I am currently facing challenges with data binding in AngularJs. Within my .html file, I have the following markup that includes a custom directive: <my-directive ng-repeat="i in object" attr-1="{{i.some_variable}}"></my-directive> Important ...

javascript execute code on an element that is created later

I am facing an issue with my current HTML setup and need help solving it. Whenever a button is clicked in the HTML document, a new element is added: <div id='wrapper'> <input id='fileUpload' name='fileUpload' typ ...

A method using JQuery and Javascript to retrieve Highcharts data in JSON format, properly structured, by leveraging Selenium with C# programming

I am currently working on extracting the JSON equivalent of a highchart graph using Selenium and C# but have encountered a few obstacles along the way. To retrieve the JSON data for a specific highchart, follow these steps: Visit the URL Log in using th ...

Exploring the performance impact of using MongoDB Change Streams for profiling purposes

While investigating the performance issues on our Atlas hosted database, we noticed a significant increase in the number of documents being returned in the Atlas profiler. The high count of returned documents is mainly due to the changestream getMore calls ...

Simple guide to maintaining the integrity of an absolute path within a script tag in index.html using Vite

When using Vite, absolute paths in the index.html file are automatically transformed based on the base configuration, which is quite convenient. For example, if my index.html looks like this: <script type="text/javascript" src="/config.j ...

I can't get the base64 image to show up in my ReactJS project

Struggling to showcase base64 data in ReactJS, but it seems to be not working properly. I've been attempting to display base64 in ReactJS without much success. Here's my attempt at doing so... import React, { useContext, useEffect, useState } fr ...

(Discord.JS) Bot failing to send direct message upon user joining the server

When attempting to send a DM message, I am using the following code: bot.on('guildMemberAdd', (member) => { member.send('a'); }); I am experiencing difficulty in understanding why the private message is not being successfully se ...

Sending properties to a Vue 3 composable does not result in reactivity

Having an issue where I am attempting to pass a component prop to a composable, but the computed function within the composable is not being called when the prop changes. import { toRefs } from 'vue'; export default { props: { aProp: Strin ...

Spin a Collada model on its y-axis

I am struggling with a three.js scene that features a 3D Homer Simpson standing on a plane. My goal is to rotate him around his y-axis using the mouse. The code I have put together is almost there, with pieces borrowed from various sources. However, I am ...

"A curious situation arose when using Axios in a Firebase function, where a pending promise was unexpectedly returned even within the confines

My asynchronous problem with async/await has been baffling me. Both my child and HOF functions are declared as async, and I await the returned results before trying to log them. However, all I get is 'pending'. The function hangs for 60s and time ...

Exploring Angular2: A Guide to Interpolating Expressions in Templates

Is it possible to interpolate different types of Javascript expressions? Along with displayed properties like object.property and short expressions such as {{1+1}}, what other valid Javascript expressions can be used for interpolation? ...

Discover how to extract a whole number (and *exclusively* a whole number) using JavaScript

Is it possible to convert a string to a number in a way that only integers produce a valid result? func('17') = 17; func('17.25') = NaN func(' 17') = NaN func('17test') = NaN func('') = NaN func('1e2& ...

Is there a way for me to discover the top trending photos on Instagram today?

Finding information on the Instagram API can be quite challenging due to poor documentation. Is there a method available to discover the most liked Instagram photos by location, such as identifying the top picture liked by Danish users today? Any insight ...

Leverage PHP server variables in JavaScript

I am facing a situation where I have multiple values stored in a database table that need to be used as parameters in Javascript. Instead of using inline scripts, my approach is to create a 'parameters.php' file that retrieves and returns all the ...

Take action right away following a post in jQuery

I'm a beginner at using jQuery, and I have a question about displaying content immediately after making a post request. Currently, my code looks like this: $('#cur_select').on('change', function() { $.post( "getTable.php", { v ...

Ensuring Form Integrity through jQuery Validation

I am struggling to customize the errorClass and validClass using JQuery validation. I believe that by adding the .validate function and setting the parameters, it should work. However, even though the validation message displays correctly, the classes re ...

Is it possible to verify the date range using Jquery in C#?

How can I validate the date range between my fromdatepicker1 and todatepicker2 fields in jQuery? The date format is dd/mm/yy, and the condition is always fromdate < todate. This is my from date textbox: <asp:RequiredFieldValidator ControlToValidate ...

Trigger ng-click after ng-model updates

Is there a way to create an ng-click function that will only execute after the value of the ng-model in a scope variable changes? In my scenario, I have a list of analysis objects within a list called analyses, each with an include boolean attribute used f ...

Is there a way to continuously cycle through multiple images fading in and out at various locations on a webpage using absolute positioning?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head> <title>Harley's Art Gallery</title> <script t ...

Updating a newly added item in an array using MongoDB aggregation techniques

Here are the schemas I am working with: Item = { name: String, price: Number; } Sales = { items: [Item], totalSaleValue: Number, } Clients = { clientName: String, sales: [Sales] } Each time a sale is added or updated, the totalSaleValue fiel ...