<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Pravesh Vyas's Blog. Software Engineer]]></title><description><![CDATA[Pravesh is a software Engineer has worked across multiple startups and has tried multiple side along his carrier.]]></description><link>https://blog.vp5h.com</link><generator>RSS for Node</generator><lastBuildDate>Sun, 12 Apr 2026 02:06:46 GMT</lastBuildDate><atom:link href="https://blog.vp5h.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Simple Concurrent Port Scanner in Go]]></title><description><![CDATA[When learning network programming or cybersecurity basics, building a port scanner is a classic, hands-on project. It’s a practical way to explore sockets, concurrency, and command-line interface (CLI) development. In this post, we’ll break down a li...]]></description><link>https://blog.vp5h.com/writing-a-simple-concurrent-port-scanner-in-go</link><guid isPermaLink="true">https://blog.vp5h.com/writing-a-simple-concurrent-port-scanner-in-go</guid><category><![CDATA[Go Language]]></category><category><![CDATA[Golang developer]]></category><dc:creator><![CDATA[Pravesh Vyas]]></dc:creator><pubDate>Wed, 30 Jul 2025 08:56:05 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1753865799637/f97e7055-7339-45f9-b0ba-b58ae7da64ad.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When learning network programming or cybersecurity basics, building a port scanner is a classic, hands-on project. It’s a practical way to explore sockets, concurrency, and command-line interface (CLI) development. In this post, we’ll break down a lightweight port scanner implemented in Go, showing how its concurrency model makes large-scale scanning both simple and fast.</p>
<h2 id="heading-what-is-a-port-scanner">What is a Port Scanner?</h2>
<p>A <strong>port scanner</strong> is a tool that probes a target machine for open ports. Each open port could represent a running service, like SSH (22), HTTP (80), or custom applications. Port scanning is essential for network diagnostics and is foundational in security assessments.</p>
<h2 id="heading-why-go-for-port-scanning">Why Go for Port Scanning?</h2>
<p>Go (or Golang) offers unique advantages for this task:</p>
<ul>
<li><p><strong>Goroutines</strong> enable lightweight concurrent execution.</p>
</li>
<li><p><strong>Channels and WaitGroups</strong> help coordinate parallel tasks with ease.</p>
</li>
<li><p>Robust networking support built into the standard library.</p>
</li>
</ul>
<p>These features allow us to write highly scalable port scanners with minimal code.</p>
<h2 id="heading-breaking-down-the-port-scanner">Breaking Down the Port Scanner</h2>
<p>Let's examine the most important parts and why they matter.</p>
<h2 id="heading-1-project-structure">1. Project Structure</h2>
<ul>
<li><p><strong>main.go</strong>: Entry point. Sets up flags, concurrency, and result reporting.</p>
</li>
<li><p><strong>scanner/scanner.go</strong>: Contains the scanning logic.</p>
</li>
</ul>
<h2 id="heading-2-command-line-flags">2. Command-Line Flags</h2>
<p>The user can specify a target host via the <code>-host</code> flag (defaults to <a target="_blank" href="http://localhost"><code>localhost</code></a>). The scanner covers the entire range of possible TCP ports (1 to 65535), but you could easily extend this.</p>
<pre><code class="lang-plaintext">gohost := flag.String("host", "localhost", "Target host to scan")
flag.Parse()
</code></pre>
<h2 id="heading-3-concurrency-with-goroutines-and-waitgroups">3. Concurrency with Goroutines and WaitGroups</h2>
<p>The heart of the scanner is a loop that kicks off a goroutine ("lightweight thread") for each port. All goroutines share a common WaitGroup for graceful shutdown and a channel to report results.</p>
<pre><code class="lang-plaintext">gofor port := startPort; port &lt;= endPort; port++ {
    wg.Add(1)
    go func(p int) {
        ps.CheckPort(p, openPorts, &amp;wg)
    }(port)
}
</code></pre>
<h2 id="heading-4-the-port-checking-logic">4. The Port Checking Logic</h2>
<p>Under the hood, <code>CheckPort</code> tries to open a TCP connection to the target host and port.</p>
<pre><code class="lang-plaintext">goaddress := net.JoinHostPort(ps.Host, strconv.Itoa(port))
conn, err := net.DialTimeout("tcp", address, 1*time.Second)
if err == nil {
    conn.Close()
    ch &lt;- port // Report open ports over the channel
}
</code></pre>
<p>A timeout ensures the scanner doesn’t hang on unresponsive ports.</p>
<h2 id="heading-5-collecting-results-asynchronously">5. Collecting Results Asynchronously</h2>
<p>As each scanner goroutine discovers open ports, it sends the port number to the main goroutine via a channel. The main code reads from this channel and prints results as they come:</p>
<pre><code class="lang-plaintext">gofor port := range openPorts {
    fmt.Printf("Port %d is open\n", port)
}
</code></pre>
<p>The result: you see open ports displayed live as they’re found, even while other scans continue in the background.</p>
<h2 id="heading-why-this-pattern-rocks">Why This Pattern Rocks</h2>
<ul>
<li><p><strong>High Performance:</strong> Thanks to Go’s concurrency primitives, you can scan all 65,535 ports very quickly (although in practice, for network friendliness, you may want to limit concurrency in real-world use).</p>
</li>
<li><p><strong>Simplicity:</strong> The code is short, clear, and idiomatic; robust error handling and CLI parsing are easy to add.</p>
</li>
<li><p><strong>Extensible:</strong> You can easily add features like custom port ranges, configurable timeouts, or service banner grabbing.</p>
</li>
</ul>
<h2 id="heading-possible-enhancements">Possible Enhancements</h2>
<ul>
<li><p><strong>Limit concurrent goroutines</strong> to avoid overwhelming the host or your own machine.</p>
</li>
<li><p>Support for <strong>port ranges</strong> or port list input instead of always scanning all ports.</p>
</li>
<li><p>Add <strong>result output in formats</strong> like JSON or CSV.</p>
</li>
<li><p>Try <strong>UDP scanning</strong> by using <code>"udp"</code> with <code>net.DialTimeout</code>.</p>
</li>
<li><p>Include <strong>verbose logging</strong> for closed/filtered ports, if desired.</p>
</li>
</ul>
<h2 id="heading-final-thoughts">Final Thoughts</h2>
<p>This simple scanner highlights Go’s power for network programming. With just a couple dozen lines, we get a highly concurrent, responsive tool that’s a great starting point for anyone looking to learn about sockets, protocols, or Go’s concurrency story.</p>
<p><strong>Try extending the code—experiment with new features, or run it against test servers in your environment!</strong></p>
<p><strong>Sample Usage:</strong></p>
<pre><code class="lang-plaintext">bashgo run main.go -host example.com
</code></pre>
<p>You’ll see output like:</p>
<pre><code class="lang-plaintext">textPort 22 is open
Port 80 is open
Port 443 is open
</code></pre>
<p>Happy scanning—use responsibly</p>
]]></content:encoded></item><item><title><![CDATA[Complex Promise Problems]]></title><description><![CDATA[Before we start incase you are a complete new-bie in the JS world and want to know basics of promiese. There is no better place than MDN docs to do that.
for ease, we can use the below fucntion which generates Bulk Promises.
const makeArrayOfPromises...]]></description><link>https://blog.vp5h.com/complex-promise-problems</link><guid isPermaLink="true">https://blog.vp5h.com/complex-promise-problems</guid><category><![CDATA[Javascript Promises]]></category><category><![CDATA[javscript]]></category><category><![CDATA[asynchronous JavaScript]]></category><dc:creator><![CDATA[Pravesh Vyas]]></dc:creator><pubDate>Sun, 22 Jun 2025 08:31:18 GMT</pubDate><content:encoded><![CDATA[<p>Before we start incase you are a complete new-bie in the JS world and want to know basics of promiese. There is no better place than <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">MDN</a> docs to do that.</p>
<p>for ease, we can use the below fucntion which generates Bulk Promises.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> makeArrayOfPromises = <span class="hljs-function">(<span class="hljs-params">count</span>)=&gt;</span>{
    <span class="hljs-keyword">const</span> promises = []

    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i = <span class="hljs-number">1</span>;  i&lt;=count; i++){
    promises.push(
        <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve, reject</span>)=&gt;</span>{
        <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">()=&gt;</span>{
           <span class="hljs-built_in">Math</span>.random()&gt;<span class="hljs-number">0.6</span> ? resolve(<span class="hljs-string">"success"</span>) :reject(<span class="hljs-string">"error"</span>)
        }, <span class="hljs-number">100</span>*i)
    })
        )}
    <span class="hljs-keyword">return</span> promises;

}
</code></pre>
<p>Starting with one of most common ones</p>
<ol>
<li><p>Implement Polyfil for <code>Promise.all</code></p>
<pre><code class="lang-javascript"> <span class="hljs-keyword">const</span> myPromiseAll = <span class="hljs-keyword">async</span> (promises) =&gt; {
     <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve, reject</span>) =&gt;</span> {
         <span class="hljs-keyword">const</span> responses = []
         promises.map(<span class="hljs-function">(<span class="hljs-params">each, idx</span>) =&gt;</span> {
             each.then(<span class="hljs-function">(<span class="hljs-params">res</span>) =&gt;</span> {
                 responses[idx] = res
                 <span class="hljs-keyword">if</span> (responses.length === promises.length) resolve(responses)
             }).catch(<span class="hljs-function">(<span class="hljs-params">err</span>) =&gt;</span> {
                 <span class="hljs-built_in">console</span>.error(<span class="hljs-string">`Promise <span class="hljs-subst">${i + <span class="hljs-number">1</span>}</span> failed with`</span>, err);
                 reject(err)

             })
         })
     })

 }

 myPromiseAll(promises).then(<span class="hljs-function"><span class="hljs-params">res</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(res)).catch(<span class="hljs-function"><span class="hljs-params">err</span> =&gt;</span> <span class="hljs-built_in">console</span>.error(err))
</code></pre>
</li>
<li><p>Implmenet a Promise batcher that take N promsiese and executes then sequentially</p>
<pre><code class="lang-javascript"> <span class="hljs-keyword">const</span> runPromisesInBatches = <span class="hljs-keyword">async</span> (promises, batch) =&gt; {
     <span class="hljs-keyword">const</span> results = []
     <span class="hljs-keyword">while</span> (promises.length &gt; <span class="hljs-number">0</span>) {
         <span class="hljs-keyword">const</span> result = <span class="hljs-keyword">await</span> <span class="hljs-built_in">Promise</span>.allSettled(promises.slice(<span class="hljs-number">0</span>, batch))
         results.push(result)
         promises = promises.slice(<span class="hljs-number">10</span>)

     }
     <span class="hljs-keyword">return</span> results
 }
</code></pre>
</li>
<li><p>Implement a promise queue where at any given time N promises are active</p>
<p> ```bash</p>
<p> function promiseQueue(tasks, concurrencyLimit) {
   let index = 0;
   let active = 0;
   const results = [];</p>
<p>   return new Promise((resolve, reject) =&gt; {
     const next = () =&gt; {
       if (index === tasks.length &amp;&amp; active === 0) {
         resolve(results);
         return;
       }</p>
<p>       while (active &lt; concurrencyLimit &amp;&amp; index &lt; tasks.length) {</p>
</li>
</ol>
<p>            const currentIndex = index++;
            const task = tasks[currentIndex];
            active++;</p>
<p>             task
              .then((result) =&gt; {
                results[currentIndex] = result;
                active--;
                next(); // Start next task
              })
              .catch((err) =&gt; {
                reject(err); // Stop everything on first error
              });
          }
          console.log(results)
        };</p>
<p>        next(); // Start the first batch
      });
    }
    ```</p>
<ol start="4">
<li><p>MapLimit</p>
<p> Implement a mapLimit function that is similar to the <a target="_blank" href="http://Array.map"><code>Array.map</code></a><code>()</code> which returns a promise that resolves on the list of output by mapping each input through an asynchronous iteratee function or rejects it if any error occurs. It also accepts a limit to decide how many operations can occur at a time.</p>
<p> The asynchronous iteratee function will accept a input and a callback. The callback function will be called when the input is finished processing, the first argument of the callback will be the error flag and the second will be the result</p>
<p> <a target="_blank" href="https://learnersbucket.com/examples/interview/implement-maplimit-async-function/">https://learnersbucket.com/examples/interview/implement-maplimit-async-function/</a></p>
</li>
</ol>
]]></content:encoded></item><item><title><![CDATA[Why should you care About Sockets?]]></title><description><![CDATA[Before diving into Sockets Lets Understand, Why do we need it in the first place, So the Protocol, We generally use is HTTP.  The issue with HTTP is you can only make unidirectional where client sends the request and the server sends the responses to...]]></description><link>https://blog.vp5h.com/why-should-you-care-about-sockets</link><guid isPermaLink="true">https://blog.vp5h.com/why-should-you-care-about-sockets</guid><category><![CDATA[SocketIO]]></category><category><![CDATA[websockets]]></category><category><![CDATA[http]]></category><dc:creator><![CDATA[Pravesh Vyas]]></dc:creator><pubDate>Sat, 16 Oct 2021 10:45:43 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1634379128425/jiW7pbQLG.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Before diving into Sockets Lets Understand, Why do we need it in the first place, So the Protocol, We generally use is HTTP.  The issue with HTTP is you can only make unidirectional where client sends the request and the server sends the responses to it and the connection gets terminated. Which is great but in certain use cases, It doesn't make sense to connect again and again to fetch data.</p>
<blockquote>
<p>HTTP is stateless protocol runs on the top of TCP which is a connection-oriented protocol it guarantees the delivery of data packet transfer using the three-way handshaking methods and re-transmit the lost packets.</p>
</blockquote>
<p>Consider a Situation, Where You want to implement a chat system Every time someone sends a message a Web Hook(A external or internal service which checks incoming requests and Pings the receiver about its existence )  is fired from sender and that makes Request to receiver to Render the whole component again to get a new message, What web sockets is achieving is Direct communication between two Parties, The issue in first approach is we are building connection every time we want to send a message by sent. And in the second one we just keeping connection alive and sending, receiving messages as we they are sent</p>
<blockquote>
<p>Web Socket is bidirectional, a full-duplex protocol that is used in the same scenario of client-server communication, unlike HTTP it starts from ws:// or wss://. It is a stateful protocol, which means the connection between client and server will keep alive until it is terminated by either party (client or server). after closing the connection by either of the client and server, the connection is terminated from both the end.</p>
</blockquote>
<p>So far you might think, The Problems is solved we are using Sockets, But that's not the case Sockets are a great concept in themselves, It turns out that sockets themselves are not enough to get things done. What we do instead is use a combination of Http Polling and Sockets to get best results, This approach is used in <a target="_blank" href="http://Sockets.io">Sockets.io</a> Which is a very popular library to achieve Realtime communication  </p>
<p>Http Polling is a  process where client sends a request and the server hangs on to the particular request until there is  a new data to be sent</p>
<p><a target="_blank" href="http://Socket.io">Socket.io</a> needs a server side installation and a client side installation to work.</p>
<p>All events are defined in server side and client side sends and receives data from socket server as the  communication happens</p>
<p>You can read more about socket.io works here</p>
<p><a target="_blank" href="https://socket.io/docs/v4/how-it-works">https://socket.io/docs/v4/how-it-works</a></p>
<p>You can follow these Blogs get yourself started in Sokets</p>
<p><a target="_blank" href="https://socket.io/get-started/chat">https://socket.io/get-started/chat</a></p>
<p><a target="_blank" href="https://medium.com/swlh/chat-rooms-with-socket-io-25e9d1a05947">https://medium.com/swlh/chat-rooms-with-socket-io-25e9d1a05947</a></p>
]]></content:encoded></item><item><title><![CDATA[Map, Filter and Reduce]]></title><description><![CDATA[Map, reduce, and filter are all array methods in JavaScript. Each one will iterate over an array and perform a transformation or computation. Each will return a new array based on the result of the function. In a nutshell is a more Syntactically suga...]]></description><link>https://blog.vp5h.com/map-filter-and-reduce</link><guid isPermaLink="true">https://blog.vp5h.com/map-filter-and-reduce</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[array methods]]></category><category><![CDATA[prototyping]]></category><dc:creator><![CDATA[Pravesh Vyas]]></dc:creator><pubDate>Wed, 13 Oct 2021 11:40:34 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1634383245283/s6n4AuPfD.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Map, reduce, and filter are all array methods in JavaScript. Each one will iterate over an array and perform a transformation or computation. Each will return a new array based on the result of the function. In a nutshell is a more Syntactically sugared way of writing loops in Js</p>
<h2 id="map">Map</h2>
<p>The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.</p>
<p>Syntax</p>
<pre><code><span class="hljs-keyword">var</span> new_array = arr.map(<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">callback</span>(<span class="hljs-params">element, index, <span class="hljs-keyword">array</span></span>) </span>{
    <span class="hljs-comment">// Return value for new_array</span>
}[, thisArg])
</code></pre><p>In the above example, Element is the value of element at that particular Index, Element refers the postion in array, Array is the copy of original array which could be used as a conditional for the upcoming render or the returned array.</p>
<p>In the callback, only the array element is required. Usually some action is performed on the value and then a new value is returned.</p>
<p>Example
In the following example, each number in an array is doubled.</p>
<pre><code><span class="hljs-string">const</span> <span class="hljs-string">numbers</span> <span class="hljs-string">=</span> [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]<span class="hljs-string">;</span>
<span class="hljs-string">const</span> <span class="hljs-string">doubled</span> <span class="hljs-string">=</span> <span class="hljs-string">numbers.map(item</span> <span class="hljs-string">=&gt;</span> <span class="hljs-string">item</span> <span class="hljs-string">*</span> <span class="hljs-number">2</span><span class="hljs-string">);</span>
<span class="hljs-string">console.log(doubled);</span> <span class="hljs-string">//</span> [<span class="hljs-number">2</span>, <span class="hljs-number">4</span>, <span class="hljs-number">6</span>, <span class="hljs-number">8</span>]
</code></pre><p>The below example involves uses of all the arguments for Better understanding. </p>
<pre><code><span class="hljs-string">const</span> <span class="hljs-string">oldArray</span> <span class="hljs-string">=</span> [<span class="hljs-number">16</span>, <span class="hljs-number">9</span>, <span class="hljs-number">4</span>, <span class="hljs-number">1</span>]<span class="hljs-string">;</span>

<span class="hljs-string">const</span> <span class="hljs-string">newArray</span> <span class="hljs-string">=</span> <span class="hljs-string">oldArray.map((val,</span> <span class="hljs-string">index,</span> <span class="hljs-string">arr)</span> <span class="hljs-string">=&gt;</span> {
  <span class="hljs-string">let</span> <span class="hljs-string">nextItem</span> <span class="hljs-string">=</span> <span class="hljs-string">index</span> <span class="hljs-string">+</span> <span class="hljs-number">1</span> <span class="hljs-string">&lt;</span> <span class="hljs-string">arr.length</span> <span class="hljs-string">?</span> <span class="hljs-string">arr</span>[<span class="hljs-string">index</span> <span class="hljs-string">+</span> <span class="hljs-number">1</span>] <span class="hljs-string">:</span> <span class="hljs-number">0</span>
  <span class="hljs-string">return</span> <span class="hljs-string">val</span> <span class="hljs-bullet">-</span> <span class="hljs-string">nextItem;</span>
}<span class="hljs-string">);</span>

<span class="hljs-string">//</span> <span class="hljs-string">newArray</span> <span class="hljs-string">=</span> [<span class="hljs-number">7</span>, <span class="hljs-number">5</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1</span>]
</code></pre><h2 id="filter">Filter</h2>
<p>The filter() method creates a new array with all elements that pass the test implemented by the provided function.</p>
<p>Syntax</p>
<pre><code>var new_array = arr.<span class="hljs-keyword">filter</span>(<span class="hljs-keyword">function</span> callback(element, <span class="hljs-keyword">index</span>, <span class="hljs-keyword">array</span>) {
    // <span class="hljs-keyword">Return</span> <span class="hljs-keyword">true</span> <span class="hljs-keyword">or</span> <span class="hljs-keyword">false</span>
}[, thisArg])
</code></pre><p>The syntax for filter is similar to map, except the callback function should return true to keep the element, or false otherwise. In the callback, only the element is required.</p>
<p>Examples
In the following example, odd numbers are "filtered" out, leaving only even numbers.</p>
<pre><code><span class="hljs-string">const</span> <span class="hljs-string">numbers</span> <span class="hljs-string">=</span> [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]<span class="hljs-string">;</span>
<span class="hljs-string">const</span> <span class="hljs-string">evens</span> <span class="hljs-string">=</span> <span class="hljs-string">numbers.filter(item</span> <span class="hljs-string">=&gt;</span> <span class="hljs-string">item</span> <span class="hljs-string">%</span> <span class="hljs-number">2</span> <span class="hljs-string">===</span> <span class="hljs-number">0</span><span class="hljs-string">);</span>
<span class="hljs-string">console.log(evens);</span> <span class="hljs-string">//</span> [<span class="hljs-number">2</span>, <span class="hljs-number">4</span>]
<span class="hljs-string">In</span> <span class="hljs-string">the</span> <span class="hljs-string">next</span> <span class="hljs-string">example,</span> <span class="hljs-string">filter()</span> <span class="hljs-string">is</span> <span class="hljs-string">used</span> <span class="hljs-string">to</span> <span class="hljs-string">get</span> <span class="hljs-string">all</span> <span class="hljs-string">the</span> <span class="hljs-string">students</span> <span class="hljs-string">whose</span> <span class="hljs-string">grades</span> <span class="hljs-string">are</span> <span class="hljs-string">greater</span> <span class="hljs-string">than</span> <span class="hljs-string">or</span> <span class="hljs-string">equal</span> <span class="hljs-string">to</span> <span class="hljs-number">90</span><span class="hljs-string">.</span>

<span class="hljs-string">const</span> <span class="hljs-string">students</span> <span class="hljs-string">=</span> [
  { <span class="hljs-attr">name:</span> <span class="hljs-string">'Quincy'</span>, <span class="hljs-attr">grade:</span> <span class="hljs-number">96</span> },
  { <span class="hljs-attr">name:</span> <span class="hljs-string">'Jason'</span>, <span class="hljs-attr">grade:</span> <span class="hljs-number">84</span> },
  { <span class="hljs-attr">name:</span> <span class="hljs-string">'Alexis'</span>, <span class="hljs-attr">grade:</span> <span class="hljs-number">100</span> },
  { <span class="hljs-attr">name:</span> <span class="hljs-string">'Sam'</span>, <span class="hljs-attr">grade:</span> <span class="hljs-number">65</span> },
  { <span class="hljs-attr">name:</span> <span class="hljs-string">'Katie'</span>, <span class="hljs-attr">grade:</span> <span class="hljs-number">90</span> }
]<span class="hljs-string">;</span>
<span class="hljs-string">const</span> <span class="hljs-string">studentGrades</span> <span class="hljs-string">=</span> <span class="hljs-string">students.filter(student</span> <span class="hljs-string">=&gt;</span> <span class="hljs-string">student.grade</span> <span class="hljs-string">&gt;=</span> <span class="hljs-number">90</span><span class="hljs-string">);</span>
<span class="hljs-string">return</span> <span class="hljs-string">studentGrades;</span> <span class="hljs-string">//</span> [ { <span class="hljs-attr">name:</span> <span class="hljs-string">'Quincy'</span>, <span class="hljs-attr">grade:</span> <span class="hljs-number">96</span> }, { <span class="hljs-attr">name:</span> <span class="hljs-string">'Alexis'</span>, <span class="hljs-attr">grade:</span> <span class="hljs-number">100</span> }, { <span class="hljs-attr">name:</span> <span class="hljs-string">'Katie'</span>, <span class="hljs-attr">grade:</span> <span class="hljs-number">90</span> } ]
</code></pre><h2 id="reduce">Reduce</h2>
<p>The reduce() method executes a user-supplied “reducer” callback function on each element of the array, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.</p>
<p>The reduce() method reduces an array of values down to just one value. To get the output value, it runs a reducer function on each element of the array.</p>
<p>Syntax</p>
<pre><code>arr.reduce(callback, [initialValue])

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">callbackFn</span>(<span class="hljs-params">previousValue, currentValue, currentIndex, <span class="hljs-keyword">array</span></span>)</span>{ ... }
</code></pre><p>The callback argument is a function that will be called once for every item in the array. This function takes four arguments, but often only the first two are used.</p>
<p>accumulator - the returned value of the previous iteration
currentValue - the current item in the array
index - the index of the current item
array - the original array on which reduce was called
The initialValue argument is optional. If provided, it will be used as the initial accumulator value in the first call to the callback function.
Examples
The following example adds every number together in an array of numbers.</p>
<pre><code><span class="hljs-keyword">const</span> numbers = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>];
<span class="hljs-keyword">const</span> sum = numbers.reduce(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">result, item</span>) </span>{
  <span class="hljs-keyword">return</span> result + item;
}, <span class="hljs-number">0</span>);
<span class="hljs-built_in">console</span>.log(sum); <span class="hljs-comment">// 10</span>
</code></pre><p>In the next example, reduce() is used to transform an array of strings into a single object that shows how many times each string appears in the array. Notice this call to reduce passes an empty object {} as the initialValue parameter. This will be used as the initial value of the accumulator (the first argument) passed to the callback function.</p>
<pre><code>
<span class="hljs-keyword">var</span> pets = [<span class="hljs-string">'dog'</span>, <span class="hljs-string">'chicken'</span>, <span class="hljs-string">'cat'</span>, <span class="hljs-string">'dog'</span>, <span class="hljs-string">'chicken'</span>, <span class="hljs-string">'duck'</span>, <span class="hljs-string">'rabbit'</span>];

<span class="hljs-keyword">var</span> petCounts = pets.reduce(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">obj, pet</span>)</span>{
    <span class="hljs-keyword">if</span> (!obj[pet]) {
        obj[pet] = <span class="hljs-number">1</span>;
    } <span class="hljs-keyword">else</span> {
        obj[pet]++;
    }
    <span class="hljs-keyword">return</span> obj;
}, {});

<span class="hljs-built_in">console</span>.log(petCounts); 

<span class="hljs-comment">/*
Output:
 { 
    dog: 2, 
    chicken: 2, 
    cat: 1,
    duck: 1, 
    rabbit: 1 
 }
 */</span>
</code></pre><p>Refrences:</p>
<p>https://developer.mozilla.org/</p>
<p>https://www.freecodecamp.org/</p>
<p>https://www.youtube.com/watch?v=zdp0zrpKzIE</p>
<p>Thanks for Reading!</p>
]]></content:encoded></item><item><title><![CDATA[Git Merge]]></title><description><![CDATA[Git Merge, It is one of those concepts that newbies Barely use, These are Majorly used in cases where we need to development on a Experimental feature or Want to try something which we don't like to ship. The Idea of Merging two separate branches to ...]]></description><link>https://blog.vp5h.com/git-merge</link><guid isPermaLink="true">https://blog.vp5h.com/git-merge</guid><category><![CDATA[GitHub]]></category><category><![CDATA[Git]]></category><dc:creator><![CDATA[Pravesh Vyas]]></dc:creator><pubDate>Thu, 12 Aug 2021 19:04:56 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1634384620023/awUMS1cqH.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Git Merge, It is one of those concepts that newbies Barely use, These are Majorly used in cases where we need to development on a Experimental feature or Want to try something which we don't like to ship. The Idea of Merging two separate branches to one always fascinated me, But I always use to think How would Merge Conflicts be resolved in a case Where things don't add Up or we have a file which has two different version on two separate branches. Lets see How Merging Works in Git.</p>
<p>##According to definition </p>
<blockquote>
<p>Git merge will combine multiple sequences of commits into one unified history. In the most frequent use cases, git merge is used to combine two branches. The following examples in this document will focus on this branch merging pattern. In these scenarios, git merge takes two commit pointers, usually the branch tips, and will find a common base commit between them. Once Git finds a common base commit it will create a new "merge commit" that combines the changes of each queued merge commit sequence.</p>
</blockquote>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x05h36a9zhcmb43t68ey.png" alt="Unmerged Git Branches" />
<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/th23nsektnnp661dxm52.png" alt="Merged branches" /></p>
<p>In the above image we can see that there is a Common base between the Two Points we want to Merge, What happens here is Git Diffs Both the Branches Against there Common Ancestor and Calculates Changes based on it. </p>
<h2 id="types-of-merges">Types Of Merges</h2>
<ol>
<li><p>Fast Forward Merge.</p>
<p> It occurs when there's a direct path between the current branch tip and the Current Tip. It Basically merges the history of Both the Tips.</p>
<p> <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eg81wj6d5ujk0lfs69df.png" alt="03-04 Fast forward merge.svg" /></p>
</li>
<li><p>3 way merge</p>
<p> This Happens when there's no direct path between the Main and Current Branch. The Name comes from the Fact that git uses three commits to get make the merge commit it combines the source, feature and common base to come to a new merged commit .</p>
</li>
</ol>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ee2bkisvrr576sfmibe.png" alt="05-06 Fast forward merge.svg" /></p>
<p>In a three way there could conflicts to files changes in the both the Tips</p>
<h2 id="conflict-resolution">Conflict Resolution</h2>
<p>To resolve them what we need to do is change the files and Use the Command </p>
<pre><code>Git <span class="hljs-keyword">Add</span>
</code></pre><p>and then </p>
<pre><code>GIt <span class="hljs-keyword">Commit</span> -m "SomeThing here"
</code></pre><p>To get the Tips Merges </p>
<p>and it will create the Merged Branch.</p>
<p>Various Commands that can help in resolution of conflicts.</p>
<pre><code>git <span class="hljs-keyword">log</span> —merge
</code></pre><p>It helps in producing the list of command that are causing the conflicts</p>
<pre><code><span class="hljs-attribute">git</span> Diff
</code></pre><p>It helps in finding the difference between files between two States</p>
<pre><code>Git <span class="hljs-keyword">merge</span> —<span class="hljs-keyword">abort</span>
</code></pre><p>It helps  by ending the merge process returns the repo into the original state as it was before merging happened.</p>
<p>That Sums up how merging Works Across Branches.</p>
<p><a target="_blank" href="https://www.atlassian.com/git/tutorials/using-branches/git-merge">Credits</a></p>
]]></content:encoded></item></channel></rss>