0
0
DSA Javascriptprogramming~10 mins

Linear Search Algorithm 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 return the index of the target if found, or -1 if not found.

DSA Javascript
function linearSearch(arr, target) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === [1]) {
      return i;
    }
  }
  return -1;
}
Drag options to blanks, or click blank then click option'
Atarget
Bi
Carr
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using the loop index i instead of the target for comparison.
Comparing arr[i] with arr instead of target.
2fill in blank
medium

Complete the code to search through the array and return true if the target exists.

DSA Javascript
function contains(arr, target) {
  for (let i = 0; i < [1]; i++) {
    if (arr[i] === target) {
      return true;
    }
  }
  return false;
}
Drag options to blanks, or click blank then click option'
Atarget.length
Barr
Ci.length
Darr.length
Attempts:
3 left
💡 Hint
Common Mistakes
Using target.length which is incorrect because target is a value, not an array.
Using arr instead of arr.length in the loop condition.
3fill in blank
hard

Fix the error in the code to correctly perform linear search and return the index or -1.

DSA Javascript
function linearSearch(arr, target) {
  let index = -1;
  for (let i = 0; i <= [1]; i++) {
    if (arr[i] === target) {
      index = i;
      break;
    }
  }
  return index;
}
Drag options to blanks, or click blank then click option'
Aarr.length
Barr.length + 1
Carr.length - 1
Di.length
Attempts:
3 left
💡 Hint
Common Mistakes
Using arr.length causes index out of range error.
Using arr.length + 1 causes runtime error.
4fill in blank
hard

Fill both blanks to create a function that returns the index of the target or -1 if not found.

DSA Javascript
function search(arr, target) {
  for (let i = 0; i [1] arr.length; i++) {
    if (arr[i] [2] target) {
      return i;
    }
  }
  return -1;
}
Drag options to blanks, or click blank then click option'
A<
B<=
C===
D!==
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= in the loop condition causes out of bounds error.
Using !== instead of === in the if condition.
5fill in blank
hard

Fill all three blanks to create a function that returns true if the target is found, false otherwise.

DSA Javascript
function exists(arr, target) {
  for (let [1] = 0; [2] < arr.length; [3]++) {
    if (arr[i] === target) {
      return true;
    }
  }
  return false;
}
Drag options to blanks, or click blank then click option'
Ai
Dj
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in loop parts causes errors.
Using 'j' in one part and 'i' in others causes reference errors.