Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9149339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:27:49+00:00 2026-06-17T11:27:49+00:00

I am declaring a global variable, nrOfFoundIncidents that gets iterated inside a function every

  • 0

I am declaring a global variable, nrOfFoundIncidents that gets iterated inside a function every time an “incident” is found. However, after the function is executed the variable is empty again, even though it’s declared outside the function.

Why is that and what can I do about it?

var INCIDENT_MATCHES = {
    trafficAccidents: /(traffic|car) accident|/
    robberies: /...
    ...you get it.
};

// theIncidents[0] = "There was a robbery on the 52th street last nigth...";
// theIncidents[1] = "Two days ago a car crashed into a house...";
// theIncidents[2] = "One person got stabbed outside his home...";
... and so on...

var nrOfFoundIncidents = 0;

function FindIncidents(incidentReports) {
    var incidentCounts = {};
    var incidentTypes = Object.keys(INCIDENT_MATCHES);
    incidentReports.forEach(function(incident) {
        incidentTypes.forEach(function(type) {
            if(typeof incidentCounts[type] === 'undefined') {
                incidentCounts[type] = 0;
            }
            var matchFound = incident.match(INCIDENT_MATCHES[type]);
            if(matchFound){
                var matchFound = incident.match(INCIDENT_MATCHES[type]);
                nrOfFoundIncidents += 1;
                console.log(nrOfFoundIncidents);    // 1, 2, 3, 4, 5, 6, 7...
            }
        });
     });

     return incidentCounts;    // <- returns as it supposed
}

var objectOfIncidents = FindIncidents(theIncidents); <-- as an argument an object containing of categories with reg exp to find them in the text that is searched is provied.

console.log(nrOfFoundIncidents); // <--- 0

EDIT: I updated the function with the rest of the code because of the risk to leave relevant information out.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:27:50+00:00Added an answer on June 17, 2026 at 11:27 am

    Your code actually works exactly as you expect it to. See this fiddle for proof.

    http://jsfiddle.net/vxbRV/

    It logs:

    found a match 1
    found a match 2
    found a match 3
    after code runs: 3 
    

    Which proves the variable is being incremented as you expect it to be.

    Whatever problem you have is not directly part of this posted code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Declaring a global variable inside a function Is it possible to declare
Which is the right way of declaring a global javascript variable? The way I'm
In C, declaring a variable static in the global scope makes it a global
In an .h file, I am declaring a global variable as: #pragma data_seg(.shared) #ifndef
I'm declaring a global variable in my program: double mat[3200][3200]; when passing this array
I am declaring a global variable for String selectedImagePath. On button click I am
Possible Duplicate: declaring a global dynamic variable in python >>> def f(): global cat
I have a int that resets itself every time the view re-opens/leaves. I have
What is the syntax for declaring a global 2-dimensional array in MATLAB? I want
I'm declaring a varchar variable in T-SQL declare @var varchar(max); I have a select

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.