# Basic Algorithm Scripting: Reverse a String*

Hi, I need a little orientation in this problem: characters cannot be count, how can I compare the spaces, or to add a value for reversing.?

Do you have any code?

1 Like

No. Because I am reading the documentation to start, but I do not how to add values nor use a method to reverse them.

Then I would recomend going through the JS course because to do what you asked you will need to code it

1 Like

I’m guessing you could google this and find a code example in a matter of seconds.

1 Like

Of, course. But I am avoiding that. I want to learn with documentantions. I am trying to get a hint how to start it

for, example how I make a comparation beetween letters if the do not have a value? If I know a value, I could know the first value and the last value in a string.

How would you do this manually? What steps would you physically take to reverse a string given to you? Don’t think in code right now. Think about the algorithm.

See if you can describe to me in words how to reverse the string “hello” so that it becomes “olleh”.

1 Like

I should take ", set it at the first place. Then, I need to take h, and set it at the last position. Then, I will take e and I will set it in 4th place, the l at the third place l, and ll in the second place. The last place will occupied by the h.

I know how to separate them and I know how to jin them, but I do now know yet how to change positions. I want to take the first element and I want to set it in the last position. Is there a method for it?

``````function reverseString(str) {

let newArray=str.split('');

//let jointString=newArray.join('');

console.log(jointString);
return str;
}

reverseString("ab");

``````

Think about how you could “build” a new string that starts as an empty string. You do not need to split the string up. You can simply iterate through the string in reverse and concatenate the letters to the new string and return the new string when you have iterated through all the letters in the original string.

1 Like
``````function reverseString(str) {

let newArray=str.split('');

let reversedArray=newArray.reverse();

let jointString=reversedArray.join('');

console.log(jointString);

return jointString;
}

reverseString("h");

``````

I have done it. I am thankful for your time.

See if you can do it like I said. You do not need to use arrays or array methods here.

1 Like

How can iterate inversely?

``````function reverseString(str) {
let newString="";
for (var i =0;i <= str.length; i++){
console.log(i);
}
return str;
}

reverseString("jasda");

``````

You need to start i at a different number, change the for loop condition, and decrement i instead of incrementing it.

I am getting the returned numbers, but I am not getting the letters.

``````function reverseString(str) {

for (var i =3;i >= str; i--){
let newString="";

let reversedString=newString.concat(i);

console.log(reversedString);
}
return str;
}

reverseString("");
``````

A few things:

• We don’t use ‘var’ to declare variables any more, so change that to ‘let’
• You want the for loop to transverse the entire length of the string you are passing into the function, so you can’t set ‘i’ to an arbitrary number, you need it to start at the last letter in the string (what is the idiom for specifying the last index in a string?)
• You want the for loop to stop after you reach the first letter in the string, so ‘i’ should be greater or equal to what number? (what is the index of the first letter in the string?)
• You can’t declare reversedString inside of the for loop because that creates a new variable and thus will wipe away what you had saved to it in the previous iteration
• You are not using the String.concat method properly (it takes two arguments). Besides, there is an easier way to concatenate strings using ‘+’ and ‘=’ together
• You want to return the new reversed string you create in the function, not the string that was passed into the function

I have the letter of every cycle, but I need to concatenate them.

``````function reverseString(str) {

for(let i=str.lastIndexOf('');i>-1;i--){

let character = str.charAt(i);

}

}

reverseString("hello");

``````

A string can be treated like an array when you want to access an individual character in it, so each letter in the string can be accessed using array notation. For example, the first letter in the string can be accessed at index 0:

``````let str = 'hello';
let firstLetter = str[0];
``````

After the above code runs, the value of firstLetter is ‘h’.

Concerning your code above, there are a few issues:

• Declare ‘i’ using ‘let’. Never declare a variable without using ‘let’ or ‘const’.
• You are not setting the initial value of ‘i’ properly. Now that you know that strings can be accessed like arrays, how do you find the index number of the last item in an array. That is what you need to do here.
• You are still returning the original string passed into the function. You want to return the new reversed string you create in the function.
• Speaking of which, where is the declaration for the new reversed string? It was there previously, it was just in the wrong place.
1 Like