Forum Discussion

funmay's avatar
funmay
Contributor
7 years ago

Return value in array

How can i return list of values into array.See example below

var lists=[];

for(var i=0;i<=10;i++){             

  lists = 5 * i

Log.Message(lists)

}

I want the return for the variable should be in this format 0,5,10,15,20,25,30,35,40,45,50 in other for my to get the maximum and minimum.

  • Please read the following web page and associated tutorials on using Arrays in JavaScript.

    https://www.w3schools.com/js/js_arrays.asp

     

    Basically, while you defined the variable lists as an empty array, as soon as you assigned 5*i to lists, you changed the data type of the variable from array to integer.  JavaScript is not strongly typed so any variable can be anything and assignments like this will override the initial declaration.

     

    As I understand it, you want to assign to the i element of the array the value of i*5.  Here's the code with a screenshot of the output.

     

    function arrayProcessing(){
        var lists=[];
        for(var i=0;i<=10;i++){             
            lists[i] = 5 * i
            Log.Message("Array element " + i + " = " + lists[i])
        }
        Log.Message("The full array = " + lists)
    }

  • You can do like below,

     

    var lists=[];
    
    for(var i=0;i<=10;i++){             
    
      lists[i] = 5 * i;
    
    Log.Message(lists)
    
    }
    return lists;//if you want to return as array
    return aqConvert.VartoStr(lists);//if you want to return as string like "0,1,2,3,4"

     

  • shankar_r's avatar
    shankar_r
    Community Hero

    You can do like below,

     

    var lists=[];
    
    for(var i=0;i<=10;i++){             
    
      lists[i] = 5 * i;
    
    Log.Message(lists)
    
    }
    return lists;//if you want to return as array
    return aqConvert.VartoStr(lists);//if you want to return as string like "0,1,2,3,4"

     

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    Please read the following web page and associated tutorials on using Arrays in JavaScript.

    https://www.w3schools.com/js/js_arrays.asp

     

    Basically, while you defined the variable lists as an empty array, as soon as you assigned 5*i to lists, you changed the data type of the variable from array to integer.  JavaScript is not strongly typed so any variable can be anything and assignments like this will override the initial declaration.

     

    As I understand it, you want to assign to the i element of the array the value of i*5.  Here's the code with a screenshot of the output.

     

    function arrayProcessing(){
        var lists=[];
        for(var i=0;i<=10;i++){             
            lists[i] = 5 * i
            Log.Message("Array element " + i + " = " + lists[i])
        }
        Log.Message("The full array = " + lists)
    }