Created
February 9, 2021 23:03
-
-
Save alpinstang/3a1192cb3ac2244c4a772168d12c3d2b to your computer and use it in GitHub Desktop.
Example to reverse a string in JS
This file contains hidden or 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
// given a string 'Hello my name is John.' | |
// reverse it | |
// output should be '.nhoJ si eman ym olleH | |
const s = 'Hello my name is John.'; | |
function reverseString(string) { | |
string = string.split(''); | |
string.reverse(); | |
return string.toString('').replace(/,/g, ''); | |
} | |
const reverseStringES6 = str => [...str].reverse().join(''); | |
console.log(reverseStringES6(s)); | |
// Solution Results... | |
// Solution time complexity: O(1) | |
// Solution space complexity: O(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment