Search code examples
javascriptfunctionfor-loopvar

Need to access variable defined in a function. Is nested loop the solution?


I have the following code and need to access the variable "density" and put it in a popup. Density is defined in the mapCities function, but the markers I'm adding and the popups I'm creating are created in a for loop outside of the map cities function resulting in a ReferenceError: density is not defined index.html:151:42. This is error is expected. I just don't know how to access that density variable. Should it be doe with a nested loop?

              function mapCities(units) {
                for (var i = 0; i < cities.length; i++) {
                    var cityName = cities[i];
                    var cityNumPeeps = cityPops[i];
                    var cityZone = cityAreas[i];
                    var cityXY = cityCoords[i];
                    var density = calcPopDensity(cityNumPeeps, cityZone, units); // call calcPopDensity passing population, cityZone and units as arguments

                    console.log(density);
                }
            };

            function calcPopDensity(cityNumPeeps, cityZone, units) {
                if (units == "miles") {
                    return cityNumPeeps / cityZone // calculate population density in miles and return it 


                } else if (units == "km") {
                    return cityNumPeeps / cityZone * 1.60934 // calculate population density in miles and return it 

                }

            };

            for (var i = 0; i < cities.length; i++) {
                var cityName = cities[i];
                var cityNumPeeps = cityPops[i];
                var cityZone = cityAreas[i];
                var cityXY = cityCoords[i];
                var popup = `<b>${cityName}</b><br>
                    <b>population</b>: ${density}<br>`
                L.marker(cityXY).addTo(map) // add marker to map based on cityCoords array and bind to popup
                    .bindPopup(popup);
                console.log(popup)
                //if statement determines units to place in popup text
                if (units == "miles") {
                    popup += `miles`; // add text if if var density returns miles
                } else if (units == "km")
                    popup += `kilometers`; // add text if if var density returns kilometers
                L.marker(cityXY).addTo(map) // add popup again based on if statement
                    .bindPopup(popup);
                console.log(popup)

            }
```

​

Solution

  • Just create a variable for the density local to the for-loop where you need it. The above density variable is out of scope so you won't be able to use it as is.

    for (var i = 0; i < cities.length; i++) {
        var cityName = cities[i];
        var cityNumPeeps = cityPops[i];
        var cityZone = cityAreas[i];
        var cityXY = cityCoords[i];
    
        var density = calcPopDensity(cityNumPeeps, cityZone, units);
    
        var popup = `<b>${cityName}</b><br>
            <b>population</b>: ${density}<br>`
        L.marker(cityXY).addTo(map) // add marker to map based on cityCoords array and bind to popup
            .bindPopup(popup);
        console.log(popup)
        //if statement determines units to place in popup text
        if (units == "miles") {
            popup += `miles`; // add text if if var density returns miles
        } else if (units == "km")
            popup += `kilometers`; // add text if if var density returns kilometers
        L.marker(cityXY).addTo(map) // add popup again based on if statement
            .bindPopup(popup);
        console.log(popup)
    }