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 loop


for loop repeatedly executes a block of code until a specified condition evaluates to false.

The loop runs a block of code a set number of times, defined by an initialization, a condition, and an increment.

for (let x = 0; x < 4; x++) {

console.log(x);

}

Loop variable x is initialized to 0

Condition x < 4 is checked before each iteration.

The loop will continue to run as long as x is less than 4

The loop repeatedly executes a block of code 4 times, from 0 to 3

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

After each iteration, x is incremented by 1 x++

When x reaches 4 the condition evaluates to false, terminating the loop.

0

1

2

3 → printed to console


length property returns the number of characters in a string.

const str3 = "Hello World";

str3.length; returns ↴

11 → there are 11 characters in the string

Strings are zero indexed, the first character will be index 0

The last character will be at index length -1

To find the last index in a string.

const str4 = "Hello World";

str4.length - 1; returns ↴

10 → end index of string


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

for loop iterates through str backwards.

for (let x = str.length - 1; x >= 0; x--) {}

Strings are zero indexed ...

First character will be index str[0]

Last character will be index str[str.length - 1]

The loop variable is set to str.length - 1 → length of string - 1

And it will loop until str[x] is negative.

The loop will decrement by 1 after each iteration.

Append the current character str[x] to reversed

reversed += str[x]

The addition assignment += takes the value from the right of the operator and adds it to the variable on the left.

x += y is equivalent to x = x + y

Return the reversed string.

return reversed


Call the function with ↴

reverseString(string1)


Reverse a string.

str = "Hello"

reversed = "" variable to hold the reversed string.

Loop iterates backwards from the end of the string.

During each iteration reversed will be appended at the end of the string with the current character ↴

x Iteration ↴

4 oreversed "o"

3 l →  reversed "ol"

2 l →  reversed "oll

1 ereversed "olle"

0 Hreversed "olleH"

-1 for loop condition is false: loop ends.

The reversed string "olleH" is returned.


Reverse a string.

const string1 = "Hello World";

function reverseString(str) {

let reversed = "";

for (let x = str.length - 1; x >= 0; x--) {

reversed += str[x];

}

return reversed;

}

call function

reverseString(string1); returns ↴

"dlroW olleH"

Reverse a string