-1

I am trying to loop through an array but it am not able to see an intended output. Instead when I tried debugging, it says that the variable r is undefined. This is the variable that am using for the for loop.

Debugging Interface

Debugged

Code in Context

function checkClassType(idOfEl,dropElem)
{
    var ElementType="";
    if (dropElem == "stream ui-draggable") {

        for(var r=0;r<100;r++)
        {
            if(createdImportStreamArray[r][0]==idOfEl)
            {
                ElementType="importStream";
                finalArray[idOfEl-1][2]= createdImportStreamArray[r][1]; //Selected Stream from Predefined Streams
                finalArray[idOfEl-1][3]= createdImportStreamArray[r][2]; //asName

            }
            else if(createdExportStreamArray[r][0]==idOfEl)
            {
                ElementType="exportStream";
                finalArray[idOfEl-1][2]= createdExportStreamArray[r][1]; //Selected Stream from Predefined Streams
                finalArray[idOfEl-1][3]= createdExportStreamArray[r][2]; //asName
            }
            else 
            {
                ElementType="definedStream";
                finalArray[idOfEl-1][2]= createdDefinedStreamArray[r][1]; //Stream Name
                finalArray[idOfEl-1][3]= createdExportStreamArray[r][4]; //Number of Attributes
                finalArray[idOfEl-1][4]=[];
                for(var f=0;f<createdExportStreamArray[r][4];f++)
                {
                    finalArray[idOfEl-1][4][f][0]=createdDefinedStreamArray[r][2][f][0]; //Attribute Name
                    finalArray[idOfEl-1][4][f][1]=createdDefinedStreamArray[r][2][f][1]; // Attribute Type
                }
            }
        }
    }

    else if (dropElem == "wstream ui-draggable") {
             //Continues...

But, according to my assumption, r will keep on getting values ranging from 0 to 99. So how is it possible for it to be undefined?

Nayantara Jeyaraj
  • 2,624
  • 7
  • 34
  • 63
  • Please copy+paste your code in to the question. It makes it easier for you to create the question and others to read and amend to fix the problem. – Rory McCrossan Sep 05 '16 at 07:08
  • 1
    `var` in JavaScript hoists the variable declaration for `r` to the very top of the function scope you're looking at (i.e. the beginning of `checkClassType`). So if you're in another area of the function in which `r` isn't used, or the loop hasn't been started, `r` will be `undefined`. – grovesNL Sep 05 '16 at 07:16
  • @grovesNL But, according to the above context, am I deviating from the concept you've explained? – Nayantara Jeyaraj Sep 05 '16 at 07:21
  • @TaraWilfred: It's just an explanation for why `r` is declared at the very beginning of the function, and hence will be `undefined` until it hits your loop above or something else that assigns a value to it. – grovesNL Sep 05 '16 at 07:22
  • @grovesNL I've tried as you mentioned, and I placed `var r=0` outside the looping statement and the loop only checks executes r=0 against the loop body statements and doesn't iterate by incrementing. Could you please post an answer showing the way in which I need to change the above. Thanks in advance – Nayantara Jeyaraj Sep 05 '16 at 07:25

1 Answers1

1

Try declaring r variableat the very beginning of the method

function checkClassType(idOfEl,dropElem)
{
var r=0
var ElementType="";
if (dropElem == "stream ui-draggable") {

    for(;r<100;r++)
    {
        if(createdImportStreamArray[r][0]==idOfEl)
        {
            ElementType="importStream";
            finalArray[idOfEl-1][2]= createdImportStreamArray[r][1]; //Selected Stream from Predefined Streams
            finalArray[idOfEl-1][3]= createdImportStreamArray[r][2]; //asName

        }
        else if(createdExportStreamArray[r][0]==idOfEl)
        {
            ElementType="exportStream";
            finalArray[idOfEl-1][2]= createdExportStreamArray[r][1]; //Selected Stream from Predefined Streams
            finalArray[idOfEl-1][3]= createdExportStreamArray[r][2]; //asName
        }
        else 
        {
            ElementType="definedStream";
            finalArray[idOfEl-1][2]= createdDefinedStreamArray[r][1]; //Stream Name
            finalArray[idOfEl-1][3]= createdExportStreamArray[r][4]; //Number of Attributes
            finalArray[idOfEl-1][4]=[];
            for(var f=0;f<createdExportStreamArray[r][4];f++)
            {
                finalArray[idOfEl-1][4][f][0]=createdDefinedStreamArray[r][2][f][0]; //Attribute Name
                finalArray[idOfEl-1][4][f][1]=createdDefinedStreamArray[r][2][f][1]; // Attribute Type
            }
        }
    }
}

else if (dropElem == "wstream ui-draggable") {
         //Continues...