Created
December 19, 2015 12:22
-
-
Save rurtubia/0ee7e0318eb1a14a030d to your computer and use it in GitHub Desktop.
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment