<?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[Siddhi Gate's Blog]]></title><description><![CDATA[A full-stack developer solving problems through code]]></description><link>https://blog.siddhigate.com</link><generator>RSS for Node</generator><lastBuildDate>Fri, 10 Apr 2026 07:27:12 GMT</lastBuildDate><atom:link href="https://blog.siddhigate.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Video Query AI: Search your videos like you search text 🧠🎥]]></title><description><![CDATA[The Problem
Video content is rich but hard to search through. If you've ever wanted to find that one moment in a long video a specific visual, object, or scene you know how frustrating it can be. Unlike text, video isn't naturally searchable by keywo...]]></description><link>https://blog.siddhigate.com/video-query-ai-search-your-videos-like-you-search-text</link><guid isPermaLink="true">https://blog.siddhigate.com/video-query-ai-search-your-videos-like-you-search-text</guid><category><![CDATA[React]]></category><category><![CDATA[Python]]></category><category><![CDATA[FastAPI]]></category><category><![CDATA[llm]]></category><category><![CDATA[Redis]]></category><dc:creator><![CDATA[Siddhi Gate]]></dc:creator><pubDate>Fri, 18 Jul 2025 21:12:12 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1752862333365/bdff9be1-3e60-440f-9a53-8b06ecbee6dc.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-the-problem">The Problem</h2>
<p>Video content is rich but hard to search through. If you've ever wanted to find that <em>one</em> moment in a long video a specific visual, object, or scene you know how frustrating it can be. Unlike text, video isn't naturally searchable by keywords or phrases unless someone takes the time to manually add descriptions (and honestly, who has time for that?).</p>
<p>That’s where <strong>Video Query AI</strong> comes in.</p>
<p>With Video Query AI, you can ask a question like:</p>
<blockquote>
<p>❓ <em>"When does the chef dice the onions in this cooking tutorial?"</em></p>
<p>❓ <em>"Where is the elephant in this video"</em></p>
<p>❓ <em>"Can you show me where koalas are clapping?"</em></p>
</blockquote>
<p>...and it will instantly give you the exact timestamps where those items appear in your videos. Whether you're searching across all your content or a specific video, you'll get precise results without any manual tagging required.</p>
<h2 id="heading-introducing-video-query-ai">Introducing Video Query AI 🧠</h2>
<p>A fully local, privacy-first app that helps you semantically search through videos using natural language. Simply upload a video, and we’ll do the heavy lifting to process frames, generate descriptions using AI, embed them, and allow fast search.</p>
<p><strong>🎯 Use cases:</strong></p>
<ul>
<li><p>Find key moments in tutorials</p>
</li>
<li><p>Detect objects/scenes in surveillance videos</p>
</li>
<li><p>Find specific scenes or moments in videos with text search</p>
</li>
</ul>
<hr />
<h2 id="heading-app-walkthrough">App Walkthrough ✨</h2>
<h3 id="heading-1-home-amp-video-library">1️⃣ 🏠 Home &amp; Video Library</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1752862225260/b5994750-d236-4ac6-a28c-e84afeea6d0b.gif" alt class="image--center mx-auto" /></p>
<ul>
<li><p>Land on a clean dashboard with a sidebar listing all uploaded videos.</p>
</li>
<li><p>Browse or search through your video collection easily.</p>
</li>
</ul>
<h3 id="heading-2-video-upload-amp-processing">2️⃣ 📤 Video Upload &amp; Processing</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1752862242679/4adcc684-5210-454a-a633-b8f2e79408d8.gif" alt class="image--center mx-auto" /></p>
<ul>
<li><p>Drag and drop a video file for processing.</p>
</li>
<li><p>The backend kicks off a background job to:</p>
<ul>
<li><p>Extract frames using <code>ffmpeg</code></p>
</li>
<li><p>Generate descriptions via <strong>LLaVA</strong></p>
</li>
<li><p>Generate vector embeddings of those descriptions for semantic search</p>
</li>
</ul>
</li>
<li><p>Real-time progress updates</p>
<ul>
<li><p>Updates are streamed live via WebSockets.</p>
</li>
<li><p>Progress persists across reloads using Redis Pub/Sub for state sync</p>
</li>
</ul>
</li>
</ul>
<h3 id="heading-3-natural-language-search">3️⃣ 🔍 Natural Language Search</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1752862256793/877b3735-0fa8-4f64-8764-7eddb6003a5e.gif" alt class="image--center mx-auto" /></p>
<ul>
<li><p>Enter a query like <em>"Where is the elephant?"</em> or <em>"Chef chopping onions"</em></p>
</li>
<li><p>The app performs a vector similarity search against frame captions.</p>
</li>
<li><p>You’ll get timestamps + thumbnails of the best matching moments in the video.</p>
</li>
</ul>
<hr />
<h2 id="heading-system-architecture">⚙ System Architecture</h2>
<h2 id="heading-under-the-hood">Under the Hood 🛠</h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1752862278277/faa90937-b973-4ebd-9da4-1961d47714c4.png" alt class="image--center mx-auto" /></p>
<p>Video Query AI follows a modular architecture built by:</p>
<ul>
<li><p><strong>Frontend:</strong> React + TypeScript with Vite and React Router</p>
</li>
<li><p><strong>Backend:</strong> FastAPI serving REST and WebSocket endpoints</p>
</li>
<li><p><strong>Job Queue:</strong> Redis + RQ for background processing</p>
</li>
<li><p><strong>Embedding Store:</strong> ChromaDB for vector search</p>
</li>
<li><p><strong>Realtime Updates:</strong> WebSockets with Redis Pub/Sub for progress tracking and resumable streams</p>
</li>
</ul>
<h3 id="heading-video-processing-flow">📼 Video Processing Flow</h3>
<p>Here’s what happens behind the scenes when you upload a video:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1752862439792/7a198ef5-81a6-44ca-9307-a09c63521697.png" alt class="image--center mx-auto" /></p>
<ol>
<li><p><strong>Upload</strong></p>
<ul>
<li>File is saved to disk and its metadata is stored in ChromaDB.</li>
</ul>
</li>
<li><p><strong>Job Queuing</strong></p>
<ul>
<li>A video processing job is pushed to a <strong>Redis Queue</strong> and handled asynchronously by a worker.</li>
</ul>
</li>
<li><p><strong>Frame Extraction</strong></p>
<ul>
<li>Frames are extracted from the video using <code>ffmpeg</code>.</li>
</ul>
</li>
<li><p><strong>Frame Analysis</strong></p>
<ul>
<li><p>Each frame is sent through <strong>LLaVA (via Ollama)</strong> to describe it.</p>
</li>
<li><p>Description is embedded into a vector using a sentence transformer.</p>
</li>
</ul>
</li>
<li><p><strong>Storage</strong></p>
<ul>
<li>Vector embeddings + metadata is stored in ChromaDB.</li>
</ul>
</li>
<li><p><strong>Progress Updates</strong></p>
<ul>
<li>Real-time progress is sent to the frontend via WebSockets + Redis PubSub.</li>
</ul>
</li>
</ol>
<h3 id="heading-search-flow">🔍 Search Flow</h3>
<p>Users can search across:</p>
<ul>
<li><p><strong>All uploaded videos</strong></p>
</li>
<li><p><strong>A single selected video</strong></p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1752862430476/7144c89f-148b-4b72-8a15-99b255f4163a.png" alt class="image--center mx-auto" /></p>
<p>When a query is made:</p>
<ol>
<li><p>The backend embeds the query using the same embedding model.</p>
</li>
<li><p>A <strong>vector similarity search</strong> is performed in ChromaDB.</p>
</li>
<li><p>Top 10 closest matches (timestamps + thumbnails) are returned.</p>
</li>
</ol>
<h3 id="heading-real-time-progress-amp-resumability">🔁 Real-Time Progress &amp; Resumability</h3>
<p>Even if the user refreshes the page mid-processing:</p>
<ul>
<li><p>The frontend reconnects via WebSocket.</p>
</li>
<li><p>The backend reads the current job state from Redis and resumes updates seamlessly.</p>
</li>
</ul>
<hr />
<h2 id="heading-whats-next">What’s Next ✨</h2>
<ul>
<li><p>🔍 <strong>Multi-modal search:</strong> Use both audio and visual cues</p>
</li>
<li><p>🧩 <strong>Tag-based search:</strong> Filter by detected objects/tags</p>
</li>
<li><p>🎬 <strong>Scene-wise processing:</strong> Break videos into meaningful segments and let users search by scene, not just individual frames.</p>
</li>
</ul>
<h2 id="heading-useful-links"><strong>Useful links 🔗</strong></h2>
<ul>
<li><p><a target="_blank" href="https://www.loom.com/share/10c1299c709545598d2f533761b57972?sid=afb5fb28-a13c-45ae-88c7-535d5e11a34e"><strong>Demo link</strong></a></p>
</li>
<li><p><a target="_blank" href="https://github.com/siddhigate/video-query-ai"><strong>GitHub Repo</strong></a></p>
</li>
</ul>
<h2 id="heading-thats-all-folks"><strong>That’s</strong> <strong>all folks</strong> <strong>��</strong></h2>
<p>Building <strong>Video Query AI</strong> was a fun way to bring together my interests in video processing, LLMs, and systems engineering. I hope it helps you explore your video content more easily or gives you an idea for something you'd like to build!</p>
<p>Let’s connect:</p>
<ul>
<li><p><a target="_blank" href="https://x.com/SiddhiGate">Twitter</a></p>
</li>
<li><p><a target="_blank" href="https://github.com/siddhigate">GitHub</a></p>
</li>
<li><p><a target="_blank" href="https://www.linkedin.com/in/siddhigate/">LinkedIn</a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Exploring Promise Methods and their Polyfills in JavaScript: all(), any(), allSettled() and race()]]></title><description><![CDATA[In this article, I’ve explained the Promise methods: all(), any(), race(), allSettled() along with their polyfills.
Promise.all()

It accepts an iterable of promises as its argument and returns a Promise

If all promises resolve, then the returned pr...]]></description><link>https://blog.siddhigate.com/exploring-promise-methods-and-their-polyfills-in-javascript-all-any-allsettled-and-race</link><guid isPermaLink="true">https://blog.siddhigate.com/exploring-promise-methods-and-their-polyfills-in-javascript-all-any-allsettled-and-race</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[promise methods]]></category><category><![CDATA[polyfills]]></category><dc:creator><![CDATA[Siddhi Gate]]></dc:creator><pubDate>Thu, 16 Feb 2023 08:24:48 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1676524315093/49aac228-f0c4-4f87-89af-413d22e4aa8e.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In this article, I’ve explained the Promise methods: all(), any(), race(), allSettled() along with their polyfills.</p>
<h2 id="heading-promiseall">Promise.all()</h2>
<ul>
<li><p>It accepts an iterable of promises as its argument and returns a Promise</p>
</li>
<li><p>If all promises resolve, then the returned promise is resolved with an array of fulfilled values. The order of the values is the same as that of the promises.</p>
</li>
<li><p>If any of the promises reject, then the returned promise is rejected with the reason of the first rejected promise.</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1676523116147/b88dad29-d8f9-47f5-aa67-f50a9716b293.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-custom-implementation">Custom implementation:</h3>
<pre><code class="lang-jsx"><span class="hljs-built_in">Promise</span>.myAll = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">promises</span>) </span>{
  <span class="hljs-keyword">const</span> result = [];
  <span class="hljs-keyword">let</span> resolvedCount = <span class="hljs-number">0</span>;

  <span class="hljs-keyword">if</span>(promises.length === <span class="hljs-number">0</span>) {
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">Promise</span>.resolve(promises)
  }

  <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> {
    promises.forEach(<span class="hljs-function">(<span class="hljs-params">promise, idx</span>) =&gt;</span> {
      <span class="hljs-built_in">Promise</span>.resolve(promise)
        .then(<span class="hljs-function">(<span class="hljs-params">res</span>) =&gt;</span> {
          result[idx] = res;
          resolvedCount++;

          <span class="hljs-keyword">if</span> (resolvedCount === promises.length) {
            resolve(result);
          }
        })
        .catch(<span class="hljs-function">(<span class="hljs-params">err</span>) =&gt;</span> {
            reject(err)
        });
    });
  });
};
</code></pre>
<h3 id="heading-test-cases">Test cases:</h3>
<p><strong>Test case 1:</strong> If all the promises resolve</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> case1 = [
  <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve</span>) =&gt;</span> {
    timer2 = <span class="hljs-built_in">setTimeout</span>(resolve, <span class="hljs-number">3000</span>, <span class="hljs-string">"resolved"</span>);
  }),
  <span class="hljs-number">200</span>,
  <span class="hljs-built_in">Promise</span>.resolve(<span class="hljs-string">"Hello"</span>),
];

<span class="hljs-built_in">Promise</span>.myAll(case1)
  .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>.log(<span class="hljs-string">"Error: "</span>, err));

<span class="hljs-comment">// Output:</span>
<span class="hljs-comment">// [ "resolved", 200, "Hello"]</span>
</code></pre>
<p><strong>Test case 2:</strong> If any of the promises reject</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> case2 = [
  <span class="hljs-built_in">Promise</span>.reject(<span class="hljs-string">"rejected"</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> {
    timer2 = <span class="hljs-built_in">setTimeout</span>(reject, <span class="hljs-number">3000</span>, <span class="hljs-string">"rejectedReason"</span>);
  }),
];

<span class="hljs-built_in">Promise</span>.myAll(case2)
  .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>.log(<span class="hljs-string">"Error: "</span>, err));

<span class="hljs-comment">// Output:</span>
<span class="hljs-comment">// Error: rejected</span>
</code></pre>
<p><strong>Test case 3:</strong> If an empty array</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> case3 = [];

<span class="hljs-built_in">Promise</span>.myAll(case3)
  .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>.log(<span class="hljs-string">"Error: "</span>, err));

<span class="hljs-comment">// Output:</span>
<span class="hljs-comment">// []</span>
</code></pre>
<h2 id="heading-promiseany">Promise.any()</h2>
<ul>
<li><p>It accepts an iterable of promises as its argument and returns a Promise</p>
</li>
<li><p>The Promise resolves when the first promise in the array resolves. It is resolved with the value of the first promise.</p>
</li>
<li><p>The Promise rejects when all the promises are rejected. It is rejected with an AggregateError which includes all the rejected promises’ reasons.</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1676523148098/33795c6b-9d2d-493b-8f26-2b7550a1b75e.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-custom-implementation-1">Custom implementation:</h3>
<pre><code class="lang-jsx"><span class="hljs-built_in">Promise</span>.myAny = <span class="hljs-function">(<span class="hljs-params">promises</span>) =&gt;</span> {
  <span class="hljs-keyword">const</span> errorsList = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Array</span>(promises.length);
  <span class="hljs-keyword">let</span> counter = <span class="hljs-number">0</span>;

  <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> {
    promises.forEach(<span class="hljs-function">(<span class="hljs-params">promise, idx</span>) =&gt;</span> {
      <span class="hljs-built_in">Promise</span>.resolve(promise)
        .then(<span class="hljs-function">(<span class="hljs-params">res</span>) =&gt;</span> resolve(res))
        .catch(<span class="hljs-function">(<span class="hljs-params">err</span>) =&gt;</span> {
          errorsList[idx] = err;
          counter++;

          <span class="hljs-keyword">if</span> (counter === promises.length) {
            reject(
              <span class="hljs-keyword">new</span> AggregateError(errorsList, <span class="hljs-string">"All promises were rejected"</span>)
            );
          }
        });
    });
  });
};
</code></pre>
<h3 id="heading-test-cases-1">Test cases:</h3>
<p><strong>Test case 1:</strong> If any of the promises resolve</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> case1 = [
  <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve</span>) =&gt;</span> {
    timer2 = <span class="hljs-built_in">setTimeout</span>(resolve, <span class="hljs-number">3000</span>, <span class="hljs-string">"resolved"</span>);
  }),
  <span class="hljs-number">200</span>,
  <span class="hljs-built_in">Promise</span>.reject(<span class="hljs-string">"Hello"</span>),
];

<span class="hljs-built_in">Promise</span>.myAny(case1)
  .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>.log(<span class="hljs-string">"Error: "</span>, err));

<span class="hljs-comment">// Output:</span>
<span class="hljs-comment">// 200</span>
</code></pre>
<p><strong>Test case 2:</strong> If all of the promises reject</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> case2 = [
  <span class="hljs-built_in">Promise</span>.reject(<span class="hljs-string">"rejected"</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> {
    timer2 = <span class="hljs-built_in">setTimeout</span>(reject, <span class="hljs-number">3000</span>, <span class="hljs-string">"rejectedReason"</span>);
  }),
];

<span class="hljs-built_in">Promise</span>.myAny(case2)
  .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>.log(<span class="hljs-string">"Error: "</span>, err.name, err.message, err.errors));

<span class="hljs-comment">// Output:</span>
<span class="hljs-comment">// Error: AggregateError All promises were rejected ["rejected", "rejectedReason"]</span>
</code></pre>
<h2 id="heading-promiserace">Promise.race()</h2>
<ul>
<li><p>It accepts an iterable of promises as its argument and returns a Promise</p>
</li>
<li><p>It returns a Promise when any of the passed promises is settled.</p>
</li>
<li><p>If the first settled promise is resolved, the returned promise is resolved.</p>
</li>
<li><p>If the first settled promise is rejected, the returned promise is rejected.</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1676523168319/9d049adc-ee8b-4c12-9d92-52e50bf9f227.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-polyfill">Polyfill:</h3>
<pre><code class="lang-jsx"><span class="hljs-built_in">Promise</span>.myRace = <span class="hljs-function">(<span class="hljs-params">promises</span>) =&gt;</span> {
  <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> {
    promises.forEach(<span class="hljs-function">(<span class="hljs-params">promise</span>) =&gt;</span> {
      <span class="hljs-built_in">Promise</span>.resolve(promise).then(resolve, reject).catch(reject);
    });
  });
};
</code></pre>
<h3 id="heading-test-cases-2">Test cases:</h3>
<p><strong>Test case 1:</strong> If the first settled promise resolves</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> case1 = [
  <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve</span>) =&gt;</span> {
    timer2 = <span class="hljs-built_in">setTimeout</span>(resolve, <span class="hljs-number">3000</span>, <span class="hljs-string">"resolved"</span>);
  }),
  <span class="hljs-number">200</span>,
  <span class="hljs-built_in">Promise</span>.reject(<span class="hljs-string">"Hello"</span>),
];

<span class="hljs-built_in">Promise</span>.myRace(case1)
  .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>.log(<span class="hljs-string">"Error: "</span>, err));

<span class="hljs-comment">// Output:</span>
<span class="hljs-comment">// 200</span>
</code></pre>
<p><strong>Test case 2:</strong> If the first settled promise rejects</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> case2 = [
  <span class="hljs-built_in">Promise</span>.reject(<span class="hljs-string">"rejected"</span>),
  <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve</span>) =&gt;</span> {
    timer2 = <span class="hljs-built_in">setTimeout</span>(resolve, <span class="hljs-number">3000</span>, <span class="hljs-string">"rejectedReason"</span>);
  }),
];

<span class="hljs-built_in">Promise</span>.myRace(case2)
  .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>.log(<span class="hljs-string">"Error: "</span>, err));

<span class="hljs-comment">// Output:</span>
<span class="hljs-comment">// Error: rejected</span>
</code></pre>
<h2 id="heading-promiseallsettled">Promise.allSettled()</h2>
<ul>
<li><p>It accepts an iterable of promises as its argument and returns a Promise</p>
</li>
<li><p>It returns a Promise when all of the passed promises are settled.</p>
</li>
<li><p>It is resolved with an array of values that provided the status of the promise i.e. whether it is fulfilled or rejected and the value or the reason with which the promise was fulfilled or rejected respectively.</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1676523181286/d742f7e7-37df-4670-bd2c-b54851378fff.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-polyfill-1">Polyfill:</h3>
<pre><code class="lang-jsx"><span class="hljs-built_in">Promise</span>.myAllSettled = <span class="hljs-function">(<span class="hljs-params">promises</span>) =&gt;</span> {
  <span class="hljs-keyword">const</span> results = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Array</span>(promises.length);
  <span class="hljs-keyword">let</span> counter = <span class="hljs-number">0</span>;

  <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> {
    promises.forEach(<span class="hljs-function">(<span class="hljs-params">promise, index</span>) =&gt;</span> {
      <span class="hljs-built_in">Promise</span>.resolve(promise)
        .then(<span class="hljs-function">(<span class="hljs-params">res</span>) =&gt;</span> {
          results[index] = { <span class="hljs-attr">status</span>: <span class="hljs-string">"fulfilled"</span>, <span class="hljs-attr">value</span>: res };
        })
        .catch(<span class="hljs-function">(<span class="hljs-params">err</span>) =&gt;</span> {
          results[index] = { <span class="hljs-attr">status</span>: <span class="hljs-string">"rejected"</span>, <span class="hljs-attr">reason</span>: err };
        })
        .finally(<span class="hljs-function">() =&gt;</span> {
          counter++;
          <span class="hljs-keyword">if</span> (counter === promises.length) {
            resolve(results);
          }
        });
    });
  });
};
</code></pre>
<h3 id="heading-test-cases-3">Test cases:</h3>
<p><strong>Test case:</strong> When all promises settle</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> case1 = [
  <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve</span>) =&gt;</span> {
    timer2 = <span class="hljs-built_in">setTimeout</span>(resolve, <span class="hljs-number">3000</span>, <span class="hljs-string">"resolved"</span>);
  }),
  <span class="hljs-number">200</span>,
  <span class="hljs-built_in">Promise</span>.reject(<span class="hljs-string">"Hello"</span>),
];

<span class="hljs-built_in">Promise</span>.myAllSettled(case1)
  .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>.log(<span class="hljs-string">"Error: "</span>, err));

<span class="hljs-comment">// Output:</span>
<span class="hljs-comment">// [</span>
<span class="hljs-comment">//  { status: 'fulfilled', value: 'resolved' },</span>
<span class="hljs-comment">//  { status: 'fulfilled', value: 200 },</span>
<span class="hljs-comment">//  { status: 'rejected', reason: 'Hello' }</span>
<span class="hljs-comment">// ]</span>
</code></pre>
<h2 id="heading-thats-all-folks">That’s all folks 👋</h2>
<p>Hope you liked this article! If there are any improvements that can be made in the polyfills, feel free to raise an issue here 👇</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://github.com/siddhigate/js-polyfills">https://github.com/siddhigate/js-polyfills</a></div>
<p> </p>
<p>Let’s connect:</p>
<ul>
<li><p><a target="_blank" href="https://twitter.com/SiddhiGate"><strong>Twitter</strong></a></p>
</li>
<li><p><a target="_blank" href="https://github.com/siddhigate"><strong>GitHub</strong></a></p>
</li>
<li><p><a target="_blank" href="https://www.linkedin.com/in/siddhigate/"><strong>LinkedIn</strong></a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Skip Links: Improving Accessibility and Navigation on Your Website 🔗]]></title><description><![CDATA[Skip links allow screen readers, keyboard users, and other assistive technology users to quickly navigate to the main content and skip over large headers or navigation. In this article, I’ve explained the need for skip links, how skip links improve a...]]></description><link>https://blog.siddhigate.com/skip-links-improving-accessibility-and-navigation-on-your-website</link><guid isPermaLink="true">https://blog.siddhigate.com/skip-links-improving-accessibility-and-navigation-on-your-website</guid><category><![CDATA[Accessibility]]></category><category><![CDATA[HTML5]]></category><dc:creator><![CDATA[Siddhi Gate]]></dc:creator><pubDate>Sat, 04 Feb 2023 09:25:40 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1675439681532/107e112f-1c1a-4d49-8500-f2c1b741ded4.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Skip links allow screen readers, keyboard users, and other assistive technology users to quickly navigate to the main content and skip over large headers or navigation. In this article, I’ve explained the need for skip links, how skip links improve accessibility, and the steps to build a skip link.</p>
<h2 id="heading-1-understanding-the-problem">1. Understanding the Problem 💻</h2>
<p>Nowadays websites have large headers or navigation. This makes it difficult for screen reader and keyboard users to quickly navigate to the main content. They need to tab through each navigation link before reaching the main content. This results in a bad user experience.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1675438331195/457e2544-9483-4527-a5ff-655ae754eeea.gif" alt class="image--center mx-auto" /></p>
<h2 id="heading-2-skip-links-the-solution">2. Skip Links: The Solution 🔗</h2>
<p>To make it easy for screen readers, keyboard users, and other assistive technology users to navigate to the main content, skip links should be built.</p>
<ul>
<li><p>Skip links are present at the top of the website and are visually hidden.</p>
</li>
<li><p>On the first <code>Tab</code> press, this link gains focus and is visible to the users.</p>
</li>
<li><p>The users can then navigate directly to the main content by using the link.</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1675438343066/4777cff0-2ed0-4a11-8772-7f9681cc7e81.gif" alt class="image--center mx-auto" /></p>
<h2 id="heading-3-building-a-skip-link">3. Building a Skip Link 👩‍💻</h2>
<p>Following are the steps to build a skip link:</p>
<ol>
<li><p>Create a link that navigates it to the main content.</p>
</li>
<li><p>Place the link at the top of the website so that it gains focus on the first <code>Tab</code> press.</p>
</li>
<li><p>Make the link visually hidden.</p>
</li>
<li><p>Show the link on focus.</p>
</li>
</ol>
<h3 id="heading-30-the-basic-setup">3.0 The basic setup</h3>
<p>The body of the HTML has 4 navigation links and then the main content. Screen readers or keyboard users will travel through all these navigation links before going to the main content.</p>
<pre><code class="lang-jsx">&lt;body&gt;
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">nav</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/"</span>&gt;</span>Nav link 1<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/"</span>&gt;</span>Nav link 2<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/"</span>&gt;</span>Nav link 3<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/"</span>&gt;</span>Nav link 4<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span></span>
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"main"</span>&gt;</span>Hello world <span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/"</span>&gt;</span>About us<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span></span>
&lt;/body&gt;
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1675438358064/574634db-76d1-48e0-aa11-5afa31a27ded.gif" alt class="image--center mx-auto" /></p>
<h3 id="heading-31-creating-a-link">3.1. Creating a link</h3>
<ul>
<li><p>Create a link to the main content on the page.</p>
</li>
<li><p>Place it at the top of the web page.</p>
</li>
</ul>
<pre><code class="lang-jsx">&lt;body&gt;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#main"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"skip-link"</span>&gt;</span>Skip to content<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span></span>

    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">nav</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/"</span>&gt;</span>Nav link 1<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/"</span>&gt;</span>Nav link 2<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/"</span>&gt;</span>Nav link 3<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/"</span>&gt;</span>Nav link 4<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span></span>
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">main</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"main"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Hello world <span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/"</span>&gt;</span>About us<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span></span>

&lt;/body&gt;
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1675438468263/ab19ee7b-60f1-406b-a063-8057441ffba9.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-32-hiding-the-link">3.2. Hiding the link</h3>
<p>Giving the link an absolute position and translating it by the X-axis by <code>-100%</code> will make it hidden.</p>
<pre><code class="lang-jsx">.skip-link {
    <span class="hljs-attr">position</span>: absolute;
    background: white;
    transform: translateX(<span class="hljs-number">-100</span>%);
    overflow: hidden;
    width: <span class="hljs-number">1</span>px;
}
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1675438478683/8c63bd6b-20f7-4245-89bd-799c4ff89e50.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-33-showing-the-link-on-focus">3.3. Showing the link on focus</h3>
<p>Translating the skip link to 0% will make the link visible.</p>
<pre><code class="lang-jsx">.skip-link:focus {
    <span class="hljs-attr">transform</span>: translateX(<span class="hljs-number">0</span>%);
    overflow: auto;
    width: auto;
}
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1675438492463/b417e840-c9a1-4ba3-965f-8db952437861.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-4-result">4. Result ✨</h2>
<p>Now, on <code>Tab</code> press, the Skip to content link is visible. By clicking on that link, we can skip the navigation links and go directly to the main content.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1675438372910/48b72040-98e3-4b03-a2ad-8f199cfd85cb.gif" alt class="image--center mx-auto" /></p>
<h2 id="heading-5-thats-all-folks">5. That’s all folks 🙌</h2>
<p>By providing a quick and easy way for users to navigate directly to the main content, skip links can improve the overall user experience and accessibility of a website.</p>
<p>Let’s connect:</p>
<ul>
<li><p><a target="_blank" href="https://twitter.com/SiddhiGate"><strong>Twitter</strong></a></p>
</li>
<li><p><a target="_blank" href="https://github.com/siddhigate"><strong>GitHub</strong></a></p>
</li>
<li><p><a target="_blank" href="https://www.linkedin.com/in/siddhigate/"><strong>LinkedIn</strong></a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[How to run CUDA and OpenMP code on Google Colaboratory]]></title><description><![CDATA[As a Computer Science student, I was excited to learn about the possibilities of using Google Colab's free cloud platform to run my CUDA and OpenMP code. However, when I tried to use Colab to run my CUDA and OpenMP-accelerated code, I encountered a n...]]></description><link>https://blog.siddhigate.com/how-to-run-cuda-and-openmp-code-on-google-colaboratory</link><guid isPermaLink="true">https://blog.siddhigate.com/how-to-run-cuda-and-openmp-code-on-google-colaboratory</guid><category><![CDATA[openmp]]></category><category><![CDATA[cuda]]></category><category><![CDATA[Google Colab]]></category><dc:creator><![CDATA[Siddhi Gate]]></dc:creator><pubDate>Sun, 04 Dec 2022 07:04:30 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1670137273084/A4EsPgCXK.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>As a Computer Science student, I was excited to learn about the possibilities of using Google Colab's free cloud platform to run my CUDA and OpenMP code. However, when I tried to use Colab to run my CUDA and OpenMP-accelerated code, I encountered a number of challenges and obstacles. After spending countless hours searching through online forums and documentation, I was determined to find a solution. Through persistence and experimentation, I was able to successfully run my CUDA and OpenMP code on Colab and take advantage of its powerful computational capabilities. In this article, I will share the steps I took to implement this solution 👇</p>
<h2 id="heading-1-setup-google-colab">1. Setup Google Colab</h2>
<h3 id="heading-11-create-a-new-notebook-on-google-colab">1.1. Create a new notebook on Google Colab</h3>
<p>To create a new notebook on Google Colab, follow these steps:</p>
<ol>
<li><p>Go to <a target="_blank" href="http://colab.research.google.com">colab.research.google.com</a> in your web browser.</p>
</li>
<li><p>Sign in to your Google account if you are not already signed in.</p>
</li>
<li><p>In the top-left corner of the page, click on File and then select the "New Notebook" button.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1670132687398/Z-PsJWr0x.png" alt /></p>
</li>
<li><p>This will open a new notebook in a new tab in your web browser.</p>
</li>
<li><p>In the top-left corner of the notebook, you will see the name "Untitled0" - you can click on this to give your notebook a new, more descriptive name.</p>
</li>
<li><p>You are now ready to start using your new notebook on Google Colab.</p>
</li>
</ol>
<h3 id="heading-12-change-the-runtime">1.2. Change the Runtime</h3>
<p>To change the runtime in Google Colab, follow these steps:</p>
<ol>
<li><p>Open the notebook that you want to change the runtime for in Google Colab.</p>
</li>
<li><p>In the top-left corner of the notebook, click on the "Runtime" dropdown menu.</p>
</li>
<li><p>From the dropdown menu, select the "Change runtime type" option.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1670132805764/_A_38hCPN.png" alt /></p>
</li>
<li><p>This will open the "Runtime type" pop-up window.</p>
</li>
<li><p>In the "Hardware accelerator" dropdown menu, select GPU.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1670132848102/Y-vQdDezs.png" alt /></p>
</li>
<li><p>Click on the "Save" button to save your changes and apply the new runtime.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1670132888952/24F_44KfV.png" alt /></p>
</li>
<li><p>The runtime for your notebook will now be changed to the selected option.</p>
</li>
</ol>
<h2 id="heading-2-setup-cuda">2. Setup Cuda</h2>
<h3 id="heading-21-clean-the-instance">2.1 Clean the instance</h3>
<p>In a code cell, run the following command to remove all CUDA-related packages and files from the Google Colab instance:</p>
<pre><code class="lang-apache">!<span class="hljs-attribute">apt</span>-get --purge remove cuda nvidia* libnvidia-*
!<span class="hljs-attribute">dpkg</span> -l | grep cuda- | awk '{print $<span class="hljs-number">2</span>}' | xargs -n<span class="hljs-number">1</span> dpkg --purge
!<span class="hljs-attribute">apt</span>-get remove cuda-*
!<span class="hljs-attribute">apt</span> autoremove
!<span class="hljs-attribute">apt</span>-get update
</code></pre>
<h3 id="heading-22-install-cuda">2.2 Install Cuda</h3>
<p>To install CUDA on Google Colab, in a code cell, run the following command:</p>
<pre><code class="lang-apache">!<span class="hljs-attribute">wget</span>  --no-clobber https://developer.download.nvidia.com/compute/cuda/repos/ubuntu<span class="hljs-number">1804</span>/x<span class="hljs-number">86</span>_<span class="hljs-number">64</span>/cuda-repo-ubuntu<span class="hljs-number">1804</span>_<span class="hljs-number">10</span>.<span class="hljs-number">0</span>.<span class="hljs-number">130</span>-<span class="hljs-number">1</span>_amd<span class="hljs-number">64</span>.deb
!<span class="hljs-attribute">dpkg</span> -i cuda-repo-ubuntu<span class="hljs-number">1804</span>_<span class="hljs-number">10</span>.<span class="hljs-number">0</span>.<span class="hljs-number">130</span>-<span class="hljs-number">1</span>_amd<span class="hljs-number">64</span>.deb
!<span class="hljs-attribute">sudo</span> apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu<span class="hljs-number">1804</span>/x<span class="hljs-number">86</span>_<span class="hljs-number">64</span>/<span class="hljs-number">7</span>fa<span class="hljs-number">2</span>af<span class="hljs-number">80</span>.pub
!<span class="hljs-attribute">apt</span>-get update
!<span class="hljs-attribute">apt</span>-get install cuda-<span class="hljs-number">10</span>-<span class="hljs-number">0</span>
</code></pre>
<h3 id="heading-23-check-installation">2.3 Check Installation</h3>
<p>After the installation is complete, run the following command to verify that CUDA has been installed correctly:</p>
<pre><code class="lang-apache">!<span class="hljs-attribute">nvcc</span> --version
</code></pre>
<p>This should print the version of CUDA that has been installed on your Google Colab instance.</p>
<p><strong>Output:</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1670134096769/j62xr_M5k.png" alt /></p>
<h2 id="heading-3-install-the-nvcc-plugin">3. Install the NVCC plugin</h2>
<h3 id="heading-31-what-is-the-nvcc-plugin">3.1 What is the NVCC plugin?</h3>
<ul>
<li><p>The NVCC plugin is an extension that allows you to compile and run CUDA C/C++ code directly in your Google Colab notebook.</p>
</li>
<li><p>The NVCC plugin uses the NVCC (NVIDIA CUDA Compiler) command-line tool to compile and run CUDA code, providing a convenient and easy-to-use interface for writing, testing, and debugging CUDA code within the notebook environment.</p>
</li>
<li><p>Once the NVCC plugin is installed and loaded, you can use it to write and run CUDA C/C++ code in the code cells of your notebook.</p>
</li>
<li><p>The plugin will automatically compile your code using NVCC and run it on the available CUDA-capable GPU, allowing you to take advantage of the GPU's parallel processing capabilities to speed up your code.</p>
</li>
</ul>
<h3 id="heading-32-install-the-nvcc-plugin">3.2 Install the nvcc plugin</h3>
<p>To load the NVCC plugin on Google Colab, in a code cell, run the following command to install the NVCC plugin:</p>
<pre><code class="lang-apache">!<span class="hljs-attribute">pip</span> install git+https://github.com/andreinechaev/nvcc<span class="hljs-number">4</span>jupyter.git
</code></pre>
<h3 id="heading-33-load-the-plugin">3.3 Load the plugin</h3>
<p>After the installation is complete, to load the extension run the following command:</p>
<pre><code class="lang-apache">%<span class="hljs-attribute">load_ext</span> nvcc_plugin
</code></pre>
<h2 id="heading-4-run-the-cuda-program">4. Run the CUDA program</h2>
<p>To start a CUDA code block in Google Colab, you can use the <code>%%cu</code> cell magic. To use this cell magic, follow these steps:</p>
<ol>
<li><p>In a code cell, type <code>%%cu</code> at the beginning of the first line to indicate that the code in the cell is CUDA C/C++ code.</p>
</li>
<li><p>After the <code>%%cu</code> cell magic, you can write your CUDA C/C++ code as usual.</p>
</li>
<li><p>When the code is executed, the <code>%%cu</code> cell magic will automatically compile the code using NVCC and run it on the available CUDA-capable GPU. The results of the code execution will be displayed in the output of the code cell.</p>
</li>
</ol>
<p><strong>Example:</strong></p>
<pre><code class="lang-apache">%%<span class="hljs-attribute">cu</span>
<span class="hljs-comment">#include &lt;stdio.h&gt;</span>
<span class="hljs-comment">#include &lt;stdlib.h&gt;</span>
<span class="hljs-attribute">int</span> main() {
    <span class="hljs-attribute">printf</span>(<span class="hljs-string">"Hello world"</span>);
    <span class="hljs-attribute">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p><strong>Output:</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1670134516912/CDhE11mAv.png" alt /></p>
<h2 id="heading-5-run-the-openmp-program">5. Run the OpenMP program</h2>
<h3 id="heading-51-write-code-using-the-cuda-cell-magic">5.1. Write code using the %%cuda cell magic</h3>
<p>To start an OpenMP code block in Google Colab, you can use the <code>%%cuda</code> cell magic followed by the <code>--name</code> option and the name of the CUDA file that will contain your OpenMP code. To use this cell magic, follow these steps:</p>
<ol>
<li><p>Open the notebook that you want to use the <code>%%cuda</code> cell magic in Google Colab.</p>
</li>
<li><p>In a code cell, type <code>%%cuda --name omp_cuda.cu</code> at the beginning of the first line to indicate that the code in the cell is CUDA C/C++ code that will be saved in a file named "omp_cuda.cu".</p>
</li>
<li><p>After the <code>%%cuda</code> cell magic, you can write your OpenMP code as usual.</p>
</li>
</ol>
<pre><code class="lang-apache">%%<span class="hljs-attribute">cuda</span> --name omp_cuda.cu
</code></pre>
<p><strong>Example:</strong></p>
<pre><code class="lang-apache">%%<span class="hljs-attribute">cuda</span> --name omp_cuda.cu
<span class="hljs-comment">#include &lt;stdio.h&gt;</span>
<span class="hljs-comment">#include &lt;omp.h&gt;</span>
<span class="hljs-attribute">int</span> main() {
    <span class="hljs-attribute">printf</span>(<span class="hljs-string">"Hello world"</span>);
    <span class="hljs-attribute">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p><strong>Output:</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1670134621898/0AnH6b9Jo.png" alt /></p>
<h3 id="heading-52-run-the-openmp-code">5.2. Run the OpenMP code</h3>
<p>To run an OpenMP code on Google Colab, in a code cell, run the following command to compile the OpenMP code using NVCC and generate an executable file:</p>
<pre><code class="lang-apache">!<span class="hljs-attribute">nvcc</span> -Xcompiler=<span class="hljs-string">"-fopenmp"</span> -arch=sm_<span class="hljs-number">75</span> -o /content/src/omp_cuda /content/src/omp_cuda.cu
!/<span class="hljs-attribute">content</span>/src/omp_cuda
</code></pre>
<ul>
<li><p>The command in the first line will compile OpenMP code in the file "omp_cuda.cu" using NVCC and generate an executable file named "omp_cuda" in the content/src directory.</p>
</li>
<li><p>The <code>f-openmp</code> option indicates the code uses the OpenMP library.</p>
</li>
<li><p>The <code>-arch=sm_75</code> option specifies the compute capability of the GPU the code will run on.</p>
</li>
<li><p>The command in the second line will run the "omp_cuda" executable file, which will in turn execute the OpenMP code on the available CUDA-capable GPU. The output of the code execution will be displayed in the output of the code cell.</p>
</li>
</ul>
<p><strong>Output:</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1670134644858/ZjqWIxf5l.png" alt /></p>
<h2 id="heading-thats-all-folks">That's all folks 👋</h2>
<p>I hope this article helps you to run your CUDA and OpenMP program on Google Collab! Let's connect:</p>
<ul>
<li><p><a target="_blank" href="https://twitter.com/SiddhiGate"><strong>Twitter</strong></a></p>
</li>
<li><p><a target="_blank" href="https://github.com/siddhigate"><strong>GitHub</strong></a></p>
</li>
<li><p><a target="_blank" href="https://www.linkedin.com/in/siddhigate/"><strong>LinkedIn</strong></a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[I wrote polyfills for 32 JavaScript Array methods!]]></title><description><![CDATA[Introduction
While working on projects, I used many JavaScript methods. I was curious about their implementation. So, I tried to implement some of them and it was fun! Fast forward to today, I wrote polyfills for 32 JavaScript Array methods. 
In this...]]></description><link>https://blog.siddhigate.com/i-wrote-polyfills-for-32-javascript-array-methods</link><guid isPermaLink="true">https://blog.siddhigate.com/i-wrote-polyfills-for-32-javascript-array-methods</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[array]]></category><category><![CDATA[array methods]]></category><category><![CDATA[polyfills]]></category><dc:creator><![CDATA[Siddhi Gate]]></dc:creator><pubDate>Sun, 11 Sep 2022 14:18:23 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1662881502255/xfrdEk3X5.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-introduction">Introduction</h2>
<p>While working on projects, I used many JavaScript methods. I was curious about their implementation. So, I tried to implement some of them and it was fun! Fast forward to today, I wrote polyfills for 32 JavaScript Array methods. </p>
<p>In this article, I’ve shared my custom implementation of the JavaScript methods. </p>
<p>While implementing, I’ve divided the methods into 6 categories:</p>
<ol>
<li>Searching methods</li>
<li>Manipulation(Mutating) methods</li>
<li>Iterative methods</li>
<li>Map, filter, and reduce methods</li>
<li>Returning array iterator methods</li>
<li>Other operations methods</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1662827454851/BXlRRsGmo.png" alt="array methods" /></p>
<p>PS: There might be some improvements that can be made or some test cases that I’ve missed addressing in the custom implementations. You can create an issue in this <a target="_blank" href="https://github.com/siddhigate/js-polyfills">repository</a> if you find any improvement that can be made. Thanks!</p>
<h2 id="heading-searching-methods">Searching methods:</h2>
<h3 id="heading-1-at">1. at()</h3>
<p><code>at()</code> method accepts an integer and returns the element present at that index in the array</p>
<p><strong>Test cases:</strong></p>
<ol>
<li>Positive integer<ul>
<li>returns the element present at that index</li>
</ul>
</li>
<li>Negative integer<ul>
<li>counts the element from the last index and returns the element present at that index</li>
</ul>
</li>
<li>Integer which is out of range of the array length<ul>
<li>returns undefined</li>
</ul>
</li>
</ol>
<p><strong>Custom implementation:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-built_in">Array</span>.prototype.myAt = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">index</span>) </span>{
  <span class="hljs-keyword">if</span> (index &gt;= <span class="hljs-number">0</span>) {
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>[index];
  } <span class="hljs-keyword">else</span> {
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>[<span class="hljs-built_in">this</span>.length + index];
  }
};
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> arr = [<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>, <span class="hljs-string">"c"</span>, <span class="hljs-string">"d"</span>, <span class="hljs-string">"e"</span>];

<span class="hljs-comment">// Test case 1:</span>
<span class="hljs-built_in">console</span>.log(arr.myAt(<span class="hljs-number">3</span>));
<span class="hljs-comment">//  output: d </span>

<span class="hljs-comment">// Test case 2:</span>
<span class="hljs-built_in">console</span>.log(arr.myAt(<span class="hljs-number">-1</span>));
<span class="hljs-comment">// output: e</span>

<span class="hljs-comment">// Test case 3:</span>
<span class="hljs-built_in">console</span>.log(arr.myAt(<span class="hljs-number">7</span>));
<span class="hljs-comment">// output: undefined</span>
</code></pre>
<h3 id="heading-2-indexof">2. indexOf()</h3>
<p><code>indexOf()</code> method accepts an element and returns the first index at which the element is present in the array</p>
<p><strong>Test cases:</strong></p>
<ol>
<li>Element found<ul>
<li>returns the index of the first element where the given element is present</li>
</ul>
</li>
<li>Element not found<ul>
<li>returns -1</li>
</ul>
</li>
</ol>
<p><strong>Custom implementation:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-built_in">Array</span>.prototype.myIndexOf = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">element, fromIndex = <span class="hljs-number">0</span></span>) </span>{
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = fromIndex; i &lt; <span class="hljs-built_in">this</span>.length; i++) {
    <span class="hljs-keyword">if</span> (<span class="hljs-built_in">this</span>[i] === element) {
      <span class="hljs-keyword">return</span> i;
    }
  }
  <span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>;
};
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> arr = [<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>, <span class="hljs-string">"c"</span>, <span class="hljs-string">"d"</span>, <span class="hljs-string">"e"</span>, <span class="hljs-string">"a"</span>];

<span class="hljs-comment">// Test case 1:</span>
<span class="hljs-built_in">console</span>.log(arr.myIndexOf(<span class="hljs-string">"a"</span>));
<span class="hljs-comment">//  output: 0 </span>

<span class="hljs-comment">// Test case 2:</span>
<span class="hljs-built_in">console</span>.log(arr.myIndexOf(<span class="hljs-string">"z"</span>));
<span class="hljs-comment">// output: -1</span>
</code></pre>
<h3 id="heading-3-lastindexof">3. lastIndexOf()</h3>
<p><code>lastIndexOf()</code> method accepts an element and returns the last index at which the element is present in the array</p>
<p><strong>Test cases:</strong></p>
<ol>
<li>Element found<ul>
<li>returns the index of the last element where the given element is present</li>
</ul>
</li>
<li>Element not found<ul>
<li>returns -1</li>
</ul>
</li>
</ol>
<p><strong>Custom implementation:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-built_in">Array</span>.prototype.myLastIndexOf = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">
  element,
  fromIndex = this.length - <span class="hljs-number">1</span>
</span>) </span>{
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = fromIndex; i &gt;= <span class="hljs-number">0</span>; i--) {
    <span class="hljs-keyword">if</span> (<span class="hljs-built_in">this</span>[i] === element) {
      <span class="hljs-keyword">return</span> i;
    }
  }
  <span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>;
};
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> arr = [<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>, <span class="hljs-string">"c"</span>, <span class="hljs-string">"d"</span>, <span class="hljs-string">"e"</span>, <span class="hljs-string">"a"</span>];

<span class="hljs-comment">// Test case 1:</span>
<span class="hljs-built_in">console</span>.log(arr.myLastIndexOf(<span class="hljs-string">"a"</span>));
<span class="hljs-comment">//  output: 5 </span>

<span class="hljs-comment">// Test case 2:</span>
<span class="hljs-built_in">console</span>.log(arr.myLastIndexOf(<span class="hljs-string">"z"</span>));
<span class="hljs-comment">//  output: -1</span>
</code></pre>
<h3 id="heading-4-find">4 find()</h3>
<ul>
<li><code>find()</code> method accepts a callback testing function which is executed on every element of the array.</li>
<li>The first element in the given array that fulfills the testing function is returned by the find() method.</li>
</ul>
<p><strong>Test cases:</strong></p>
<ol>
<li>Element found<ul>
<li>returns the first element for which the callback function returns a truthy value</li>
</ul>
</li>
<li>Element not found<ul>
<li>returns undefined</li>
</ul>
</li>
</ol>
<p><strong>Custom Implementation:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-built_in">Array</span>.prototype.myFind = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">cb</span>) </span>{
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-built_in">this</span>.length; i++) {
    <span class="hljs-keyword">if</span> (cb(<span class="hljs-built_in">this</span>[i], i, <span class="hljs-built_in">this</span>)) {
      <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>[i];
    }
  }
};
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> arr = [<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">31</span>, <span class="hljs-number">44</span>, <span class="hljs-number">55</span>, <span class="hljs-number">67</span>];

<span class="hljs-keyword">const</span> result1 = arr.myFind(<span class="hljs-function">(<span class="hljs-params">element</span>) =&gt;</span> element % <span class="hljs-number">20</span> === <span class="hljs-number">0</span>);
<span class="hljs-built_in">console</span>.log(result1);
<span class="hljs-comment">// output: 20</span>

<span class="hljs-keyword">const</span> result2 = arr.myFind(<span class="hljs-function">(<span class="hljs-params">element</span>) =&gt;</span> element % <span class="hljs-number">3</span> === <span class="hljs-number">0</span>);
<span class="hljs-built_in">console</span>.log(result2);
<span class="hljs-comment">// output: undefined</span>
</code></pre>
<h3 id="heading-5-findindex">5 findIndex()</h3>
<ul>
<li><code>findIndex()</code> method accepts a testing callback function that executes on every element of the array</li>
<li>The index of the first element in the given array that fulfills the testing function is returned by the find() method.</li>
</ul>
<p><strong>Test cases:</strong></p>
<ol>
<li>Element found<ul>
<li>returns the index of the first element for which the callback function returns a truthy value</li>
</ul>
</li>
<li>Element not found<ul>
<li>returns -1, if the callback function doesn’t return a truthy value for any element in the array</li>
</ul>
</li>
</ol>
<p><strong>Custom Implementation:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-built_in">Array</span>.prototype.myFindIndex = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">cb</span>) </span>{
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-built_in">this</span>.length; i++) {
    <span class="hljs-keyword">if</span> (cb(<span class="hljs-built_in">this</span>[i], i, <span class="hljs-built_in">this</span>)) {
      <span class="hljs-keyword">return</span> i;
    }
  }
  <span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>;
};
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> arr = [<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">31</span>, <span class="hljs-number">44</span>, <span class="hljs-number">55</span>, <span class="hljs-number">67</span>];

<span class="hljs-keyword">const</span> result1 = arr.myFindIndex(<span class="hljs-function">(<span class="hljs-params">element</span>) =&gt;</span> element % <span class="hljs-number">20</span> === <span class="hljs-number">0</span>);
<span class="hljs-built_in">console</span>.log(result1);
<span class="hljs-comment">// output: 1</span>

<span class="hljs-keyword">const</span> result2 = arr.myFindIndex(<span class="hljs-function">(<span class="hljs-params">element</span>) =&gt;</span> element % <span class="hljs-number">3</span> === <span class="hljs-number">0</span>);
<span class="hljs-built_in">console</span>.log(result2);
<span class="hljs-comment">// output: -1</span>
</code></pre>
<h3 id="heading-6-findlast">6 findLast()</h3>
<ul>
<li><code>findLast()</code> method accepts a testing callback function that executes on every element of the array</li>
<li>The last element in the given array that fulfills the testing function is returned by the find() method.</li>
</ul>
<p><strong>Test cases:</strong></p>
<ol>
<li>Element found<ul>
<li>returns the last element for which the callback function returns a truthy value</li>
</ul>
</li>
<li>Element not found<ul>
<li>returns undefined</li>
</ul>
</li>
</ol>
<p><strong>Custom Implementation:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-built_in">Array</span>.prototype.myFindLast = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">cb</span>) </span>{
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-built_in">this</span>.length; i &gt;= <span class="hljs-number">0</span>; i--) {
    <span class="hljs-keyword">if</span> (cb(<span class="hljs-built_in">this</span>[i], i, <span class="hljs-built_in">this</span>)) {
      <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>[i];
    }
  }
};
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> arr = [<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">31</span>, <span class="hljs-number">44</span>, <span class="hljs-number">55</span>, <span class="hljs-number">67</span>];

<span class="hljs-keyword">const</span> result1 = arr.myFindLast(<span class="hljs-function">(<span class="hljs-params">element</span>) =&gt;</span> element % <span class="hljs-number">5</span> === <span class="hljs-number">0</span>);
<span class="hljs-built_in">console</span>.log(result1);
<span class="hljs-comment">// output: 55</span>

<span class="hljs-keyword">const</span> result2 = arr.myFindLast(<span class="hljs-function">(<span class="hljs-params">element</span>) =&gt;</span> element % <span class="hljs-number">3</span> === <span class="hljs-number">0</span>);
<span class="hljs-built_in">console</span>.log(result2);
<span class="hljs-comment">// output: undefined</span>
</code></pre>
<h3 id="heading-7-findlastindex">7 findLastIndex()</h3>
<ul>
<li><code>findLastIndex()</code> method accepts a callback function that executes on every element of the array</li>
<li>The index of the last element in the given array that fulfills the testing function is returned by the find() method.</li>
</ul>
<p><strong>Test cases:</strong></p>
<ol>
<li>Element found<ul>
<li>returns the index of the last element for which the callback function returns a truthy value</li>
</ul>
</li>
<li>Element not found<ul>
<li>returns -1, if the callback function doesn’t return a truthy value for any element in the array</li>
</ul>
</li>
</ol>
<p><strong>Custom Implementation:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-built_in">Array</span>.prototype.myFindLastIndex = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">cb</span>) </span>{
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-built_in">this</span>.length; i &gt;= <span class="hljs-number">0</span>; i--) {
    <span class="hljs-keyword">if</span> (cb(<span class="hljs-built_in">this</span>[i], i, <span class="hljs-built_in">this</span>)) {
      <span class="hljs-keyword">return</span> i;
    }
  }
  <span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>;
};
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> arr = [<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">31</span>, <span class="hljs-number">44</span>, <span class="hljs-number">55</span>, <span class="hljs-number">67</span>];

<span class="hljs-keyword">const</span> result1 = arr.myFindLastIndex(<span class="hljs-function">(<span class="hljs-params">element</span>) =&gt;</span> element % <span class="hljs-number">5</span> === <span class="hljs-number">0</span>);
<span class="hljs-built_in">console</span>.log(result1);
<span class="hljs-comment">// output: 4</span>

<span class="hljs-keyword">const</span> result2 = arr.myFindLastIndex(<span class="hljs-function">(<span class="hljs-params">element</span>) =&gt;</span> element % <span class="hljs-number">3</span> === <span class="hljs-number">0</span>);
<span class="hljs-built_in">console</span>.log(result2);
<span class="hljs-comment">// output: -1</span>
</code></pre>
<h3 id="heading-8-includes">8 includes()</h3>
<p><code>includes()</code> checks whether the given element is present in the array if it is present, the method returns true, else false.</p>
<p><strong>Test cases:</strong></p>
<ol>
<li>Element found<ul>
<li>returns true</li>
</ul>
</li>
<li>Element not found<ul>
<li>returns false</li>
</ul>
</li>
</ol>
<p><strong>Custom implementation:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-built_in">Array</span>.prototype.myIncludes = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">element, fromIndex = <span class="hljs-number">0</span></span>) </span>{
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = fromIndex; i &lt; <span class="hljs-built_in">this</span>.length; i++) {
    <span class="hljs-keyword">if</span> (<span class="hljs-built_in">this</span>[i] === element) {
      <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
    }
  }
  <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
};
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> arr = [<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>, <span class="hljs-string">"c"</span>, <span class="hljs-string">"d"</span>, <span class="hljs-string">"e"</span>, <span class="hljs-string">"a"</span>];

<span class="hljs-comment">// Test case 1:</span>
<span class="hljs-keyword">const</span> result1 = arr.myIncludes(<span class="hljs-string">"a"</span>);
<span class="hljs-built_in">console</span>.log(result1)
<span class="hljs-comment">// output: true</span>

<span class="hljs-comment">// Test case 2:</span>
<span class="hljs-keyword">const</span> result2 = arr.myIncludes(<span class="hljs-string">"z"</span>);
<span class="hljs-built_in">console</span>.log(result2)
<span class="hljs-comment">// output: false</span>
</code></pre>
<h3 id="heading-9-some">9 some()</h3>
<ul>
<li><code>some()</code> method accepts a callback function that executes on every element of the array</li>
<li>it returns true if at least one element in the array returns a truthy value for the callback function provided.</li>
</ul>
<p><strong>Test cases:</strong></p>
<ol>
<li>At least one element found<ul>
<li>returns true</li>
</ul>
</li>
<li>No element found<ul>
<li>returns false</li>
</ul>
</li>
</ol>
<p><strong>Custom Implementation:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-built_in">Array</span>.prototype.mySome = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">cb</span>) </span>{
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-built_in">this</span>.length; i++) {
    <span class="hljs-keyword">if</span> (cb(<span class="hljs-built_in">this</span>[i])) {
      <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
    }
  }
  <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
};
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-comment">// Test case 1:</span>
<span class="hljs-keyword">const</span> arr1 = [<span class="hljs-number">10</span>, <span class="hljs-number">21</span>, <span class="hljs-number">32</span>, <span class="hljs-number">43</span>, <span class="hljs-number">54</span>, <span class="hljs-number">65</span>];
<span class="hljs-keyword">const</span> hasEven = arr1.mySome(<span class="hljs-function">(<span class="hljs-params">el</span>) =&gt;</span> el % <span class="hljs-number">2</span> === <span class="hljs-number">0</span>);
<span class="hljs-built_in">console</span>.log(hasEven);
<span class="hljs-comment">// output: true </span>

<span class="hljs-comment">// Test case 2:</span>
<span class="hljs-keyword">const</span> arr2 = [<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>, <span class="hljs-number">40</span>, <span class="hljs-number">50</span>, <span class="hljs-number">60</span>];
<span class="hljs-keyword">const</span> hasOdd = arr2.mySome(<span class="hljs-function">(<span class="hljs-params">el</span>) =&gt;</span> el % <span class="hljs-number">2</span> !== <span class="hljs-number">0</span>);
<span class="hljs-built_in">console</span>.log(hasOdd);
<span class="hljs-comment">// output: false</span>
</code></pre>
<h3 id="heading-10-every">10 every()</h3>
<ul>
<li><code>every()</code> method accepts a callback function that executes on every element of the array</li>
<li>It returns true if every element in the array returns a truthy value for the callback function provided</li>
</ul>
<p><strong>Test cases:</strong></p>
<ol>
<li>Every element found<ul>
<li>returns true</li>
</ul>
</li>
<li>Some elements found<ul>
<li>returns false</li>
</ul>
</li>
<li>No element found<ul>
<li>returns false</li>
</ul>
</li>
</ol>
<p><strong>Custom Implementation:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-built_in">Array</span>.prototype.myEvery = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">cb</span>) </span>{
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-built_in">this</span>.length; i++) {
    <span class="hljs-keyword">if</span> (!cb(<span class="hljs-built_in">this</span>[i])) {
      <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
    }
  }
  <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
};
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-comment">// Test case 1</span>
<span class="hljs-keyword">const</span> arr1 = [<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>, <span class="hljs-number">40</span>, <span class="hljs-number">50</span>, <span class="hljs-number">60</span>];
<span class="hljs-keyword">const</span> isAllEven = arr1.myEvery(<span class="hljs-function">(<span class="hljs-params">el</span>) =&gt;</span> el % <span class="hljs-number">2</span> === <span class="hljs-number">0</span>)
<span class="hljs-built_in">console</span>.log(isAllEven);
<span class="hljs-comment">// output: true </span>

<span class="hljs-comment">// Test case 2</span>
<span class="hljs-keyword">const</span> arr2 = [<span class="hljs-number">10</span>, <span class="hljs-number">21</span>, <span class="hljs-number">30</span>, <span class="hljs-number">41</span>, <span class="hljs-number">50</span>, <span class="hljs-number">60</span>];
<span class="hljs-keyword">const</span> isAllEven1 = arr2.myEvery(<span class="hljs-function">(<span class="hljs-params">el</span>) =&gt;</span> el % <span class="hljs-number">2</span> === <span class="hljs-number">0</span>)
<span class="hljs-built_in">console</span>.log(isAllEven1);
<span class="hljs-comment">// output: false</span>

<span class="hljs-comment">// Test case 3</span>
<span class="hljs-keyword">const</span> arr3 = [<span class="hljs-number">11</span>, <span class="hljs-number">21</span>, <span class="hljs-number">30</span>, <span class="hljs-number">41</span>, <span class="hljs-number">51</span>, <span class="hljs-number">61</span>];
<span class="hljs-keyword">const</span> isAllEven2 = arr3.myEvery(<span class="hljs-function">(<span class="hljs-params">el</span>) =&gt;</span> el % <span class="hljs-number">2</span> === <span class="hljs-number">0</span>)
<span class="hljs-built_in">console</span>.log(isAllEven2);
<span class="hljs-comment">// output: false</span>
</code></pre>
<h2 id="heading-iterative">Iterative:</h2>
<h3 id="heading-11-foreach">11 forEach()</h3>
<ul>
<li><code>forEach()</code> accepts a callback function and executes it on every element.</li>
<li>returns undefined</li>
<li>the callback function is not invoked on uninitialized elements</li>
</ul>
<p>Test cases:</p>
<ol>
<li>Array with all elements:<ul>
<li>the callback function is executed on every element</li>
</ul>
</li>
<li>Sparse array<ul>
<li>the callback function is not executed on uninitialized elements</li>
</ul>
</li>
</ol>
<p><strong>Custom implementation:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-built_in">Array</span>.prototype.myForEach = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">cb</span>) </span>{
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-built_in">this</span>.length; i++) {
    <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> <span class="hljs-built_in">this</span>[i] !== <span class="hljs-string">"undefined"</span>) {
      cb(<span class="hljs-built_in">this</span>[i], i, <span class="hljs-built_in">this</span>);
    }
  }
};
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-comment">// Test case 1:</span>
<span class="hljs-keyword">const</span> arr = [<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>, <span class="hljs-string">"c"</span>];

arr.myForEach(<span class="hljs-function">(<span class="hljs-params">element, index</span>) =&gt;</span> <span class="hljs-built_in">console</span>.log({[index]: element}));
<span class="hljs-comment">// output:</span>
<span class="hljs-comment">// {0: 'a'}</span>
<span class="hljs-comment">// {1: 'b'}</span>
<span class="hljs-comment">// {2: 'c'}</span>

<span class="hljs-comment">// Test case 2:</span>
<span class="hljs-keyword">const</span> arr1 = [<span class="hljs-string">"a"</span>, , <span class="hljs-string">"c"</span>];

arr1.myForEach(<span class="hljs-function">(<span class="hljs-params">element, index</span>) =&gt;</span> <span class="hljs-built_in">console</span>.log({[index]: element}));
<span class="hljs-comment">// output:</span>
<span class="hljs-comment">// {0: 'a'}</span>
<span class="hljs-comment">// {2: 'c'}</span>
</code></pre>
<h2 id="heading-map-filter-reduce">map, filter, reduce</h2>
<h3 id="heading-12-map">12 map()</h3>
<ul>
<li><code>map()</code> method accepts a callback function that executes on every element of the array</li>
<li>it returns a new array</li>
<li>the new array contains the return values of the callback function that is executed on every element of the array</li>
</ul>
<p><strong>Custom implementation:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-built_in">Array</span>.prototype.myMap = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">cb</span>) </span>{
  <span class="hljs-keyword">let</span> result = [];

  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-built_in">this</span>.length; i++) {
    <span class="hljs-keyword">const</span> value = cb(<span class="hljs-built_in">this</span>[i], i, <span class="hljs-built_in">this</span>);
    result.push(value);
  }

  <span class="hljs-keyword">return</span> result;
};
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> arr = [<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-number">5</span>];

<span class="hljs-keyword">const</span> result = arr.myMap(<span class="hljs-function">(<span class="hljs-params">el</span>) =&gt;</span> el * <span class="hljs-number">2</span>);

<span class="hljs-built_in">console</span>.log(result);
<span class="hljs-comment">// output: [2, 4, 6, 8, 10];</span>
</code></pre>
<h3 id="heading-13-filter">13 filter()</h3>
<ul>
<li><code>filter()</code> method accepts a callback function that executes on every element of the array</li>
<li>it returns a new array</li>
<li>the new array contains only those elements for which the callback function returns a truthy value</li>
</ul>
<p><strong>Custom implementation:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-built_in">Array</span>.prototype.myFilter = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">cb</span>) </span>{
  <span class="hljs-keyword">let</span> result = [];

  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-built_in">this</span>.length; i++) {
    <span class="hljs-keyword">if</span> (cb(<span class="hljs-built_in">this</span>[i], i, <span class="hljs-built_in">this</span>)) {
      result.push(<span class="hljs-built_in">this</span>[i]);
    }
  }

  <span class="hljs-keyword">return</span> result;
};
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> arr = [<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-number">5</span>];

<span class="hljs-keyword">const</span> result = arr.myFilter(<span class="hljs-function">(<span class="hljs-params">el</span>) =&gt;</span> el % <span class="hljs-number">2</span> === <span class="hljs-number">0</span>);

<span class="hljs-built_in">console</span>.log(result);
<span class="hljs-comment">// output: [2, 4];</span>
</code></pre>
<h3 id="heading-14-reduce">14 reduce()</h3>
<ul>
<li><code>reduce()</code>  method accepts a callback function called the reducer.</li>
<li>The reducer is executed on every element of the array and reduces it to a single value.</li>
<li>The reducer function has four arguments: previous value, current value, the current index, and the array</li>
</ul>
<p><strong>Test cases:</strong></p>
<ol>
<li>Without passing the initial value</li>
<li>Passing initial value</li>
</ol>
<p><strong>Custom implementation:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-built_in">Array</span>.prototype.myReduce = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">cb, initialValue</span>) </span>{
  <span class="hljs-keyword">let</span> result;
  <span class="hljs-keyword">let</span> startIndex = <span class="hljs-number">0</span>;

  <span class="hljs-keyword">if</span> (<span class="hljs-built_in">arguments</span>.length &lt;= <span class="hljs-number">1</span>) {
    result = <span class="hljs-built_in">this</span>[<span class="hljs-number">0</span>];
    startIndex = <span class="hljs-number">1</span>;
  }

  <span class="hljs-keyword">if</span> (<span class="hljs-built_in">arguments</span>.length &gt;= <span class="hljs-number">2</span>) {
    result = initialValue;
  }

  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = startIndex; i &lt; <span class="hljs-built_in">this</span>.length; i++) {
    result = cb(result, <span class="hljs-built_in">this</span>[i]);
  }

  <span class="hljs-keyword">return</span> result;
};
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> arr = [<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-number">5</span>];

<span class="hljs-comment">// Test case 1:</span>
<span class="hljs-keyword">const</span> sum = arr.myReduce(<span class="hljs-function">(<span class="hljs-params">prev, curr</span>) =&gt;</span> prev + curr);
<span class="hljs-built_in">console</span>.log(sum);
<span class="hljs-comment">// output: 15</span>

<span class="hljs-comment">// Test case 2:</span>
<span class="hljs-keyword">const</span> oddEvenCount = arr.myReduce(
      <span class="hljs-function">(<span class="hljs-params">prev, curr</span>) =&gt;</span>
        curr % <span class="hljs-number">2</span> == <span class="hljs-number">0</span>
          ? { ...prev, <span class="hljs-attr">even</span>: prev.even + <span class="hljs-number">1</span> }
          : { ...prev, <span class="hljs-attr">odd</span>: prev.odd + <span class="hljs-number">1</span> },
      { <span class="hljs-attr">even</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">odd</span>: <span class="hljs-number">0</span> }
    );
<span class="hljs-built_in">console</span>.log(oddEvenCount)
<span class="hljs-comment">// output: {even: 2, odd: 3};</span>
</code></pre>
<h3 id="heading-15-reduceright">15 reduceRight()</h3>
<p><code>reduceRight()</code> is the same as the <code>reduce()</code> method, the only difference being that it executes the reducer function backward from the last element of the array.</p>
<p><strong>Test cases:</strong></p>
<ol>
<li>Without passing the initial value</li>
<li>Passing initial value</li>
</ol>
<p><strong>Custom implementation:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-built_in">Array</span>.prototype.myReduceRight = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">cb, initialValue</span>) </span>{
  <span class="hljs-keyword">let</span> result;
  <span class="hljs-keyword">let</span> startIndex = <span class="hljs-built_in">this</span>.length - <span class="hljs-number">1</span>;

  <span class="hljs-keyword">if</span> (<span class="hljs-built_in">arguments</span>.length &lt;= <span class="hljs-number">1</span>) {
    result = <span class="hljs-built_in">this</span>[<span class="hljs-built_in">this</span>.length - <span class="hljs-number">1</span>];
    startIndex = <span class="hljs-built_in">this</span>.length - <span class="hljs-number">2</span>;
  }

  <span class="hljs-keyword">if</span> (<span class="hljs-built_in">arguments</span>.length &gt;= <span class="hljs-number">2</span>) {
    result = initialValue;
  }

  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = startIndex; i &gt;= <span class="hljs-number">0</span>; i--) {
    result = cb(result, <span class="hljs-built_in">this</span>[i]);
  }

  <span class="hljs-keyword">return</span> result;
};
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> arr = [<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-number">5</span>];

<span class="hljs-comment">// Test case 1:</span>
<span class="hljs-keyword">const</span> sum = arr.myReduceRight(<span class="hljs-function">(<span class="hljs-params">prev, curr</span>) =&gt;</span> prev + curr);
<span class="hljs-built_in">console</span>.log(sum);
<span class="hljs-comment">// output: 15</span>

<span class="hljs-comment">// Test case 2:</span>
<span class="hljs-keyword">const</span> oddEvenCount = arr.myReduceRight(
      <span class="hljs-function">(<span class="hljs-params">prev, curr</span>) =&gt;</span>
        curr % <span class="hljs-number">2</span> == <span class="hljs-number">0</span>
          ? { ...prev, <span class="hljs-attr">even</span>: prev.even + <span class="hljs-number">1</span> }
          : { ...prev, <span class="hljs-attr">odd</span>: prev.odd + <span class="hljs-number">1</span> },
      { <span class="hljs-attr">even</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">odd</span>: <span class="hljs-number">0</span> }
    );
<span class="hljs-built_in">console</span>.log(oddEvenCount)
<span class="hljs-comment">// output: {even: 2, odd: 3};</span>
</code></pre>
<h2 id="heading-manipulating-methods">Manipulating methods:</h2>
<h3 id="heading-16-unshift">16 unshift()</h3>
<ul>
<li><code>unshift()</code> adds elements to the start of the array.</li>
<li>It mutates the original array.</li>
<li>It returns the new length of the array.</li>
</ul>
<p><strong>Custom implementation:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-built_in">Array</span>.prototype.myUnshift = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">if</span> (<span class="hljs-built_in">arguments</span>.length &gt; <span class="hljs-number">0</span>) {
    <span class="hljs-comment">// move elements of the array ahead</span>
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-built_in">this</span>.length - <span class="hljs-number">1</span>; i &gt;= <span class="hljs-number">0</span>; i--) {
      <span class="hljs-built_in">this</span>[i + <span class="hljs-built_in">arguments</span>.length] = <span class="hljs-built_in">this</span>[i];
    }

    <span class="hljs-comment">// add the args elements at the start</span>
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-built_in">arguments</span>.length; i++) {
      <span class="hljs-built_in">this</span>[i] = <span class="hljs-built_in">arguments</span>[i];
    }
  }

  <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.length;
};
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> array = [<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>, <span class="hljs-string">"c"</span>, <span class="hljs-string">"d"</span>, <span class="hljs-string">"e"</span>];
<span class="hljs-keyword">const</span> result = array.myUnshift(<span class="hljs-string">"z"</span>, <span class="hljs-string">"y"</span>, <span class="hljs-string">"p"</span>);

<span class="hljs-built_in">console</span>.log(result)
<span class="hljs-comment">// 8</span>

<span class="hljs-built_in">console</span>.log(array)
<span class="hljs-comment">// ["z", "y", "p", "a", "b", "c", "d", "e"];</span>
</code></pre>
<h3 id="heading-17-pop">17 pop()</h3>
<ul>
<li><code>pop()</code> removes the last element of the array.</li>
<li>It mutates the original array.</li>
<li>It returns the removed element.</li>
</ul>
<p><strong>Test cases:</strong></p>
<ol>
<li>empty array<ul>
<li>returns undefined</li>
</ul>
</li>
<li>non-empty array<ul>
<li>returns the new length of the array</li>
</ul>
</li>
</ol>
<p><strong>Custom implementation:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-built_in">Array</span>.prototype.myPop = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">if</span> (<span class="hljs-built_in">this</span>.length &gt; <span class="hljs-number">0</span>) {
    <span class="hljs-keyword">let</span> lastEl = <span class="hljs-built_in">this</span>[<span class="hljs-built_in">this</span>.length - <span class="hljs-number">1</span>];
    <span class="hljs-built_in">this</span>.length -= <span class="hljs-number">1</span>;
    <span class="hljs-keyword">return</span> lastEl;
  }
};
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-comment">// Test case 1:</span>
<span class="hljs-keyword">const</span> array = [<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>, <span class="hljs-string">"c"</span>, <span class="hljs-string">"d"</span>, <span class="hljs-string">"e"</span>];
<span class="hljs-keyword">const</span> result = array.myPop();
<span class="hljs-built_in">console</span>.log(result); <span class="hljs-comment">// "e"</span>
<span class="hljs-built_in">console</span>.log(array); <span class="hljs-comment">// ["a", "b", "c", "d"];</span>

<span class="hljs-comment">// Test case 2:</span>
<span class="hljs-keyword">const</span> emptyArray = [];
<span class="hljs-keyword">const</span> result1 = emptyArray.myPop();
<span class="hljs-built_in">console</span>.log(result1); <span class="hljs-comment">// undefined</span>
<span class="hljs-built_in">console</span>.log(emptyArray); <span class="hljs-comment">// [];</span>
</code></pre>
<h3 id="heading-18-push">18 push()</h3>
<ul>
<li><code>push()</code> adds elements to the end of the array.</li>
<li>It mutates the original array.</li>
<li>It returns the new length of the array.</li>
</ul>
<p><strong>Custom implementation:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-built_in">Array</span>.prototype.myPush = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-built_in">arguments</span>.length; i++) {
    <span class="hljs-built_in">this</span>[<span class="hljs-built_in">this</span>.length] = <span class="hljs-built_in">arguments</span>[i];
  }
  <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.length;
};
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> array = [<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>, <span class="hljs-string">"c"</span>, <span class="hljs-string">"d"</span>, <span class="hljs-string">"e"</span>];
<span class="hljs-keyword">const</span> result = array.myPush(<span class="hljs-string">"z"</span>, <span class="hljs-string">"y"</span>, <span class="hljs-string">"p"</span>);

<span class="hljs-built_in">console</span>.log(result)
<span class="hljs-comment">// 8</span>

<span class="hljs-built_in">console</span>.log(array)
<span class="hljs-comment">// ["a", "b", "c", "d", "e", "z", "y", "p"];</span>
</code></pre>
<h3 id="heading-19-shift">19 shift()</h3>
<ul>
<li><code>shift()</code> removes the first element of the array.</li>
<li>It mutates the original array.</li>
<li>It returns the removed element.</li>
</ul>
<p><strong>Test cases:</strong></p>
<ol>
<li>empty array<ul>
<li>returns undefined</li>
</ul>
</li>
<li>non-empty array<ul>
<li>returns the new length of the array</li>
</ul>
</li>
</ol>
<p><strong>Custom implementation:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-built_in">Array</span>.prototype.myShift = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">if</span> (<span class="hljs-built_in">this</span>.length &gt; <span class="hljs-number">0</span>) {
    <span class="hljs-keyword">const</span> firstEl = <span class="hljs-built_in">this</span>[<span class="hljs-number">0</span>];

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-built_in">this</span>.length; i++) {
      <span class="hljs-built_in">this</span>[i] = <span class="hljs-built_in">this</span>[i + <span class="hljs-number">1</span>];
    }

    <span class="hljs-built_in">this</span>.length -= <span class="hljs-number">1</span>;

    <span class="hljs-keyword">return</span> firstEl;
  }
};
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-comment">// Test case 1:</span>
<span class="hljs-keyword">const</span> array = [<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>, <span class="hljs-string">"c"</span>, <span class="hljs-string">"d"</span>, <span class="hljs-string">"e"</span>];
<span class="hljs-keyword">const</span> result = array.myShift();
<span class="hljs-built_in">console</span>.log(result); <span class="hljs-comment">// "a"</span>
<span class="hljs-built_in">console</span>.log(array); <span class="hljs-comment">// ["b", "c", "d", "e"];</span>

<span class="hljs-comment">// Test case 2:</span>
<span class="hljs-keyword">const</span> emptyArray = [];
<span class="hljs-keyword">const</span> result1 = emptyArray.myShift();
<span class="hljs-built_in">console</span>.log(result1); <span class="hljs-comment">// undefined</span>
<span class="hljs-built_in">console</span>.log(emptyArray); <span class="hljs-comment">// [];</span>
</code></pre>
<h3 id="heading-20-fill">20 fill()</h3>
<ul>
<li><code>fill()</code> fills or replaces elements in an array with a given value.</li>
<li>It accepts three arguments: <code>value</code>, start, and, <code>end</code>.</li>
<li>It fills the elements between start and end with the given value.</li>
</ul>
<p><strong>Test cases:</strong></p>
<ol>
<li>No <code>start</code> and <code>end</code> provided<ul>
<li>fills all the elements with the value given.</li>
</ul>
</li>
<li><code>start</code> provided, <code>end</code> not provided<ul>
<li>fills all the elements starting from the start index with the value given.</li>
</ul>
</li>
<li><code>start</code> and <code>end</code> both provided<ul>
<li>fills all the elements between start and end with the value given. (element at the end index is not included)</li>
</ul>
</li>
<li>Negative indexes:<ul>
<li>counts the index backward from the last element of the array</li>
</ul>
</li>
</ol>
<p><strong>Custom implementation:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-built_in">Array</span>.prototype.myFill = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">value, start = <span class="hljs-number">0</span>, end = this.length</span>) </span>{
  <span class="hljs-keyword">if</span> (start &lt; <span class="hljs-number">0</span>) {
    start = <span class="hljs-built_in">this</span>.length + start;
  }

  <span class="hljs-keyword">if</span> (end &lt; <span class="hljs-number">0</span>) {
    end = <span class="hljs-built_in">this</span>.length + end;
  }

  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = start; i &lt; end; i++) {
    <span class="hljs-built_in">this</span>[i] = value;
  }

  <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>;
};
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-comment">// Test case 1:</span>
<span class="hljs-keyword">const</span> arr1 = [<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>, <span class="hljs-string">"c"</span>, <span class="hljs-string">"d"</span>, <span class="hljs-string">"e"</span>];
<span class="hljs-keyword">const</span> result1 = arr1.myFill(<span class="hljs-string">"*"</span>);
<span class="hljs-built_in">console</span>.log(result1)
<span class="hljs-comment">// ["*", "*", "*", "*", "*"];</span>

<span class="hljs-comment">// Test case 2:</span>
<span class="hljs-keyword">const</span> arr2 = [<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>, <span class="hljs-string">"c"</span>, <span class="hljs-string">"d"</span>, <span class="hljs-string">"e"</span>];
<span class="hljs-keyword">const</span> result2 = arr2.myFill(<span class="hljs-string">"*"</span>, <span class="hljs-number">1</span>);
<span class="hljs-built_in">console</span>.log(result2)
<span class="hljs-comment">// ["a", "*", "*", "*", "*"];</span>

<span class="hljs-comment">// Test case 3:</span>
<span class="hljs-keyword">const</span> arr3 = [<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>, <span class="hljs-string">"c"</span>, <span class="hljs-string">"d"</span>, <span class="hljs-string">"e"</span>];
<span class="hljs-keyword">const</span> result3 = arr3.myFill(<span class="hljs-string">"*"</span>, <span class="hljs-number">1</span>, <span class="hljs-number">3</span>);
<span class="hljs-built_in">console</span>.log(result3)
<span class="hljs-comment">// ["a", "*", "*", "d", "e"];</span>

<span class="hljs-comment">// Test case 4:</span>
<span class="hljs-keyword">const</span> arr4 = [<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>, <span class="hljs-string">"c"</span>, <span class="hljs-string">"d"</span>, <span class="hljs-string">"e"</span>];
<span class="hljs-keyword">const</span> result4 = arr4.myFill(<span class="hljs-string">"*"</span>, <span class="hljs-number">-4</span>, <span class="hljs-number">-1</span>);
<span class="hljs-built_in">console</span>.log(result4)
<span class="hljs-comment">// ["a", "*", "*", "*", "e"];</span>
</code></pre>
<h3 id="heading-21-reverse">21 reverse()</h3>
<p><code>reverse()</code> method reverses the array. It mutates the original array and returns the reversed array</p>
<p><strong>Custom implementation:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-built_in">Array</span>.prototype.myReverse = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">let</span> start = <span class="hljs-number">0</span>,
    end = <span class="hljs-built_in">this</span>.length - <span class="hljs-number">1</span>;

  <span class="hljs-keyword">while</span> (start &lt; end) {
    <span class="hljs-keyword">let</span> temp = <span class="hljs-built_in">this</span>[start];
    <span class="hljs-built_in">this</span>[start] = <span class="hljs-built_in">this</span>[end];
    <span class="hljs-built_in">this</span>[end] = temp;
    start++;
    end--;
  }

  <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>;
};
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> arr = [<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-number">5</span>];
<span class="hljs-keyword">const</span> result = arr.myReverse();

<span class="hljs-built_in">console</span>.log(arr); <span class="hljs-comment">// [5, 4, 3, 2, 1]</span>
<span class="hljs-built_in">console</span>.log(result); <span class="hljs-comment">// [5, 4, 3, 2, 1]</span>
</code></pre>
<h3 id="heading-22-sort">22 sort()</h3>
<ul>
<li><code>sort()</code> method sorts the array.</li>
<li>By default, it converts the elements to strings and sorts them in ascending order.</li>
<li>We can provide a comparator function to sort the elements in a custom order.</li>
</ul>
<p><strong>Test cases:</strong></p>
<ol>
<li>comparator function not provided<ul>
<li>It converts the elements to strings and sorts them in ascending order.</li>
</ul>
</li>
<li>comparator function provided<ul>
<li>It sorts the elements based on the comparator function</li>
</ul>
</li>
</ol>
<p><strong>Custom implementation:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">defaultComparator</span>(<span class="hljs-params">a, b</span>) </span>{
  a = a.toString();
  b = b.toString();
  <span class="hljs-keyword">if</span> (a &lt; b) {
    <span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>;
  }
  <span class="hljs-keyword">if</span> (a &gt; b) {
    <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
  }
  <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}

<span class="hljs-built_in">Array</span>.prototype.mySort = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">cb = defaultComparator</span>) </span>{
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-built_in">this</span>.length; i++) {
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> j = i + <span class="hljs-number">1</span>; j &lt; <span class="hljs-built_in">this</span>.length; j++) {
      <span class="hljs-keyword">if</span> (cb(<span class="hljs-built_in">this</span>[i], <span class="hljs-built_in">this</span>[j]) &gt; <span class="hljs-number">0</span>) {
        <span class="hljs-keyword">var</span> swap = <span class="hljs-built_in">this</span>[i];
        <span class="hljs-built_in">this</span>[i] = <span class="hljs-built_in">this</span>[j];
        <span class="hljs-built_in">this</span>[j] = swap;
      }
    }
  }
};
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-comment">// Test case 1:</span>
<span class="hljs-keyword">const</span> arr = [<span class="hljs-number">30</span>, <span class="hljs-number">55</span>, <span class="hljs-number">1</span>, <span class="hljs-number">3</span>, <span class="hljs-number">66</span>, <span class="hljs-number">22</span>];
arr.mySort();
<span class="hljs-built_in">console</span>.log(arr);
<span class="hljs-comment">// output: [1, 22, 3, 30, 55, 66];</span>

<span class="hljs-comment">// Test case 1:</span>
<span class="hljs-keyword">const</span> arr1 = [<span class="hljs-number">30</span>, <span class="hljs-number">55</span>, <span class="hljs-number">1</span>, <span class="hljs-number">3</span>, <span class="hljs-number">66</span>, <span class="hljs-number">22</span>];
arr1.mySort(<span class="hljs-function">(<span class="hljs-params">a, b</span>) =&gt;</span> a - b);
<span class="hljs-built_in">console</span>.log(arr1);
<span class="hljs-comment">// output: [1, 3, 22, 30, 55, 66];</span>
</code></pre>
<h3 id="heading-23-copywithin">23 copyWithin()</h3>
<p><code>copyWithin()</code> copies a portion of the array to another location within the same array. It accepts three arguments: target, start, and end.</p>
<p><strong>Test cases:</strong></p>
<ol>
<li><code>target</code> provided, <code>start</code> and <code>end</code> not provided<ul>
<li>It will copy the element at the 0th index to the target index</li>
</ul>
</li>
<li><code>target</code> and <code>start</code> provided, <code>end</code> not provided<ul>
<li>It will copy the element present at the start index to the target index</li>
</ul>
</li>
<li><code>target</code>, <code>start</code>, and, <code>end</code> provided<ul>
<li>It will copy the elements lying between the start and end to the target index. (element present at the end index will not be included)</li>
</ul>
</li>
</ol>
<p><strong>Custom implementation:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-built_in">Array</span>.prototype.myCopyWithin = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">
  target = <span class="hljs-number">0</span>,
  start = <span class="hljs-number">0</span>,
  end = this.length
</span>) </span>{
  <span class="hljs-keyword">if</span> (target &lt; <span class="hljs-number">0</span>) {
    target = <span class="hljs-built_in">this</span>.length + target;
  }

  <span class="hljs-keyword">if</span> (start &lt; <span class="hljs-number">0</span>) {
    start = <span class="hljs-built_in">this</span>.length + start;
  }

  <span class="hljs-keyword">if</span> (end &lt; <span class="hljs-number">0</span>) {
    end = <span class="hljs-built_in">this</span>.length + end;
  }

  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = start; i &lt; end &amp;&amp; target &lt; <span class="hljs-built_in">this</span>.length; i++) {
    <span class="hljs-built_in">this</span>[target] = <span class="hljs-built_in">this</span>[i];
    target++;
  }

  <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>;
};
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-comment">// Test Case 1:</span>
<span class="hljs-keyword">const</span> array1 = [<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>, <span class="hljs-string">'c'</span>, <span class="hljs-string">'d'</span>, <span class="hljs-string">'e'</span>];
array1.myCopyWithin(<span class="hljs-number">4</span>); 
<span class="hljs-built_in">console</span>.log(array1)
<span class="hljs-comment">// output: ['a', 'b', 'c', 'd', 'a']</span>

<span class="hljs-keyword">const</span> array2 = [<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>, <span class="hljs-string">'c'</span>, <span class="hljs-string">'d'</span>, <span class="hljs-string">'e'</span>];
array2.myCopyWithin(<span class="hljs-number">3</span>, <span class="hljs-number">1</span>); 
<span class="hljs-built_in">console</span>.log(array2)
<span class="hljs-comment">// output: ['a', 'b', 'c', 'b', 'c']</span>

<span class="hljs-keyword">const</span> array3 = [<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>, <span class="hljs-string">'c'</span>, <span class="hljs-string">'d'</span>, <span class="hljs-string">'e'</span>, <span class="hljs-string">'f'</span>];
array3.myCopyWithin(<span class="hljs-number">0</span>, <span class="hljs-number">3</span>, <span class="hljs-number">5</span>); 
<span class="hljs-built_in">console</span>.log(array3)
<span class="hljs-comment">// output: ['d', 'e', 'c', 'd', 'e', 'f']</span>
</code></pre>
<h2 id="heading-returning-iterator-methods">Returning iterator methods</h2>
<h3 id="heading-24-keys">24 keys()</h3>
<p><code>keys()</code> creates and returns an Array Iterator object with the key for each item in the array. </p>
<p><strong>Custom implementation:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-built_in">Array</span>.prototype.myKeys = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">let</span> keys = [];
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-built_in">this</span>.length; i++) {
    keys.push(i);
  }

  <span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">iterator</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">yield</span>* keys;
  }

  <span class="hljs-keyword">return</span> iterator();
};
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> arr = [<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>, <span class="hljs-string">"c"</span>, <span class="hljs-string">"d"</span>, <span class="hljs-string">"e"</span>];

<span class="hljs-keyword">const</span> keys = arr.myKeys();

<span class="hljs-built_in">console</span>.log(keys.next().value);
<span class="hljs-comment">//output: 0</span>

<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> key <span class="hljs-keyword">of</span> keys) {
        <span class="hljs-built_in">console</span>.log(key);
}

<span class="hljs-comment">/**
output:
1
2
3
4
**/</span>
</code></pre>
<h3 id="heading-25-values">25 values()</h3>
<p><code>values()</code> creates and returns an Array Iterator object with the value for each item in the array. </p>
<p><strong>Custom implementation:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-built_in">Array</span>.prototype.myValues = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">let</span> keys = [];
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-built_in">this</span>.length; i++) {
    keys.push(<span class="hljs-built_in">this</span>[i]);
  }

  <span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">iterator</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">yield</span>* keys;
  }

  <span class="hljs-keyword">return</span> iterator();
};
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> arr = [<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>, <span class="hljs-string">"c"</span>, <span class="hljs-string">"d"</span>, <span class="hljs-string">"e"</span>];

<span class="hljs-keyword">const</span> values = arr.myValues();

<span class="hljs-built_in">console</span>.log(values.next().value);
<span class="hljs-comment">//output: a</span>

<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> value <span class="hljs-keyword">of</span> values) {
        <span class="hljs-built_in">console</span>.log(value);
}

<span class="hljs-comment">/**
output:
a
b
c
d
e
**/</span>
</code></pre>
<h3 id="heading-26-entries">26 entries()</h3>
<p><code>entries()</code> creates and returns an Array Iterator object with key-value pairs for each item in the array. </p>
<p><strong>Custom implementation:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-built_in">Array</span>.prototype.myEntries = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">let</span> keys = [];
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-built_in">this</span>.length; i++) {
    keys.push([i, <span class="hljs-built_in">this</span>[i]]);
  }

  <span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">iterator</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">yield</span>* keys;
  }

  <span class="hljs-keyword">return</span> iterator();
};
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> arr = [<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>, <span class="hljs-string">"c"</span>, <span class="hljs-string">"d"</span>, <span class="hljs-string">"e"</span>];

<span class="hljs-keyword">const</span> entries = arr.myEntries();

<span class="hljs-built_in">console</span>.log(entries.next().value);
<span class="hljs-comment">//output: [0, 'a']</span>

<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> entry <span class="hljs-keyword">of</span> entries) {
        <span class="hljs-built_in">console</span>.log(entry);
}

<span class="hljs-comment">/**
output:
[1, 'b']
[2, 'c']
[3, 'd']
[4, 'e']
**/</span>
</code></pre>
<h2 id="heading-other-methods">Other methods:</h2>
<h3 id="heading-27-slice">27 slice()</h3>
<ul>
<li><code>slice()</code> is used to slice the array and get a portion of the array.</li>
<li>It accepts the start and end index as arguments and returns the portion of the array lying between them.</li>
</ul>
<p><strong>Test cases:</strong></p>
<ol>
<li>Positive <code>start</code> index<ul>
<li>Returns the array elements starting from the start index</li>
</ul>
</li>
<li>Positive <code>start</code> and <code>end</code> index<ul>
<li>Returns the portion array lying between the start and end index. (the element lying on the end index is not included)</li>
</ul>
</li>
<li>Negative <code>start</code> index<ul>
<li>The index is counted backward from the end.</li>
</ul>
</li>
<li>Negative <code>start</code> and <code>end</code> index<ul>
<li>The index is counted backward from the end. Returns the portion array lying between the start and end index. (the element lying on the end index is not included)</li>
</ul>
</li>
</ol>
<p><strong>Custom implementation:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-built_in">Array</span>.prototype.mySlice = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">start = <span class="hljs-number">0</span>, end = this.length</span>) </span>{
  <span class="hljs-keyword">if</span> (start &lt; <span class="hljs-number">0</span>) {
    start = <span class="hljs-built_in">this</span>.length + start;
  }

  <span class="hljs-keyword">if</span> (end &lt; <span class="hljs-number">0</span>) {
    end = <span class="hljs-built_in">this</span>.length + end;
  }

  <span class="hljs-keyword">let</span> arr = [];
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = start; i &lt; end; i++) {
    arr.push(<span class="hljs-built_in">this</span>[i]);
  }

  <span class="hljs-keyword">return</span> arr;
};
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> arr = 
[<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>, <span class="hljs-string">"c"</span>, <span class="hljs-string">"d"</span>, <span class="hljs-string">"e"</span>];

<span class="hljs-comment">// Test case 1:</span>
<span class="hljs-keyword">const</span> result1 = arr.mySlice(<span class="hljs-number">2</span>);
<span class="hljs-built_in">console</span>.log(result1);
<span class="hljs-comment">// output: ["c", "d", "e"]</span>

<span class="hljs-comment">// Test case 2:</span>
<span class="hljs-keyword">const</span> result2 = arr.mySlice(<span class="hljs-number">2</span>, <span class="hljs-number">4</span>);
<span class="hljs-built_in">console</span>.log(result2);
<span class="hljs-comment">// output: ["c", "d"]</span>

<span class="hljs-comment">// Test case 3:</span>
<span class="hljs-keyword">const</span> result3 = arr.mySlice(<span class="hljs-number">-1</span>);
<span class="hljs-built_in">console</span>.log(result3);
<span class="hljs-comment">// output: ["e"]</span>

<span class="hljs-comment">// Test case 4:</span>
<span class="hljs-keyword">const</span> result4 = arr.mySlice(<span class="hljs-number">-5</span>, <span class="hljs-number">-2</span>);
<span class="hljs-built_in">console</span>.log(result4);
<span class="hljs-comment">// output: ["a", "b", "c"]</span>
</code></pre>
<h3 id="heading-28-concat">28 concat()</h3>
<p><code>concat()</code> method merges two or more arrays or values. </p>
<p><strong>Test cases:</strong></p>
<ol>
<li>merging two arrays<ol>
<li>returns the merger array</li>
</ol>
</li>
<li>merging a variable with an array<ol>
<li>merges the variable to the array and returns the merged array</li>
</ol>
</li>
<li>merging three arrays<ol>
<li>merges the three arrays in the order given and returns the merged array</li>
</ol>
</li>
</ol>
<p><strong>Custom implementation:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-built_in">Array</span>.prototype.myConcat = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">let</span> newArr = [];

  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-built_in">this</span>.length; i++) {
    newArr.push(<span class="hljs-built_in">this</span>[i]);
  }

  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-built_in">arguments</span>.length; i++) {
    <span class="hljs-keyword">if</span> (<span class="hljs-built_in">Array</span>.isArray(<span class="hljs-built_in">arguments</span>[i])) {
      <span class="hljs-keyword">const</span> dummyArr = <span class="hljs-built_in">arguments</span>[i];
      <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; dummyArr.length; i++) {
        newArr.push(dummyArr[i]);
      }
    } <span class="hljs-keyword">else</span> {
      newArr.push(<span class="hljs-built_in">arguments</span>[i]);
    }
  }

  <span class="hljs-keyword">return</span> newArr;
};
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> arr1 = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>];
<span class="hljs-keyword">const</span> arr2 = [<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>];
<span class="hljs-keyword">const</span> arr3 = [<span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>];

<span class="hljs-comment">// Test case 1:</span>
<span class="hljs-keyword">const</span> result1 = arr1.myConcat(arr2);
<span class="hljs-built_in">console</span>.log(result1);
<span class="hljs-comment">// output: [1, 2, 3, 4, 5, 6]</span>

<span class="hljs-comment">// Test case 2:</span>
<span class="hljs-keyword">const</span> result2 = arr1.myConcat(<span class="hljs-string">"a"</span>);
<span class="hljs-built_in">console</span>.log(result2);
<span class="hljs-comment">// output: [1, 2, 3, "a"]</span>

<span class="hljs-comment">// Test case 3:</span>
<span class="hljs-keyword">const</span> result3 = arr1.myConcat(arr2, arr3);
<span class="hljs-built_in">console</span>.log(result3);
<span class="hljs-comment">// output: [1, 2, 3, 4, 5, 6, 7, 8, 9]</span>
</code></pre>
<h3 id="heading-29-flat">29 flat()</h3>
<p><code>flat()</code> methods flatten an array up to a given depth. Flattens here means it concatenates the sub-array elements into the original array. It returns the flattened array.</p>
<p><strong>Test cases:</strong></p>
<ol>
<li>no <code>depth</code> specified<ul>
<li>flattens till depth 1</li>
</ul>
</li>
<li><code>depth</code> argument passed<ul>
<li>flattens till the given depth</li>
</ul>
</li>
</ol>
<p><strong>Custom implementation:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-built_in">Array</span>.prototype.myFlat = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">depth = <span class="hljs-number">1</span></span>) </span>{
  <span class="hljs-keyword">const</span> result = [];

  (<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">flatten</span>(<span class="hljs-params">arr, depth</span>) </span>{
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; arr.length; i++) {
      <span class="hljs-keyword">if</span> (<span class="hljs-built_in">Array</span>.isArray(arr[i]) &amp;&amp; depth &gt; <span class="hljs-number">0</span>) {
        flatten(arr[i], depth - <span class="hljs-number">1</span>);
      } <span class="hljs-keyword">else</span> {
        result.push(arr[i]);
      }
    }
  })(<span class="hljs-built_in">this</span>, depth);

  <span class="hljs-keyword">return</span> result;
};

<span class="hljs-built_in">Array</span>.prototype.myDeepFlat = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">let</span> result = [];

  (<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">flatten</span>(<span class="hljs-params">arr</span>) </span>{
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; arr.length; i++) {
      <span class="hljs-keyword">if</span> (<span class="hljs-built_in">Array</span>.isArray(arr[i])) {
        flatten(arr[i]);
      } <span class="hljs-keyword">else</span> {
        result.push(arr[i]);
      }
    }
  })(<span class="hljs-built_in">this</span>);

  <span class="hljs-keyword">return</span> result;
};
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> arr = [<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-number">5</span>];

<span class="hljs-comment">// Test Case 1:</span>
<span class="hljs-keyword">const</span> arr1 = [<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, [<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>]];
<span class="hljs-keyword">const</span> result1 = arr1.myFlat();
<span class="hljs-built_in">console</span>.log(result1);
<span class="hljs-comment">// output: [10, 20, "a", "b"];</span>

<span class="hljs-comment">// Test Case 2:</span>
<span class="hljs-keyword">const</span> arr2 = [<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, [[<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>]]];;
<span class="hljs-keyword">const</span> result2 = arr2.myFlat(<span class="hljs-number">2</span>);
<span class="hljs-built_in">console</span>.log(result2);
<span class="hljs-comment">// output: [10, 20, "a", "b"];</span>
</code></pre>
<h3 id="heading-30-flatmap">30 flatMap()</h3>
<p><code>flatMap()</code> accepts a callback function. It executes the callback function on every element of the array, flattens the result to depth 1, and returns the newly formed array </p>
<p><strong>Custom implementation:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-built_in">Array</span>.prototype.myFlatMap = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">cb</span>) </span>{
  <span class="hljs-keyword">let</span> result = [];

  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-built_in">this</span>.length; i++) {
    <span class="hljs-keyword">let</span> cbRes = cb(<span class="hljs-built_in">this</span>[i], i, <span class="hljs-built_in">this</span>);

    <span class="hljs-keyword">if</span> (<span class="hljs-built_in">Array</span>.isArray(cbRes)) {
      <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; cbRes.length; i++) {
        result.push(cbRes[i]);
      }
    } <span class="hljs-keyword">else</span> {
      result.push(cbRes);
    }
  }

  <span class="hljs-keyword">return</span> result;
};
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> arr = [<span class="hljs-number">10</span>, <span class="hljs-number">21</span>, <span class="hljs-number">30</span>, <span class="hljs-number">42</span>, <span class="hljs-number">50</span>];

<span class="hljs-comment">// Test case 1: </span>
<span class="hljs-keyword">const</span> result1 = arr.myFlatMap(<span class="hljs-function">(<span class="hljs-params">el</span>) =&gt;</span> (el % <span class="hljs-number">10</span> === <span class="hljs-number">0</span> ? [] : [el]));
<span class="hljs-built_in">console</span>.log(result1);
<span class="hljs-comment">// output: [21, 42];</span>

<span class="hljs-comment">// Test case 2: </span>
<span class="hljs-keyword">const</span> result2 = arr.myFlatMap(<span class="hljs-function">(<span class="hljs-params">el</span>) =&gt;</span> el);
<span class="hljs-built_in">console</span>.log(result2);
<span class="hljs-comment">// output: [10, 21, 30, 42, 50];</span>
</code></pre>
<h3 id="heading-31-join">31 join()</h3>
<p><code>join()</code> method concatenates the array and returns a string.</p>
<p><strong>Test cases:</strong></p>
<ol>
<li>no <code>separator</code> argument passed:<ul>
<li>concatenates element using <code>,</code> a comma as a separator</li>
</ul>
</li>
<li><code>separator</code> passed<ul>
<li>concatenates elements using the separator provided</li>
</ul>
</li>
</ol>
<p><strong>Custom implementation:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-built_in">Array</span>.prototype.myJoin = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">separator = <span class="hljs-string">","</span></span>) </span>{
  <span class="hljs-keyword">let</span> result = <span class="hljs-string">""</span>;

  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-built_in">this</span>.length; i++) {
    <span class="hljs-keyword">if</span> (i === <span class="hljs-built_in">this</span>.length - <span class="hljs-number">1</span>) {
      result += <span class="hljs-built_in">this</span>[i];
    } <span class="hljs-keyword">else</span> {
      result += <span class="hljs-built_in">this</span>[i] + separator;
    }
  }

  <span class="hljs-keyword">return</span> result;
};
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> arr = [<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-number">5</span>];

<span class="hljs-comment">// Test Case 1:</span>
<span class="hljs-keyword">const</span> commaSeperatedString = arr.myJoin();
<span class="hljs-built_in">console</span>.log(commaSeperatedString);
<span class="hljs-comment">// output: 1,2,3,4,5</span>

<span class="hljs-comment">// Test Case 2:</span>
<span class="hljs-keyword">const</span> result = arr.myJoin(<span class="hljs-string">"*"</span>);
<span class="hljs-built_in">console</span>.log(result);
<span class="hljs-comment">// output: 1*2*3*4*5</span>
</code></pre>
<h3 id="heading-32-splice">32 splice()</h3>
<ul>
<li>The <code>splice()</code> method can:<ul>
<li>remove elements from the array</li>
<li>replace elements in the array</li>
<li>add elements in the array</li>
</ul>
</li>
<li>It mutates the original array.</li>
<li>It accepts the following arguments:<ul>
<li><code>start</code> - index from which to mutate the array</li>
<li><code>deleteCount</code> - the number of elements to delete, 0 or negative if no elements to delete</li>
<li>item1, item2, … itemn - items to add to the array</li>
</ul>
</li>
</ul>
<p><strong>Test cases:</strong></p>
<ol>
<li><code>start</code> is given; <code>deleteCount</code> and items not given<ul>
<li>deletes all items from the <code>start</code> index</li>
</ul>
</li>
<li><code>negative</code> start is given; <code>deleteCount</code> and items not given<ul>
<li><code>start</code> is counted backward from the end of the array and all items starting from it are deleted</li>
</ul>
</li>
<li><code>start</code> is given and <code>deleteCount</code> is given; <code>deleteCount</code> is greater than 0; items not given<ul>
<li>it deletes <code>deleteCount</code> number of elements starting from the <code>start</code> index in the array</li>
</ul>
</li>
<li>start, <code>deleteCount</code>, items are given; deleteCount is 0<ul>
<li>the items are added to the array from the <code>start</code> index</li>
</ul>
</li>
<li><code>start</code>, <code>deleteCount</code>, <code>items</code> are given; <code>deleteCount</code> is greater than 0<ul>
<li>it deletes <code>deleteCount</code> number of elements starting from the <code>start</code> index in the array</li>
<li>it then adds the items in the array from the start index</li>
</ul>
</li>
</ol>
<p><strong>Custom implementation:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-built_in">Array</span>.prototype.mySplice = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">let</span> returnResult = [];
  <span class="hljs-keyword">let</span> start, deleteCount;
  <span class="hljs-keyword">let</span> items = [];

  <span class="hljs-keyword">if</span> (<span class="hljs-built_in">arguments</span>.length === <span class="hljs-number">0</span>) {
    <span class="hljs-keyword">return</span> returnResult;
  }

  <span class="hljs-comment">// start</span>
  start = <span class="hljs-built_in">arguments</span>[<span class="hljs-number">0</span>];
  <span class="hljs-keyword">if</span> (start &gt;= <span class="hljs-built_in">this</span>.length) {
    <span class="hljs-keyword">return</span> returnResult;
  }
  <span class="hljs-keyword">if</span> (start &lt; <span class="hljs-number">0</span>) {
    start = <span class="hljs-built_in">this</span>.length + start;
  }

  <span class="hljs-comment">// deleteCount</span>
  <span class="hljs-keyword">if</span> (<span class="hljs-built_in">arguments</span>.length === <span class="hljs-number">1</span>) {
    deleteCount = <span class="hljs-built_in">this</span>.length - start;
  }
  <span class="hljs-keyword">if</span> (<span class="hljs-built_in">arguments</span>.length &gt;= <span class="hljs-number">2</span>) {
    deleteCount = <span class="hljs-built_in">arguments</span>[<span class="hljs-number">1</span>];

    <span class="hljs-keyword">if</span> (deleteCount &gt; <span class="hljs-built_in">this</span>.length - start) {
      deleteCount = <span class="hljs-built_in">this</span>.length - start;
    }
  }

  <span class="hljs-comment">// items</span>
  <span class="hljs-keyword">if</span> (<span class="hljs-built_in">arguments</span>.length &gt; <span class="hljs-number">2</span>) {
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">2</span>; i &lt; <span class="hljs-built_in">arguments</span>.length; i++) {
      items.push(<span class="hljs-built_in">arguments</span>[i]);
    }
  }

  <span class="hljs-comment">// delete elements if delete count &gt; 0</span>
  <span class="hljs-keyword">if</span> (deleteCount &gt; <span class="hljs-number">0</span>) {
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; deleteCount; i++) {
      returnResult.push(<span class="hljs-built_in">this</span>[start + i]);
    }

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = start, j = <span class="hljs-number">0</span>; i &lt;= <span class="hljs-built_in">this</span>.length - deleteCount; i++, j++) {
      <span class="hljs-built_in">this</span>[start + j] = <span class="hljs-built_in">this</span>[start + j + deleteCount];
    }

    <span class="hljs-built_in">this</span>.length = <span class="hljs-built_in">this</span>.length - deleteCount;
  }

  <span class="hljs-comment">// add elements if items are provided</span>
  <span class="hljs-keyword">if</span> (items.length &gt; <span class="hljs-number">0</span>) {
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-built_in">this</span>.length - <span class="hljs-number">1</span>; i &gt;= start; i--) {
      <span class="hljs-built_in">this</span>[i + items.length] = <span class="hljs-built_in">this</span>[i];
    }

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; items.length; i++) {
      <span class="hljs-built_in">this</span>[start + i] = items[i];
    }
  }
  <span class="hljs-keyword">return</span> returnResult;
};
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-jsx"><span class="hljs-comment">// Test case 1:</span>
<span class="hljs-keyword">const</span> arr1 = [<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>, <span class="hljs-string">"c"</span>, <span class="hljs-string">"d"</span>, <span class="hljs-string">"e"</span>];
<span class="hljs-keyword">const</span> result1 = arr1.mySplice(<span class="hljs-number">2</span>); 
<span class="hljs-built_in">console</span>.log(arr1); <span class="hljs-comment">// ['a', 'b']</span>
<span class="hljs-built_in">console</span>.log(result1); <span class="hljs-comment">// ['c', 'd', 'e']</span>

<span class="hljs-comment">// Test case 2:</span>
<span class="hljs-keyword">const</span> arr2 = [<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>, <span class="hljs-string">"c"</span>, <span class="hljs-string">"d"</span>, <span class="hljs-string">"e"</span>];
<span class="hljs-keyword">const</span> result2 = arr2.mySplice(<span class="hljs-number">-2</span>);
<span class="hljs-built_in">console</span>.log(arr2); <span class="hljs-comment">// ['a', 'b', 'c'] </span>
<span class="hljs-built_in">console</span>.log(result2); <span class="hljs-comment">//  ['d', 'e']</span>

<span class="hljs-comment">// Test case 3:</span>
<span class="hljs-keyword">const</span> arr3 = [<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>, <span class="hljs-string">"c"</span>, <span class="hljs-string">"d"</span>, <span class="hljs-string">"e"</span>];
<span class="hljs-keyword">const</span> result3 = arr3.mySplice(<span class="hljs-number">2</span>, <span class="hljs-number">1</span>);
<span class="hljs-built_in">console</span>.log(arr3); <span class="hljs-comment">// ['a', 'b', 'd', 'e']</span>
<span class="hljs-built_in">console</span>.log(result3); <span class="hljs-comment">// ['c']</span>

<span class="hljs-comment">// Test case 4:</span>
<span class="hljs-keyword">const</span> arr4 = [<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>, <span class="hljs-string">"c"</span>, <span class="hljs-string">"d"</span>, <span class="hljs-string">"e"</span>];
<span class="hljs-keyword">const</span> result4 = arr4.mySplice(<span class="hljs-number">2</span>, <span class="hljs-number">0</span>, <span class="hljs-string">"A"</span>, <span class="hljs-string">"B"</span>);
<span class="hljs-built_in">console</span>.log(arr4); 
<span class="hljs-comment">// ['a', 'b', 'A', 'B', 'c', 'd', 'e']</span>
<span class="hljs-built_in">console</span>.log(result4); <span class="hljs-comment">// []</span>

<span class="hljs-comment">// Test case 5:</span>
<span class="hljs-keyword">const</span> arr5 = [<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>, <span class="hljs-string">"c"</span>, <span class="hljs-string">"d"</span>, <span class="hljs-string">"e"</span>];
<span class="hljs-keyword">const</span> result5 = arr5.mySplice(<span class="hljs-number">2</span>, <span class="hljs-number">1</span>, <span class="hljs-string">"A"</span>);
<span class="hljs-built_in">console</span>.log(arr5);
<span class="hljs-comment">// ['a', 'b', 'A', 'd', 'e']</span>
<span class="hljs-built_in">console</span>.log(result5); 
<span class="hljs-comment">// ['c']</span>
</code></pre>
<h2 id="heading-contributing">Contributing 👩‍💻</h2>
<p>You can find the code in the following repository 👇</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://github.com/siddhigate/js-polyfills">https://github.com/siddhigate/js-polyfills</a></div>
<p>There might be some improvements that can be made or some test cases that I’ve missed addressing. You can create an issue in this repository if you find any improvement that can be made. Thanks!</p>
<h2 id="heading-thats-all-folks">That’s all folks 👋</h2>
<p>These were the custom implementations of array methods I worked on. Let's connect:</p>
<ul>
<li><a target="_blank" href="https://twitter.com/SiddhiGate">Twitter</a></li>
<li><a target="_blank" href="https://www.linkedin.com/in/siddhigate/">Linkedin</a></li>
<li><a target="_blank" href="https://github.com/siddhigate">GitHub</a></li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Get link preview - Generate a rich link preview of links via our app, API, or our NPM package 🔗]]></title><description><![CDATA[Idea 💡
You must have seen link previews when you are tweeting a link or sending a message with a link on Discord, Slack, WhatsApp, etc. like this 👇

Do you want to integrate such link previews in your chat app, social media app, etc. Or Do you just...]]></description><link>https://blog.siddhigate.com/get-link-preview</link><guid isPermaLink="true">https://blog.siddhigate.com/get-link-preview</guid><category><![CDATA[React]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[Node.js]]></category><category><![CDATA[Express]]></category><dc:creator><![CDATA[Siddhi Gate]]></dc:creator><pubDate>Wed, 10 Aug 2022 16:38:37 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1660051126040/I6Ef7Jd6y.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-idea">Idea 💡</h2>
<p>You must have seen link previews when you are tweeting a link or sending a message with a link on Discord, Slack, WhatsApp, etc. like this 👇</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1660148231534/zXlFWXNBA.gif" alt="linkpreviewexample.gif" /></p>
<p>Do you want to integrate such link previews in your chat app, social media app, etc. Or Do you just want to test how your link preview will look like when you share it on social media?</p>
<h2 id="heading-introducing-get-link-preview">Introducing get link preview 🔗:</h2>
<p><a target="_blank" href="https://get-link-preview.vercel.app/">Get link preview</a> is an app that will generate these rich previews for any given link. You can view the rich previews of any given link in our app. We provide API and an NPM package, If you want to add the feature of link preview in your app. </p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://youtu.be/IzYXZTkm1ok">https://youtu.be/IzYXZTkm1ok</a></div>
<h2 id="heading-features">Features ✨:</h2>
<ol>
<li>Generate link previews using or app 💻</li>
<li>Use our API to fetch link preview data and integrate it into your app 🌐</li>
<li>Use our NPM package which provides a custom hook to fetch link preview data and integrate it into your React app.</li>
</ol>
<h3 id="heading-1-generate-link-previews-on-our-app">1. Generate link previews on our app 💻</h3>
<p>You can view the rich preview of any link using our app!</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1660050130638/6CvKJ1K0S.gif" alt="getlinkpreviewapp.gif" /></p>
<h3 id="heading-2-get-link-preview-api">2. Get link preview API 🌐</h3>
<p>Our app also provides an API endpoint, which you can integrate in your app to fetch link previews.</p>
<p><strong>API endpoint:</strong></p>
<pre><code class="lang-jsx">https:<span class="hljs-comment">//get-link-preview.herokuapp.com/?url=https://github.com/siddhigate</span>
</code></pre>
<p><strong>Request type:</strong> GET</p>
<p><strong>Response:</strong></p>
<pre><code class="lang-jsx">{ 
  <span class="hljs-string">"success"</span>:<span class="hljs-literal">true</span>,
  <span class="hljs-string">"title"</span>:<span class="hljs-string">"siddhigate - Overview"</span>,
  <span class="hljs-string">"description"</span>:<span class="hljs-string">"Full stack developer. siddhigate has 49 repositories available. Follow their code on GitHub."</span>,
  <span class="hljs-string">"image"</span>:<span class="hljs-string">"https://avatars.githubusercontent.com/u/80971056?v=4?s=400"</span>,
  <span class="hljs-string">"sitename"</span>:<span class="hljs-string">"GitHub"</span>,
  <span class="hljs-string">"ogUrl"</span>:<span class="hljs-string">"https://github.com/siddhigate"</span>,
  <span class="hljs-string">"type"</span>:<span class="hljs-string">"profile"</span>,
  <span class="hljs-string">"domain"</span>:<span class="hljs-string">"github.com"</span>,
  <span class="hljs-string">"favicon"</span>:<span class="hljs-string">"https://github.githubassets.com/favicons/favicon.svg"</span>}
}
</code></pre>
<h3 id="heading-3-get-link-preview-npm-package">3. Get link preview NPM package 🚀</h3>
<p>get-link-preview NPM package provides a custom hook to fetch link previews and use the data in your app.</p>
<p><strong>Step 1:</strong> Install the package</p>
<pre><code class="lang-jsx">npm i get-link-preview
</code></pre>
<p><strong>Step 2: Use the custom hook</strong></p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> { getLinkPreviewData, loading, error, data } = useLinkPreview(<span class="hljs-string">"https://npmjs.com"</span>);
</code></pre>
<ul>
<li><p>You can pass the URL in the custom hook as a param</p>
<pre><code class="lang-jsx">  <span class="hljs-keyword">const</span> { data } = useLinkPreview(<span class="hljs-string">"https://npmjs.com"</span>);
</code></pre>
</li>
<li><p>When the hook is fetching the link preview data, the loading will be true, it will be false once it is done loading</p>
</li>
<li>If an error occurs, the error variable will include the error</li>
<li><p>The data will contain the link preview data, an example format for the same is:</p>
<pre><code class="lang-jsx">  { 
    <span class="hljs-string">"success"</span>:<span class="hljs-literal">true</span>,
    <span class="hljs-string">"title"</span>:<span class="hljs-string">"siddhigate - Overview"</span>,
    <span class="hljs-string">"description"</span>:<span class="hljs-string">"Full stack developer. siddhigate has 49 repositories available. Follow their code on GitHub."</span>,
    <span class="hljs-string">"image"</span>:<span class="hljs-string">"https://avatars.githubusercontent.com/u/80971056?v=4?s=400"</span>,
    <span class="hljs-string">"sitename"</span>:<span class="hljs-string">"GitHub"</span>,
    <span class="hljs-string">"ogUrl"</span>:<span class="hljs-string">"https://github.com/siddhigate"</span>,
    <span class="hljs-string">"type"</span>:<span class="hljs-string">"profile"</span>,
    <span class="hljs-string">"domain"</span>:<span class="hljs-string">"github.com"</span>,
    <span class="hljs-string">"favicon"</span>:<span class="hljs-string">"https://github.githubassets.com/favicons/favicon.svg"</span>}
  }
</code></pre>
</li>
<li><p>The hook also provides a getLinkPreview function which accepts a URL as a parameter. You can use it to fetch the link preview data for any URL.</p>
</li>
</ul>
<h2 id="heading-tools-and-technologies-used">Tools and Technologies used ⚒:</h2>
<ol>
<li>React for building frontend</li>
<li>Node.js and Express.js for building the backend</li>
<li>Heroku to deploy the backend</li>
<li>Vercel to deploy the frontend</li>
</ol>
<h2 id="heading-how-i-built-it">How I built it 👩‍💻:</h2>
<p>The data present in the head tags, more specifically the meta tags of the website is used in the link preview. To fetch this data, we will first need to fetch the website and extract the appropriate tags. I’ve extracted the following data using cheerio:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Data</td><td>Tag</td></tr>
</thead>
<tbody>
<tr>
<td>title</td><td><code>&lt;meta property="og:title"&gt;</code></td></tr>
<tr>
<td>description</td><td><code>&lt;meta property="og:description"&gt;</code></td></tr>
<tr>
<td>image</td><td><code>&lt;meta property="og:image"&gt;</code></td></tr>
<tr>
<td>type</td><td><code>&lt;meta property="og:type"&gt;</code></td></tr>
<tr>
<td>Sitename</td><td><code>&lt;meta property="og:site_name"&gt;</code></td></tr>
<tr>
<td>url</td><td><code>&lt;meta property="og:url"&gt;</code></td></tr>
<tr>
<td>favicon</td><td><code>&lt;link rel="icon"&gt;</code></td></tr>
<tr>
<td>domain</td></tr>
</tbody>
</table>
</div><p>I built an API which will respond with this data for the link provided as query in the request. I built the backend using Node.js and Express.js, and built the frontend using React. After integrating the API in frontend, I built the custom hook and published it as a NPM package.</p>
<h2 id="heading-challenges-i-faced">Challenges I faced🔨:</h2>
<ul>
<li><em>Relative path image URLs:</em>  In some websites, the favicon and <code>og:image</code> URLs were given in relative path. To get the absolute path, I converted the relative path to the absolute path.</li>
<li><em>Missing <code>og:title</code>:</em> In case, the website didn't include the meta <code>og:title</code> tag, I extracted the <code>&lt;title&gt;</code> tag from the given URL.</li>
<li><em>Invalid URL:</em> To handle invalid URLs, I used the URL constructor to check if the given URL is valid or not.</li>
<li><em>Publishing NPM package:</em> As this was my first time publishing an NPM package, I failed many times. But by referring to different resources, I was finally able to publish the package.</li>
</ul>
<h2 id="heading-useful-links">Useful links 🔗:</h2>
<ul>
<li><a target="_blank" href="https://get-link-preview.vercel.app/">Live link</a></li>
<li><a target="_blank" href="https://www.npmjs.com/package/get-link-preview">NPM package</a></li>
<li><a target="_blank" href="https://github.com/siddhigate/link-preview">GitHub source</a></li>
</ul>
<h2 id="heading-thats-all-folks">That’s all folks 👋</h2>
<p>If you have any ideas or features you wish to add to this project, feel free to contribute to the project. I hope you’ll find this app useful! Let’s connect:</p>
<ul>
<li><strong><a target="_blank" href="https://twitter.com/SiddhiGate">Twitter</a></strong></li>
<li><strong><a target="_blank" href="https://github.com/siddhigate">GitHub</a></strong></li>
<li><strong><a target="_blank" href="https://www.linkedin.com/in/siddhigate/">LinkedIn</a></strong></li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Dev Care: One stop solution to take care of your health as developers 👩‍💻]]></title><description><![CDATA[The Problem:
Developers spend almost half of the day on the desk working. 
As all of our work is done while sitting in one position in front of our computer screens, we invite all sorts of health issues like back pain and eye strain. On top of that, ...]]></description><link>https://blog.siddhigate.com/dev-care-one-stop-solution-to-take-care-of-your-health-as-developers</link><guid isPermaLink="true">https://blog.siddhigate.com/dev-care-one-stop-solution-to-take-care-of-your-health-as-developers</guid><category><![CDATA[Netlify]]></category><category><![CDATA[NetlifyHackathon]]></category><category><![CDATA[Hashnode]]></category><category><![CDATA[React]]></category><dc:creator><![CDATA[Siddhi Gate]]></dc:creator><pubDate>Mon, 28 Feb 2022 17:26:28 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1646069079364/d1rIOvO7c.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-the-problem">The Problem:</h2>
<p>Developers spend almost half of the day on the desk working. 
As all of our work is done while sitting in one position in front of our computer screens, we invite all sorts of health issues like back pain and eye strain. On top of that, various developers listen to music with earphones while working for long hours. All these habits can cause a negative impact on our eyes, back, and ears.</p>
<p>We all care about our health and we have always tried to find solutions to these problems. The solutions are fairly simple too, like, having a good posture, taking small breaks for relaxing our eyes, not using earphones for a long time, and so on. Even though the solutions are easy, the main issue is to practice these exercises consistently. This is because, once we get into the coding zone, we are solely focused on the work and we tend to forget about them. So, to help developers to take care of their health while working, I have built Dev Care.</p>
<h2 id="heading-introducing-dev-care">Introducing Dev Care 👩‍💻</h2>
<p>One-stop solution to take care of your health as developers. Dev Care will help you to take care of your back, eyes, and ears while working. The solutions we are working with are as follows:</p>
<h3 id="heading-back-health">Back health:</h3>
<p>For back health, the app has the feature of posture detection. It detects your posture at regular intervals and reminds you to sit straight if you are not.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1646059559353/mGbiu6vjH.jpg" alt="47095.jpg" /></p>

<h3 id="heading-eyes-health">Eyes health:</h3>
<p>To relax your eyes there is a 20-20-20 rule which states that:</p>
<blockquote>
<p>For every 20 minutes spent looking at a screen, a person should look at something 20 feet away for 20 seconds</p>
</blockquote>
<p>This exercise relaxes our eyes and reduces eye strain. The app reminds you to follow this exercise at regular intervals.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1646059207849/ge0f-Oyj9.png" alt="image.png" /></p>
<h3 id="heading-ear-health">Ear health:</h3>
<p>For ear health, there is a 60-60 rule:</p>
<blockquote>
<p>Only turn your device volume up to 60% and listen for no more than 60 minutes per day</p>
</blockquote>
<p>The app checks if you are using earphones for longer than 60 minutes and reminds you to control your earphone usage if it is becoming unhealthy.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1646060239820/3q6OBCAYo.png" alt="image.png" /></p>

<h2 id="heading-app-walkthrough">App walkthrough✨</h2>
<h3 id="heading-onboarding">Onboarding👋</h3>
<p>When a user visits the app for the first time, initial information and instructions are displayed to the user to make them familiar with the app. This app doesn’t require a login or signup.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1646063229646/SR6dYn1l_.gif" alt="Onboarding" /></p>
<h3 id="heading-posture-detection-initial-setup">Posture detection initial setup🧍‍♂️</h3>
<p>The app first needs to understand what a good posture is and what a bad posture is. For that, you’ll need to click at least 10 pictures for both cases. The app will learn from those images what a correct and incorrect posture is. After which, at regular intervals, the app will be able to check your posture and further inform you if it is not correct.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1646071596623/cODe9rirG.gif" alt="ezgif.com-gif-maker (8).gif" /></p>
<h3 id="heading-dashboard">Dashboard 🏠</h3>
<p>The dashboard displays the countdown timer. You can start and stop it whenever you want. You can opt to select only a few options to receive notifications for between back care, eye care, and ear care. Additionally, the app has a sweet cartoon creator whose mood will depend on how effectively you use this app.
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1646062814254/SPLzpsgMY.gif" alt="Dashboard" /></p>
<h3 id="heading-reminders">Reminders 🔔</h3>
<p>You’ll receive reminders for doing the exercise, if your posture is incorrect and if you are using your earphones more than the limit at regular intervals. </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1646067642936/hbbN63zRD.gif" alt="Notification reminders" /></p>
<h3 id="heading-analysis">Analysis📊</h3>
<p>Dev Care will perform an analysis of your habits and visualize them through different charts. For back care, a pie chart is used to visualize how many times you are found sitting in good and bad posture. For eye care, a line chart is used to depict how many times a day you follow the exercise. For ear care, a bar graph is used to depict the daily usage of earphones.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1646063976757/ElQyRLH3h.gif" alt="ezgif.com-gif-maker (5).gif" /></p>
<h2 id="heading-technologies-and-tools-used">Technologies and Tools used🛠</h2>
<ul>
<li><a target="_blank" href="https://reactjs.org/">React JS</a> for building the frontend of the app.</li>
<li><a target="_blank" href="https://www.tensorflow.org/js">TensorFlow JS</a> for posture detection.</li>
<li><a target="_blank" href="https://www.chartjs.org/">Chart JS</a> for visualization of data.</li>
<li><a target="_blank" href="https://www.netlify.com/">Netlify</a> for deploying the app.</li>
</ul>
<h2 id="heading-building-dev-care">Building Dev Care👩‍🏭</h2>
<h3 id="heading-planning">Planning 📝</h3>
<p>I used Notion to plan the project and used my diary to write daily to-dos. While planning, I divided the entire work into 2 parts; UI design and implementation. Further, I divided the implementation part into different features and made a list of all the possible tasks that need to be done.</p>
<h3 id="heading-designing">Designing 🎨</h3>
<p>For designing the app I used excalidraw. It is a simple tool to design the wireframes of the app. My initial design of the app looked like this👇</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1646065050022/032HLDftZ.gif" alt="ezgif.com-gif-maker (6).gif" /></p>
<h3 id="heading-building">Building 👩‍💻</h3>
<p>My plan was ready. The only thing left was implementing it. I used React.js which is a JavaScript library to build this app. The main feature which I felt challenging was the back posture prediction; which required machine learning. I discovered "tensorflow.js" which is a library for machine learning in JavaScript. Using this library, we can implement machine learning models in the browser.</p>
<p>It took me a lot of time in understanding new things like using tensorflow.js, notifications, etc. But with the help of documentation and some resources, I was able to implement all the features.</p>
<h3 id="heading-deploying">Deploying 🚀</h3>
<p>I used Netlify for deploying Dev Care. Netlify offers hosting services for web applications with CI-CD and makes the deployment process easier.</p>
<h2 id="heading-challenges-i-faced">Challenges I faced ☠</h2>
<p>The main challenge I faced while building this app was the bad posture detection feature. For classifying the posture into a good or a bad one, first, we need examples to train the machine learning model and I didn’t find any existing dataset for this. To solve this problem, I decided to ask the users to take their own pictures in both good and bad posture and then trained the model in the browser with those examples. It worked this way! Also, all the user data resides in their browser only, the app don’t store it anywhere else.</p>
<p>While building this app, there were a lot of things that I wasn’t familiar with. The topmost is using tensorflow.js for posture detection. It was quite difficult for me to understand the process at the start. But by going through the docs thoroughly I was able to understand &amp; then implement it for classifying between a good posture and a bad posture. The second one was implementing the notification feature which works on both desktop browsers and mobile browsers. But again, after going through the docs and a few articles I was able to build the feature. Mostly, all of my challenges were solved by going through the docs thoroughly.😉</p>
<h2 id="heading-whats-next">What’s next ✨</h2>
<p>I’m looking forward to adding more features in Dev Care:</p>
<ul>
<li>Customized reminder time intervals</li>
<li>PWA</li>
</ul>
<h2 id="heading-useful-links">Useful links 🔗</h2>
<ul>
<li><a target="_blank" href="https://devcare.netlify.app/">App link</a></li>
<li><a target="_blank" href="https://github.com/siddhigate/dev-care-app/">GitHub Repo</a></li>
</ul>
<h2 id="heading-thats-all-folks">That’s all folks 👋</h2>
<p>This is all about Dev Care. I hope that Dev Care will help you to take care of your health. Let’s connect:</p>
<ul>
<li><a target="_blank" href="https://twitter.com/SiddhiGate">Twitter</a></li>
<li><a target="_blank" href="https://github.com/siddhigate">GitHub</a></li>
<li><a target="_blank" href="https://www.linkedin.com/in/siddhigate/">LinkedIn</a></li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Managing college and work can be tough, here's how I followed a simple notion ✨]]></title><description><![CDATA[Hey, I'm Siddhi! I'm a full-time engineering student, currently doing a part-time internship and also learning along the way by building small projects. But for the past couple of months, I was having a hard time managing all of these things which in...]]></description><link>https://blog.siddhigate.com/managing-college-and-work-can-be-tough-heres-how-i-followed-a-simple-notion</link><guid isPermaLink="true">https://blog.siddhigate.com/managing-college-and-work-can-be-tough-heres-how-i-followed-a-simple-notion</guid><category><![CDATA[Productivity]]></category><category><![CDATA[College]]></category><dc:creator><![CDATA[Siddhi Gate]]></dc:creator><pubDate>Mon, 25 Oct 2021 08:33:41 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1635131156620/XxlbKYMp9.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hey, I'm Siddhi! I'm a full-time engineering student, currently doing a part-time internship and also learning along the way by building small projects. But for the past couple of months, I was having a hard time managing all of these things which in turn was affecting my mental and physical health. So, this month I decided to take a break, introspect and find a solution to this problem and I did find a solution! </p>
<p>I have a simple notion:</p>
<blockquote>
<p>Plan the work, then work the plan!</p>
</blockquote>
<p>Following is how I executed this notion and tried to manage college, work, and other activities.</p>
<h2 id="content">Content</h2>
<p><strong>Plan the work</strong></p>
<ol>
<li>Wake up with a clear plan 🌞 </li>
<li>Assign time slots to the tasks ⌛</li>
<li>Prioritize the tasks 📝 </li>
<li>Make achievable tasks ✔</li>
</ol>
<p><strong>Work the plan</strong></p>
<ol>
<li>Pomodoro and focused sessions ⏰</li>
<li>Take enough rest and breaks😴</li>
<li>Don't work in between lectures (during break) 👩‍💻❌</li>
<li>Believe in yourself 😎</li>
<li>Spend a lot of time with friends and family 👨‍👩‍👧‍👦</li>
</ol>
<h2 id="plan-the-work">Plan the work 🗓</h2>
<h3 id="1-wake-up-with-a-clear-plan">1. Wake up with a clear plan 🌞</h3>
<p>Earlier, when I woke up I had no clear idea of what I should do for the day. My mind was a mess and it kind of caused a negative start to the day.  Having a clear plan in mind while waking up, made me feel sorted and in control of my day. For that, before going to sleep I planned the day. </p>
<h3 id="2-assign-time-slots-to-the-tasks">2. Assign time slots to the tasks ⌛</h3>
<p>For planning the day, earlier, I did whatever task at whatever time of the day depending on my mood. My day didn't have any structure. This made my day messy and I didn't feel like I was in control. Later, I discovered time-boxing. Then, I assigned time slots to different tasks. For the first few days, I tried different combinations to find out which best suited me, and then, I stuck to the one! </p>
<h3 id="3-prioritize-the-tasks">3. Prioritize the tasks 📝</h3>
<p>Even though, I started time-boxing. It was not possible to give an equal amount of time to all of these things. Somedays, academic stuff was urgent and important and somedays, work stuff. Then, I decided to prioritize the tasks and then decide how much time to give to each of them. Due to this, I was able to complete every assignment or work on time.</p>
<h3 id="4-make-achievable-tasks">4. Make achievable tasks ✔</h3>
<p>Forming the tasks was itself a tough task too! For a long time, I tried to do everything at once and I expected a lot from myself. It took me a long time to realize that some tasks take time to do and it's fine! What's not fine is to burden yourself and create unnecessary pressure. Before doing the unachievable tasks, I felt the pressure that how will I do those tasks, and eventually, I was unable to complete those tasks which made me feel miserable. Things can be done in a positive mindset and without pressure. For that, forming achievable goals for the day was the most important task. By practicing this, I was able to achieve a lot than in my previous state as I spent more time doing than worrying. </p>
<h2 id="work-the-plan">Work the plan 👩‍💻</h2>
<h3 id="1-pomodoro-and-focused-sessions">1. Pomodoro and focused sessions ⏰</h3>
<p>But, assigning the time slots, is not it! I wanted to achieve the maximum from that time.  For that, I used the traditional Pomodoro method, kept the phone away from me, and just gave 100% on the task at hand without any distraction and with maximum focus. This made me complete all the tasks with the best results. Got very good feedback from professors and seniors as well! </p>
<h3 id="2-take-enough-rest-and-breaks">2. Take enough rest and breaks😴</h3>
<p>While doing all of these things, it's important to be energetic. Many-a-times, I felt tired and didn't feel like doing anything. Taking enough breaks and rest in between was the solution for this. Sometimes, I felt guilty for taking breaks. I used to think that I am not utilizing my time efficiently and instead of taking breaks I could have studied or worked. But, this mindset took a toll on my health and my performance. I then started to take enough breaks in between and did entirely different things than my current work during the break or just did nothing. Out of all my break activities, talking with my mom or friends made me feel the most relaxed and happy😇</p>
<h3 id="3-dont-work-in-between-lectures">3. Don't work in between lectures 👩‍💻❌</h3>
<p>I had this bad habit of working in between lectures. I thought that I was being productive and utilizing my time efficiently. But this was not the case. It was quite the opposite. I was not able to focus and achieve maximum output while working in those breaks. Also, attending the next lectures with full attention got difficult. Break time should be utilized for breaks and taking rest. I learned it the hard way. I then, utilized my breaks just to take a rest and refresh my mind to attend the next lecture with full enthusiasm.</p>
<h3 id="4-believe-in-yourself">4. Believe in yourself 😎</h3>
<p>Believe in yourself that you can achieve the tasks and you are halfway there.  Self-doubt, overthinking just costs us time. Even after doing all the above things, sometimes I lacked confidence and I didn't believe that I can complete any of those tasks. But soon I realized that this was just costing me time and affecting my mindset. The only solution to this is to believe in yourself and give your best. Spend time doing the things than worrying about them.</p>
<h3 id="5-spend-a-lot-of-time-with-friends-and-family">5. Spend a lot of time with friends and family 👨‍👩‍👧‍👦</h3>
<p>The most important of all the things is this! Spending an ample amount of time with friends and family. Talking to them made me feel happy and relaxed. </p>
<hr />

<h3 id="thats-all-folks">That's all folks 👋</h3>
<p>This is what I did to manage my college and work effectively. <em> Thank you for reading this article! </em></p>
<p>🌍 Let's connect</p>
<ul>
<li><a target="_blank" href="https://twitter.com/SiddhiGate">Twitter @SiddhiGate</a></li>
<li><a target="_blank" href="https://www.linkedin.com/in/siddhigate/">LinkedIn @siddhigate</a></li>
</ul>
<hr />



]]></content:encoded></item><item><title><![CDATA[Social Network and user recommendations using Graphs and Dijkstra algorithm]]></title><description><![CDATA[Hey! I'm Siddhi. I'm an engineering student studying Computer Science.  One of my favorite subjects of my curriculum is Data Structures and the topic I fear the most is Graphs. To overcome this fear, I tried to implement this data structure in a prac...]]></description><link>https://blog.siddhigate.com/social-network-and-recommendations</link><guid isPermaLink="true">https://blog.siddhigate.com/social-network-and-recommendations</guid><category><![CDATA[data structures]]></category><category><![CDATA[algorithms]]></category><dc:creator><![CDATA[Siddhi Gate]]></dc:creator><pubDate>Mon, 16 Aug 2021 20:30:49 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1629144553344/FgBFxdy0a.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hey! I'm Siddhi. I'm an engineering student studying Computer Science.  One of my favorite subjects of my curriculum is Data Structures and the topic I fear the most is Graphs. To overcome this fear, I tried to implement this data structure in a practical application - a social network. </p>
<p>In this blog, I'll explain how I used graphs to implement a social network and how I used the Dijkstra algorithm to provide people you may know user recommendations.</p>
<p>There are 2 main parts:</p>
<ol>
<li>Building a social network</li>
<li>Providing people you may know user recommendations</li>
</ol>
<h2 id="heading-what-is-a-social-network">What is a social network?</h2>
<p>A social network is a structure consisting of users and connections between them.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1629144719293/vjahkFJJ1.png" alt="social network" /></p>
<p>We can say that a social network is a graph where,</p>
<ul>
<li>Nodes are users</li>
<li>Edges are connections/relations</li>
<li>Weight is the closeness between the users</li>
</ul>
<h3 id="heading-how-to-determine-the-closeness-between-the-users">How to determine the closeness between the users?</h3>
<p>The weight of the edge between two nodes will decide the closeness between the users. Lower the weight, the closer the two users are. But how to determine the closeness between two users.</p>
<p>To calculate the closeness between two users, I designed a simple algorithm, where </p>
<ol>
<li>When the user makes a connection the weight between them is set to some MAX value. </li>
<li>After that, whenever a user likes the other user's post they become close by 2 weights, </li>
<li>When user1 comments on user2's post they become close by 4 weights.</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1629144880602/6HT487nhC.gif" alt="socialnetwork.gif" /></p>
<p>This closeness parameter will help us to provide better recommendations.</p>
<p> The less the weight between two users, the more close they are.</p>
<p>So by using graph and this closeness algorithm, the social network can be implemented. Now, the next part is providing user recommendations.</p>
<h3 id="heading-what-is-the-dijkstra-algorithm">What is the Dijkstra algorithm?</h3>
<blockquote>
<p>Dijkstra's algorithm is an algorithm for finding the shortest paths between nodes in a graph </p>
</blockquote>
<p>We will not get into the details of the implementation of the algorithm, but rather focus on what it does and how it can be used in this application.</p>
<p>Let's take an example, we have the following graph</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1629144719293/vjahkFJJ1.png" alt="social network" /></p>
<p>We want to find the shortest path between A and E, </p>
<ul>
<li>There are two paths between A and E, which are <strong>A-B-C-E</strong> and <strong>A-D-E</strong></li>
<li>The weight of the path <strong>A-B-C-E</strong> is 3 + 2 + 2 = 7</li>
<li>The weight of the path <strong>A-D-E</strong> is  2 + 2 = 4</li>
<li><strong>Dijkstra algorithm will return the shortest path. </strong></li>
<li><strong>So, in this example, it will return <em>A-D-E.</em></strong></li>
</ul>
<p>But, the main question is how will we use this algorithm to provide user recommendations?</p>
<h3 id="heading-how-to-use-dijkstra-to-provide-user-recommendations">How to use Dijkstra to provide user recommendations?</h3>
<p>The steps are simple. If we want to find user recommendations for a particular user,</p>
<ol>
<li>We will need to find the shortest path from that user to all the other users in the network.</li>
<li>Sort them according to weight.</li>
<li>Now we will have the people you many know in ascending order of how likely that user might know them.</li>
</ol>
<p>Let's understand this with an example,</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1629144818266/wksO0BVgw.png" alt="social network" /></p>
<p>Finding People you may know for <strong>User A,</strong></p>
<ol>
<li><p>Dijkstra algorithm will find the shortest path to all other users</p>
<p> B - 3,  C - 5, D - 12, E - 15</p>
</li>
<li><p>We will drop the connections of the user <strong>A,</strong> so now we have,</p>
<p> C - 5, E - 15</p>
</li>
<li><p>We will sort the list by weight</p>
<p> C - 5, E - 15</p>
</li>
<li><p>Now, we get our result. Here, User A is more likely to know user C as it is the first element in the sorted list.</p>
</li>
</ol>
<p>We are likely to know users who are friends with our closest friends. Such users can be found by using the procedure mentioned above.</p>
<h3 id="heading-what-does-the-result-mean">What does the result mean?</h3>
<p>These steps will return the users with the least path. The connection weight between the users will be less. The less the connection weight between them, the more likely they know each other. Thus, the recommendations will be useful.</p>
<p>This is how I used graphs and the Dijkstra algorithm to implement a social network and to provide people you may know user recommendations.</p>
]]></content:encoded></item><item><title><![CDATA[Eye Care - An app that will help you to take care of your eyes]]></title><description><![CDATA[Introducing Eye Care
Background
As a software developer, I am in front of my screen 24/7. With everything being remote, my screen time just went up. This increase in screen time only caused my eye strain to increase. It was then I realized how harsh ...]]></description><link>https://blog.siddhigate.com/eye-care-an-app-that-will-help-you-to-take-care-of-your-eyes</link><guid isPermaLink="true">https://blog.siddhigate.com/eye-care-an-app-that-will-help-you-to-take-care-of-your-eyes</guid><category><![CDATA[HarperDB Hackathon]]></category><dc:creator><![CDATA[Siddhi Gate]]></dc:creator><pubDate>Wed, 30 Jun 2021 20:10:25 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1625086390987/EH6sgcjvS.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1 id="heading-introducing-eye-care">Introducing Eye Care</h1>
<h3 id="heading-background">Background</h3>
<p>As a software developer, I am in front of my screen 24/7. With everything being remote, my screen time just went up. This increase in screen time only caused my eye strain to increase. It was then I realized how harsh it was for my eyes and how much the health of my eyes mattered. Sooner, I came across 20-20-20 rule. </p>
<blockquote>
<p>The rule says that for every 20 minutes spent looking at a screen, a person should look at something 20 feet away for 20 seconds.
Following the rule is a great way to remember to take frequent breaks. This should reduce eye strain caused by looking at digital screens for too long.</p>
</blockquote>
<p>Though this rule sounds easy to follow, it is much harder to maintain discipline. That's when I thought of making an app that will remind me to follow this rule and thus, help to take care of my eyes.</p>
<h3 id="heading-how-does-it-work">How does it work</h3>
<ul>
<li>Eye Care will remind you every 20 minutes to look 20 ft away for 20 seconds.</li>
<li>The main challenge is to stay disciplined, for that:<ul>
<li>This app will plant a flower</li>
<li>Every time eye care reminds you, you should follow the rule</li>
<li>The more you stay disciplined, your flower will bloom happily.</li>
</ul>
</li>
</ul>
<h3 id="heading-features">Features</h3>
<h4 id="heading-reminder-to-follow-the-202020-rule">Reminder to follow the 20:20:20 rule</h4>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1625082434709/URH_2eY_J.png" alt="timer.png" />
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1625082459131/DXsxaLNkG.png" alt="reminder.png" /></p>
<h4 id="heading-score-based-on-your-discipline">Score based on your discipline</h4>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1625082493116/BeNBKoO9H.png" alt="dashboard.png" /></p>
<ul>
<li>Every time you follow this rule your score will increase.</li>
<li>As your score increases, your flower will bloom</li>
</ul>
<h3 id="heading-technologies">Technologies</h3>
<p><strong>Frontend built with</strong></p>
<ul>
<li>JavaScript</li>
<li>HTML</li>
<li>CSS</li>
</ul>
<p><strong>Backend built with</strong></p>
<ul>
<li>Node.jS</li>
<li>Express.jS</li>
<li>HarperDB</li>
</ul>
<h3 id="heading-building-eye-care">Building Eye Care</h3>
<p>I'm new to web development. That's why, even though, the functionalities seem easy, it was quite challenging for me to implement.  </p>
<p>To simplify the development of this app, I formed smaller tasks which were:</p>
<p><strong>Section 1: Frontend</strong></p>
<ol>
<li>Timer </li>
<li>Modal dialog</li>
<li>Sign in, sign up pages</li>
<li>Api calls through JavaScript</li>
<li>Authentication</li>
</ol>
<p><strong>Section 2: Backend</strong></p>
<ol>
<li>Getting familiar with HarperDB</li>
<li>REST API for CRUD operations</li>
<li>Authentication</li>
<li>Authenticated private routes</li>
</ol>
<p>After performing the tasks, I later integrated them into one, which turned out to be this Eye Care app.</p>
<h3 id="heading-whats-next">What's next</h3>
<p>I am looking forward to add more features this app.</p>
<ul>
<li><p><strong>Garden of flowers</strong> -
  A feature where user will be able to plant a garden of flowers with a variety of different flowers.</p>
</li>
<li><p><strong>Back care</strong> -
  As almost everything is now being remote, we are constantly sitting in front of our screens. Maintaining a good posture while sitting is important. A feature to detect bad posture will help to notify the user to sit straight. This feature is next on the list.</p>
</li>
<li><p>and more</p>
</li>
</ul>
<h3 id="heading-links">Links</h3>
<ul>
<li><a target="_blank" href="https://github.com/siddhigate/eye-care-frontend">Frontend repo</a></li>
<li><a target="_blank" href="https://github.com/siddhigate/eye-care-app-backend">Backend repo</a></li>
<li><a target="_blank" href="https://eyecare-app.netlify.app/">Eye care app</a></li>
</ul>
<p>This is my submission for the #harperdbhackathon. I hope this app helps you.</p>
]]></content:encoded></item></channel></rss>