What if you could instantly and perfectly check every item in a huge list without missing a single one?
Why Array Traversal Patterns in DSA Javascript?
Imagine you have a list of your favorite songs written on paper. To find a specific song or to check all songs, you have to look at each one, one by one, from the start to the end.
Going through the list manually 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 your list without missing any. They help you move through the list smoothly and find or process items quickly and correctly.
const songs = ['Song1', 'Song2', 'Song3']; // Manually check each song console.log(songs[0]); console.log(songs[1]); console.log(songs[2]);
const songs = ['Song1', 'Song2', 'Song3']; for (let index = 0; index < songs.length; index++) { console.log(songs[index]); }
With array traversal patterns, you can easily and reliably process every item in a list, no matter how big, making your programs smarter and faster.
When you scroll through your photo gallery on your phone, the app uses array traversal patterns to show each photo one by one without missing any.
Manual checking of list items is slow and error-prone.
Array traversal patterns provide a clear, reliable way to visit each item.
This makes handling lists easier, faster, and less error-prone.
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: Check the action inside the loop
Each iteration prints arr[i], which is the element at index i, so all elements are printed in order.Final Answer:
Prints each element of the array from first to last -> Option CQuick Check:
Loop from 0 to length 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 JavaScript?Solution
Step 1: Identify the
Thefor...ofsyntaxfor...ofloop iterates over values directly, so syntax isfor (let element of arr).Step 2: Check each option
for (let element of arr) { console.log(element); } matches the correctfor...ofsyntax. Others use different loops or incorrect conditions.Final Answer:
for (let element of arr) { console.log(element); } -> Option AQuick Check:
for...ofloops over values [OK]
- Using 'for...in' instead of 'for...of'
- Using <= instead of < in loop condition
- Confusing index and element in loops
const arr = [10, 20, 30];
let result = '';
for (let i = arr.length - 1; i >= 0; i--) {
result += arr[i] + ' ';
}
console.log(result.trim());Solution
Step 1: Understand the loop direction
The loop starts at index 2 (arr.length - 1) and decrements to 0, so it goes backward through the array.Step 2: Trace the concatenation
Elements are added in order: arr[2] = 30, arr[1] = 20, arr[0] = 10, separated by spaces.Final Answer:
30 20 10 -> Option BQuick Check:
Loop backward prints reversed array [OK]
- Confusing loop direction and printing original order
- Off-by-one errors in loop condition
- Forgetting to trim trailing space
arr:const arr = [1, 2, 3];
for (let i = 0; i <= arr.length; i++) {
console.log(arr[i]);
}Solution
Step 1: Analyze loop condition
The loop runs while i <= arr.length, so i reaches arr.length which is out of bounds.Step 2: Understand effect of out-of-bounds access
Accessing arr[arr.length] returns undefined, so console.log prints undefined after last element.Final Answer:
Using <= causes accessing undefined element at the end -> Option AQuick Check:
Loop should use < not <= for array indexes [OK]
- Using <= instead of < in loop condition
- Ignoring undefined output at end
- Thinking array length is zero-based
arr = [[1, 2], [3, 4], [5, 6]], which code correctly prints all elements in order using nested loops?Solution
Step 1: Understand nested array structure
arr is an array of arrays, so to access each element, two loops are needed: outer for sub-arrays, inner for elements.Step 2: Check each option
for (let i = 0; i < arr.length; i++) { for (let j = 0; j < arr[i].length; j++) { console.log(arr[i][j]); } } uses two loops with correct ranges and indexes to print each element. for (let i of arr) { console.log(i); } prints sub-arrays, not elements. for (let i = 0; i < arr.length; i++) { console.log(arr[i]); } prints sub-arrays. for (let i in arr) { for (let j in arr) { console.log(arr[i][j]); } } incorrectly loops inner array length.Final Answer:
for (let i = 0; i < arr.length; i++) { for (let j = 0; j < arr[i].length; j++) { console.log(arr[i][j]); } } -> Option DQuick Check:
Nested loops needed for nested arrays [OK]
- Using single loop prints sub-arrays, not elements
- Using wrong inner loop length
- Confusing for...in with for...of in nested loops
Ready to prove it?
Pass the test and this pattern moves unknown
