Discover how a simple pattern can turn a tiring task into a smooth, error-free process!
Why Array Traversal Patterns in DSA Typescript?
Imagine you have a list of your favorite songs written on paper. You want to find a song or count how many songs you have. You start from the first song and look at each one, one by one.
Doing this by hand is slow and tiring, especially if the list is very long. You might lose your place or skip songs by mistake. It's hard to keep track and easy to make errors.
Array traversal patterns are like having a clear, step-by-step way to look at each song in the list without missing any. This method helps computers quickly and correctly check every item in an array, making tasks like searching or counting easy and error-free.
let songs = ['Song1', 'Song2', 'Song3']; // Manually check each song if (songs[0] === 'Song2') { console.log('Found Song2'); }
let songs = ['Song1', 'Song2', 'Song3']; for (let index = 0; index < songs.length; index++) { if (songs[index] === 'Song2') { console.log('Found Song2'); } }
It enables efficient and reliable processing of every item in a list, unlocking powerful tasks like searching, filtering, and transforming data.
When you scroll through your phone's photo gallery, the app uses array traversal patterns to show each photo one by one smoothly and quickly.
Manual checking of list items is slow and error-prone.
Array traversal patterns provide a clear way to visit each item in order.
This makes searching and processing lists fast and reliable.
Practice
for (let i = 0; i < arr.length; i++) { console.log(arr[i]); }Solution
Step 1: Understand the loop range
The loop starts at 0 and runs while i is less than arr.length, so it covers all valid indexes.Step 2: Analyze the console.log statement
Each iteration prints the element at index i, so all elements are printed in order.Final Answer:
Prints each element of the array from start to end -> Option AQuick Check:
Standard forward traversal prints all elements [OK]
- Thinking it prints in reverse order
- Assuming it prints only one element
- Confusing index with value
arr using a for-of loop in TypeScript?Solution
Step 1: Identify for-of loop syntax
The for-of loop syntax is: for (let element of array) { ... }Step 2: Match with options
for (let item of arr) { console.log(item); } matches this syntax and prints each element directly.Final Answer:
for (let item of arr) { console.log(item); } -> Option BQuick Check:
for-of loops use 'of' keyword to iterate elements [OK]
- Using for-in to get elements instead of indexes
- Confusing for-of with traditional for loop syntax
- Using wrong loop conditions
const arr = [10, 20, 30];
let result = [];
for (let i = arr.length - 1; i >= 0; i--) {
result.push(arr[i]);
}
console.log(result);Solution
Step 1: Understand the loop direction
The loop starts at index 2 (arr.length - 1) and decrements to 0, traversing the array backward.Step 2: Track elements pushed to result
Elements pushed in order: arr[2] = 30, arr[1] = 20, arr[0] = 10, so result = [30, 20, 10].Final Answer:
[30, 20, 10] -> Option DQuick Check:
Reverse traversal collects elements backward [OK]
- Assuming forward order output
- Confusing index bounds causing undefined
- Thinking it causes syntax error
arr:const arr = [1, 2, 3];
for (let i = 0; i <= arr.length; i++) {
console.log(arr[i]);
}Solution
Step 1: Check loop condition
The loop runs while i <= arr.length, which means i can be equal to arr.length (3), an invalid index.Step 2: Understand array indexing
Array indexes go from 0 to arr.length - 1, so accessing arr[3] is undefined and may cause unexpected output.Final Answer:
The loop condition should be i < arr.length, not i <= arr.length -> Option AQuick Check:
Array indexes go from 0 to length-1 [OK]
- Using <= instead of < in loop condition
- Starting index at 1 instead of 0
- Thinking for-of is mandatory
arr of numbers, which code snippet correctly creates a new array containing only the even numbers from arr using array traversal?Solution
Step 1: Analyze const evens = []; for (let i = 0; i < arr.length; i++) { if (arr[i] % 2 === 0) evens.push(arr[i]); }
Uses a for loop with index to check each element and push even numbers. Correct traversal and filtering.Step 2: Analyze const evens = arr.filter(x => x % 2 === 0); and C
const evens = arr.filter(x => x % 2 === 0); uses filter method which internally traverses array and filters evens. const evens = []; for (let x of arr) { if (x % 2 === 0) evens.push(x); } uses for-of loop to traverse and filter evens. Both are correct.Step 3: Combine understanding
All three options correctly traverse and select even numbers.Final Answer:
All of the above -> Option CQuick Check:
Multiple traversal patterns can filter arrays correctly [OK]
- Assuming only for loop works
- Confusing filter syntax
- Forgetting to check modulo condition
Ready to prove it?
Pass the test and this pattern moves unknown
