Pages

Saturday, December 25, 2010

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

No comments:

Post a Comment