Skip to main content
2 of 3
edited tags
200_success
  • 144.2k
  • 22
  • 188
  • 473

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?

Mast
  • 13.5k
  • 12
  • 54
  • 119