Sunday, February 28, 2016

FIND THE LONGEST WORD IN A STRING

Challenge is to return the length of the longest word in the provided sentence.

Response should be a number.

It was tough one for me initially then i went through a post on git-hub describing what to do when you are struck up. I am putting the link here for reference.
  

This is a three part series. A must read for all the new coder. My next step was to make a flow chart. but in part II of the series, the above mentioned challenge is discussed with flow chart along with pseudo-code. It helped me a a lot. With the help of this pseudo-code, I quickly came up the solution. 

My solution is given below



 function findLongestWord(str) {
     var arr = str.split(' ');
     var max = 0; 
    for (var i = 0; i < arr.length; i++){
        var item = arr[i].length;
  
         if (item > max){
             max = item;
          }
  }  
   return max;  
}

No comments:

Post a Comment