Skip to content

Instantly share code, notes, and snippets.

@xorcat
Created November 19, 2015 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xorcat/77cf7e888b4fa34bc624 to your computer and use it in GitHub Desktop.
Save xorcat/77cf7e888b4fa34bc624 to your computer and use it in GitHub Desktop.
freeCodeCamp - Bonfire - Reverse a String
// Bonfire: Reverse a String
// Author: @sycrat
// Challenge: http://www.freecodecamp.com/challenges/bonfire-reverse-a-string?solution=function%20reverseString(str)%20%7B%0A%20%20var%20stringArray%20%3D%20%5B%5D%3B%0A%20%20%0A%20%20return%20str%3B%0A%7D%0A%0AreverseString(%22hello%22)%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
// reverse the characters in a string
function reverseString(str) {
// initialise variables
var stringArray = [];
var reversed = "";
// split the string into an array, and reverse the elements
stringArray = str.split('');
stringArray.reverse();
// join the characters back into a string
reversed = stringArray.join('');
return reversed;
}
reverseString("hello");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment