rurtubia
12/19/2015 - 12:22 PM

My solution to the fifth bonfire at FreeCodeCamp Return the length of the longest word in the provided sentence. Your response should be a

My solution to the fifth bonfire at FreeCodeCamp

Return the length of the longest word in the provided sentence. Your response should be a number.

function findLongestWord(str) {
  
  //Splits the string into an array
  var countArray = str.split(' ');
  
  //int variable to store the longest character count.
  var max = 0;
  
  //loops through the array
  //countArray.length obtains the number of elements in the array
  for (var i=0; i<countArray.length; i++)
  {
    //creates a temporary variable test which stores the length of each string
    test = countArray[i].length;
    
    //compares the lenght of these strings to the maximum stored
    if(test > max)
      //if the string is longer than the stored max, this value is replaced.
      max = test;
  }
  //returns the max stored.
  return max;
  
}

findLongestWord("What if we try a super-long word such as otorhinolaryngology");