Javascript String longestWord()

Description

Javascript String longestWord()


String.prototype.longestWord = function() {
 var words = this.split(" ");
 var index = 0;/*from   ww  w .ja va 2 s  .c om*/
 var maxLength = 0;
 var getIndex = function(element, i, array){
  if(maxLength < element.length) {
   maxLength = element.length;
   index = i;
  }
 }
 words.forEach(getIndex);
 return words[index];
};

module.exports = String;



PreviousNext

Related