Reputation: 738
I need to write a code regarding last 12 months calculation every next month so that when a user enters the value in the current month or in next month he should get the result here how the calculation goes
CA (jan 14) = (Avg of B.Y. 2001=100 for the past 12 months - 115.76)*100/115.76
CA (jan 14)=( 232.66-115.76)*100/115.76=100.009
so every month if some body enters it we should get the value by the above calculation. I tried with some sort of coding in JavaScript please checkout in this link
http://jsfiddle.net/kundansingh/SLC5F/
B.Y. 2001=100 = value will be enter by user Total of 12 Months = total value of last 11 months and current month so 12 months % Increase over 115.763 = 232.66-115.76 App. CA = ( 232.66-115.76)*100/115.76
i need dynamically for every month if input for next month value also it should show the result... what i have created is for one month ..please help me to sort the issue
Upvotes: 0
Views: 315
Reputation: 19750
In this case I'd really recommend using jQuery to make your life a lot easier, especially since you're going to want to do at least one for loop, if not two.
You need to loop through each row, get the previous 11 values, then do your calculations.
In your jsFiddle, the following code should work. I have edited your jsFiddle accordingly: http://jsfiddle.net/SLC5F/1/
$('tr', '#order-table').each(function() {
var total = 0;
var $rows = $(this).prevAll();
// return if we don't have enough rows
// probably better to just add a class to the active rows instead,
// but it's not my job to format your code.
if ( $rows.length < 13) return;
$rows = $rows.slice(0, 11);
// again, would be better to add a class to active inputs instead of using
// a not() filter. you can do this yourself.
total += parseFloat( $(this).find('input').not('.row-total-input, [readonly]').val() );
$rows.each(function() {
total += parseFloat( $(this).find('input').not('.row-total-input').val() );
});
// return if total isn't a number
if ( isNaN(total) ) return;
var monthly_average = total / 12;
// again, would be better to add classes to these TDs instead of using eq()
// make sure you do this before using this script in a live application.
$('td', this).eq(2).text(total);
$('td', this).eq(3).text(Math.round(monthly_average * 100) / 100);
$('td', this).eq(4).text(Math.round(( monthly_average - 115.763 ) * 100 / 115.764 * 100) / 100);
});
Upvotes: 1