Find the length without the length() method.
Cerosh Jacob
Software Engineering Manager @ Commonwealth Bank | Automation testing, technology innovation
The function accepts a string as input. It begins by initializing a variable, set to 0, to store the string's length. The function then uses a?for...of?loop to iterate through each character in the string. Within this loop, the variable is incremented for each character encountered. After iterating through all characters, the function returns the final value, representing the string's length.
The?for...of?loop is a construct designed to iterate over elements in iterable objects like strings, arrays, maps, sets, and other similar data structures. It provides a clear and concise syntax for individual element access. However, the?for...ofloop doesn't offer access to an element's index or position within the iterable. A traditional loop with an index variable may be more suitable if you require the index.
function findStringLength(str: string): number {
let count = 0;
for (const char of str) {
count++;
}
return count;
}
const count = findStringLength("Hello World");
console.log(count);
My recent publication compiled a comprehensive collection of 100 similar typescript programs. Each program not only elucidates essential Typescript concepts and expounds upon the significance of test automation but also provides practical guidance on its implementation using Playwright. This resource will certainly be valuable if you look deeper into similar topics.