Skip to main content
edited title
Link
Mast
  • 13.5k
  • 12
  • 54
  • 119

Find the longest wordword's length

Tweeted twitter.com/StackCodeReview/status/679602743967875072
edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
Source Link
Mast
  • 13.5k
  • 12
  • 54
  • 119

Find the longest word

The challenge is simple:

Return the length of the longest word in the provided sentence.

The solution is just as simple:

function findLongestWord(str) {
  arr = str.split(' ');
  size = 0;
  for (var s in arr) {
    if (arr[s].length > size) {
      size = arr[s].length;
    }
  }
  return size;
}

However, I vaguely remember you're not supposed to use for..in in JavaScript unless absolutely necessary. What would be the more idiomatic approach for this loop?