Course: JavaScript

Progress (0%)

Exercise: Linear Search

Exercise 4 Very easy

Prerequisites for the exercise

  1. JavaScript Functions
  2. JavaScript Control Flow
  3. All previous chapters

Objective

Create a function to search for a given value in an array.

Description

Suppose we have a given array and want to check whether it has a given item in it or not.

This can easily be done by sequentially iterating over the array and comparing each subsequent element with the item to find.

An algorithm that operates in this manner in order to search for a value in a array is generally known as a sequential search, or linear search, algorithm.

The term 'linear' comes from the fact that the running time of this algorithm can be expressed as a linear function on the length of the array.

In this exercise, you have to create a function linearSearch() that implements the linear search algorithm in order to search for an item in a given array.

Its general form should be as follows:

function linearSearch(arr, target) {
   // Code here...
}

Two arguments should be provided to the function — the first is the array where to search for the target item, while the second is the item itself.

The function should return true if the target item exists in the array, or else false.

Note that the function should search exactly for the target in the array.

Shown below are a couple of examples of the usage of the function:

linearSearch([1, 2, 3], 2)
true
linearSearch([1, 2, 3], '2')
false
linearSearch(['2', '4', '6'], '2')
true
linearSearch(['2, 6', '1, 4'], '2')
false
linearSearch([false, false, false, true], false)
true
View Solution

New file

Inside the directory you created for this course on JavaScript, create a new folder called Exercise-4-Linear-Search and put the .html solution files for this exercise within it.

Solution

The idea of linear search is very simple: use a for loop to iterate over all the items in the given array, comparing each element with the target item, and then return true if at any point, a match is found; or else return false.

Woah, that was a long paragraph!

In the code below, we set up the loop to iterate over arr:

function linearSearch(arr, target) {
   for (var i = 0; i < arr.length; i++) {
      // Code to go here...
   }
}

So far, so good.

Inside the loop, only one elementary if conditional ought to be placed. It checks if the current element is identical to the target. If it really is, true is returned, immediately halting further execution.

Otherwise, if the conditional's body never executes, which means that the target doesn't exist in the given array, false is returned at the end of the function.

Altogether, we get to the following code:

function linearSearch(arr, target) {
   for (var i = 0; i < arr.length; i++) {
      if (arr[i] === target) {
         return true;
      }
   }

   return false;
}

"I created Codeguage to save you from falling into the same learning conundrums that I fell into."

— Bilal Adnan, Founder of Codeguage