Monday, November 16, 2015

Linear Search in JavaScript

A simple program that I  wrote in JavaScript the shows the concept of linear search or sequential search of a series of numbers given by the user.

If you  have some questions please send me an email at jake.r.pomperada@gmail.com and jakerpomperada@yahoo.com

My mobile number here in the Philippines is 09173084360.




Sample Program Output


Program Listing

<html>
 <head>
   <title> Linear Search </title>
  </head>
<body> 
   <script>

   var values=[];

  for (a=0; a<5; a++) {
   input = values.push(Number(prompt("Enter item value at no. " + (a+1))));
 }

  search_value = Number(prompt("Enter value to find"));

  document.write("<font face='arial' size='4'>List of values");
  document.write(" given by the user </font>");
  document.write("<br><br>");
  
  for (a=0;a<5; a++) {
    document.write("<font face='arial' size='4'> " +values[a] + "");
   }
  
  document.write("<br>");
  
  for (a=0; a<5; a++) {
    if (values[a] == search_value)
{
 document.write("<br>");
 document.write("<font face='arial' size='4'>The value number "
 +search_value +  " is present at location " + (a+1) + ". </font>");
  break;
}
  }  
  if (values[a] != search_value) {
   document.write("<br>")
   document.write("<font face='arial' size='4'> The value number "  
   + search_value + " is not present from the list. </font>");
   }
  </script>
  </body>
 </html>  

   

No comments:

Post a Comment