Find the Longest Word in a String - code critique

Find the Longest Word in a String - code critique


The following code passed the challenge but I would like feedback with regards to what I could improve and how comprehensible it is from other points of view. My inquiry has to do with how to solutions are written. Is code more likely to be written as the solutions are?

function findLongestWord(str) {
  function strLength(str) {
    return str.length;
  function maxN(num1, num2) {
    return Math.max(num1, num2);
  var strArr = str.split(" "); // split the string into substrings of an array
  var strLen =; // retreive the length of each substring
  var maxNumber = strLen.reduce(maxN); 
  return maxNumber; // return the length of the longest word

findLongestWord("The quick brown fox jumped over the lazy dog");
function findLongestWord(s) {
  return s.split(' ')
    .reduce(function(x, y) {
      return Math.max(x, y.length)
    }, 0);

Link to the challenge:


The first one is more readable IMO. You can also drop the intermediate variables and chain the method calls:

return str.split(' ')