I don’t get it. I just don’t get it. I’m trying to not look for the answer, because I want to solve this the hard way. Could anyone give me some little tips, without giving too much away, as to where i’m going wrong?
I have to find the longest word in the array.
function findLongestWord(str) {
var splitStringArr = str.split(" ");
var longestWord = splitStringArr[0];
for(var i = 0; i < splitStringArr.length; i++) {
if (longestWord.length >= splitStringArr[i+1].length) {
longestWord = longestWord;
}
else if (splitStringArr[i+1].length > longestWord.length) {
longestWord = splitStringArr[i+1];
}
}
return longestWord.length;
}
findLongestWord("The quick brown fox jumped over the lazy dog");
Splitting the string into an array works fine. Assigning longestWord to splitStringArr[0] works fine too. I’ve tested those individual parts and I can play around with them. It’s all fine there. The problem seems to be here:
for(var i = 0; i < splitStringArr.length; i++) { if (longestWord.length >= splitStringArr[i+1].length)
The error it is giving me is "cannot read property “length” of undefined. Okay. That must mean that either splitStringArr or longestWord or splitStringArr[i+1] is undefined. But why? What’s the case here?
splitStringArr.length should be 9; longestWord.length should be 3 in the first loop; splitStringArr[i+1].length should be 5 in the first loop;
Why does one of them come back as undefined? Thank you for reading.
I misspoke. You’re not running it too many times, but on the last run, you’re asking for one more element than the array actually has. So if it has 9 elements, you’re asking for the 10th element on the last run.
You got it! Little things like that can get SUPER frustrating. Just remember that everyone goes through it. It’s totally normal when learning this stuff. Keep at it and don’t give up!! Good work.
Would you say that the way I did it was inefficient?
Here’s my code after fixing it:
function findLongestWord(str) {
var splitStringArr = str.split(" ");
var longestWord = splitStringArr[0];
for(var i = 0; i < splitStringArr.length - 1; i++) {
if (longestWord.length >= splitStringArr[i+1].length) {
longestWord = longestWord;
}
else if (splitStringArr[i+1].length > longestWord.length) {
longestWord = splitStringArr[i+1];
}
}
return longestWord.length;
}
findLongestWord("The quick brown fox jumped over the lazy dog");
It works, but I guess I can imagine that there’s a better way to do it. Almost always is with any code. I guess my question is - Is it poor quality?
You could always shorten your variables to remove context. Make it easier to read, and add documentation elsewhere to understand it.
I don’t know if it’s against the rules to use other methods either, but here is what I did.
Summary
function findLongestWord(str) {
var result = str.split(" ");
var count = 0;
result.forEach(function(x,y,z){
if( x.length > count ){ count = x.length; }
});
return count;
}
findLongestWord("The quick brown fox jumped over the lazy dog");
function findLongestWord(str) {
var words = str.split(' ');
var longest = "".split("");
for (var i = 0; i < words.length; i++) {
if (words[i].length > longest.length) {
longest = words[i];
}
}
return longest+ ", " +longest.length;
}
findLongestWord("The quick brown fox jumped over the lazy dog");