Q:

using variables within .each() scope

I got a little bit of help with this code earlier, but I've run into another problem.  How can I modify a variable inside the each function and use them outside that scope?
Here is a simple explanation of my code: For each webpage, grab the contents of the elements and insert them into an array.  After this, manipulate the array to create a message and send an alert to the browser.
HTML
<div id="insert"></div>

Javascript
$(document).ready(function() {
    userid = 17320;
    courseid = 4745;

    allLessonsArray = script(userid, courseid);

    function script(userid, courseid) {
        var lessonInTopicSectionArray = new Array();
        lessonInTopicSectionArray['Floods, external'] = 376;
        var allLessonsArray = new Array();

        $.each(lessonInTopicSectionArray, function(name, lesson) {
            lastAttempt = 0;
            url = '---------/learn/mod/lesson/report.php?id=' + lesson + '&action=reportdetail&userid=' + userid + '&try=' + lastAttempt;
            var lessonArray = new Array();

            $.get(url, function(data) {
                var page = $(data);                
                $('tr.r1.lastrow td.cell.c0').html(page).each(function(index, content) {
                    score = parseInt(content.substr(content.length - 1));
                    lessonArray[index] = score;
                });

                allLessonsArray[name] = lessonArray;
            });

        });
        return allLessonsArray;
    }

    var message = "";

    $.each(allLessonsArray, function(name, lessonArray) {
        if (!message) {
            message = "<li>Lesson <b>" + name + "</b> scores are:</li>";
        }
        else {
            message = message + "<li>Lesson <b>" + name + "</b> scores are:</li>";
        }

        probability = lessonArray[0];
        humanImpact = lessonArray[1];
        infrastructureImpact = lessonArray[2];
        communityImpact = lessonArray[3];
        training = lessonArray[4];
        preparedness = lessonArray[5];
        message = message + "<ul><li>Probability score: " + probability + "</li><li>Human Impact score: " + humanImpact + "</li><li>Public Health Infrastructure Impact score: " + infrastructureImpact + "</li><li>Community Impact score: " + communityImpact + "</li><li>Training score: " + training + "</li><li>Preparedness score: " + preparedness + "</li></ul>";
        risk = (probability / 3) * ((humanImpact + infrastructureImpact + communityImpact + training + preparedness) / 15);
        message = message + "<p>Risk of <b>" + name + "<b> is" + risk + ".</p>";

    });

    $('#insert').html(message);

});​

A:

Scopes seem to be correct.
Maybe your problem is related to the fact that $.each() does not iterate the elements in an Array that have a String as their index.
Try the following here and you'll only get a pop-up with 0 -> 1:
var list = new Array();
list['Floods, external'] = 376;
list[0] = 1;

$.each(list, function(index, item) {
    alert(index + " -> " + item);
});​

From jQuery's $.each() documentation page:
Description: A generic iterator function, which can be used to seamlessly iterate over both objects and arrays. Arrays and array-like objects with a length property (such as a function's arguments object) are iterated by numeric index, from 0 to length-1. Other objects are iterated via their named properties.
You can accomplish the equivalent with the following:
var list = new Array();
list[0] = {name : 'Floods, external', value : 376};

$.each(list, function(index, item) {
    alert(item.name + " -> " + item.value);
});​

