Tom
Tom

Reputation: 461

Integer not being updated properly

I have a fairly long javascript file below, but it's not important to understand the entire file, only the 'totalItemCount' variable and how it is updated. This code uses the Igloo API to recursively count the number of files within a directory (and that count is stored in the totalItemCount var). I have never used recursion with Javascript before, so I may have made a mistake there. For some reason the count increases and then goes down later on in the javascript console, which makes me think I have made a mistake updating that variable somewhere (and it's only updated in a few places but I can't find it)

EDIT: I now have it updating more accurately, but items are over-counted or under-counted by about 10%. I think the last few updates might be where the problem is but i'm not sure. The code below is updated:

new ApiClient({
apimethod: 'objects/7f6d706e-6754-e411-b5b8-d4ae5294c399/children/view',
method: 'get',
queryparams: {
    maxcount: 8000,
    startindex: 0,
    includefuturepublished: true
},
onSuccess: function (responseText) 
{
    var result = JSON.parse(responseText);
    var originalObject = result; //first, top-level object
    var totalItemCount = 0; //UPDATED HERE

    console.log(result.response);
    console.log(result.response.items);
    console.log('RESPONSE TITLE: ' + result.response.items[0].title);
    console.log("\n\n\n");
    totalItemCount += parseInt(result.response.totalCount); //UPDATED HERE
    console.log("totalItemCount: " + totalItemCount);

    //Check if object has children and add to totalItemCount accordingly FOR EACH object:

    function getItemsRecursively(totalItemCount1)
    {
        for(var i = 0; i < parseInt(totalItemCount); i++) //at this point, totalCount == #objects at this lvl
        {
            console.log("FOR LOOP TEST: " + i);
            var currentObject = result.response.items[i];
            console.log("href/dir: " + currentObject.href);
            console.log("title: " + currentObject.title);
            console.log("numchildren: " + currentObject.numchildren);
            if(currentObject.numchildren > 0 && currentObject.numchildren != undefined)
            {
                console.log("it has children...");
                getChildrenItemCount(totalItemCount1, currentObject);
            }

            console.log("New totalItemCount: " + totalItemCount);
            console.log("\n~~~~~ NEXT OBJECT ~~~~~\n");
        }
    }

    function getChildrenItemCount(totalItemCount2, previousObject)
    {
        //totalItemCount2 = totalItemCount;
        var childID = previousObject.id;
        console.log("childID: " + childID);
        new ApiClient
        ({
            apimethod: 'objects/' + childID + '/children/view',
            method: 'get',
            queryparams: {
                maxcount: 8000,
                startindex: 0,
                includefuturepublished: true
            },
            onSuccess: function (responseText) 
            {
                console.log("getChildrenItemCount successful...");
                var result = JSON.parse(responseText);
                console.log(result);
                var currentObject = result.response;

                var currentFolderItemCount = currentObject.totalCount;
                console.log("currentFolderItemCount: " + currentFolderItemCount);

                for(var i = 0; i < parseInt(currentFolderItemCount); i++) //at this point, totalCount == #objects at this lvl
                {
                    console.log("CHILDREN FOR LOOP TEST: " + i);
                    var currentObject = result.response.items[i];
                    console.log("href/dir: " + currentObject.href);
                    console.log("title: " + currentObject.title);
                    console.log("numchildren: " + currentObject.numchildren);
                    if(currentObject.numchildren > 0 && currentObject.numchildren != undefined)
                    {
                        console.log("it's children has children...");
                        totalItemCount += parseInt(currentObject.numchildren); //UPDATED HERE
                        totalItemCount2 = totalItemCount; //UPDATED HERE
                        console.log("totalItemCount after one sub-child total: " + totalItemCount);
                        getChildrenItemCount(totalItemCount2, currentObject);
                    }

                    console.log("New totalItemCount after ENTIRE getChildrenItemCount: " + totalItemCount);
                    console.log("\n~~~~~ NEXT OBJECT WITHIN CHILDREN ~~~~~\n");
                    console.log("\n\n\n\nFINAL ITEM COUNT: " + totalItemCount);

                }
            }
        })
        //return totalItemCount;
    }

    getItemsRecursively(totalItemCount);

    console.log("\n\n\n\nFINAL ITEM COUNT: " + totalItemCount);
}

});

Upvotes: 0

Views: 53

Answers (1)

Alessandro
Alessandro

Reputation: 4472

It's a "scope" problem, you're using the same variable name as parameter of function and out the function, so the parameter overwrite the global variable.

See following please:

var global = 10;

function test(global){
  global += 100;
  console.log(global);
}

console.log(global);
test(global);
console.log(global);

You simply have to remove the var function's parameters.

var global = 10;

function test(){
  global += 100;
  console.log(global);
}

console.log(global);
test(global);
console.log(global);

I hope it was clear, bye.

Upvotes: 1

Related Questions