Typescript - Finding Array Length
1. Typescript - Finding Array Length
Typescript - Finding Array Length
Create a function "arrLength" that takes an array as input that accept any type of array, calculate the number of elements present in it and return it. Use the function to find out the length of the array. Define proper types as and when required - return type and the param type of the function argument should be mentioned.
Sample Input 1:
["hi", "there"]
Sample Output 1: 2
Sample Input 2: [1,5,4]
Sample Output 2: 3
function arrLength<T>(arr: T[]): number {
return arr.length;
}
First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.