I came up with the following code. This is the first time I was able to complete a challenge without hints, so kinda proud. With that said, I am not surprised if it looks like a mess. Can anyone share different ways of refactoring this or easier, more efficient, ways of approaching a problem like this? Anything is greatly appreciated.
function findLongestWordLength(str) {
//Setting up a counting variable and the array to iterate through.
let strCount = 0;
let splitStr = str.split(' ');
//Iterating through each word(index) and checking the length against the strCount variable.
for (let i = 0; i < splitStr.length; i++) {
let splitStrIndex = splitStr[i].length;
if (splitStrIndex > strCount) {//If higher, than set strCount variable to new length and continue.
strCount = splitStrIndex;
}
}
return strCount;//Return the final count(should be the .length of the longest word.)
}
findLongestWordLength("The quick brown fox jumped over the lazy dog");
You did great! The code meets the challenge, both your comments and the code itself is well structured and readable…what makes you think it needs a refactor?
Two comments. First, when posting solutions, wrap them in spoiler tags. Second, consider revisiting this challenge in did months, or when you have learned a few other tools, and see if there’s a better way.
I can think of three different ways, but honestly not one is better.
I feel like I am always skipping steps in my logic or making assumptions, so I just default on my code being inefficient. I have to learn to be more confident in that respect. That is why I was asking, the logic makes sense and the code is readable to me, but I was not sure if it would be to others.
I will make sure to revisit these challenges when I have more under my belt
In this case there isn’t much to do in terms of efficiency. Depending on the sentence length it’s just one comparison per word, that seem to be as good as it can be in such problem. It’s simple, without unneeded complications.
Function is easy to follow and figure out what is where, however it may be worth to take a closer look at some of the variable names. I think with some improvements in the variable naming, it would be possible to get rid of comments, without losing any clarity.
So all my following feedback are essentially nitpicks.
Consistently indent your code.
Most of your indentation is spot on “two-space”, which is the current recommended indentation. However, 3 lines are un-indented. I personally usually run my code through an auto-formatter like prettier. When working on FCC challenges you can use something like this online sandbox to autoformat your code via copy-paste: Prettier
Consistently place comments in the same place.
One of the more common conventions I’ve seen are single-line comments to be placed on their own line above/below the line its referring to. Primarily so a single line doesn’t end up super long.
No amount of comments make up for clear and consistent names.
A variable name strCount might be misleading in what is represents, since this challenge is about “finding the longest word” maybe something a long the lines of longestWordLength is more clear.
There’s also names that are actually confusing, such as this line:
let splitStrIndex = splitStr[i].length
Where splitStrIndex is actually the length of the word, not the index of the word (thats i)
There are fancier/shorter methods on solving this challenge, but the current code is fine.
Such fancier methods can reduce the amount of code you write, but not increase performance, or clarity. They could also just decrease clarity by “being too fancy” and confuse those reading the code. As such I’d consider the code you have to be close to optimal in regards to readability and performance. “Terseness” or “elegance” is overrated when the code works, and is clear and readable.