0

I have been working a javascript algorithm and have been confused why one works and the other one doesn't work. I have thought this through and I need a thorough explanation from many of you to hear different explanations to solidify my understanding of what is going on here.

Explain the difference between the two algorithms:

function findLongestWord(str) {
  var longestNum = 0;
  var splitStr = str.split(" ");
  for(var i = 0; i < splitStr.length; i++) {
    if(splitStr[i].length > longestNum) {
      longestNum = splitStr[i].length;
    }
  }
  return longestNum;
}

findLongestWord("The quick brown fox jumped over the lazy dog");

Why does the top one work and the bottom one NOT work?

function findLongestWord(str) {
  var longestNum = 0;
  var splitStr = str.split(" ").length;
  for(var i = 0; i < splitStr; i++) {
    if(splitStr[i].length > longestNum) {
      longestNum = splitStr[i].length;
    }
  }
  return longestNum;
}

findLongestWord("The quick brown fox jumped over the lazy dog");
0

3 Answers 3

3

splitStr contains the length of the array and not the array itself.

var splitStr = str.split(" ").length;
//                            ^^^^^^

It does not work with an index

splitStr[i]
//      ^^^

because numbers have no index.

The whole nonworking code with console.log parts. It breaks with access of splitStr[i], because primitive data types does not have a property. therefor no access via an index.

function findLongestWord(str) {
    var longestNum = 0;
    console.log('longestNum', longestNum);               // 0
    var splitStr = str.split(" ").length;
    console.log('splitStr', splitStr);                   // 9
    for (var i = 0; i < splitStr; i++) {
        console.log('i', i);                             // 0
        console.log('longestNum', longestNum);           // 0
        console.log('splitStr', splitStr);               // 9
        if (splitStr[i].length > longestNum) {           // << Unable to get property |
            console.log('splitStr['+i+']', splitStr[i]); //  | 'length' of undefined  |
            longestNum = splitStr[i].length;             //  | or null reference      <<
        }
        console.log('longestNum', longestNum);
        console.log('--');
    }
    return longestNum;
}

console.log(findLongestWord("The quick brown fox jumped over the lazy dog"));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Please have a look to this questions and answers as well.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm just curious why this line var splitStr = str.split(" ").length; wouldn't work because var splitStr is holding the length of the array which is 9!
0

In the bottom one, you are assigning the value of splitStr a number.

2 Comments

The if statement isn't pulling anything, because splitStr[i] is meaningless when splitStr is a number.
oh yeah, i see that now
0
function findLongestWord(str) {
var longestNum = 0;
var splitStr = str.split(" ").length; 

for(var i = 0; i < splitStr; i++) {
if(splitStr[i].length > longestNum) {
  longestNum = splitStr[i].length;
 }
}
        return longestNum;
}

    findLongestWord("The quick brown fox jumped over the lazy dog");

var splitStr = str.split(" ").length

str.split(" ") // this returns a string array

str.split(" ").lenght //returns the length of the array, say 3

so splitStr ends up holding 3

so doing splitStr[i].length is like doing 3[i].length which do no make sense

1 Comment

but isn't that the same logic I have in the first example? I have splitStr[i].length in the first example and this works just fine

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.