/**
 * Class to provide arithmatics services using Javascript
 * 
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 25, 2010
 * @version : 2.0  
 */
/**
 * Constructor service
 * @param start the first value of the series
 * @param diff the difference between the values in the series
 * @n how many value in the series
 */
function ArithmeticSeries(start,diff,n){
this.data = new Array();
this.start = start;
this.diff = diff;
this.n = n;
}
this.data = new Array();
this.start = start;
this.diff = diff;
this.n = n;
}
/**
 * method to get the series
 * @return array of the series
 */
ArithmeticSeries.prototype.getSeries = function(){
for(i=0;i<this.n;i++){
this.data[i] = this.start + (this.diff*i);
}
return this.data;
}
ArithmeticSeries.prototype.getSeries = function(){
for(i=0;i<this.n;i++){
this.data[i] = this.start + (this.diff*i);
}
return this.data;
}
/**
 * method to get the total sum of the individual value of
 * the series.
 * @return sum total summation of the series.
 */
ArithmeticSeries.prototype.getSum = function(){
var sum = 0;
for(i=0;i<this.n;i++){
sum = sum+(this.start+(this.diff*i));
}
return sum;
}
ArithmeticSeries.prototype.getLast = function(){
return this.getSeries()[this.getSeries().length-1];
}
ArithmeticSeries.prototype.getFirst = function(){
return this.getSeries()[0];
}
ArithmeticSeries.prototype.getNth = function(n){
var bound = this.getSeries().length;
if(typeof n=='undefined')
return 'please supply an argument';
else if(n<=0)
return 'index must be positive';
    
else if(n>bound)
return 'index out of bounds';
else
return this.getSeries()[n-1];
}
ArithmeticSeries.prototype.getSum = function(){
var sum = 0;
for(i=0;i<this.n;i++){
sum = sum+(this.start+(this.diff*i));
}
return sum;
}
/**
 * method to get the last item of the series
 * @return the last item
 */ArithmeticSeries.prototype.getLast = function(){
return this.getSeries()[this.getSeries().length-1];
}
/**
 * method to get the first item in the series
 * @return the first item in the series
 */ArithmeticSeries.prototype.getFirst = function(){
return this.getSeries()[0];
}
/**
 * method to get the n-th item of the series 
 * @return the n-th item of the series
 */ArithmeticSeries.prototype.getNth = function(n){
var bound = this.getSeries().length;
if(typeof n=='undefined')
return 'please supply an argument';
else if(n<=0)
return 'index must be positive';
else if(n>bound)
return 'index out of bounds';
else
return this.getSeries()[n-1];
}
 
No comments:
Post a Comment