Javascript Algorithm Array Search Linear Search

Description

Javascript Algorithm Array Search Linear Search

function linearSearch(array, n) {
    for (var i = 0; i < array.length; i++) {
        if (array[i] == n) {
            return true;
        }//from   w  ww. j  av a 2  s .co  m
    }
    return false;
}

console.log(linearSearch([1,2,3,4], 4));
console.log(linearSearch([1,2,3,4], 5));



PreviousNext

Related