Nodejs Array Search Linear linearSearch(val)

Here you can find the source of linearSearch(val)

Method Source Code

Array.prototype.linearSearch = function(val){
  var answer = this.reduce(function(prev, curr, i){
    if (curr === val) {
      prev = i;//from w w  w. ja  v  a 2s .  co m
      return prev;
    } else {
      return prev;
    }
  }, -1);
  return answer;
};

Related

  1. linearSearch(val)
    Array.prototype.linearSearch = function(val){
      for (var i=0; i<this.length; i++) {
        if (this[i] === val) {
          return i;
      return -1;
    };
    
  2. linearSearch(val)
    Array.prototype.linearSearch = function(val){
      for (var i = 0 ; i < this.length; i++) {
        if (this[i] === val)
          return i;
      return -1;
    };
    
  3. linearSearch(val)
    Array.prototype.linearSearch = function(val){
      for (var i = 0; i < this.length; i++) {
        if(this[i] === val) {
          return i
      return -1;
    Array.prototype.binarySearch = function(val){
    ...