Forked from anonymous/bonfire-find-the-longest-word-in-a-string.js
Last active
December 11, 2015 02:46
-
-
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
This file contains hidden or 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
// 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