Skip to content

Instantly share code, notes, and snippets.

@eldyvoon
Last active January 3, 2018 06:58
Show Gist options
  • Save eldyvoon/ab28979f26a50e5c4e4fe0b6754cdd75 to your computer and use it in GitHub Desktop.
Save eldyvoon/ab28979f26a50e5c4e4fe0b6754cdd75 to your computer and use it in GitHub Desktop.
Reverse String in JavaScript
//specs
reverseString('hello world') //dlrow olleh
//solution 1
function reverseString(str) {
return str.split('').reverse().join('')
}
//solution 2
function reverseString(str){
let reversed = '';
for(let char of str){
reversed = char + reversed;
}
return reversed;
}
//solution 3
function reverseString(str) {
return str.split('').reduce((reversed, char)=>char + reversed,'')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment