Practice Challenges1/15

Reverse a String

Challenge: Reverse a String 🔄

Why this matters: reversing text shows up in palindromes, formatting utilities, and low‑level string processing—great practice for thinking about indexes and order.

Write a function that reverses any string.

Examples

reverse("hello");     // "olleh"
reverse("JavaScript"); // "tpircSavaJ"
reverse("12345");     // "54321"

Hints

There are many ways to solve this:

Method 1: Split, Reverse, Join

str.split("");   // ["h", "e", "l", "l", "o"]
.reverse();      // ["o", "l", "l", "e", "h"]
.join("");       // "olleh"

Method 2: Loop backwards

let result = "";
for (let i = str.length - 1; i >= 0; i--) {
  result += str[i];
}

Your Turn!

Implement the reverse function.