# Reverse a string & Find the Longest Word in a String

Question 1: I am wondering why in the first solution, shown here:

``````function reverseString(str) {
for (var reversedStr = "", i = str.length - 1; i >= 0; i--) {
reversedStr += str[i];
}
return reversedStr;
}
``````

Why the str.length has a -1 after it? Doesn’t the -1 leave out the last letter, for example if the string is “hello” won’t it miss out “o”? Sorry if this is a really dumb question.

Question 2:

``````function findLongestWordLength(str) {
var words = str.split(' ');
var maxLength = 0;

for (var i = 0; i < words.length; i++) {
if (words[i].length > maxLength) {
maxLength = words[i].length;
}
}

return maxLength;
}
``````

In the code explanation it says: Then check for the longest word by comparing the current word to the previous one and storing the new longest word - referring to the if statement. But I don’t understand how it is comparing with the one before it - if maxLength is 0 then how are we sure that something just because it is bigger than 0 that it is the longest word?

It is to do with how an array (a string is basically just an array of characters) is indexed from 0 rather than 1. So counting becomes:
0,1,2,3,4,5,6,7,8,9 (there are 10 elements here), rather than 1,2,3,4,5,6,7,8,9,10 (as we would usually count to ten… note there are also 10 elements here).

1 Like

thanks. Although I just saw this lesson:

and it seems like it’s more to do with the fact that we need to specify which character we mean, and -1 is the most logical way to specify it as it kind of means the first from the end, if you see what I mean? I feel like this makes more sense than the indexing reason that you suggested? What do you think

@chunzg It is 100% related to how strings (and arrays) are indexed.

Let’s say I have a string with 4 characters:

``````var myStr = ['abcd'];
``````

If I want to reference the last character `'d'`, then I would write:

``````console.log(myStr[myStr.length - 1]); // displays 'd'
``````

The length of the string is `4`, but the last character resides at index `3`, which is why the `myStr.length - 1` is used.

In the challenge, you need to start iterating from the last character in thestring, which is why the variable `i` must start at `str.length - 1`.

1 Like

In this case they are decrementing the length of the string by a variable i. Which results in running a for loop from str[str.length-1] down to str[0]. So if the string is “Happy”, str.length = 5 (because there are five characters). So the loop does the following (note ‘i’ initially equals str.length-1, and in each iteration it is decremented by 1):
reversedStr = “”
reversedStr = reversedStr + str[str.length -1] = “” + str[5-1] = “Y”
reversedStr = reversedStr + str[str.length-1-1] = “Y” + str[(5-1)-1] = “YP”
reversedStr = reversedStr + str[str.length-1-2] = “YP” + str[((5-1)-1)-1] = “YPP” …
So they are still using the index, it is just that they are starting from one less than the length of the string (i.e. 4 in the string “Happy”) and then decrementing that index by one on each iteration, until they reach the zeroth element str[0] = ‘H’ after which the loop exits as it has reached the termination condition (as 0 - 1 = -1).

1 Like

Oh I see! Thank you so much.
It’s super confusing because sometimes I don’t know when the number is referring to an index or to an actual number!

`i` is an actual number. It just so happens it represents the index of the string.

1 Like

ok. I’ll get there!

as a stopped and beginner person on JS applications, for this task I come up with this solution, I think it is easy to understand for a beginner like me lol

``````function findLongestWordLength(str) {
var str2 = str.split(' ');
var arr = [];
for(var i= 0;i< str2.length; i++){
arr.push(str2[i].length);
}
const result = Math.max.apply(null, arr);
return result;
}

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

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with `[spoiler]` and `[/spoiler]` tags on the line above and below your solution code.

1 Like