Skip to content

Instantly share code, notes, and snippets.

@hacktivist123
Created April 6, 2018 14:03
Show Gist options
  • Save hacktivist123/e75931c4f1d2f1881d2907e8d88e2bca to your computer and use it in GitHub Desktop.
Save hacktivist123/e75931c4f1d2f1881d2907e8d88e2bca to your computer and use it in GitHub Desktop.
Reverse-a-String created by hacktivist123 - https://repl.it/@hacktivist123/Reverse-a-String
//Given a String, return a new string with the reversed order of characters
// We'll be impementing it with 4 different solutions, to test out each solution just un-comment it
//solution 1
function reverse (str){
const arr = str.split('');
arr.reverse();
return arr.join('');
}
reverse('apple');
//solution 2
/*
function reverse (str){
return str
.split('').
reverse()
.join('');
}
reverse ('apple');
*/
//solution 3 using for loops and the ES6 forloop style
/*
function reverse (str){
let reversed = ('');
for(let character of str ){
reversed = character + reversed;
}
return reversed;
}
reverse ('apple');
*/
//solution 4 using array helper and reduce();
/*
function reverse(str){
str.split('').reduce((rev, char) =>
char + rev,
'' );
}
reverse('apple');
*/
//unforutnately the new ES6 features is not suppported on Repl.it so the code wont run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment