Skip to content

Instantly share code, notes, and snippets.

@alpinstang
Created February 9, 2021 23:03
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 alpinstang/3a1192cb3ac2244c4a772168d12c3d2b to your computer and use it in GitHub Desktop.
Save alpinstang/3a1192cb3ac2244c4a772168d12c3d2b to your computer and use it in GitHub Desktop.
Example to reverse a string in JS
// 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