Reverse String
Challenge Description
Given a string, reverse the string and return the result. For example:
- "hello" becomes "olleh"
- "apple" becomes "elppa"
Tips to Solve
tip
- Convert the string to an array using the
splitmethod. - Reverse the array using the
reversemethod. - Convert the array back to a string using the
joinmethod.
Step-by-Step Solution
Let's break down the solution into easy steps:
-
Convert the String to an Array: Use the
splitmethod to convert the string into an array of characters.const strArray = "hello".split(""); // ["h", "e", "l", "l", "o"] -
Reverse the Array: Use the
reversemethod to reverse the order of the elements in the array.const reversedArray = strArray.reverse(); // ["o", "l", "l", "e", "h"] -
Convert the Array Back to a String: Use the
joinmethod to combine the elements of the array back into a string.const reversedString = reversedArray.join(""); // "olleh"
Full Solution Code
Here is the complete solution using the steps above:
const reverseString = (str) => str.split("").reverse().join("");
console.log(reverseString("hello")); // Output: "olleh"
console.log(reverseString("apple")); // Output: "elppa"