freeCodeCamp Challenge Guide: Reverse a String

freeCodeCamp Challenge Guide: Reverse a String
0

Reverse a String


Hints

Hint 1

We need to take the string and reverse it, so if it originally reads ‘hello’, it will now read ‘olleh’. You can solve this challenge by creating a new string (initializing it to a blank string “”) and then iterating the string starting from the last character through the first character and the concatenating each character to the new string. After iterating through all the characters in the string, you return the new string.


Solutions

Solution 1 (Click to Show/Hide)
function reverseString(str) {
  for (var reversedStr = "", i = str.length - 1; i >= 0; i--) {
    reversedStr += str[i];
  }
  return reversedStr;
}

Code Explanation

  • Starting at the last character of the string passed to the function, you build a new string reversedStr from str.

  • During each iteration of the for loop, reversedStr gets concatenated with itself and the current character.

  • Finally, you return the final value of reversedStr.

Solution 2 (Click to Show/Hide)
function reverseString(str) {
  return str
    .split("")
    .reverse()
    .join("");
}

Code Explanation

  • Our goal is to take the input, str, and return it in reverse. Our first step is to split the string by characters using split(''). Notice that we don’t leave anything in between the single quotes, this tells the function to split the string by each character.

  • Using the split() function will turn our string into an array of characters, keep that in mind as we move forward.

  • Next we chain the reverse() function, which takes our array of characters and reverses them.

  • Finally, we chain join('') to put our characters back together into a string. Notice once again that we left no spaces in the argument for join, this makes sure that the array of characters is joined back together by each character.

Relevant Links

16 Likes

Here is my own solution:

CATEGORY: BASIC:

function reverseString(str) {
var array = [];

array = str.split("");
arr = array.reverse();

return arr.join("");

}

3 Likes

Here is mine. Maybe is more clear for beginners. You can first split, hit the ENTER-see result, then
reverse it and at the end joint it together. On every step you can see you result on board.

function reverseString(str) {
str = str.split(’’);
str = str.reverse(’’);
str = str.join(’’);
return str;
}

reverseString(“hello”);

8 Likes

I am a beginner to this world of coding, but I was pretty proud of what I came up with. I created a variable rev to hold the split array, then reverse it, then join it together again in the return statement.

function reverseString(str) {
var rev = str.split("");
rev.reverse();
return str = rev.join("");
}

reverseString(“hello”);

2 Likes

My 2 cents code (but works):

function reverseString(str) {
var splitStr = str.split("");
var reverseStr = splitStr.reverse();
var joinedStr = reverseStr.join("");

return joinedStr;
}

reverseString(“hello”);

There is indeed a much bette way to do it after reading your posts :slight_smile:

this is simple way of making it work,

function reverseString(str) {
  var array = [];
  array = str.split('');
  array = array.reverse();
  str = array.join('');
  return str;
}

reverseString("hello");

also, try using cloud9 ide for debugging and console logging https://ide.c9.io/ or https://jsbin.com/ , freecodecamp doesn’t provide too much info on debugging

3 Likes

I ended up taking the “manual” route.

function reverseString(str) {
  var rev = "";
  str.split("");
  for(var i = str.length -1; i >= 0; i--){
    rev += str[i];
  }
  return rev;
}
reverseString("hello");

So, basically, I created the empty string variable: rev, split str into an array, looped through str from back to front and put each letter into my rev variable.

2 Likes

I played around with solutions and discovered that using the JavaScript methods above is actually a really code heavy solution. The methods hide the work that’s going on in the background to make them happen. I ended up creating this solution instead. Even though it looks a little longer it’s actually a lot shorter than the solution posted above :slight_smile:

function reverseString(str) {
newStr = “”;
for(i = str.length-1; i >= 0; i–) {
newStr += str[i];
} return newStr;
}

reverseString("magical);

13 Likes

Here is how I approached it:

function reverseString(str){

var strReverse = str.split(’ ‘).reverse().join(’ ');

return str.split( ’ ‘).reverse().join(’ ');
}

reverseString(“Greatings from Earth”);

Sorry,I made some mistakes with typing the single quotation marks.Should be like this:

function reverseString(str) {

var strReverse = str.split(’ ‘).reverse().join(’ ');

return str.split(’ ‘).reverse().join(’ ');
}
reverseString(“Greetings from Earth”);


function reverseString(str) {
  
  var strReverse = str.split('').reverse().join('');
  
  return str.split('').reverse().join('');
}

reverseString("hello");
2 Likes

No need to run all of those methods twice just return your new variable, or don’t declare a new variable and just use your existing return statement. Also, the methods look fancy and chaining them is fun, but @Becca941 is %100 correct. From what I understand, methods are fast, easy, and super useful, but often not the most efficient solution as far as processing time. (Something about big O notation??)

You only need to split, reverse, and join once. You are doing it twice, which is returning the same thing as with less code. Good work though, just some ‘useless’ code in there, meaning it is pointless because it’s already being done.

Here is what I mean:

function reverseString(str) {

return str.split(’ ‘).reverse().join(’ ');

}
reverseString(“Greetings from Earth”);

or you can do

function reverseString(str) {

var strReverse = str.split(’ ‘).reverse().join(’ ');

return strReverse;
}
reverseString(“Greetings from Earth”);
2 Likes

Thank you @kaym0.Now understand why I should practice writing short code.

This is my code so far. What is wrong? Thank if you can correct me!

function reverseString(str) {
str.split("");
str.reverse();
str.join();
return;
}

reverseString(“hello”);

Nevermind, got it. This is intermediate:
function reverseString(str) {
var splitted = str.split("");
var reversed = splitted.reverse();
var joined = reversed.join("");
return joined;
}

reverseString(“hello”);

My solution is same as yours :slight_smile:

function reverseString(str) {
var reversedStr=’’;

for (i=str.length-1; i>-1; i–)
{
reversedStr += str[i];
}
return reversedStr;
}

reverseString(“hello”);

2 Likes