<?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>Forem: Kazi Md. Mehedi Hasan</title>
    <description>The latest articles on Forem by Kazi Md. Mehedi Hasan (@kazimdmehedihasan).</description>
    <link>https://forem.com/kazimdmehedihasan</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%2F779579%2F3d6d508a-d691-4202-9053-9391fd53d5e7.jpeg</url>
      <title>Forem: Kazi Md. Mehedi Hasan</title>
      <link>https://forem.com/kazimdmehedihasan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/kazimdmehedihasan"/>
    <language>en</language>
    <item>
      <title>Binary Search with JavaScript</title>
      <dc:creator>Kazi Md. Mehedi Hasan</dc:creator>
      <pubDate>Thu, 10 Mar 2022 05:32:00 +0000</pubDate>
      <link>https://forem.com/kazimdmehedihasan/binary-search-with-javascript-29l</link>
      <guid>https://forem.com/kazimdmehedihasan/binary-search-with-javascript-29l</guid>
      <description>&lt;p&gt;Binary Search is one of the most intuitive and optimal algorithms for finding an element in a sorted array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A quick look at binary search&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;To implement binary search, the array must be sorted&lt;/li&gt;
&lt;li&gt;Much Faster than linear search.&lt;/li&gt;
&lt;li&gt;Has a time complexity of O(logN).&lt;/li&gt;
&lt;li&gt;Rather than eliminating one element at a time, it can eliminate half of the remaining elements at a time.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Execution Methodology :&lt;/strong&gt;&lt;br&gt;
Binary search algorithm makes use of the "Divide and Conquer" approach.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Binary Search pseudocode:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;This function accepts a sorted array and a value.&lt;/li&gt;
&lt;li&gt;Create a left pointer at the start of the array, and a right *pointer at the end of the array.&lt;/li&gt;
&lt;li&gt;0 index is the left pointer and the end of the array is the right pointer.&lt;/li&gt;
&lt;li&gt;Pick a middle pointer. Typically, an average of the left and right pointer. If the middle point is a fraction number, we round it up or floor it down.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;While the left pointer comes before the right pointer:&lt;br&gt;
a) Create a pointer in the middle.&lt;br&gt;
b) If you find the value you want, return the index.&lt;br&gt;
c) If the value is too small, move the left pointer up.&lt;br&gt;
d) If the value is too large, move the right pointer down.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you never find the value, return -1&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Code in JavaScript :&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;function binarySearch(arr, value) {
  // setting the lest pointer at the start of the array
  let left = 0;
  // setting the left pointer at the end of the array
  let right = arr.length - 1;
  // picking the middle of the array for even and odd number of elements
  let mid = Math.floor((left + right) / 2);
  while (left &amp;lt;= right) {
    //   If the value is too small, move the left pointer up.
    if (arr[mid] &amp;lt; value) {
      left = mid + 1;
      mid = Math.floor((left + right) / 2);
    }
    // If the value is too large, move the right pointer down.
    else if (arr[mid] &amp;gt; value) {
      right = mid - 1;
      mid = Math.floor((left + right) / 2);
    } else {
      return mid;
    }
  }

  // If you never find the value , return -1
  return -1;
}

console.log(binarySearch( [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,], 15)); // returns 14

console.log(binarySearch([2,6,25,89,100,110,120,127,150],2)); //returns 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>ds</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>Context API</title>
      <dc:creator>Kazi Md. Mehedi Hasan</dc:creator>
      <pubDate>Thu, 23 Dec 2021 16:22:36 +0000</pubDate>
      <link>https://forem.com/kazimdmehedihasan/context-api-4c1h</link>
      <guid>https://forem.com/kazimdmehedihasan/context-api-4c1h</guid>
      <description>&lt;p&gt;The notion of Context API was first introduced in React with the release of version 16.3 .&lt;br&gt;
By this React applications have become easier in terms of the global state concept.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Context API?&lt;/strong&gt;&lt;br&gt;
Context API is an idea to produce global variables. These variables then can be passed around within the application. Any component that requires to access the variables AKA “state” can easily be done with the help of Context API. One can say that Context API is a lightweight version of Redux.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How does it work?&lt;/strong&gt;&lt;br&gt;
There’s a built-in function in react called &lt;strong&gt;createContext().&lt;/strong&gt; This function is required to incorporate &lt;strong&gt;Context API&lt;/strong&gt; in any React application.&lt;br&gt;
&lt;strong&gt;createContext()&lt;/strong&gt; returns a &lt;strong&gt;Provider&lt;/strong&gt; and a &lt;strong&gt;Consumer&lt;/strong&gt;. &lt;strong&gt;Provider&lt;/strong&gt; serves the children components with &lt;strong&gt;state&lt;/strong&gt;. If you have any idea about redux store provider kind of acts like that. If you’re not familiar with redux that’s absolutely fine. Think &lt;strong&gt;Context API&lt;/strong&gt; as a &lt;strong&gt;Jug of Juice&lt;/strong&gt; and to all the components it supplies juice via the &lt;strong&gt;Provider&lt;/strong&gt;. Take the components as mugs to which you’re going to pour juice. Juice here represents the passable &lt;strong&gt;state&lt;/strong&gt;, &lt;strong&gt;Provider&lt;/strong&gt; as a funnel through which juice doesn’t go outside the mugs.&lt;br&gt;
Consumers are the component which takes in the states and uses them. In terms of our analogy The “Mugs” are the Consumers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why use Context API?&lt;/strong&gt;&lt;br&gt;
We often need to pass our state across components. Sometimes what happens is that we need to pass state in multiple components in the application. And those happen to be in multiple level. In those scenarios, we need to do some improvisations. Either we do lift up state or we have to drill the props. Which is repetitive. We may have to pass a prop to a component where we are never going to use that prop. It just acts as a corridor for that prop to pass. It makes the application unorganized. To get rid of this inconvenience, &lt;strong&gt;Context API&lt;/strong&gt; was introduced in React V16.3. Which eliminates code repetition and makes the process satisfactory.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TF2A-24i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c3csiuuhku6e79ybtllu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TF2A-24i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c3csiuuhku6e79ybtllu.png" alt="Image description" width="880" height="359"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to use Context API?&lt;/strong&gt;&lt;br&gt;
No hanky panky! We’ll go straight down to the example to understand the Context API better.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a folder called &lt;strong&gt;contexts(convention)&lt;/strong&gt; in your root app directory.&lt;/li&gt;
&lt;li&gt;Create a file with a name you like , i.e &lt;strong&gt;customContext.js&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Create context with the help of &lt;strong&gt;“createContext()”&lt;/strong&gt; function and import it in your custom context file.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { createContext } from "react";
const CustomContext = createContext();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Create a component that will wrap the child components with provider.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const CustomProvider = ({ children }) =&amp;gt; {
  const [name, setName] = useState("Kamaluddin Jaffory");
  const [age, setAge] = useState(1);
  const happyBirthday = () =&amp;gt; setAge(age++);
  return (
    &amp;lt;CustomContext.Provider value={{ name, age, happyBirthday }}&amp;gt;
      {children}
    &amp;lt;/CustomContext.Provider&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;5.Create a higher order component to receive the context. The standard naming convention starts with “with”.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const withUser = (Child) =&amp;gt; (props) =&amp;gt; (
  &amp;lt;CustomContext.Consumer&amp;gt;
    {(context) =&amp;gt; &amp;lt;Child {...props} {...context} /&amp;gt;}
    {/* Another option is:  {context =&amp;gt; &amp;lt;Child {...props} context={context}/&amp;gt;}*/}
  &amp;lt;/CustomContext.Consumer&amp;gt;
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then export them&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export { CustomProvider, withUser };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And finally use them however you like the most.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return(
&amp;lt;CustomProvider&amp;gt;
  &amp;lt;App/&amp;gt;
&amp;lt;/CustomProvider&amp;gt;
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>react</category>
      <category>context</category>
    </item>
  </channel>
</rss>
