Linear Search | Javascript program for linear search ; Run loop from 0 to right ; If search_element is found with left variable ; If search_element is found with right variable ; If element not found ; Driver code ; Function call
function search ( arr , search_Element ) { let left = 0 ; let length = arr . length ; let right = length - 1 ; let position = - 1 ; for ( left = 0 ; left <= right ; ) { if ( arr [ left ] == search_Element ) { position = left ; document . write ( " " + ( position + 1 ) + " " + ( left + 1 ) + " " ) ; break ; } if ( arr [ right ] == search_Element ) { position = right ; document . write ( " " + ( position + 1 ) + " " + ( length - right ) + " " ) ; break ; } left ++ ; right -- ; } if ( position == - 1 ) document . write ( " " + left + " " ) ; } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let search_element = 5 ; search ( arr , search_element ) ;