Pages

Saturday, December 25, 2010

Linear Equation Algorithm

/**
 * Class for solving linear equation
 * 
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 25, 2010
 * @version : 1.0
 */


/**
 * Constructor service.
 * The equation supplied must be in the form:
 * ax+bx+c
 * This class is very limited in the for of the simple equation
 *
 * @param fa string the first linear equation
 * @param fb string the second linear equation
 */
function LinearEquation(fa,fb){
    this.fa = fa;
    this.fb = fb;

    var terma = this.fa.split(/[-+]/g);
    var a1 = terma[0].split(/x/)[0];
    var b1 = terma[1].split(/y/)[0];
    var c1 = terma[2];
    a1 = eval(a1);
    b1 = eval(b1);
    c1 = eval(c1);

    var termb = this.fb.split(/[+-]/g);
    var a2 = termb[0].split(/x/)[0];
    var b2 = termb[1].split(/y/)[0];
    var c2 = termb[2];
    a2 = eval(a2);
    b2 = eval(b2);
    c2 = eval(c2);
  
    this.x = ((b2*c1)-(c2*b1)) / ((a2*b1)-(b2*a1));
    this.y = (-c1 - a1*this.x) / b1;
}
/**
 * method to get the x,y value in array
 * @return array x,y value
 */
LinearEquation.prototype.getXY = function(){
    var data = new Array();
    data[0] = this.x;
    data[1] = this.y;
    return data;
};
/**
 * method to get x value
 * @return number x value
 */
LinearEquation.prototype.getX = function(){
    return this.x;
};
/**
 * method to get y value
 * @return number y value
 */
LinearEquation.prototype.getY = function(){
    return this.y;
};
           

Arithmetics Series

/**
 * 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;
}


/**
 * 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;
}


/**
 * 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;
}


/**
 * 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];
}

          

Aritmathics Series

/**
 * Class to provide aritmathics services using Javascript
 * 
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 25, 2010
 * @version : 1.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 AritmathicSeries(start,diff,n){
    this.data = new Array();
    this.start = start;
    this.diff = diff;
    this.n = n;
}


/**
 * method to get the series
 * @return array of the series
 */
AritmathicSeries.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.
 */
AritmathicSeries.prototype.getSum = function(){
    var sum = 0;
    for(i=0;i<this.n;i++){
        sum = sum+(this.start+(this.diff*i));
    }
    return sum;
}
          

Number Generator

/**
 * This function generate number between seeds.
 * 
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 25, 2010
 * @version : 1.0
 */


/**
 * Constructor service
 * @param a the start number desired
 * @param b the end number desired
 */
function Number(a,b){
     this.start = a;
     this.end = b;
     this.data = new Array();
     this.msg = 'Error: 1st argument must less than 2nd';
     this.condition = this.start < this.end;
}

/**
 * method to get the odd numbers.
 * @return array of the odd numbers
 */
Number.prototype.getOdd = function(){
     if(!this.condition) return this.msg;
     var k = 0;
     for(i=this.start;i<=this.end;i++){
         if(i%2!=0){
             this.data[k] = i;
             k++;
         }
     }
     return this.data;
}


/**
 * method to get the even numbers
 * @return array of the even numbers
 */
Number.prototype.getEven = function(){
    if(!this.condition) return this.msg;
    var k = 0;
    for(i=this.start;i<=this.end;i++){
        if(i%2==0){
            this.data[k] = i;
            k++;
        }
    }
    return this.data;
}
           

Selection Sort Algorithm

/**
 * This framework provide a sorting service based on 
 * selection sort using Javascript.
 *
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 25, 2010
 * @version : 2.0
 */


/**
 * Constructor service
 * @param data Array of data
 */
function SelectionSort(data){
    this.data = data;
    this.len = this.data.length;
}


/**
 * method that provide ascending sorting services.
 * @return ascending order of array
 */
SelectionSort.prototype.getAsc = function(){
    for(i=0;i<this.len;i++){
        var minIndex = i;
        var tempValue = this.data[minIndex];
        for(j=i;j<this.len-1;j++){
            if(this.data[j+1]<this.data[minIndex]){
                minIndex = j+1;
            }
        }
        this.data[i] = this.data[minIndex];
        this.data[minIndex] = tempValue;
    }
    return this.data;
}


/**
 * method that give a service to give descending order
 * @return descending order of an array
 */
SelectionSort.prototype.getDesc = function(){
    for(i=0;i<this.len;i++){
        var maxIndex = i;
        var tempValue = this.data[maxIndex];
        for(j=i;j<this.len-1;j++){
            if(this.data[j+1]>this.data[maxIndex]){
                  maxIndex = j+1;
            }
        }
        this.data[i] = this.data[maxIndex];
        this.data[maxIndex] = tempValue;
    }
    return this.data;
}
       

Linear Search Algorithm

/**
 * Linear Search Algorithm
 * 
 * This class provide a framework on linear search algorithm
 * using Javascript.
 *
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 25, 2010
 * @version : 2.0
 * 
 */


/**
 * Constructor for this linear search algorithm
 * @param d array of data
 */ 
function LinearSearch(d){
    this.data = d;
}


/**
 * This function does to give a min value from the data
 * @return minValue min value from the data
 */
LinearSearch.prototype.getMinValue = function(){
    var minValue = this.data[0];
    for(i=0;i<this.data.length;i++){
        if(this.data[i+1]<minValue){
           minValue = this.data[i+1];
        }
    }
    return minValue;
}


/**
 * This function does to give a min index of the value from 
 * the data
 * @return minIndex an index of the min value from the data
 */
LinearSearch.prototype.getMinIndex = function(){
    var minIndex = 0;
    for(i=0;i<this.data.length;i++){
        if(this.data[i+1]<this.data[minIndex]){
            minIndex = i+1;
        }
    }
    return minIndex;
}


/**
 * This function does to give a max value from the data
 * @return maxValue max value from the data
 */
LinearSearch.prototype.getMaxValue = function(){
    var maxValue = this.data[0];
    for(i=0;i<this.data.length;i++){
        if(this.data[i+1]>maxValue){
             maxValue = this.data[i+1];
        }
    }
    return maxValue;
}


/**
 * This function does to give an index of the max value from
 * the data
 * @return maxIndex an index of the max value from the data
 */
LinearSearch.prototype.getMaxIndex = function(){
    var maxIndex = 0;
    for(i=0;i<this.data.length;i++){
        if(this.data[i+1]>this.data[maxIndex]){
             maxIndex = i+1;
        }
    }
    return maxIndex;
}
        

Friday, December 24, 2010

Linear Search Algorithm

/**
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 24, 2010
 * @version : 1.0
 *
 * This function provide search algorithm and it give the index
 * number of the desired values
 *
 * @param data Arrays of the data
 * @param value the desired value
 * @return i index of the value
 */

function linearSearch(data, value){
    for(i=0;i<data.length;i++){
        if(data[i]==value){
            return i;
        }
    }
    return "No found match!";
}