Basic Algorithm Scripting - Reverse a String

Tell us what’s happening:
Describe your issue in detail here.
Hi yall, could someone please explain to me what the -1 is for in the line let i = str.length -1 I thought the decrementing by 1 each iteration was already taken care of by the i-- part
Your code so far

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

reverseString("hello");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.50

Challenge: Basic Algorithm Scripting - Reverse a String

Link to the challenge:

You can access individual characters in a string with array notation, which is what you are doing here:

reversedStr += str[i];

What index value does an array start with? What is the last index value of an array? What does the value of length return for an array or string?

1 Like

index value starts with 0, last index value is -1 and value length returns the whole string/array i guess i just didnt see it the same since it wasnt str.length[-1] instead of str.length -1

Oh! Ok actually i just visualized it in my head alongside with what you just said it makes sense now, thank you!

If the index value of the first item in an array is 0, then the last index value is the length of the array minus 1. So the last character in a string is also the length of the string minus 1. That’s where str.length -1 is coming from.

1 Like

Yeah thats how i just saw it in my head, -1 became the starting point then it made sense. Do you think you could point me to a specific math subject i can study that could better help me understand these types of equations/setups? Is it just algebra or arithmetic i never payed much attention in math so i dont know what types of problems im doing but i learn fast so ill be able to pick it up.

I’m no math wiz, so I don’t have have anything specific off the top of my head, but I know there are some on this forum that might.

But I’m not sure you really need more math in order to understand this. We usually start counting at 1. But with arrays we start counting at 0. So you just have to make the adjustment that the last number (index) won’t be the length of the array but rather the length of the array minus 1.

-1 isn’t the starting point here.

let i = str.length -1;

The starting point is the length of the string minus 1. So if the string has 10 characters in it then the starting point is 9. In this case, the first time through the for loop:

reversedStr += str[i];

will actually be:

reversedStr += str[9];

Ok so the -1 is because its saying the first iteration is already being done right there in that line

let i = str.length -1;

This is setting i to its initial value in the for loop. So the first time through the for loop the value of i is going to be one less than the length of the string you are looping through, which is the last character in the string. No iterations have been done at this point.

1 Like