Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print all elements of the array using a for loop.
DSA Typescript
const arr = [1, 2, 3, 4, 5]; for (let i = 0; i [1] arr.length; i++) { console.log(arr[i]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= causes an out-of-bounds error.
Using > or == will not iterate correctly.
✗ Incorrect
The loop should run while i is less than the array length to avoid going out of bounds.
2fill in blank
mediumComplete the code to sum all elements in the array using a for-of loop.
DSA Typescript
const numbers = [10, 20, 30]; let sum = 0; for (const num [1] numbers) { sum += num; } console.log(sum);
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'in' instead of 'of' causes iteration over indices.
Using 'from' or 'on' are invalid syntax.
✗ Incorrect
The 'for...of' loop iterates over values in the array.
3fill in blank
hardFix the error in the code to correctly reverse traverse the array.
DSA Typescript
const data = [5, 10, 15]; for (let i = data.length - 1; i [1] 0; i--) { console.log(data[i]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' or '<=' causes the loop to never run.
Using '>' excludes index 0.
✗ Incorrect
To reverse traverse the array including index 0, the condition should be i >= 0 when using i--.
4fill in blank
hardFill both blanks to create a loop that prints only even numbers from the array.
DSA Typescript
const nums = [1, 2, 3, 4, 5, 6]; for (let i = 0; i [1] nums.length; i++) { if (nums[i] [2] 2 === 0) { console.log(nums[i]); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causes out-of-bounds error.
Using '==' instead of '%' in the condition.
✗ Incorrect
The loop runs while i is less than the array length, and the modulo operator '%' checks if the number is even.
5fill in blank
hardFill all three blanks to create a new array with doubled numbers greater than 3.
DSA Typescript
const values = [1, 2, 3, 4, 5]; const result = values.filter(v => v [1] 3).map(v => v [2] [3]); console.log(result);
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' or '<=' in filter.
Using '+' instead of '*' in map.
Using wrong number instead of 2.
✗ Incorrect
Filter keeps numbers greater than 3, then map doubles them by multiplying by 2.
