Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to swap two elements in the arrays.
DSA Javascript
if (arr1[i] > arr2[j]) { let temp = arr1[i]; arr1[i] = arr2[j]; arr2[j] = [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning arr1[i] to arr2[j] again instead of temp.
Using wrong variable names for swapping.
✗ Incorrect
We store arr1[i] in temp, so to swap, arr2[j] should be assigned temp.
2fill in blank
mediumComplete the code to iterate through the first array.
DSA Javascript
for (let i = 0; i [1] arr1.length; i++) { // merging logic }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causes index out of range error.
Using '>' or '>=' causes the loop to never run.
✗ Incorrect
The loop should run while i is less than arr1.length to avoid out-of-bounds errors.
3fill in blank
hardFix the error in the condition to compare elements correctly.
DSA Javascript
while (j < arr2.length && arr1[i] [1] arr2[j]) { i++; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' reverses the logic and causes wrong merging.
Using '==' or '!=' does not help in sorting.
✗ Incorrect
We want to move i forward while arr1[i] is less than arr2[j] to find the correct position.
4fill in blank
hardFill both blanks to correctly swap and move pointers.
DSA Javascript
if (arr1[i] > arr2[j]) { let temp = arr1[i]; arr1[i] = arr2[j]; arr2[j] = [1]; j[2]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Incrementing wrong pointer after swap.
Not incrementing any pointer causing infinite loop.
✗ Incorrect
We assign temp to arr2[j] to swap, then increment j to move forward in arr2.
5fill in blank
hardFill all three blanks to complete the final sorting step.
DSA Javascript
for (let i = 0; i [1] arr2.length - 1; i++) { for (let j = 0; j [2] arr2.length - i - 1; j++) { if (arr2[j] [3] arr2[j + 1]) { let temp = arr2[j]; arr2[j] = arr2[j + 1]; arr2[j + 1] = temp; } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' in loops causes index errors.
Using '<' in if condition swaps incorrectly.
✗ Incorrect
The outer and inner loops use '<' to iterate correctly, and the if condition uses '>' to swap if current is greater than next.
