www.webdeveloper.com
Results 1 to 8 of 8

Thread: Javascript --how do i reduce the length of an array by elimnating some array elements

Hybrid View

  1. #1
    Join Date
    Oct 2012
    Posts
    6

    Javascript --how do i reduce the length of an array by elimnating some array elements

    my array consists of 6 elements out of which as on date 4 are empty or 0 because previous month data comes next month as under:
    July-12 Aug-12 Sep-12 Oct-12 nov-12 Dec-12
    212 214
    the data for the sep is availabel on 31st oct and so on.
    i wanted to calculate the average every month of the available data.
    so I wrote the follwing code in js.
    Code:
    script type="text/javascript">
     	function SumAverage()
    	{		
    	var indxnos = new Array(6)
    	indxnos[0]= document.DA_Slabs.jul12.value
    	indxnos[1]= document.DA_Slabs.aug12.value
    	indxnos[2]= document.DA_Slabs.sep12.value
    	indxnos[3]= document.DA_Slabs.oct12.value
    	indxnos[4]= document.DA_Slabs.nov12.value
    	indxnos[5]= document.DA_Slabs.dec12.value
    	var Sum = 0
    	var Average 
    	for (var i = 0; i < indxnos.length; i++)	
       	Sum=Sum+parseInt(indxnos[i]		
    	Average = Sum/indxnos.length
                   Math.round(Average)			document.DA_Slabs.avg.value= Average
    	</script
    if i populate the empty data 0 ,as on date the average comes to 71 instead 213 because the array length is 6 instead of 2 required to be used for correct results.
    I am new to JS and arrays so I donot know how to reduce the length of the array while calculating average every month till the half year.I have taken the array as 6 as an example although the data that is required to be used is for more than 5 years. It seems easy to divide by 2 and proceed so on but in actual case the array consists of about 60 months and so I need help.

    Further advise me to further clean or shorten my code
    so that every month I get the correct average.
    thanks

    vkwd7























    [/CODE]

  2. #2
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,270
    Your have only to count the valid value with something like this

    Code:
    function SumAverage()
    	{		
    	var indxnos = new Array(6);
    	indxnos[0]= document.DA_Slabs.jul12.value;
    	indxnos[1]= document.DA_Slabs.aug12.value;
    	indxnos[2]= document.DA_Slabs.sep12.value;
    	indxnos[3]= document.DA_Slabs.oct12.value;
    	indxnos[4]= document.DA_Slabs.nov12.value;
    	indxnos[5]= document.DA_Slabs.dec12.value;
    	var Sum = 0;
    	var NmbVle = 0;
    	var Vle;
    	var Average; 
    	for (var i = 0; i < indxnos.length; i++) if (Vle=parseInt(indxnos[i]))	
       	   	{
       	   	Sum=Sum+Vle;
       	   	NmbVle++;}	
    	 Average = Sum/NmbVle;
             document.DA_Slabs.avg.value= Average.toFixed(2);
    }
    Do not forgot to close the bracket !
    EDIT : Its perhaps useful to make a test not to divide by zero !
    Last edited by 007Julien; 10-25-2012 at 09:39 AM.

  3. #3
    Join Date
    Oct 2012
    Posts
    6
    007Jullian sir,
    thanks.This is exactly I wanted.But beyond me to solve.incidentally I think if (Vle=parseInt(indxnos[i]) will work irrespective of the fact whether indxnos value is 0 0r "".
    What you suggest for further learning and understanding of arrays and loops in functions.
    regards
    vkwd7

  4. #4
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,270
    No problem 0 or "" are false boolean. See this page w3schools.com and particularly the link Check Boolean value.
    I suggest to read w3schools.com tutorial with at first this page about blocks.

  5. #5
    Join Date
    Oct 2012
    Posts
    6
    thanks.
    i further will like to know in what manner I can change the function provided by you so that the average is calculated afresh every quarter instead of half-year as now. for example in the half year figures of July-dec average is taken only for first 3 months 1.e. july aug & sept and then susequently average for the months Sept to Dec is taken without taking in account the previous quarters average figures although these figures may continue to be shown.
    the prototype of my calculator based on the function may be seen at www.airbiopf.netau.net

    regards
    vkwd7

  6. #6
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Code:
    function SumAverage()
    	{		
    	var indxnos = new Array(6)
    	indxnos[0]= document.DA_Slabs.jul12.value
    	indxnos[1]= document.DA_Slabs.aug12.value
    	indxnos[2]= document.DA_Slabs.sep12.value
    	indxnos[3]= document.DA_Slabs.oct12.value
    	indxnos[4]= document.DA_Slabs.nov12.value
    	indxnos[5]= document.DA_Slabs.dec12.value
    	indxnos = indxnos.filter(/./.test, /^[\-\d.]+$/).map(Number);
        return avg= indxnos.reduce(function(a,b){return a+b}) / indxnos.length ;
    }
    this should work for any set of values in the form fields, counting 0, ignoring "" in determining length...

    if you want different sections, simply make more arrays and use the functions to calc their averages.

  7. #7
    Join Date
    Oct 2012
    Posts
    2
    Hi, everybody. This is the right place to be ... definitely.

    I have had problems while trying to create a small bookstore for a client. I need to create an array to have (there) the information of each book of the bookstore. Each position in this array must contain the following book´s information : book type (e-book or PDF), name of the book, price, name of the file with the image of its cover, author, editor´s establishment and ISBN. Creating and inicializing this array must be done inside a js extension file which will be included in two html pages, “digital books” and “orders”.
    I have received some tips : to insert a book´s information inside an Array too, so that the list of books remains being an array formed by arrays, is that right ? A friend told me that in javascript it is possible to put together different types of elements in the array is it true ?
    Ok, partners, excuse me for being just a "wannabe" ... regards.

  8. #8
    Join Date
    Oct 2012
    Posts
    2
    Quote Originally Posted by ProgrammerBR View Post

    Hi, everybody. This is the right place to be ... definitely.

    I have had problems while trying to create a small bookstore for a client. I need to create an array to have (there) the information of each book of the bookstore. Each position in this array must contain the following book´s information : book type (e-book or PDF), name of the book, price, name of the file with the image of its cover, author, editor´s establishment and ISBN. Creating and inicializing this array must be done inside a js extension file which will be included in two html pages, “digital books” and “orders”.
    I have received some tips : to insert a book´s information inside an Array too, so that the list of books remains being an array formed by arrays, is that right ? A friend told me that in javascript it is possible to put together different types of elements in the array is it true ?
    Ok, partners, excuse me for being just a "wannabe" ... regards.
    Preview ... http://farm9.staticflickr.com/8325/8...67539507_m.jpg. Please, dont´t laugh ... regards

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles