Reverse a String challenge basic algorithms

Tell us what’s happening:

Hey guys,
The code i have written works in chrome console but i am not able to pass the challenge with this.
What’s wrong?

Your code so far


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

reverseString("hello");

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/reverse-a-string

Change your console.log() to console.log(str). Then see what does the console say? The console should tell you what is happening as a result of your logic.

Also, you can’t change individual characters in a string using bracket notation.

2 Likes
str[i] = str[i] + str[i];

What do you think what this line of code does?

1 Like

Hey @miku86,
I just found it does nothing.
Even if i do not put it, my code outputs the same thing.
I am at very basic level. So, i am sorry for that.

1 Like

Hey @zapcannon99,
Thanks for the reply.

I just understood this.

I don’t get this part however.

right now you have console.log(str[i]). It’s not going to help you very much in terms of letting you know what’s happening. All it’ll tell you is what you pulled out of str[i].

Let me change a little and say that instead of replacing, just add console.log(str) below or above the log you already have.

1 Like

you will need a new variable to hold the reversed string’s characters (can you guess what the variable type should be?). Then go through the given string , character by character, and assemble the new reversed string. This is just a hint so you have to investigate exactly what methods will work…

1 Like

Hey @hbar1st,
Thanks for the reply.

I think it should be an array. I declared the empty variable and then pushed string’s characters in it.
After that i googled for joining elements in array and i got join() method. It worked for me.
Am i approaching it right?

function reverseString(str) {
var reversed = ;
for(let i = str.length-1;i>=0;i–){
reversed.push(str[i]);

}
return reversed.join(“”);
}

reverseString(“hello”);

2 Likes

yes you got it! Nice.

1 Like

Now just make sure to wrap it in spoiler tags if it’s a working solution like [spoiler] You’re code in here [/spolier].

1 Like

Yes it works.
I have wrapped it in spoiler tags.
Thanks.

1 Like

Great work in solving the problem. The concepts of algorithms are tough for most people - particularly when you’re learning a new language at the same time and trying to apply both!

Just a tip that could shorten your code (though the wording of the challenge makes me suspect its tests will fail if you take this approach) - the JavaScript language has a couple of built-in methods that complete some of your steps while writing less code:

I see you’ve already found .join(). You could also use .split() to create an array from a string and .reverse() to flip it.

1 Like

Hi everyone this is my first post on the forums.
Once I pass the tests, I usually come here and see how my peers solved the challenge just to learn if there are better ways and so on. I enjoyed seeing your solutions to this challenge.

My solution to this challenge avoids using functions that we don’t have already seen on the curriculum. It uses a simple for loop that reads every single character in reverse order and then concatenates it to the string. Feel free to share your thoughts about my solution.

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

your code is Good but the loop runs n times that is the length of the string.
we can write the solution where loop runs half the time of the length of the string.