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
split
method. - Reverse the array using the
reverse
method. - Convert the array back to a string using the
join
method.
Step-by-Step Solution
Let's break down the solution into easy steps:
-
Convert the String to an Array: Use the
split
method to convert the string into an array of characters.const strArray = "hello".split(""); // ["h", "e", "l", "l", "o"]
-
Reverse the Array: Use the
reverse
method 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
join
method 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"