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 ↴
recursion → programming technique where a function calls itself repeatedly to solve a problem.
substring() method → extracts characters from start index to end of string.
charAt() method → returns the character at a specified index in a string.
Recursion The act of a function calling itself.
Recursion is used to solve problems that contain smaller sub-problems.
A recursive function can receive two inputs: a base case (ends recursion) or a recursive case (resumes recursion).
Use recursion to find the factorial of 5.
let x = 5;
function factorial(num) {
if (num > 1) { Recursive call
return num * factorial(num - 1);
}
else { Base case
return 1;
};
}
call function
factorial(x); returns ↴
120 factorial of 5 → 120
substring() method returns the part of this string from the start index up to and excluding the end index, or to the end of the string if no end index is supplied. The original string is unchanged.
syntax
substring(indexStart)
substring(indexStart, indexEnd)
indexStart index of the first character to include in the returned substring.
indexEnd index of the first character to exclude from the returned substring.
const str3 = "hello world";
str3.substring(1); returns ↴
"ello world" characters from index 1 to end of string.
const str4 = "hello world";
str4.substring(3, 9); returns ↴
"lo wor" characters from index 3 to index 9 exclusive.
charAt() method returns the character at the given index.
If the index is out of range an empty string "" is returned.
const str5 = "hello";
str5.charAt(0); → "h" first character
str5.charAt(1); → "e"
str5.charAt(2); → "l"
str5.charAt(3); → "l"
str5.charAt(4); → "o"
str5.charAt(5); → "" not found
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.
Base case
The first line inside the function checks if str is an empty string.
if (str === "") return "" Base case
If true, it returns an empty string "" which serves as the stopping condition for the recursion, and ends execution of the function.
Recursive case
If str is not empty, the function calls itself with str.substring(1)
which returns the substring of str starting from the second character.
It then appends the first character of str at str.charAt(0) to the result of the recursive call. The result is then returned.
else return reverseString(str.substring(1)) + str.charAt(0) Recursive case
Call the function with ↴
reverseString(string1);
Reverse a string.
str = "Hello";
Iteration ↴
0 reverseString("Hello") calls reverseString("ello") + "H"
1 reverseString("ello") calls reverseString("llo") + "e"
2 reverseString("llo") calls reverseString("lo") + "l"
3 reverseString("lo") calls reverseString("o") + "l"
4 reverseString("o") returns "o"
After it reaches the Base case where str === "" it starts returning values as it unwinds the recursion.
The order in which the function calls are returned is the reverse order of the function calls.
The characters are concatenated to form the reversed string.
Iteration ↴
0 "o" + "l" gives "ol"
1 "ol" + "l" gives "oll"
2 "oll" + "e" gives "olle"
3 "olle" + "H" gives "olleH"
This gives the reversed string "olleH"
Reverse a string.
const string1 = "Hello World";
function reverseString(str) {
if (str === "") return ""; Base case
else return reverseString(str.substring(1)) + str.charAt(0); Recursive case
}
call function
reverseString(string1); returns ↴
"dlroW olleH"
Alternative ↴
Instead of an if/else statement use the ternary (conditional) operator.
function reverseString2(str) {
return str === "" ? "" : reverseString2(str.substring(1)) + str.charAt(0);
}
or
function reverseString3(str) {
return str === "" ? "" : reverseString3(str.slice(1)) + str[0];
}