FCC Algorithm Challenges / Reverse a String
Tags: freecodecamp, algorithms, challenge, javascript
Published: 2020-04-24 13:45:19

---
title: FCC Algorithm Challenges / Reverse a String
published: true
description: My guide, notes, and solution to FreeCodeCamp's basic algorithm challenge, "Reverse a String"
tags: #freeCodeCamp, #algorithms, #challenge, #javascript
canonical_url: https://virenb.cc/fcc-002-reverse-string
series: Solving freeCodeCamp's Algorithm Challenges
---

Original post can also found at [https://virenb.cc/fcc-002-reverse-string](https://virenb.cc/fcc-002-reverse-string)

![Reverse a String](//images.ctfassets.net/yzwpq1epaq68/2Bjz8KsM3I26uqgBDZGvZy/ad740201ba5a90a012a136be0f0fd6d6/fcc-002-problem.png)

```javascript
function reverseString(str) {
  return str;
}

reverseString("hello");

/// TESTS
reverseString("hello") should return a string.
reverseString("hello") should become "olleh".
reverseString("Howdy") should become "ydwoH".
reverseString("Greetings from Earth") should return 
"htraE morf sgniteerG".
```

Above is the starter code provided for the challenge, "Reverse a String".

Our goal is to write a function that to take the input of a string and reverse the string (the tests show some examples). Let's think this through. Here is how I would aim to solve this problem.


## Method
1. Read (!) 
	* Read the instructions first. Make sure you understand what it being asked of you.
	* Read the starter code. Go line by line, just making sure you know what is going on initially.
	* Have a look at the tests. If the problem isn't clear to you, looking at the tests might give you an inclination of what kind of output you should aim for (i.e. instead of returning an array, maybe the problem is only asking for an index within the array).
	
  
2. Think & Write
	
	 Now that you've read through the instructions, starter code, and tests, it's time to analyze what to do and in what order. It may be handy to write out pseudocode.
	 
   
3. Code
	
	Once you've thought about what you'd like to do, and in what order, start to convert your pseudocode into JavaScript code. 


There's been too many times where I've tried to jump write into writing the code without thinking it through (in projects and coding challenges). That will leave you testing it way too many times, creating unnecessary variables, and running into more problems then you need to handle. If I try to follow the above method, it leaves me with a more clear mind of what I'm doing and hopefully writing some DRY code. Let's try to solve this problem now.

## Thoughts

* We're given a string and we have to reverse it and return a string
* The string data type has a few built in, String.length. It will provide us with the number of characters in the string. We would need length if we decided to use a loop
* There are some other handy methods and properties for strings and arrays (reverse for array)
* We can separate our string to become an array with the split() method. We use split('') so each character is separated into their own index in the array
* Running split() method on the string will give an array, then we can call reverse(), which will reverse the order of the array (backwards to forwards)
* Once it is reversed, there is a join() method that will convert the array back to a string, which is the data type we need to return
* We have to call join('') with quotations otherwise value is returned as "o,l,l,e,h" not "olleh"


## Solution

**Some Pseudocode**

```javascript
function reverseString(str) {
	// we have an argument of str, a string data type
	split it into an array
	reverse the array
	convert array back to string
	return string value
}

```

<details>
  <summary>**[SPOILER: SOLUTION TO CODE BELOW]**</summary>
  
```javascript
function reverseString(str) {
  return str.split('').reverse().join('');
}
```

**Alternative Solutions**

```javascript
function reverseString(str) {
  let strArray = [];
  for (let i = str.length; i >= 0; i--) {
    strArray.push(str[i]);
  }
  return strArray.join('');
}
```

The alternative solution is a little more verbose. We declare a variable and then loop through the string array.

  
</details>


## Links & Resources

[Reverse a String Challenge on FCC](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/reverse-a-string "Basic Algorithm Scripting: Reverse a String on FCC Website")

[FreeCodeCamp](https://www.freecodecamp.org/, "FreeCodeCamp")

[Donate to FCC!](https://www.freecodecamp.org/donate "FCC Donation Page")

[String.length on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length "MDN String.length")

[Array.join() on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join "Array.join() documentation on MDN")

[Array.reverse() on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse "Array.reverse() docs on MDN")

[String.split() on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split "String.split() docs on MDN")

[Solution on my GitHub](https://github.com/virenb/fcc-algorithms/blob/master/002---reverse-a-string.js "GitHub solution")

Thank you for reading!