Reverse a string

Write a function that takes a string and returns a new string with the characters reversed.

The first character will become the last character, the last character will become the first character, and so on.


Example ...

Enter a string ...

"Hello World" → original string

"dlroW olleH" → reversed string

The function returns a new string with the characters reversed.

The original string is unchanged.

Strings are a sequence of zero or more characters written inside quotes used to represent text.

Strings may consist of letters, numbers, symbols, words, or sentences.

Strings are immutable, they cannot be changed.

Each character in a string has an index.

The first character will be index 0 the second character will be index 1 and so on.

There are two ways to access an individual character in a string.

charAt() method

const str1 = "abc"; string

str1.charAt(0); character at index 0 → "a"

str1.charAt(1); character at index 1 → "b"

str1.charAt(2); character at index 2 → "c"

str1.charAt(3); character at index 3 → "" index not found

Alternatively use at() or slice() methods

bracket notation []

const str2 = "abc"; string

str2[0]; character at index 0 → "a"

str2[1]; character at index 1 → "b"

str2[2]; character at index 2 → "c"

str2[3]; character at index 3 → undefined index not found


Reverse a string using a for...of loop.


for...of loop iterates through the values of an iterable object, such as an Array, String, Set, Map, ...

syntax

for (variable of iterable) {}

variable holds the current value of the iteration.

of keyword indicates that the loop should iterate over the values of the iterable.

iterable object that is iterable, such as an Array, String, Set, Map, ...

Iterate over each character in the string.

const str3 = "ABC";

for (const char of str3) {

console.log(char);

} returns ↴

A

B

C → printed to console

The loop will run three times, once for each character in the string.

On each iteration, the value of the current element is stored in the variable char

For each iteration of the loop, the current value of char is printed to the console.


Initialize a variable to hold the string to be reversed.

const string1 = "Hello World"; → user input


Define a function reverseString to reverse a string.

function reverseString(str) {}

The function takes a string as input str and returns a new string with the characters reversed. The original string remains unchanged.

Initialize a variable to hold the reversed string.

let reversed = "" reversed

Iterate over str

for (let character of str) {}

Prepend each character to reversed

reversed = character + reversed

Return the reversed string.

return reversed


Call the function with ↴

reverseString(string1);


Reverse a string.

str = "Hello"

reversed = "" variable to hold the reversed string.

for (let char of str) Iterate over str

During each iteration, the current character char will be prepended to reversed at the start of the string ↴

x Iteration ↴

0 char Hreversed "H"

1 char ereversed "eH"

2 char l →  reversed "leH

3 char l →  reversed "lleH"

4 char oreversed "olleH"

Loop exits when the iterator has completed.

The reversed string "olleH" is returned.


Reverse a string.

const string1 = "Hello World";

function reverseString(str) {

let reversed = "";

for (let character of str) {

reversed = character + reversed;

}

return reversed;

}

call function

reverseString(string1); returns ↴

"dlroW olleH"

Reverse a string