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
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 !
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
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
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.
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.
Bookmarks