Last active
January 3, 2018 06:58
-
-
Save eldyvoon/ab28979f26a50e5c4e4fe0b6754cdd75 to your computer and use it in GitHub Desktop.
Reverse String in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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