# freeCodeCamp Algorithm Challenge Guide: Reverse a String

freeCodeCamp Algorithm Challenge Guide: Reverse a String
0

#1

Remember to use `Read-Search-Ask` if you get stuck. Try to pair program and write your own code

### Problem Explanation:

We need to take the string and reverse it, so if it originally reads ‘hello’, it will now read ‘olleh’. We will need to split the string, and therefore we will be working with Arrays as well.

## Hint: 1

Start by splitting the string by characters.

try to solve the problem now

## Hint: 2

Look up the built in function to reverse a string.

try to solve the problem now

## Hint: 3

Do not forget to join the characters back together after you reverse them.

try to solve the problem now

## Hint: 4
You don’t need to specify a plit character

## Basic Code Solution:

``````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.

## NOTES FOR CONTRIBUTIONS:

• DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.
• Categorize the solution in one of the following categories — Basic, Intermediate and Advanced.

See `Wiki Challenge Solution Template` for reference.

Discussion, Questions, and Resources for Part 2 (JavaScript Basic Algorithms, Object-Oriented, and Functional Programming - April 2018 Cohort)
#2

#3

#4

Here is my own solution:

CATEGORY: BASIC:

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

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

return arr.join("");

}

#5

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

#6

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

#7

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

#8

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

#9

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.

#10

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

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

reverseString("magical);

#11

Here is how I approached it:

function reverseString(str){

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

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

reverseString(“Greatings from Earth”);

#12

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

#13
``````
function reverseString(str) {

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

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

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

#14

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??)

#15

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”);``````

#16

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

#17

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

#18

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

#19

My solution is same as yours

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

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

reverseString(“hello”);