Q:

Variables pulled from .csv won't add

I'm having a bit of trouble adding a couple variables together. Nothing I've tried has fixed the problem, and I'm doing some stuff I've never done before which is making me wonder what is actually causing the problem. I have a .csv file which I need to read through to find the line I want, pull some variables from there, add them together, and display all this on a page.
Here's my code:
<script src="jquery_min.js" type="text/javascript"></script>
<script src="jquery.csv-0.71.js" type="text/javascript"></script>
<script>

jQuery.get('fileName.csv', function(data) {
var csv = $.csv.toArrays(data);
var index, value, result, var1, var2, var3;
for (index=0; index < csv.length; ++index) {
    value = csv[index];

    if (value[3] === "1234") {
        window.var1 = parseFloat(value[5]);
        window.var2= parseFloat(value[6]);
        window.var3= var1 + var2;
        alert(var3);
        result = var3;
        break;
    }
}
});

</script>

and here's a sample line from the .csv file:
"NEW YORK","A123","SMITH INC","1234","JOHN SMITH",4112237,5279,,"Y"
I'm writing to the page with this:
<script>
$('td#id1').html(var1);
$('td#id2').html(var2);
$('td.class1').html(var3);
</script>

The alert pops back with "undefined" while var1 and var 2 are written to the page correctly, as 4112237 and 5279 respectively. Var3 writes to the page as NaN. At this point I've tried too many fixes to recall. Any help would be appreciated.

A:

Remove those window. things! That will access properties of the global object (aka global variables), but your var1, var2, var3 are locally declared - and therefore still undefined when you access them (in the alert call or result assignment)!
jQuery.get('fileName.csv', function(data) {
    var csv = $.csv.toArrays(data);
    var index, value, result, var1, var2, var3;
    for (index=0; index < csv.length; ++index) {
        value = csv[index];

        if (value[3] === "1234") {
            var1 = parseFloat(value[5]);
            var2 = parseFloat(value[6]);
            var3 = var1 + var2;
            alert(var3);
            result = var3;
            break;
        }
    }

    $('td#id1').html(var1);
    $('td#id2').html(var2);
    $('td.class1').html(var3);
});

If those global variables were supposed to be read by the next script, have a look at How do I return the response from an asynchronous call? and maybe at Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference.

