<?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: Gurmeet Singh</title>
    <description>The latest articles on DEV Community by Gurmeet Singh (@binarysingh).</description>
    <link>https://dev.to/binarysingh</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%2F985802%2F4e74c77f-3db8-411e-b180-84503b9cb165.jpg</url>
      <title>DEV Community: Gurmeet Singh</title>
      <link>https://dev.to/binarysingh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/binarysingh"/>
    <language>en</language>
    <item>
      <title>How to use HeadlessUI with React Hook Form</title>
      <dc:creator>Gurmeet Singh</dc:creator>
      <pubDate>Sat, 15 Apr 2023 14:57:25 +0000</pubDate>
      <link>https://dev.to/binarysingh/how-to-use-headlessui-with-react-hook-form-5dhe</link>
      <guid>https://dev.to/binarysingh/how-to-use-headlessui-with-react-hook-form-5dhe</guid>
      <description>&lt;p&gt;React Hook Form is a popular library for managing form state and validation in React applications. It makes handling form input and validation a breeze. Headless UI, on the other hand, is a library that provides accessible, unstyled UI components that can be easily customized according to your design system. &lt;/p&gt;

&lt;p&gt;Creating complex forms with accessible and stylish UI components can be a challenging task. In this blog post, we will explore how to seamlessly integrate Headless UI with React Hook Form to handle complex form elements like ComboBox and Radio Group. We will also discuss how to add validations and default values to your forms. By the end of this post, you'll be able to confidently use these tools to enhance your projects.&lt;/p&gt;




&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;To follow along with this tutorial, you should have a basic understanding of React and be familiar with React Hook Form and Headless UI. If you are new to these libraries, you can check out their respective documentation:&lt;/p&gt;

&lt;p&gt;React: &lt;a href="https://reactjs.org/"&gt;https://reactjs.org/&lt;/a&gt;&lt;br&gt;
React Hook Form: &lt;a href="https://react-hook-form.com/"&gt;https://react-hook-form.com/&lt;/a&gt;&lt;br&gt;
Headless UI: &lt;a href="https://headlessui.dev/"&gt;https://headlessui.dev/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Integrating ComboBox and Radio Group with React Hook Form
&lt;/h2&gt;

&lt;p&gt;To integrate Headless UI components like ComboBox and Radio Group with React Hook Form, you'll need to utilize the Controller component and the control prop provided by React Hook Form&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Integration&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Import the required components and hooks from Headless UI and React Hook Form.&lt;/li&gt;
&lt;li&gt;Inside your form component, use the useForm hook from React Hook Form, passing the validation schema as an option.&lt;/li&gt;
&lt;li&gt;Wrap the component from Headless UI with the Controller component from React Hook Form.&lt;/li&gt;
&lt;li&gt;Pass the control prop from the useForm hook to the Controller component.&lt;/li&gt;
&lt;li&gt;Use the render prop in the Controller component to render the ComboBox and pass down the necessary props, such as field, fieldState, and formState.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You can see a live example of it here - &lt;a href="https://rhf-with-headlessui.vercel.app/"&gt;https://rhf-with-headlessui.vercel.app/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Adding Default Values&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To set default values for your form elements, simply pass an object containing the default values to the defaultValues option when using the useForm hook.&lt;/p&gt;

&lt;p&gt;An example with default values prefilled can be found here - &lt;a href="https://rhf-with-headlessui.vercel.app/?storage=business&amp;amp;user=arlene"&gt;https://rhf-with-headlessui.vercel.app/?storage=business&amp;amp;user=arlene&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Default Behaviour&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The headless ui components like combobox and radio group use an array of options and each option in this array is an object. By default, headless ui sends you the complete information of selected option i.e the complete object of the selected option. But, it is very rare that you'll need this. So, I have modified the components to return only the value of the selected option instead of complete object. If you want to stick to the default behaviou you can use the default components i.e, ComboBoxDefault and RadioGroupDefaul from the &lt;a href="https://github.com/singhBinary/rhf-with-headlessui"&gt;git repository&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Live example of this can be found here - &lt;a href="https://rhf-with-headlessui.vercel.app/default"&gt;https://rhf-with-headlessui.vercel.app/default&lt;/a&gt;&lt;br&gt;
With a prefilled version - &lt;a href="https://rhf-with-headlessui.vercel.app/default?storage=business&amp;amp;user=arlene"&gt;https://rhf-with-headlessui.vercel.app/default?storage=business&amp;amp;user=arlene&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By integrating Headless UI with React Hook Form, you can easily create complex and accessible forms with ComboBox and Radio Group components. With the Controller component and the control prop, adding validations and default values becomes a breeze. Now you can confidently use these tools to enhance your projects and create powerful, accessible forms.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The complete source code can be found here - &lt;a href="https://github.com/singhBinary/rhf-with-headlessui"&gt;https://github.com/singhBinary/rhf-with-headlessui&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>reacthookform</category>
      <category>headlessui</category>
      <category>tailwindcss</category>
    </item>
    <item>
      <title>How to easy access localhost on your mobile device</title>
      <dc:creator>Gurmeet Singh</dc:creator>
      <pubDate>Tue, 07 Mar 2023 10:35:05 +0000</pubDate>
      <link>https://dev.to/binarysingh/how-to-easy-access-localhost-on-your-mobile-device-29ce</link>
      <guid>https://dev.to/binarysingh/how-to-easy-access-localhost-on-your-mobile-device-29ce</guid>
      <description>&lt;p&gt;As developers, we spend the majority of our time working on localhost, which is the local server environment for our applications. Localhost is where we test and debug our applications before deploying them to production. However, there are times when we want to access our localhost application on our mobile devices, and that's what we'll be discussing in this post.&lt;/p&gt;

&lt;p&gt;There are many reasons why a developer might want to run a localhost application on their mobile device. For example, you might want to test how your application looks and behaves on different screen sizes or operating systems. Additionally, you might want to demo your application to stakeholders.&lt;/p&gt;

&lt;p&gt;Whatever the reason may be, we'll guide you through the steps on how to open your localhost application on your mobile device. Although, there are many ways out there to do this but I will be sharing the most easiest ones ways below. By the end of this post, you'll be able to access your localhost application on your mobile device, and you'll be one step closer to creating a seamless development and testing experience. So, let's get started!&lt;/p&gt;

&lt;h2&gt;
  
  
  Method 1 -
&lt;/h2&gt;

&lt;p&gt;The most intuitive and developer friendly way which I personally use is through &lt;a href="https://github.com/formspark/localview"&gt;localview&lt;/a&gt;. Localview is a no BS solution to run any application on your mobile devices with just single line of command without any need for you to search for your IP details. It simply returns a URL/QR code which you can easily share with others.&lt;/p&gt;

&lt;p&gt;Simply run the &lt;code&gt;npx localview --port xxxx&lt;/code&gt; on your terminal where xxxx is the port where your application is ruuning and that's it.&lt;/p&gt;

&lt;p&gt;If you already have localview installed it will show you the QR code and the URL using which you can access the application. If not, it will simply prompt you to install one which you can do by typing "Y" as shown below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yahw0m3G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7e5n4orm7v2adsnufihq.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yahw0m3G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7e5n4orm7v2adsnufihq.jpg" alt="using localview" width="365" height="414"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note -&lt;/strong&gt; A port is like a door that allows communication between different programs on a computer or over a network. When you run a web application on your computer, it uses a specific port to communicate with the web browser. By default, most web servers use port number 80 or 8080. To find out which port your application is using, you can look for it in the code or configuration files of your application or even in your browsers address bar. For instance, for a URL as below&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---N_53gZ---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yzg6ajnv6czih9mw7ero.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---N_53gZ---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yzg6ajnv6czih9mw7ero.jpg" alt="URL with port" width="378" height="42"&gt;&lt;/a&gt;&lt;br&gt;
the port is 3000. The port is generally the number that you see after &lt;code&gt;:&lt;/code&gt;&lt;br&gt;
Alternatively, you can check the documentation or guides of your application to find out which port it uses by default.&lt;/p&gt;

&lt;h2&gt;
  
  
  Method 2 -
&lt;/h2&gt;

&lt;p&gt;If you face any issue in the above approach or you do not want to install a dependency for such a minor task. You can follow the steps below&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Find your IP address (IPv4)
There are a number of ways to do this.&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;ipconfig&lt;/code&gt; command on terminal&lt;/li&gt;
&lt;li&gt;On windows, go to "Network &amp;amp; internet" -&amp;gt; click on "Advanced network settings" -&amp;gt; Expand the "Wi-fi" option -&amp;gt; click on "View additional properties" 
Once, you have the IPv4 proceed to the next step. &lt;/li&gt;
&lt;li&gt;On your mobile device, open the following URL in any browser: IP(x.x.x.x):PORT(xxxx) (example: 192.168.1.37:3000) where 192.168.1.37 is the IP obtained from step1 above and 3000 is the application port number.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Please note that both the laptop and the mobile device must be on the same network to be able to use any of the above approach.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>webdev</category>
      <category>debugging</category>
    </item>
    <item>
      <title>Polyfill for map in javascript</title>
      <dc:creator>Gurmeet Singh</dc:creator>
      <pubDate>Sun, 05 Mar 2023 08:47:47 +0000</pubDate>
      <link>https://dev.to/binarysingh/polyfill-for-map-in-javascript-1d6k</link>
      <guid>https://dev.to/binarysingh/polyfill-for-map-in-javascript-1d6k</guid>
      <description>&lt;p&gt;The &lt;code&gt;map()&lt;/code&gt; method in JavaScript is &lt;strong&gt;used to iterate over an array and apply a function to each element in the array, returning a new array with the results&lt;/strong&gt;. For those who are new to javascript here is a basic example of how &lt;code&gt;map()&lt;/code&gt; works&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Define an array of numbers
const numbers = [1, 2, 3, 4, 5];

// Use the map() method to square each number in the array
const squaredNumbers = numbers.map(function(number) {
  return number * number;
});

// Output the new array of squared numbers
console.log(squaredNumbers); // [1, 4, 9, 16, 25]

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

&lt;/div&gt;



&lt;p&gt;For an indepth understanding of &lt;code&gt;map()&lt;/code&gt; please refer this &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map"&gt;link&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;As seen above &lt;code&gt;map()&lt;/code&gt; is a very useful method, but not all browsers support it. In this blog post, we will create a polyfill for the &lt;code&gt;map()&lt;/code&gt; method so that it can be used in all browsers.&lt;/p&gt;

&lt;p&gt;Here's the code for the &lt;code&gt;map()&lt;/code&gt; polyfill -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (!Array.prototype.map) {
  Array.prototype.map = function(callback, thisArg) {
    if (typeof callback !== 'function') {
      throw new TypeError(callback + ' is not a function');
    }

    var newArray = [];

    for (let i = 0; i &amp;lt; this.length; i++) {
      if (this.hasOwnProperty(i)) {
        newArray[i] = callback.call(thisArg, this[i], i, this);
      }
    }

    return newArray;
  };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's break down this code -&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First, we check if the &lt;code&gt;map()&lt;/code&gt; method already exists on the &lt;code&gt;Array prototype&lt;/code&gt;. If it does not exist, we create our implementation.&lt;/li&gt;
&lt;li&gt;We define the &lt;code&gt;map()&lt;/code&gt; function on the &lt;code&gt;Array prototype&lt;/code&gt;, taking two arguments: a &lt;code&gt;callback&lt;/code&gt;function and an optional &lt;code&gt;thisArg&lt;/code&gt; parameter.&lt;/li&gt;
&lt;li&gt;We check if the &lt;code&gt;callback&lt;/code&gt;argument is a function, and &lt;code&gt;throw&lt;/code&gt; a &lt;code&gt;TypeError&lt;/code&gt; if it is not.&lt;/li&gt;
&lt;li&gt;We create a new empty array to hold the results of the mapping operation.&lt;/li&gt;
&lt;li&gt;We iterate over each element in the array, using the &lt;code&gt;hasOwnProperty()&lt;/code&gt; method to skip any non-existent elements such as properties added to the array's prototype.&lt;/li&gt;
&lt;li&gt;For each element, we call the callback function with the element, the index, and the original array as arguments, using the &lt;code&gt;call()&lt;/code&gt; method to set the &lt;code&gt;this&lt;/code&gt; value to &lt;code&gt;thisArg&lt;/code&gt; if it is provided.&lt;/li&gt;
&lt;li&gt;We store the result of the callback function in the new array.&lt;/li&gt;
&lt;li&gt;We return the new array containing the mapped results.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And that's it! With this &lt;code&gt;map()&lt;/code&gt; polyfill, you can use the &lt;code&gt;map()&lt;/code&gt; method on arrays in all browsers, even if they do not support it natively.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>interview</category>
      <category>polyfill</category>
    </item>
    <item>
      <title>git merge vs git pull</title>
      <dc:creator>Gurmeet Singh</dc:creator>
      <pubDate>Sat, 21 Jan 2023 06:30:10 +0000</pubDate>
      <link>https://dev.to/binarysingh/git-merge-vs-git-pull-36m6</link>
      <guid>https://dev.to/binarysingh/git-merge-vs-git-pull-36m6</guid>
      <description>&lt;p&gt;Over the years I have seen developers use two approaches to add code from one branch to another. For example, suppose you have two branches: "main" and "new-feature". You have been working on "new-feature" for a while and now you want to merge the changes you made into "main". To do this, developers generally use one of two ways:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Checkout the &lt;code&gt;main&lt;/code&gt; branch and run &lt;code&gt;git merge new-feature&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Checkout the &lt;code&gt;main&lt;/code&gt; branch and run &lt;code&gt;git pull origin new-feature&lt;/code&gt; (This is the approach which is generally used when we create pull requests from the UI of various git hosting platforms (e.g. GitHub, GitLab, Bitbucket))&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And I asked many people about this but no one seems to know the exact difference and they had just picked up one approach and been using it forever.&lt;/p&gt;




&lt;h2&gt;
  
  
  Here's the difference -
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;git merge&lt;/code&gt; is used to merge a branch into the current branch, while &lt;code&gt;git pull&lt;/code&gt; is used to fetch and merge changes from a remote repository into the current branch. In other words, &lt;code&gt;git pull&lt;/code&gt; is a combination of &lt;code&gt;git fetch&lt;/code&gt; and &lt;code&gt;git merge&lt;/code&gt;. &lt;code&gt;git fetch&lt;/code&gt; retrieves new commits from a remote repository, but does not merge them into the current branch. &lt;code&gt;git merge&lt;/code&gt; is then used to integrate the changes from the remote branch into the local branch.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>webdev</category>
      <category>html</category>
      <category>css</category>
    </item>
    <item>
      <title>Staircase problem in JS</title>
      <dc:creator>Gurmeet Singh</dc:creator>
      <pubDate>Sat, 24 Dec 2022 13:18:38 +0000</pubDate>
      <link>https://dev.to/binarysingh/staircase-problem-in-js-42kh</link>
      <guid>https://dev.to/binarysingh/staircase-problem-in-js-42kh</guid>
      <description>&lt;p&gt;Interviews can be an intimidating process, and the pressure can be overwhelming. It’s not uncommon for employers to ask questions in which if you don’t think in the right direction, it could be a game over. With the right preparation and practice, you can be ready for any difficult question that comes your way. In this article, we'll explore one of the questions that got the better of me.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem Statement -&lt;/strong&gt; &lt;br&gt;
There exists a staircase with N steps, and you can climb up either 1 or 2 steps at a time. &lt;br&gt;
Given N, write a function that returns the number of unique ways you can climb the staircase.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const totalSteps=4; 
const stepsInSingleJump=[1,2]; 
function getAllCombinations(){ }
console.log(getAllCombinations(totalSteps, stepsInSingleJump)) 

// output: [[1,1,1,1], [1,1,2], [1,2,1], [2,1,1], [2,2]]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why not try to write a pseudo code before you see the answer below? It's pretty easy if you can determine what programming concept you have to use here.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Hint -&lt;/strong&gt; Recursion&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getAllCombinations(totalSteps, stepsInSingleJump) {
  let combinations = [];

  function recurse(currentSteps, currentCombo) {
    if (currentSteps === totalSteps) {
      combinations.push(currentCombo);
      return;
    }
    if (currentSteps &amp;gt; totalSteps) {
      return;
    }

    for (const step of stepsInSingleJump) {
      recurse(currentSteps + step, [...currentCombo, step]);
    }
  }

  recurse(0, []);
  return combinations;
}

console.log(getAllCombinations(4, [1, 2]));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation -&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The function uses recursion to explore all the possible combinations of steps. It maintains a combinations array to store the combinations and a currentCombo array to store the current combination being built.&lt;/p&gt;

&lt;p&gt;The base case is when the currentSteps is equal to totalSteps. In this case, the currentCombo is added to the combinations array. The recursion ends when currentSteps becomes greater than totalSteps, since it is not possible to exceed the total number of steps.&lt;/p&gt;

&lt;p&gt;For each step in stepsInSingleJump, the function calls itself with the updated currentSteps (which is the sum of the current step and the number of steps already taken) and the updated currentCombo (which includes the current step). This continues until the base case is reached, at which point the combinations are returned.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>interview</category>
    </item>
    <item>
      <title>What is git merge and how to undo it</title>
      <dc:creator>Gurmeet Singh</dc:creator>
      <pubDate>Sun, 18 Dec 2022 16:41:06 +0000</pubDate>
      <link>https://dev.to/binarysingh/what-is-git-merge-and-how-to-undo-it-13j2</link>
      <guid>https://dev.to/binarysingh/what-is-git-merge-and-how-to-undo-it-13j2</guid>
      <description>&lt;p&gt;When you want to combine the changes from one branch into another, you can use the git merge command. For example, suppose you have two branches: "main" and "new-feature". You have been working on "new-feature" for a while and now you want to merge the changes you made into "main". To do this, you can use the following steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Switch to the main branch: &lt;code&gt;git checkout main&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Merge the changes from new-feature into main: &lt;code&gt;git merge new-feature&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Git will automatically try to merge the changes from "new-feature" into "main". If there are no conflicts, the merge will be completed and you will be able to see the changes you made in "new-feature" in the "main" branch.&lt;/p&gt;

&lt;p&gt;If there are conflicts, git will not be able to automatically merge the changes and you will have to resolve the conflicts manually. This means that you will have to edit the conflicting files and decide which changes to keep and which to discard. Once you have resolved the conflicts, you can commit the merged changes to complete the merge.&lt;/p&gt;

&lt;h2&gt;
  
  
  Undo git merge -
&lt;/h2&gt;

&lt;p&gt;There are several ways to undo a git merge, depending on what you want to achieve and in most cases first two will be sufficient. Here are some options:&lt;/p&gt;

&lt;p&gt;1) &lt;code&gt;git merge --abort&lt;/code&gt;: This command will abort the merge and restore the state of the repository to before the merge was started. This is the most straightforward way to undo a merge, but &lt;strong&gt;it will only work if the merge has not been completed and the repository is still in a "merging" state i.e., there was a merge conflict and the changes have not been pushed&lt;/strong&gt; to a remote repository.&lt;/p&gt;

&lt;p&gt;In git, a repository is considered to be in a "merging" state when a merge operation is in progress but has not yet been completed. &lt;strong&gt;This typically occurs when you are using the git merge command to combine changes from one branch into another, and git has detected conflicts that need to be resolved.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When git is in the process of merging, it creates a special merge commit that represents the combined changes from the two branches. This commit has two parent commits, one for each branch being merged. However, until the merge conflicts are resolved and the merge commit is finalized, the repository remains in a "merging" state.&lt;/p&gt;

&lt;p&gt;2) &lt;code&gt;git reset --hard HEAD~1&lt;/code&gt;: This command will discard the changes from the merge and restore the repository to the state it was in before the merge. It will also remove any commits that were added during the merge. &lt;strong&gt;This option is useful if you want to completely discard the changes from the merge and start over.&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;This command works for both cases whether the changes were pushed or not.&lt;/p&gt;

&lt;p&gt;However, it's important to keep in mind that using git reset --hard will permanently discard the changes made in the commits that are being removed. This means that the changes will be lost and cannot be recovered. Therefore, it's generally a good idea to use this command with caution and only if you are sure that you want to permanently discard the changes made in the merge commit.&lt;/p&gt;

&lt;p&gt;3) &lt;code&gt;git revert &amp;lt;commit_hash&amp;gt;&lt;/code&gt;: Alternatively, you can use git revert to undo specific changes from a commit. It will undo the changes made in the specified commit by creating a new commit that reverses the changes. This is a safer option if you are not sure which changes you want to discard, as it preserves the history of the repository.&lt;/p&gt;

&lt;h2&gt;
  
  
  Good to know -
&lt;/h2&gt;

&lt;p&gt;If there are no conflicts between the branches, &lt;strong&gt;Git will automatically create a new commit&lt;/strong&gt; that merges the changes from the feature branch into the master branch. If there are conflicts, Git will stop the merge and ask you to resolve the conflicts before completing the merge.&lt;/p&gt;

&lt;p&gt;You can also use the git merge command with the &lt;code&gt;--no-commit&lt;/code&gt; flag, which will perform the &lt;strong&gt;merge but not create a new commit.&lt;/strong&gt; This can be useful if you want to review the changes before committing them to the repository.&lt;/p&gt;

&lt;p&gt;It's important to note that a &lt;strong&gt;merge creates a new commit in the history of the branch you are merging into&lt;/strong&gt;. This means that after a merge, the two branches have a common ancestor commit, and any commits on the merged branch are considered to be part of the branch's history.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Flatten a nested object in JS</title>
      <dc:creator>Gurmeet Singh</dc:creator>
      <pubDate>Tue, 13 Dec 2022 19:05:19 +0000</pubDate>
      <link>https://dev.to/binarysingh/flatten-a-nested-object-in-js-556g</link>
      <guid>https://dev.to/binarysingh/flatten-a-nested-object-in-js-556g</guid>
      <description>&lt;p&gt;When an interviewer wants to test your logical as well as programming capabilities a good-to-go question for them is to ask you to write a function to flatten a given nested object. Such that, an object as given below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "companyName": "Movetoabroad",
    "fieldOfWork": "Immigartaion",
    "website": "http://movetoabroad.com",
    "highlights": {
        "visaSuccessRate": "High",
        "fees": "Free"
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;must become&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "companyName": "Movetoabroad",
  "fieldOfWork": "Immigartaion",
  "website": "http://movetoabroad.com",
  "highlights.visaSuccessRate": "High",
  "highlights.fees": "Free"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Seems simple. Right? We can just add a check to see if the value of the property is object if yes just iterate over the child object and prepend the parent property name to the child property name before adding it to a new object which contains all the flattened properties.&lt;/p&gt;

&lt;p&gt;But in most cases, the interviewer will add a condition here that the object can have an "n" level of nesting. Such that, we can have an object in the form of&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "companyName": "Movetoabroad",
  "fieldOfWork": "Immigartaion",
  "website": "http://movetoabroad.com",
  "highlights": {
    "visaSuccessRate": "High",
    "fees": "Free",
    "timings": {
      "weekdays": "9am - 6pm",
      "weekends": "10am - 7pm",
    },
  },
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which must become&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "companyName": "Movetoabroad",
  "fieldOfWork": "Immigartaion",
  "website": "http://movetoabroad.com",
  "highlights.visaSuccessRate": "High",
  "highlights.fees": "Free",
  "highlights.timings.weekdays": "9am - 6pm",
  "highlights.timings.weekends": "10am - 7pm",
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the logic which you thought earlier still applies but since the level of nesting is unknown we have to keep on applying the same logic again and again until the nesting stops. You can think of doing it using a loop but here's where the programming abilities kick in and you must think about using &lt;a href="https://en.wikipedia.org/wiki/Recursion"&gt;Recursion&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One common way to solve this is -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const flattenObj = (obj) =&amp;gt; {
  const result = {};
  for (let key in obj) {
    if (typeof obj[key] === "object") {
      const temp = flattenObj(obj[key]);
      for (let prop in temp) {
        result[key + "." + prop] = temp[prop];
      }
    } else {
      result[key] = obj[key];
    }
  }
  return result;
};

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

&lt;/div&gt;



&lt;p&gt;Or even better&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function flattenObj(obj, res = {}, parentKey = '') {
    for(let key in obj) {
    let modifiedKeyName = parentKey ? `${parentKey}.${key}` : key;
    if(typeof obj[key] == 'object') {
        flattenObj(obj[key], res, modifiedKeyName);
    }else{
        res[modifiedKeyName] = obj[key];
    }
  }
  return res;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Disclaimer - This code must be considered only for interview purposes for real-world usage you might need to optimize it more based on the objects you are passing to this function. For instance, if the value of a property is an array then you might need to add another check &lt;code&gt;Array.isArray(obj[key])&lt;/code&gt; along with &lt;code&gt;typeof obj[key] == 'object'&lt;/code&gt; because in JS typeof arrays also returns "object".&lt;/p&gt;

</description>
      <category>interview</category>
      <category>javascript</category>
    </item>
    <item>
      <title>I used ChatGPT extensively for a day and it just blew my mind</title>
      <dc:creator>Gurmeet Singh</dc:creator>
      <pubDate>Sat, 10 Dec 2022 16:42:01 +0000</pubDate>
      <link>https://dev.to/binarysingh/i-used-chatgpt-extensively-for-a-day-and-it-just-blew-my-mind-4ij9</link>
      <guid>https://dev.to/binarysingh/i-used-chatgpt-extensively-for-a-day-and-it-just-blew-my-mind-4ij9</guid>
      <description>&lt;p&gt;It has been some days since the official launch of &lt;a href="https://chat.openai.com/chat"&gt;ChatGPT&lt;/a&gt; and with it crossing 1 million + users in just 5 days it has been the talk of the town. When I first heard about it I straight away went ahead to try it out. Initially, I just took it as a toy and played with it by asking it to write some articles for me or explain some topics to me in easy language and write some common programming code for me. But that was it I soon left the site thinking it is good and didn't bother much about it as my first instinct was it might have used some existing text from the internet and rephrased it using some basic AI logic as the things which I asked were pretty common. &lt;/p&gt;

&lt;p&gt;But as the hype around it is growing with each passing day I decided to revisit the website again. It has been some hours of me seriously working on it to see its capabilities and I can just say one thing.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"The winter of AI is coming, and with it, the end of many jobs as we know them. We must be prepared for this future and adapt to the changing world." - ChatGPT&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here I am going to articulate some of the responses received from ChatGPT with minimum effort and sometimes in just the first try that will help you understand what left me awe-struck as well as fearful at the same time.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Beginner -&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;I am a front-end developer who is responsible for implementing the visual and interactive elements of a website. The technology which I am working on in my day-to-day life is React.js. So, I decided to spin up the ChatGPT to create something for me in React.js. I still remember when I initially started to work with React one of the tasks I was given was to automatically create a "Table of Contents" for a given article which will allow the user to go to a particular section of the article with ease. I still carry the sense of accomplishment I felt when I was able to do it. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--T95EZUWc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4fid2zgqpz4vxhbgq29r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--T95EZUWc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4fid2zgqpz4vxhbgq29r.png" alt="Table of content example" width="800" height="396"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, I decided to try the same and provided it as a task to ChatGPT.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ME -&lt;/strong&gt; "code a component in react which acts as a table of content for the given article. It will take all heading tags from the article placed inside a div with a class name of "article-wrapper"  and create a list of those headings to act as a table of contents for that article. On clicking on an option in the table of contents user must be scrolled to that position on the article."&lt;/p&gt;

&lt;p&gt;And in just two attempts I was able to get the following -&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ChatGPT -&lt;/strong&gt; Here is an example of a table of contents component in React that can be used to create a list of headings from an article and allow the user to navigate to those positions in the article:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, useRef, useEffect } from 'react';

const TableOfContents = () =&amp;gt; {
  const articleRef = useRef(null);
  const [headings, setHeadings] = useState([]);

  useEffect(() =&amp;gt; {
    // get all heading elements from the article
    const articleHeadings = articleRef.current.querySelectorAll('h1, h2, h3, h4, h5, h6');
    // create an array of heading objects with text and id attributes
    const headingData = Array.from(articleHeadings).map(heading =&amp;gt; ({
      text: heading.textContent,
      id: heading.id,
    }));
    // update the state with the heading data
    setHeadings(headingData);
  }, []);

  const handleClick = id =&amp;gt; {
    // find the heading element in the article by id
    const headingElement = articleRef.current.querySelector(`#${id}`);
    // scroll to the position of the heading element in the article
    headingElement.scrollIntoView({ behavior: 'smooth' });
  };

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h3&amp;gt;Table of Contents:&amp;lt;/h3&amp;gt;
      &amp;lt;ul&amp;gt;
        {headings.map(heading =&amp;gt; (
          &amp;lt;li key={heading.id}&amp;gt;
            &amp;lt;a href="#" onClick={() =&amp;gt; handleClick(heading.id)}&amp;gt;
              {heading.text}
            &amp;lt;/a&amp;gt;
          &amp;lt;/li&amp;gt;
        ))}
      &amp;lt;/ul&amp;gt;
      &amp;lt;div className="article-wrapper" ref={articleRef}&amp;gt;
        {/* article content goes here */}
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

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

&lt;/div&gt;



&lt;p&gt;In this component, we use the useRef hook to create a reference to the article element, and the useState hook to store the array of heading data in the component's state. The useEffect hook is used to get the heading elements from the article and create the heading data when the component is mounted. The handleClick method is used to handle clicks on the table of contents links, and scroll to the corresponding heading in the article. Finally, the component renders the table of contents and the article content inside the article-wrapper element.&lt;/p&gt;

&lt;p&gt;You can customize this component to suit your needs, such as adding styles or providing options to filter the heading levels that are included in the table of contents.&lt;/p&gt;




&lt;p&gt;I am sure that not everyone reading this might relate to the answer but having 8 years of experience in this field can tell you that this is pretty impressive. Given that, I was still not convinced as there are many articles on the internet which have the solution to this. So, I tried to play some more.&lt;/p&gt;

&lt;h2&gt;
  
  
  Advanced -
&lt;/h2&gt;

&lt;p&gt;Now, I wanted it to generate something for me which might not be easily available on the internet. Maybe a startup idea and ask it to code it for me? But I was not able to come up with one so I decided to ask ChatGPT for an idea :P &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ME -&lt;/strong&gt; give me a startup idea&lt;br&gt;
&lt;strong&gt;ChatGPT -&lt;/strong&gt; How about a platform that connects independent contractors with businesses that need their services? This platform could make it easier for businesses to find and hire skilled professionals, while also helping independent contractors find work and manage their schedules. The platform could also offer tools and resources to help contractors run their businesses more efficiently.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ME -&lt;/strong&gt; can you be a bit more specific?&lt;br&gt;
&lt;strong&gt;ChatGPT -&lt;/strong&gt; Sure, here's a more specific idea: How about a platform that connects home cooks with people in their local area who are looking for home-cooked meals? The platform could allow home cooks to create profiles showcasing their cooking skills and specialties, and customers could browse these profiles to find a home cook who makes the kind of food they're looking for. The platform could also handle scheduling and payment, making it easy for both cooks and customers to use. This could be a great way for people to enjoy delicious, homemade meals without having to do all the cooking themselves, and it could also help home cooks turn their passion for cooking into a profitable business.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ME -&lt;/strong&gt; how will we solve the problem of hygiene and make sure that cooking is done with good grade and fresh ingredients ?&lt;br&gt;
&lt;strong&gt;ChatGPT -&lt;/strong&gt; To ensure that the food being cooked is safe and of high quality, the platform could require home cooks to register with the local health department and comply with all relevant food safety regulations. This could include regular inspections of their home kitchens to ensure that they meet the necessary standards for cleanliness and food safety. Additionally, the platform could require home cooks to use only fresh, high-quality ingredients, and it could provide resources and guidance to help them source these ingredients from local farmers and markets. By implementing these measures, the platform could help ensure that the food being cooked is safe and delicious.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ME -&lt;/strong&gt; and how will we earn from this?&lt;br&gt;
&lt;strong&gt;ChatGPT -&lt;/strong&gt; One way the platform could earn money is by charging home cooks a small fee to register and use the platform. This could help to cover the costs of maintaining the platform and providing resources and support to home cooks. Additionally, the platform could take a small commission on each transaction made through the platform, allowing it to earn a profit from the sale of each meal. This model could allow the platform to earn a steady stream of revenue while also providing a valuable service to both home cooks and customers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ME -&lt;/strong&gt; what about logistics?&lt;br&gt;
&lt;strong&gt;ChatGPT -&lt;/strong&gt; Good question! Logistics will be an important consideration for this platform. One way to handle logistics would be to allow customers to pick up the meals they order directly from the home cook's house. This could help to keep costs down and make the platform more accessible to a wider range of people. Alternatively, the platform could offer delivery services, either by partnering with a third-party delivery company or by using its own network of drivers. This could make the platform more convenient for customers, but it would also add to the cost of each transaction. Ultimately, the platform will need to strike the right balance between convenience and cost in order to be successful.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ME -&lt;/strong&gt; how many people will I need to start this businesS?&lt;br&gt;
&lt;strong&gt;ChatGPT -&lt;/strong&gt; It's difficult to say exactly how many people you will need to start this business, as it will depend on a number of factors, such as the scope of your initial offering, the size of your target market, and the resources you have available. However, it's likely that you will need at least a small team of people to help you get the business off the ground. This could include people with expertise in areas such as product development, marketing, and operations. As the business grows, you may need to add more people to your team to handle the increased demand. It's important to start small and build your team gradually as the need arises.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ME -&lt;/strong&gt; can you help me write code for this idea?&lt;br&gt;
&lt;strong&gt;ChatGPT -&lt;/strong&gt; I'm sorry, but I am not able to write code. I am a large language model trained by OpenAI, and I can provide general information and advice on a wide range of topics, but I am not able to write code or perform specific technical tasks. If you need help with coding, I would recommend reaching out to a professional software developer or consulting a relevant programming guide or tutorial.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ME -&lt;/strong&gt; can you suggest a good name for this platform?&lt;br&gt;
&lt;strong&gt;ChatGPT -&lt;/strong&gt; Sure, here are a few potential names for this platform:&lt;/p&gt;

&lt;p&gt;"MealConnect"&lt;br&gt;
"KitchenLink"&lt;br&gt;
"HomemadeHQ"&lt;br&gt;
"Plateful"&lt;br&gt;
"FoodFusion"&lt;br&gt;
These are just suggestions, and the right name for your platform will depend on a number of factors, such as your target market, your brand values, and the unique features of your platform. Ultimately, the best name will be one that is memorable, easy to pronounce and spell, and that accurately represents what your platform does.&lt;/p&gt;




&lt;p&gt;Pretty amazing. Right? To be honest I really considered leaving my job and creating a startup with this idea for a moment but then I recalled the amount of EMI's I have to pay :P&lt;/p&gt;

&lt;h2&gt;
  
  
  Human level -
&lt;/h2&gt;

&lt;p&gt;One of the problems which I came across since lockdown was to keep a track of the time I spent in meetings. So, last weekend I decided to create a chrome extension which will automatically track the time I spend on calls and show me a report of the same.&lt;/p&gt;

&lt;p&gt;I spent last weekend writing the chrome extension from scratch and still the reporting part is pending... (God please give me the power to complete the project and not let it go to my infinite list of side projects which I started but never completed). So, I decided to let ChatGPT create the same extension for me.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bcrPS0ua--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7jhhnhkyu0i69arlx50e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bcrPS0ua--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7jhhnhkyu0i69arlx50e.png" alt="chatGPT screenshot" width="800" height="372"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--K5Clcv5k--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cak9fiegl2tv0fm4sz5z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--K5Clcv5k--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cak9fiegl2tv0fm4sz5z.png" alt="chatGPT screenshot" width="800" height="372"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Z3vKzplc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9hjn1g1uy6c1x1mrf5qy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Z3vKzplc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9hjn1g1uy6c1x1mrf5qy.png" alt="chatGPT screenshot" width="800" height="372"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CeP_c76E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e2ujtjzdp5oce36ga8z6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CeP_c76E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e2ujtjzdp5oce36ga8z6.png" alt="chatGPT screenshot" width="800" height="372"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bVNkX2Ag--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/db2vrzjrr8hg05ihwls5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bVNkX2Ag--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/db2vrzjrr8hg05ihwls5.png" alt="chatGPT screenshot" width="800" height="372"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--o-veHp5I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k7ejrmscb0xoqhccm4vm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--o-veHp5I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k7ejrmscb0xoqhccm4vm.png" alt="chatGPT screenshot" width="800" height="372"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sDYJM8H8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4eeu2i84julonmdqrlq5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sDYJM8H8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4eeu2i84julonmdqrlq5.png" alt="chatGPT screenshot" width="800" height="372"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;This is what we have after four attempts. Look how clearly it lists down the steps such that someone who has never created an extension or even written any code is able to follow up and actually create it. As soon as I had the code I noticed something it was not recording time automatically. So, I asked it to correct it and it did without breaking a sweat. I did not run the extension to check it out but I could clearly see the effort of my weekend's worth of work in front of me in just a couple of minutes and to be honest, this sank my heart a bit. I was both amazed and shaken by this. Because I knew no such open-source extension existed as I had done thorough research before I started to create one. Suddenly, I see a dark tunnel with no light as I never thought that the work I did could be done so easily by some bot. With this chain of thoughts, I continued...&lt;/p&gt;

&lt;h2&gt;
  
  
  God level -
&lt;/h2&gt;

&lt;p&gt;You might have noted that the above conversation is provided as screenshots instead of text as the first two. Reason - As soon I completed the chrome extension I wanted to share it with you all in the form of a blog but the first thing that came to my mind was how shall I share all these chats as text or screenshots? &lt;br&gt;
Then it clicked on me why not use ChatGPT to create another chrome extension which will provide me with the ability to export all chat that I have with ChatGPT as screenshots or JSON? &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ME -&lt;/strong&gt; code a chrome extension which helps me save this chat as JSON or a screenshot on click of a button&lt;/p&gt;

&lt;p&gt;And within a minute I had the code of the extension in front of my eyes. I was not able to believe what I was seeing and I just thought to myself that this cannot be possible it is like the ability to get what you want in an instant. So, this time I decided to test if it will actually work and to be honest I badly wanted it to fail and it did.&lt;/p&gt;

&lt;p&gt;As soon as I copied all code and ran it I got an error. I just copied the error on ChatGPT and it told me how to fix it. I received a couple of errors which it was easily able to tell how to fix apart from one which was a minor one so I gave it the benefit of doubt and fixed it myself. And just like that in less than 5 minutes I have a chrome extension up and running in my browser from which the above screenshots have been taken. &lt;/p&gt;

&lt;p&gt;Obviously, it has some drawbacks at the moment like not being able to take a screenshot of a complete page in one go but let me tell you it could have been easily done with my next prompt and I was so sure of it that I did not even bother trying. Experiencing all this has already made me think, rethink and trying to comprehend what I just saw.&lt;/p&gt;




&lt;p&gt;After much thought my take on this is as simple as to embrace this change that is upon us and start using AI to improve our efficiency by 100X. AI must be a tool in our arsenal to improve what we do and not fear it because both AI and Humans are here to stay.&lt;/p&gt;

</description>
      <category>ai</category>
    </item>
    <item>
      <title>Sort a binary array in Javascript without using extra spaces and having O(n) time complexity</title>
      <dc:creator>Gurmeet Singh</dc:creator>
      <pubDate>Thu, 08 Dec 2022 17:16:10 +0000</pubDate>
      <link>https://dev.to/binarysingh/sort-a-binary-array-in-javascript-without-using-extra-spaces-and-having-on-time-complexity-1bbd</link>
      <guid>https://dev.to/binarysingh/sort-a-binary-array-in-javascript-without-using-extra-spaces-and-having-on-time-complexity-1bbd</guid>
      <description>&lt;p&gt;One of the basic questions which are generally asked in technical interviews is to sort a given array which contains only 0 and 1 as elements without using another array and having a time complexity of O(n)&lt;/p&gt;

&lt;p&gt;Example -&lt;br&gt;
&lt;strong&gt;Input:&lt;/strong&gt; [1, 0, 1, 0, 1, 0, 1, 0] &lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [0, 0, 0, 0, 1, 1, 1, 1]&lt;/p&gt;

&lt;p&gt;After reading O(n) the first thing that would come to mind is &lt;a href="https://en.wikipedia.org/wiki/Quicksort" rel="noopener noreferrer"&gt;quick sort&lt;/a&gt; but having a binary array as an input makes the problem simpler for us. Let me share 2 methods to do it. But wouldn't you like to think of a pseudo-code before checking the solutions below?&lt;/p&gt;


&lt;h2&gt;
  
  
  Method 1 -
&lt;/h2&gt;

&lt;p&gt;We count the number of zeros and ones in the array and then repopulate the array based on the count.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const sortBinaryArr = (arr) =&amp;gt; {
  let zerosCount = 0;
  let arrLen = arr.length;

  // Iterate over the array once to get the count of the number of zeros preset
  for (let index = 0; index &amp;lt; arrLen; index++) {
    if (arr[index] === 0) {
      ++zerosCount;
    }
  }

  // Iterate over the array till the number of zeros we got above and set all array elements up to this index to zero
  for (let index = 0; index &amp;lt; zerosCount; index++) {
    arr[index] = 0;
  }

  // Iterate over the remaining items and set them to one
  for (let index = zerosCount; index &amp;lt; arrLen; index++) {
    arr[index] = 1;
  }

  return arr;
};

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

&lt;/div&gt;



&lt;p&gt;Technically we are iterating over the array twice hence these operations combined are O(2n). &lt;strong&gt;Therefore the function sortBinaryArr() is an O(2 * n) operation, which is equivalent to O(n).&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Method 2 -
&lt;/h2&gt;

&lt;p&gt;A more optimized method is as below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const sortBinaryArr = (arr) =&amp;gt; {
  let pointer = -1;
  const arrLen = arr.length;
  for (let index = 0; index &amp;lt; arrLen; index++) {
    // if the number is smaller than 1 then swap it with the number at the pointer index
    if (arr[index] &amp;lt; 1) {
      pointer++;
      const temp = arr[pointer];
      arr[pointer] = arr[index];
      arr[index] = temp;
    }
  }

  return arr;
};

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

&lt;/div&gt;



&lt;p&gt;The algorithm is pretty simple and self-explanatory. Here, we make use of a variable which acts as a pointer and helps us to keep track of zeros and ones while we iterate over the array.&lt;/p&gt;

&lt;p&gt;Such that,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If the element is 0 then we increment the pointer by 1 and swap the current element with the element at the pointer position.&lt;/li&gt;
&lt;li&gt;If the element is 1 we continue without any action.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Can you think of a better solution? Comment down your solution...&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>career</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Undo git add command</title>
      <dc:creator>Gurmeet Singh</dc:creator>
      <pubDate>Thu, 08 Dec 2022 13:30:00 +0000</pubDate>
      <link>https://dev.to/binarysingh/undo-git-add-4dhk</link>
      <guid>https://dev.to/binarysingh/undo-git-add-4dhk</guid>
      <description>&lt;p&gt;For adding any files for the commit we add them using &lt;code&gt;git add .&lt;/code&gt; for adding all modified files or &lt;code&gt;git add ${fileName}&lt;/code&gt; for adding one file at a time.&lt;/p&gt;

&lt;p&gt;In some situations, you might want to undo this git add command. You can use the following command to do so&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git reset
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command can be used to unstage all files that were staged for the commit.&lt;/p&gt;

&lt;p&gt;Whereas if you want to unstage only one particular file you can use the following command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git restore --staged ${fileName}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>watercooler</category>
    </item>
    <item>
      <title>Remove trailing slash from URLs in Next.Js</title>
      <dc:creator>Gurmeet Singh</dc:creator>
      <pubDate>Wed, 07 Dec 2022 14:30:00 +0000</pubDate>
      <link>https://dev.to/binarysingh/remove-trailing-slash-from-urls-in-nextjs-2hn5</link>
      <guid>https://dev.to/binarysingh/remove-trailing-slash-from-urls-in-nextjs-2hn5</guid>
      <description>&lt;p&gt;Websites which are concerned with SEO usually know that having the URLs like following - &lt;br&gt;
&lt;a href="http://movetoabroad.com/about" rel="noopener noreferrer"&gt;http://movetoabroad.com/about&lt;/a&gt; and &lt;a href="http://movetoabroad.com/about/" rel="noopener noreferrer"&gt;http://movetoabroad.com/about/&lt;/a&gt; are treated differently by the crawlers. &lt;br&gt;
If you look closely the only difference between the URLs is just the trailing slash. Having these types of URLs creates an issue of duplicate content as crawlers find it pretty difficult to determine the original source.&lt;/p&gt;

&lt;p&gt;Here check out this twitter thread &lt;/p&gt;

&lt;p&gt;&lt;iframe class="tweet-embed" id="tweet-943076424130363392-479" src="https://platform.twitter.com/embed/Tweet.html?id=943076424130363392"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-943076424130363392-479');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=943076424130363392&amp;amp;theme=dark"
  }



&lt;/p&gt;

&lt;p&gt;Does it make sense now why your application needs to get rid of trailing slashes?&lt;/p&gt;

&lt;p&gt;If you are working in Next.js then there are &lt;strong&gt;two ways to fix this&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Method 1 -
&lt;/h2&gt;

&lt;p&gt;Well the Next.Js team was aware of this which is why they have an inbuilt provision to handle this you can read about it more &lt;a href="https://nextjs.org/docs/api-reference/next.config.js/trailing-slash" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The idea is to Open next.config.js and add the trailingSlash config:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
  trailingSlash: true,
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it. You are good to go.&lt;/p&gt;

&lt;h2&gt;
  
  
  Method 2 -
&lt;/h2&gt;

&lt;p&gt;Method 1 discussed above somehow doesn't work at all times or if you are still on an older version of Next.Js. You can see the internet full of discussion around it. So, another method which you can consider especially if you are using a &lt;a href="https://nextjs.org/docs/advanced-features/custom-server" rel="noopener noreferrer"&gt;custom server&lt;/a&gt; to run the Next.Js application is described below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;server.get("*", (req, res, next) =&amp;gt; {
  try {
    if (
      req.path.endsWith("/") &amp;amp;&amp;amp;
      req.path.length &amp;gt; 1
    ) {
      let query = req.url.slice(req.path.length);
      // if req.path contains only slashes it means user is trying to access the home page. Check the same using regex and if true redirect him to home page.
      if (/^[\/]+$/.test(req.path)) {
        res.redirect(301, "/" + query);
      } else {
        res.redirect(301, req.path.replace(/\/+$/, "") + query);
      }
    } else {
      next();
    }
  } catch (error) {
    console.log(`Error in redirection ${error}`);
    next();
  }
});

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

&lt;/div&gt;



&lt;p&gt;This piece of code simply takes requests and it will check if the URL that the user has accessed ends with a trailing slash or not. If not it will let it continue as it would have without this code. But, if there is a trailing slash we do the following -&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Save all query parameters. Such that if the user tries to access &lt;a href="http://movetoabroad.com/about/?utmSource=Wikipedia" rel="noopener noreferrer"&gt;http://movetoabroad.com/about/?utmSource=Wikipedia&lt;/a&gt; we want him to be redirected to &lt;a href="http://movetoabroad.com/about?utmSource=Wikipedia" rel="noopener noreferrer"&gt;http://movetoabroad.com/about?utmSource=Wikipedia&lt;/a&gt; which make sure that the query params passed are not affected in any way.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We then remove all trailing slashes before the query parameters using regex because who is stopping users from trying to access &lt;a href="http://movetoabroad.com/about///" rel="noopener noreferrer"&gt;http://movetoabroad.com/about///&lt;/a&gt;? Our regular expression makes sure of that.&lt;br&gt;
But we have another check that if the user is trying to access home page of the site in that case we will redirect him to home page. Such that &lt;a href="http://movetoabroad.com///" rel="noopener noreferrer"&gt;http://movetoabroad.com///&lt;/a&gt; redirect to &lt;a href="http://movetoabroad.com" rel="noopener noreferrer"&gt;http://movetoabroad.com&lt;/a&gt; but you might notice that we are not using the regex to remove all slashes but have added an extra check this is because the second param of res.redirect cannot be empty and if we remove all slashes using &lt;code&gt;req.path.replace(/\/+$/, "")&lt;/code&gt; we have an empty sting which will lead to error so we redirect it with a single slash instead using &lt;code&gt;res.redirect(301, "/");&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Once we have the URL without trailing slashes and our query params received we just merge them to create the updated URL without trailing slashes and permanent redirect (301) on it.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>ai</category>
      <category>cloudcomputing</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
