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
Find if all characters are unique using ↴
Set Object → collection of unique values.
for loop → executes a block of code a number of times.
has() method → returns true if a specified value exists in a Set, otherwise returns false.
add() method → insert a new element into a Set.
Set Object is a collection of unique values.
Each value can only occur once, each value is unique.
const mySet = new Set();
console.log(mySet); returns ↴
Set(0) {size: 0} → empty Set
To add values to the Set we can use the add() method.
mySet.add("a");
mySet.add("b");
mySet.add("c");
console.log(mySet); returns ↴
Set(3) {"a", "b", "c"} → values added
Try to add a duplicate value.
mySet.add("c"); character "c" is already in mySet
console.log(mySet); returns ↴
Set(3) {"a", "b", "c"} → no change
To determine if mySet has an element present we can use the has() method.
mySet.has("a"); → true
mySet.has("d"); → false
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
Initialize a variable to hold the string to be checked to find if all characters are unique.
const string1 = "ABCDEFG"; → user input
Define a function allCharactersUnique to find if all characters are unique.
function allCharactersUnique(str) {}
The function takes a string as input str and uses a Set to track characters that have already been encountered.
If a character is found in the Set, it means the character is not unique.
Initialize a Set to store unique characters.
const charSet = new Set() charSet
Loop through each character in str
for (let x = 0; x < str.length; x++) {}
Get the current character str[x]
const char = str[x] char
Check if the character char is already in the Set charSet
if (charSet.has(char)) {}
If it is, return false (not all characters are unique) and end execution of the function.
return false
If character not found, add it to the Set.
charSet.add(char)
If no duplicates were found, return true (all characters are unique).
return true
If the function returns true, all characters are unique ✔
If the function returns false, all characters are not unique ✖
Call the function with ↴
allCharactersUnique(string1);
Find if all characters are unique.
const str = "ABCCD"; string with duplicate character
const charSet = new Set(); charSet empty Set
Iterate through the string and check if the current character is in the Set.
for (let x = 0; x < str.length; x++) {} loop through string
if (charSet.has(char)) {} → is current character char in charSet Set
If current character char found, return false - character already exists in Set and so cannot be unique and end execution of function.
If current character char NOT found, add it to charSet Set.
charSet.add(char) → add char to charSet Set
If charSet has current character, return false
If charSet does not have current character, add to charSet Set.
Iteration ↴
0 has : "A" {"A"} add to Set
1 has : "B" {"A", "B"} add to Set
2 has : "C" {"A", "B", "C"} add to Set
3 has : "C" character not unique : return false and end execution of function.
If the loop completes without finding duplicates, the function returns true
Find if all characters are unique.
const string1 = "ABCDEFG";
function allCharactersUnique(str) {
const charSet = new Set();
for (let x = 0; x < str.length; x++) {
const char = str[x];
if (charSet.has(char)) {
return false;
}
charSet.add(char);
}
return true;
}
call function
allCharactersUnique(string1); returns ↴
true