0
0
DSA Javascriptprogramming~10 mins

Binary Search Iterative Approach in DSA Javascript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the middle index in the binary search loop.

DSA Javascript
let mid = Math.floor(([1] + high) / 2);
Drag options to blanks, or click blank then click option'
A0
Bmid
Chigh
Dlow
Attempts:
3 left
💡 Hint
Common Mistakes
Using mid instead of low causes incorrect calculation.
Using high alone does not consider the lower bound.
2fill in blank
medium

Complete the code to update the low index when the target is greater than the middle element.

DSA Javascript
if (arr[mid] < target) {
  [1] = mid + 1;
}
Drag options to blanks, or click blank then click option'
Amid
Blow
Chigh
Dtarget
Attempts:
3 left
💡 Hint
Common Mistakes
Updating high instead of low.
Setting low to mid instead of mid + 1.
3fill in blank
hard

Fix the error in the loop condition to correctly run the binary search.

DSA Javascript
while ([1] <= high) {
  let mid = Math.floor((low + high) / 2);
  // rest of code
}
Drag options to blanks, or click blank then click option'
Atarget
Bmid
Clow
Dhigh
Attempts:
3 left
💡 Hint
Common Mistakes
Using mid in the condition causes infinite loops.
Using target in the condition is invalid.
4fill in blank
hard

Fill both blanks to correctly update the high index when the target is less than the middle element.

DSA Javascript
if (arr[mid] > target) {
  [1] = mid [2] 1;
}
Drag options to blanks, or click blank then click option'
Ahigh
Blow
C-
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Updating low instead of high.
Adding 1 instead of subtracting 1.
5fill in blank
hard

Fill both blanks to return the index if the target is found, or -1 if not found.

DSA Javascript
if (arr[mid] === target) {
  return [1];
}

// After loop
return [2];
Drag options to blanks, or click blank then click option'
Amid
B-1
Clow
Dhigh
Attempts:
3 left
💡 Hint
Common Mistakes
Returning low or high instead of mid.
Returning 0 instead of -1 when not found.