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");
Saia Fonua
  • 45
  • 8

3 Answers3

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.

Community
  • 1
  • 1
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • 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! – Saia Fonua Jan 28 '17 at 00:43
0

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

Ally Rippley
  • 525
  • 2
  • 6
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

wokoro douye samuel
  • 2,194
  • 3
  • 15
  • 29
  • 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 – Saia Fonua Jan 28 '17 at 00:59