<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: generally23</title>
    <description>The latest articles on DEV Community by generally23 (@generally23).</description>
    <link>https://dev.to/generally23</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F182077%2F8a968bb2-626b-4824-b9a5-278aee1bc681.png</url>
      <title>DEV Community: generally23</title>
      <link>https://dev.to/generally23</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/generally23"/>
    <language>en</language>
    <item>
      <title>Linear Searching Algorithm in Javascript</title>
      <dc:creator>generally23</dc:creator>
      <pubDate>Mon, 17 Jun 2019 22:06:54 +0000</pubDate>
      <link>https://dev.to/generally23/linear-searching-algorithm-in-javascript-g76</link>
      <guid>https://dev.to/generally23/linear-searching-algorithm-in-javascript-g76</guid>
      <description>&lt;h1&gt;
  
  
  Naive Linear Searching Approach
&lt;/h1&gt;

&lt;h2&gt;
  
  
  First what is linear search?
&lt;/h2&gt;

&lt;p&gt;Linear search is the process of looking for a certain value in a list or set of values (e.g) looking for a value in a javascript array in a linear fashion, which means looking by order since javascript arrays are indexed we can compare what we're looking for to the current value we're looking at in the array and return true if we find it, false if not. note that there are different ways of searching but in this particular post we're going to focus on linear search only&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementation
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// first, let's create the function used to do the search
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// as you can see in this post I am using ES6(new version of Javascript)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;linearSearch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;currentValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;currentValue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Hell no!!! what is all of this? don't worry it's simple&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
javascript
/*
1. First we created a function called linearSearch.
2. We passed two arguments to our function.
3. The first argument is the list we are searching in.
4. The second argument is the value to look for inside the list.
5. We declared a variable called i.
6. Now we want to iterate or (loop) over the list.
7. We are comparing the currentValue on the list to the query argument
8. if they are equal we are returning the index we found the value we were looking for.
9. if not we are returning -1 which means we did not found it.
/*

That's it Enjoy!!!




&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

</description>
      <category>javascript</category>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
