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.

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.

Thanks for your answer.

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");


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.

How can iterate inversely?

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

reverseString("jasda");

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

I am getting undefinedollh in the last cycle. How may I solve that?

function reverseString(str) {
let reversedStr=""; 
  for(let i=str.lastIndexOf('');i>-1;i--){

        let character = str.charAt(i);
              
           
          // console.log(str[i]);
             console.log(reversedStr+=str[i])  
             
  }               
                        
        
}

reverseString("hello");

Take a look at the second bullet point in my last post:

  • You are not setting the initial value of ‘i’ properly

When you try to access an element in an array that hasn’t been given a value you get ‘undefined’. It works the same way if you try to access a position in a string that doesn’t exist (i.e if your string is 5 characters long and you try to access the 6th character you will get ‘undefined’). In an array, how do you get the last index value of the array? That same logic works for strings. That’s the value you need to set ‘i’ to initially.

1 Like

I think I can access to the last index of a string this way.

let lengthStr=str.length;
  let lastLetter=str[lengthStr-1];

I can get the last letter this way, but it does not help me to finish the challenge. Why does it get undefined?

let lastLetter=str[str.legth-1];