Created
October 17, 2023 15:40
-
-
Save kevboutin/22feeb36a70e0ee1872e64dda901125d to your computer and use it in GitHub Desktop.
Reverse a string
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
/* The old way: | |
const reverseString = (str) => { | |
let reversed = ''; | |
for (let i = str.length - 1; i >= 0; i--) { | |
const ch = str[i]; | |
reversed += ch; | |
} | |
return reversed; | |
}; | |
const reverse = reverseString('javascript'); | |
console.log(reverse); // tpircsavaj | |
*/ | |
// The better way: | |
const reverseString = (str) => str.split('').reverse().join(''); | |
const reverse = reverseString('javascript'); | |
console.log(reverse); // tpircsavaj |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment