Javascript String toNumber(toInt)

Description

Javascript String toNumber(toInt)


String.prototype.toNumber = function(toInt){
    var sum = 0, i = 0;
 while(i < this.length){
  sum += Number(this.charCodeAt(i));
  i++;//  w  w  w .jav a 2 s.  c o  m
 }
 if(toInt){sum = Math.floor(sum/this.length);}else{sum /= this.length;}
 return sum;
};



PreviousNext

Related