<?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: Jaspal Singh</title>
    <description>The latest articles on DEV Community by Jaspal Singh (@jaspalsingh1998).</description>
    <link>https://dev.to/jaspalsingh1998</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%2F722707%2F9605abbc-1d45-41cf-b546-78aefee60e1c.jpeg</url>
      <title>DEV Community: Jaspal Singh</title>
      <link>https://dev.to/jaspalsingh1998</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jaspalsingh1998"/>
    <language>en</language>
    <item>
      <title>Binary Search in JavaScript🔥</title>
      <dc:creator>Jaspal Singh</dc:creator>
      <pubDate>Wed, 13 Oct 2021 06:46:01 +0000</pubDate>
      <link>https://dev.to/jaspalsingh1998/binary-search-in-javascript-2g4k</link>
      <guid>https://dev.to/jaspalsingh1998/binary-search-in-javascript-2g4k</guid>
      <description>&lt;p&gt;Hi everyone, I'm back with another blog in which we will understand binary search and implement it in JavaScript 🔥💜&lt;/p&gt;

&lt;h3&gt;
  
  
  Outline
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;What is Binary Search?&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Condition to implement Binary Search.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Key Points&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use Cases&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Working of Binary Search&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Code Implementation of Binary Search&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What is Binary Search?
&lt;/h3&gt;

&lt;p&gt;Binary Search is a searching algorithm. It is more efficient than other searching algorithms such as Linear Search. Binary Search basically works on &lt;strong&gt;divide and conquer&lt;/strong&gt; approach. That is after every search iteration the search space will get halved.&lt;/p&gt;

&lt;h3&gt;
  
  
  Condition to implement Binary Search
&lt;/h3&gt;

&lt;p&gt;The main condition to implement the binary search is that your &lt;strong&gt;array/list should be sorted&lt;/strong&gt; (either in increasing order or decreasing order).&lt;/p&gt;

&lt;h3&gt;
  
  
  Key points.
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;It is more efficient than Linear Search.&lt;/li&gt;
&lt;li&gt;It has better time complexity that is &lt;strong&gt;O(logn)&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Cannot be used with unsorted array/list&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Use Cases
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;If you want to search for smallest or greatest number in array.&lt;/li&gt;
&lt;li&gt;To check whether the target number is present in array or not.&lt;/li&gt;
&lt;li&gt;You can even search for users' data if users are stored in sorted manner.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Working of Binary Search
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Take an sorted array and a number that you wish to search in array.&lt;/li&gt;
&lt;li&gt;We need two variables for start and end that are going to act as pointers&lt;/li&gt;
&lt;li&gt;Value of start initially will be 0.&lt;/li&gt;
&lt;li&gt;Value of end will be last index of the array &lt;strong&gt;(you can easily find it as array.length-1)&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Now you need to have another variable known as mid point. &lt;strong&gt;(You can calculate mid point as Math.floor((start+end)/2) )&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;If the value at &lt;strong&gt;array[mid] is equal to your target number&lt;/strong&gt; than it is your answer. &lt;/li&gt;
&lt;li&gt;If target number is great than array[mid] value then update your start variable to &lt;strong&gt;start = mid + 1&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;If target number is less than array[mid] value then update your end variable to &lt;strong&gt;end = mid - 1&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Repeat it until start &amp;lt;= end.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Code Implementation.
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function binarySearch(arr, num){
  let start = 0;
  let end = arr.length-1;

  while(start &amp;lt;= end){
    let mid = Math.floor((start + end) / 2);

    if(arr[mid] == num){
      return mid;
    }else if(num &amp;gt; arr[mid]){
      start = mid + 1;
    }else if(num &amp;lt; arr[mid]){
      end = mid - 1;
    }
  }
  return -1; // if num is not present in the array
}

let studentIds = [11,12,15,19,23,45,54,91,100]

let result = binarySearch(studentIds, 100);
console.log(result);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;PS:- Checkout &lt;a href="https://www.cs.usfca.edu/~galles/visualization/Search.html"&gt;This amazing resource&lt;/a&gt; to see the visualization of Binary Search&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;I hope I was able to deliver something good to you guys ☺. Feedback, suggestions, etc are always welcomed.&lt;/p&gt;

&lt;p&gt;Have a fun and safe time and Thank you so much for dedicating your time to go through this blog ❤.&lt;/p&gt;

&lt;p&gt;You can follow me on &lt;a href="https://twitter.com/JaspalSCodes"&gt;Twitter&lt;/a&gt;💜😅&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Let's Learn and Grow Together. Adios amigos/amigas hasta la proxima vez 💜☕"&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>algorithms</category>
      <category>webdev</category>
    </item>
    <item>
      <title>RegEx in JavaScript with a cool project 🔥</title>
      <dc:creator>Jaspal Singh</dc:creator>
      <pubDate>Sun, 10 Oct 2021 13:07:51 +0000</pubDate>
      <link>https://dev.to/jaspalsingh1998/regex-in-javascript-with-a-cool-project-2e6m</link>
      <guid>https://dev.to/jaspalsingh1998/regex-in-javascript-with-a-cool-project-2e6m</guid>
      <description>&lt;p&gt;Hi everyone, This is my first blog on this platform in which we are going to learn regex with the help of a cool mini project.&lt;/p&gt;

&lt;h3&gt;
  
  
  Outline
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;What is RegEx?&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use cases of RegEx.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Different ways to define RegEx in JavaScript.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;RegEx Cheatsheet&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Some useful Flags in RegEx.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MetaCharacters&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Quantifiers&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Project&lt;/strong&gt; &lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  What is RegEx?
&lt;/h3&gt;

&lt;p&gt;RegEx is short for Regular Expresssions. RegEx are &lt;strong&gt;strings of special characters&lt;/strong&gt; that are used as &lt;strong&gt;patterns&lt;/strong&gt; to match against strings.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use cases of RegEx.
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;For validations (email validation, phone validation etc).&lt;/li&gt;
&lt;li&gt;Match and replace programs.&lt;/li&gt;
&lt;li&gt;Password pattern matching etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Ways to define RegEx in JavaScript.
&lt;/h3&gt;

&lt;p&gt;There are basically &lt;strong&gt;two&lt;/strong&gt; ways to define RegEx in JavaScript.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Using Literals&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Using RegEx Object&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's have a look at how to define RegEx using both ways.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Using Literals&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let regPat = /ab/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Using RegEx object&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let regPat = new RegExp('abc');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the RegEx object part, you &lt;strong&gt;do not need to import/require&lt;/strong&gt; anything to use it. Since, it is a &lt;strong&gt;global object&lt;/strong&gt; that is available to you everywhere in your code file.&lt;/p&gt;

&lt;p&gt;For this blog, I'll be using the &lt;strong&gt;literal way&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  RegEx Cheatsheet
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsdsxqfhbltxzxk0wrhsk.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsdsxqfhbltxzxk0wrhsk.jpg" alt="RegEx Cheatsheet"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Some useful Flags.
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;i&lt;/strong&gt; --&amp;gt; It is used to ignore case sensitivity.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;g&lt;/strong&gt; --&amp;gt; It is used to perform global search.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;s&lt;/strong&gt; --&amp;gt; It is used to match newline character.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;m&lt;/strong&gt; --&amp;gt; It is used to perform multi-line search.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You can use more than one flag in your RegEx pattern but it is important to write flags at the end.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For Example&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/gold/i.test('GolD')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this will output to &lt;strong&gt;true&lt;/strong&gt; because i flag at the end will ignore the case senstivity.&lt;/p&gt;

&lt;h3&gt;
  
  
  Metacharacters
&lt;/h3&gt;

&lt;p&gt;Metacharacters are used to match the following character either as the special character or the literals.&lt;br&gt;
Some of the metacharacters are written below.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;\d&lt;/strong&gt; --&amp;gt; to match the next character from [0-9]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;\D&lt;/strong&gt; --&amp;gt; to match the following character anything but digits.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;\s&lt;/strong&gt; --&amp;gt; to match the next character as whitespace/space.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;\w&lt;/strong&gt; --&amp;gt; to match the alphabets(both capital and small ones).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;\W&lt;/strong&gt; --&amp;gt; to match anything but alphabets.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Quantifiers.
&lt;/h3&gt;

&lt;p&gt;Quantifiers are used to tell how many number of characters or expressions you want to match.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For example: If you want to match 10 digits you will do something like this&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let regPat = /\d{10}/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Some of the quantifiers are as follow&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;[]?&lt;/strong&gt; --&amp;gt; It will match the occurrence of anything in bracket for 0 or 1 times.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;[]+&lt;/strong&gt; --&amp;gt; Check if occurs 1 or more times.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;[]&lt;/strong&gt;* --&amp;gt; Occurs 0 or more times.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;[]{n}&lt;/strong&gt; --&amp;gt; occurs n times.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;[]{n,}&lt;/strong&gt; --&amp;gt; occurs n or more times.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;[]{n,m}&lt;/strong&gt; --&amp;gt; occurs at least n times but should be less than m times.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Project.
&lt;/h2&gt;

&lt;p&gt;We will be building a cool mini project using RegEx to validate mobile number in US format.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function telephoneCheck(num){
  let result = false;
  const patterns = [
  /^\d{3}-\d{3}-\d{4}/,
/^\d{10}$/,
/^\d{3}\s\d{3}\s\d{4}/,
/^\(\d{3}\)\d{3}-\d{4}/,
/^1\(\d{3}\)\d{3}-\d{4}/,
/^1\s\(\d{3}\)\s\d{3}-\d{4}/,
/^1\s\d{3}\s\d{3}\s\d{4}/,
/^1\s\d{3}-\d{3}-\d{4}/
]

result = patterns.some(reg =&amp;gt; reg.test(num))
return result;
}

telephoneCheck("1 555 555 5555")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will return &lt;strong&gt;true&lt;/strong&gt; for every format that respects the US mobile number format otherwise &lt;strong&gt;false&lt;/strong&gt; will be returned.&lt;/p&gt;

&lt;p&gt;Following are the valid &lt;strong&gt;US phone number format&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;555-555-5555&lt;br&gt;
(555)555-5555&lt;br&gt;
(555) 555-5555&lt;br&gt;
555 555 5555&lt;br&gt;
5555555555&lt;br&gt;
1 555 555 5555&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;--&lt;/p&gt;

&lt;p&gt;I hope I was able to deliver something good to you guys ☺. Feedback, suggestions, etc are always welcomed.&lt;/p&gt;

&lt;p&gt;Have a fun and safe time and Thank you so much for dedicating your time to go through this blog ❤.&lt;/p&gt;

&lt;p&gt;You can follow me on &lt;a href="https://twitter.com/JaspalSCodes" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; 💜😅&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Let's Learn and Grow Together. Adios amigos/amigas hasta la proxima vez 💜☕"&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>codenewbie</category>
    </item>
  </channel>
</rss>
