Today's tip is about how to reverse a string in Javascript, using only one line of code.
Explanation
If you are given a string to reverse. As an example, let's say you were asked to reverse my name 😜, Wemimo. This can be broken down to 4 steps 👇👇👇
const toReverse = 'Wemimo';
const toReverse = str => str.split('').reverse().join('');
Example:
console.log(toReverse('Wemimo'));
//Expected output: omimeW
Step One
- Split the given string into an array of substrings (a substring is a character between a string)
stepOne = toReverse.split('');
Step Two
- Reverse the array of substrings
stepTwo = stepOne.split('');
Step Three
- Join the substrings together to have a single string
stepThree = stepOne.reverse();
Step Four
- Join the substrings together to have a single string
stepThree = stepTwo.join('');
Food for Thought: “Whether you want to uncover the secrets of the universe, or you just want to pursue a career in the 21st century, basic computer programming is an essential skill to learn.”- Stephen Hawking
HAPPY CODING!!! ❤️
Add your comment