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
Numbers are used to represent both integer and floating-point values.
Numbers are most commonly expressed in literal forms like 255 or 3.14159 ↴
let num1 = 5; → number
let num2 = 2.5; → number
let num3 = num1 + num2;
console.log(num3); returns ↴
7.5 → number
Objects are a data structure used to store related data collections.
It stores data as key-value pairs, where each key is a unique identifier for the associated value.
Each key must be a string and must be unique, each value can be any data type.
If you define an object with duplicate keys, the last one will overwrite any preceding ones.
Find the value for any given key in the object.
const obj1 = {"A": 4, "B": 5, "C": 6 }; object
obj1["A"]; key "A" → 4
obj1["B"]; key "B" → 5
obj1["C"]; key "C" → 6
obj1["D"]; key "D" → undefined key not found
Find the value for any given key in the object.
const obj2 = {"A": 4, "B": 5, "C": 6 }; object
const str = "ABC"; string
obj2[str[0]]; → 4
obj2[str[1]]; → 5
obj2[str[2]]; → 6
obj2[str[3]]; → undefined key not found
Find first non-repeating character in 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 find first non-repeating character.
const string1 = "AABBCDDE"; → user input
Define a function firstNonRepeatedChar to find first non-repeating character.
function firstNonRepeatedChar(str) {}
The function takes a string as input str and returns the first non-repeated character in that string.
The function counts the number of occurrences of each character and stores them in an Object.
By iterating through the string twice, it first builds a count of each character and then identifies the first character that appears only once.
If all characters repeat more than once anywhere in the string, it returns null
The first loop counts character occurrences, while the second loop identifies the first unique character.
The function does not use a nested loop; it utilizes two separate loops, each iterating through the string independently.
Create an object to store character counts.
const charCount = {} charCount
The function employs two loops: the first for counting characters and the second for finding the first non-repeated character.
Loop 1:
A variable char is used to represent the current item of the iterable being looped through.
for (const char of str) {
charCount[char] = (charCount[char] || 0) + 1
}
for-of loop iterates over each character in str, updating the charCount object.
For each character, if it is encountered for the first time, initialize its count to 0
otherwise increment its existing count by 1
const str = "AABBCDDE"
const charCount = {}
for (const char of str) {
charCount[char] = (charCount[char] || 0) + 1
console.log(charCount)
} returns ↴
"A" → {A: 1}
"A" → {A: 2}
"B" → {A: 2, B: 1}
"B" → {A: 2, B: 2}
"C" → {A: 2, B: 2, C: 1}
"D" → {A: 2, B: 2, C: 1, D: 1}
"D" → {A: 2, B: 2, C: 1, D: 2}
"E" → {A: 2, B: 2, C: 1, D: 2, E: 1}
Count of occurrences of each character ↴
"A" → 2 occurrences
"B" → 2 occurrences
"C" → 1 occurrence
"D" → 2 occurrences
"E" → 1 occurrence
Loop 2:
The second loop is another for-of loop and it checks each character in str against the charCount object.
Does the current char in charCount have a count of exactly 1
charCount[char] === 1
for (const char of str) {
if (charCount[char] === 1) { 1 only
return char first char with a count of 1
}
}
The first character with a count of 1 is returned as the result.
"C" is the first character with a count of 1
If no non-repeated character is found, return null.
return null
Call the function with ↴
firstNonRepeatedChar(string1);
Find first non-repeating character in string.
const string1 = "AABBCDDE";
function firstNonRepeatedChar(str) {
const charCount = {};
for (const char of str) {
charCount[char] = (charCount[char] || 0) + 1;
}
for (const char of str) {
if (charCount[char] === 1) {
return char;
}
}
return null;
}
call function
firstNonRepeatedChar(string1); returns ↴
"C"