feat(curriculum): Add interactive examples to How Can You Use String and Array Methods to Reverse a String (#63288)

Co-authored-by: Ilenia <26656284+ilenia-magoni@users.noreply.github.com>
This commit is contained in:
Clarence Bakosi
2025-10-30 12:21:14 +01:00
committed by GitHub
parent c228f6d1dc
commit 47d0079484

View File

@@ -5,13 +5,13 @@ challengeType: 19
dashedName: how-can-you-use-string-and-array-methods-to-reverse-a-string
---
# --description--
# --interactive--
Reversing a string is a common programming task that can be accomplished in JavaScript using a combination of string and array methods. The process involves three main steps:
- Splitting the string into an array of characters.
- Reversing the array.
- Joining the characters back into a string.
- Joining the characters back into a string.
Let's explore each of these steps using the `split()`, `reverse()`, and `join()` methods.
@@ -25,32 +25,44 @@ The first step in reversing a string is to convert it into an array of individua
Here's an example of using the split method to create an array of characters:
:::interactive_editor
```js
let str = "hello";
let charArray = str.split("");
console.log(charArray); // ["h", "e", "l", "l", "o"]
```
:::
In this example, we use `split("")` (with an empty string pass to it) to convert the string `hello` into an array of its individual characters. Once we have an array of characters, we can use the `reverse()` method to reverse the order of elements in the array.
The `reverse()` method is an array method that reverses an array in place. This means it modifies the original array rather than creating a new one. Here's how we can use it:
:::interactive_editor
```js
let charArray = ["h", "e", "l", "l", "o"];
charArray.reverse();
console.log(charArray); // ["o", "l", "l", "e", "h"]
```
:::
In this example, `reverse()` changes the order of elements in `charArray`, reversing it from `["h", "e", "l", "l", "o"]` to `["o", "l", "l", "e", "h"]`.
The final step is to convert the reversed array of characters back into a string. We can accomplish this using the `join()` method. The `join()` method creates and returns a new string by concatenating all of the elements in an array, separated by a specified separator string. If you want to join the characters without any separator, you can use an empty string as the argument. Here's an example:
:::interactive_editor
```js
let reversedArray = ["o", "l", "l", "e", "h"];
let reversedString = reversedArray.join("");
console.log(reversedString); // "olleh"
```
:::
In this example, `join("")` (with an empty string pass to it as an argument) combines all the characters in the array into a single string without any separator between them.
Remember that strings in JavaScript are immutable, which means you can't directly reverse a string by modifying it. That's why we need to convert it to an array, reverse the array, and then convert it back to a string. This combination of string and array methods provides a powerful and flexible way to manipulate strings in JavaScript.