Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jiyoon-koo/11268a48572f4234645d to your computer and use it in GitHub Desktop.
Save jiyoon-koo/11268a48572f4234645d to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/thetinybeaker 's solution for Bonfire: Find the Longest Word in a String
// Bonfire: Find the Longest Word in a String
// Author: @thetinybeaker
// Challenge: http://www.freecodecamp.com/challenges/bonfire-find-the-longest-word-in-a-string
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function findLongestWord(str) {
// split up the words and put them into an array
var words = str.split(' ');
// make an empty array to put the lengths of each words into
var numbers = [];
// for loop. start at 0 (because arrays are zero-indexed, go until length of the word array, add 1 each time )
for (i = 0; i < words.length; i++) {
// set var wordLength to length of each word
var wordLength = words[i].length;
//push each length into the empty array, numbers
numbers.push(wordLength);
}
//return the biggest value
return Math.max.apply(Math, numbers);
}
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