<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Pranita Panigrahi on Medium]]></title>
        <description><![CDATA[Stories by Pranita Panigrahi on Medium]]></description>
        <link>https://medium.com/@pranitapanigrahi50?source=rss-765adebaf43f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*tevVULBm0W0rBk2xa_JaHA.jpeg</url>
            <title>Stories by Pranita Panigrahi on Medium</title>
            <link>https://medium.com/@pranitapanigrahi50?source=rss-765adebaf43f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 19:27:08 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pranitapanigrahi50/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Basic Nesting in React Router]]></title>
            <link>https://medium.com/@pranitapanigrahi50/basic-nesting-in-react-router-2a0829bd80b8?source=rss-765adebaf43f------2</link>
            <guid isPermaLink="false">https://medium.com/p/2a0829bd80b8</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[interview-questions]]></category>
            <category><![CDATA[basics]]></category>
            <category><![CDATA[react-router]]></category>
            <dc:creator><![CDATA[Pranita Panigrahi]]></dc:creator>
            <pubDate>Sat, 28 Sep 2024 12:13:20 GMT</pubDate>
            <atom:updated>2024-09-28T12:13:20.848Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eiiYXgOqdxoS31ofsqg7Ng.jpeg" /></figure><p>So, here I was, building my React app, feeling pretty good about it. I had a Home page, an About page—things were going great. But then I realized, “Hey, what if I need <em>more</em>? Like, I want a page within a page.” That’s when I discovered the power of <strong>nested routes</strong>.</p><p>Enter <strong>Outlet</strong>, React Router’s answer to, “How do I fit more pages into this page?” Think of it as a magic window. The Home page is just the beginning. With Outlet, I could nest more routes inside it—like having rooms inside a room. Want a <strong>Section 1</strong>? Done. How about a <strong>Section 2</strong>? Easy. And all of them sit neatly inside the Home page without breaking a sweat.</p><p>The trick is simple: just put an Outlet in your parent component, and voilà—nested routes will appear like magic. It’s like opening a door to another door. All smooth, no fuss.</p><p>And just like that, I went from a flat, boring app to one with layers, all thanks to Outlet. Simple, clean, and now my app feels like a multi-story building—without any of the heavy lifting.</p><pre>import { Link } from &quot;react-router-dom&quot;;<br><br>export const Navbar = () =&gt; {<br>  return (<br>    &lt;nav&gt;<br>      &lt;Link to=&quot;/&quot;&gt;Home&lt;/Link&gt;<br>      &lt;Link to=&quot;/about&quot;&gt;About&lt;/Link&gt;<br>    &lt;/nav&gt;<br>  );<br>};</pre><pre>import { Navbar } from &quot;./Navbar&quot;;<br>import { Outlet } from &quot;react-router-dom&quot;;<br><br>export const Layout = () =&gt; {<br>  return (<br>    &lt;&gt;<br>      &lt;Navbar /&gt;<br>      {/* Renders child routes */}<br>      &lt;Outlet /&gt;<br>    &lt;/&gt;<br>  );<br>};</pre><pre>import { Link, Outlet } from &quot;react-router-dom&quot;;<br><br>export const Home = () =&gt; {<br>  return (<br>    &lt;div&gt;<br>      &lt;h2&gt;Welcome to the Home Page&lt;/h2&gt;<br>      &lt;nav&gt;<br>        &lt;Link to=&quot;section1&quot;&gt;Section 1&lt;/Link&gt; | <br>        &lt;Link to=&quot;section2&quot;&gt;Section 2&lt;/Link&gt;<br>      &lt;/nav&gt;<br>      {/* Render nested routes here */}<br>      &lt;Outlet /&gt;<br>    &lt;/div&gt;<br>  );<br>};</pre><pre>import &quot;./styles.css&quot;;<br>import { BrowserRouter, Routes, Route } from &quot;react-router-dom&quot;;<br>import { Layout } from &quot;./Layout&quot;;<br>import { Home } from &quot;./Home&quot;;<br>import { Contact } from &quot;./Contact&quot;;<br><br>export default function App() {<br>  return (<br>    &lt;div className=&quot;App&quot;&gt;<br>      &lt;h1&gt;Hello Nested Routes&lt;/h1&gt;<br>      &lt;BrowserRouter&gt;<br>        &lt;Routes&gt;<br>          &lt;Route element={&lt;Layout /&gt;}&gt;<br>            &lt;Route path=&quot;/&quot; element={&lt;Home /&gt;}&gt;<br>              {/* Nested routes under Home */}<br>              &lt;Route path=&quot;section1&quot; element={&lt;div&gt;Section 1 Content&lt;/div&gt;} /&gt;<br>              &lt;Route path=&quot;section2&quot; element={&lt;div&gt;Section 2 Content&lt;/div&gt;} /&gt;<br>            &lt;/Route&gt;<br>          &lt;/Route&gt;<br>        &lt;/Routes&gt;<br>      &lt;/BrowserRouter&gt;<br>    &lt;/div&gt;<br>  );<br>}<br></pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2a0829bd80b8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Quick dive into Event Loop in JavaScript]]></title>
            <link>https://medium.com/@pranitapanigrahi50/quick-dive-into-the-event-loop-in-javascript-c08d9c0dc5b5?source=rss-765adebaf43f------2</link>
            <guid isPermaLink="false">https://medium.com/p/c08d9c0dc5b5</guid>
            <category><![CDATA[refresher]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[event-loop-in-js]]></category>
            <dc:creator><![CDATA[Pranita Panigrahi]]></dc:creator>
            <pubDate>Sat, 21 Sep 2024 07:09:16 GMT</pubDate>
            <atom:updated>2024-09-21T09:46:41.687Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Iv1FGbSqcZ_A4Eol81eKAw.jpeg" /></figure><p>We are all familiar with the event loop, this article serves as a quick refresher.</p><p>Our browser has JavaScript engine. JavaScript engine has <strong>call stack</strong> . Call stack has<strong> stack memory.</strong> <br>Stack memory stores primitive values and objects.<br> <strong><br></strong>Primitive values are allocated memory in compile time <strong>. </strong>Primitives are small and predictable in size. The values of objects are stored in heap memory and call stack holds the reference to the original values in heap.<br>Objects are grow or shrink in size . So the memory required cannot be determined during compile time</p><p>When we have a piece of code to be executed, the js engine starts from the first line. First it creates a global execution context and puts it into the call stack as the first item.</p><p>The global execution context is created when a JavaScript script first starts to run, and it represents the global scope in JavaScript. A function execution context is created whenever a function is called, representing the function’s local scope.<br>The execution context has two phases :<br>1. Creation Phase <br>2. Execution Phase</p><p>In the creation phase we allocate memory to the declarations . In execution phase JS engine will go through the code step by step from top to bottom and will execute the code.</p><p>Now lets see how promises behaves : When a promise is created, the <strong>executor function</strong> (the function passed to the new Promise()) is executed <strong>synchronously</strong>. This means the executor function is placed on the <strong>call stack</strong>, just like any other function.When resolve(&#39;Resolved value&#39;) is called, <strong>it does not resolve the promise immediately</strong>. Instead, it <strong>schedules</strong> the .then() callback into the <strong>microtask queue</strong> to be executed later (after the current synchronous code finishes).<br>Once the <strong>executor function</strong> finishes, its frame is removed from the <strong>call stack</strong>. At this point, the promise is considered <strong>pending</strong>, and the resolve() function has scheduled the fulfillment handler to run later.</p><p>Then our hero “Event Loop” plays a very important role here. It keeps on checking if the call stack is empty . Incase the call stack is empty it will pick any pending tasks waiting in microtask queue for processing .</p><p>We have one more task macrotask queue where all the timers are processed . Event loop first executes all the tasks in macrotask queue and then moves to microtask queue .</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c08d9c0dc5b5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React Custom Hook : useLocalStorage]]></title>
            <link>https://medium.com/@pranitapanigrahi50/react-custom-hook-uselocalstorage-7eb0761f46db?source=rss-765adebaf43f------2</link>
            <guid isPermaLink="false">https://medium.com/p/7eb0761f46db</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[hooks-in-react]]></category>
            <category><![CDATA[interview-questions]]></category>
            <category><![CDATA[basics]]></category>
            <dc:creator><![CDATA[Pranita Panigrahi]]></dc:creator>
            <pubDate>Mon, 09 Sep 2024 15:07:18 GMT</pubDate>
            <atom:updated>2024-09-09T15:08:29.757Z</atom:updated>
            <content:encoded><![CDATA[<h3>Custom Hook-useLocalStorage</h3><p>This is one of the frequently asked question in interview.<br>Create a useLocalStorage custom hook in react .</p><pre>import { useEffect, useState } from &quot;react&quot;;<br><br>export const useLocalStorage = (key, initialValue) =&gt; {<br>  const [localState, setLocalState] = useState(() =&gt; {<br>    try {<br>      const storedValue = localStorage.getItem(key);<br>      return storedValue ? JSON.parse(storedValue) : initialValue;<br>    } catch (error) {<br>      console.error(&quot;Error parsing localStorage value:&quot;, error);<br>      return initialValue;<br>    }<br>  });<br><br>  const handleLocalState = (value) =&gt; {<br>    setLocalState(value);<br>    return localStorage.setItem(key, JSON.stringify(value));<br>  };<br><br>  useEffect(() =&gt; {<br>    if (key) {<br>      const storedValue = localStorage.getItem(key);<br>      if (storedValue) setLocalState(JSON.parse(storedValue));<br>    }<br>  }, [key]);<br>  return [localState, handleLocalState];<br>};</pre><pre>//Usage of the custom hook<br>import React from &#39;react&#39;;<br>import { useLocalStorage } from &#39;./useLocalStorage&#39;; <br><br>function App() {<br>  const [name, setName] = useLocalStorage(&#39;name&#39;, &#39;John Doe&#39;); <br><br>  return (<br>    &lt;div&gt;<br>      &lt;h1&gt;Hello, {name}!&lt;/h1&gt;<br>      &lt;input<br>        type=&quot;text&quot;<br>        value={name}<br>        onChange={(e) =&gt; setName(e.target.value)}<br>      /&gt;<br>    &lt;/div&gt;<br>  );<br>}<br><br>export default App;</pre><ul><li><strong>Initialising State</strong>: When the hook is first called, it checks if there’s a value stored in the local storage under the provided key. If a value is found, it&#39;s parsed and returned as the initial state. If no value is found, the initialValue provided is used as the starting point.</li><li><strong>Updating State</strong>: The hook provides a handleLocalState function that takes a new value and updates the local storage with the latest data. Whenever the state changes, the useEffect hook ensures that the new value is immediately persisted to the local storage.</li><li><strong>Error Handling</strong>: The hook wraps all interactions with the local storage in try/catch blocks, logging any errors that may occur to the console. This helps prevent unexpected runtime errors from disrupting your application&#39;s functionality.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7eb0761f46db" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React : Basic Search and Pagination Example]]></title>
            <link>https://medium.com/@pranitapanigrahi50/react-basic-search-and-pagination-example-c4c05a71b918?source=rss-765adebaf43f------2</link>
            <guid isPermaLink="false">https://medium.com/p/c4c05a71b918</guid>
            <category><![CDATA[basic-react]]></category>
            <category><![CDATA[pagination]]></category>
            <category><![CDATA[search]]></category>
            <dc:creator><![CDATA[Pranita Panigrahi]]></dc:creator>
            <pubDate>Thu, 29 Aug 2024 13:15:01 GMT</pubDate>
            <atom:updated>2024-08-29T14:38:03.410Z</atom:updated>
            <content:encoded><![CDATA[<h3>React : Basic Search and Pagination Example</h3><p>The code demonstrates a straightforward approach to implement search functionality and pagination within a dataset</p><pre>import { useState, useEffect } from &#39;react&#39;;<br><br>export const DataTable = () =&gt; {<br> <br>  const [formData, setFormData] = useState({ name: &#39;&#39;, gender: &#39;&#39;, age: &#39;&#39; });<br>  const [editData, setEditData] = useState({ name: &#39;&#39;, gender: &#39;&#39;, age: &#39;&#39; });<br>  const [isEditIndex, setIsEditIndex] = useState(null);<br>  const [list, setList] = useState([{ name: &#39;Lisa&#39;, gender: &#39;Female&#39;, age: &#39;35&#39; }]);<br>  const [searchTerm, setSearchTerm] = useState(&#39;&#39;);<br>  const [currentPage, setCurrentPage] = useState(1);<br><br>  const itemsPerPage = 5;<br><br>  // Derived state for filtering and pagination<br>  const filteredData = list.filter(item =&gt;<br>    item.name.toLowerCase().includes(searchTerm.toLowerCase())<br>  );<br>  const paginatedList = filteredData.slice(<br>    (currentPage - 1) * itemsPerPage,<br>    currentPage * itemsPerPage<br>  );<br><br>  // Reset the current page when the search term changes<br>  useEffect(() =&gt; {<br>    setCurrentPage(1);<br>  }, [searchTerm]);<br><br>  const handleInputChange = (e, isEditing = false) =&gt; {<br>    const { name, value } = e.target;<br>    if (isEditing) {<br>      setEditData(prev =&gt; ({ ...prev, [name]: value }));<br>    } else {<br>      setFormData(prev =&gt; ({ ...prev, [name]: value }));<br>    }<br>  };<br><br>  const handleAdd = () =&gt; {<br>    if (formData.name &amp;&amp; formData.gender &amp;&amp; formData.age) {<br>      const newItem = {<br>        id: Date.now(),<br>        ...formData,<br>      };<br>      setList(prevList =&gt; [...prevList, newItem]);<br>      setFormData({ name: &#39;&#39;, gender: &#39;&#39;, age: &#39;&#39; });<br>    }<br>  };<br><br>  const handleEdit = id =&gt; {<br>    setIsEditIndex(id);<br>    const item = list.find(item =&gt; item.id === id);<br>    setEditData({ name: item.name, gender: item.gender, age: item.age });<br>  };<br><br>  const handleSave = id =&gt; {<br>    setList(prevList =&gt;<br>      prevList.map(item =&gt; (item.id === id ? { ...item, ...editData } : item))<br>    );<br>    setIsEditIndex(null);<br>    setEditData({ name: &#39;&#39;, gender: &#39;&#39;, age: &#39;&#39; });<br>  };<br><br>  const handleDelete = id =&gt; {<br>    setList(prevList =&gt; prevList.filter(item =&gt; item.id !== id));<br>    if (paginatedList.length === 1 &amp;&amp; currentPage &gt; 1) {<br>      setCurrentPage(prevPage =&gt; prevPage - 1);<br>    }<br>  };<br><br>  return (<br>    &lt;div className=&quot;container&quot;&gt;<br>      &lt;div className=&quot;add-container&quot;&gt;<br>        &lt;div className=&quot;info-container&quot;&gt;<br>          &lt;input<br>            type=&quot;text&quot;<br>            placeholder=&quot;Name&quot;<br>            name=&quot;name&quot;<br>            value={formData.name}<br>            onChange={handleInputChange}<br>          /&gt;<br>          &lt;input<br>            type=&quot;text&quot;<br>            placeholder=&quot;Gender&quot;<br>            name=&quot;gender&quot;<br>            value={formData.gender}<br>            onChange={handleInputChange}<br>          /&gt;<br>          &lt;input<br>            type=&quot;text&quot;<br>            placeholder=&quot;Age&quot;<br>            name=&quot;age&quot;<br>            value={formData.age}<br>            onChange={handleInputChange}<br>          /&gt;<br>          &lt;button onClick={handleAdd}&gt;Add&lt;/button&gt;<br>        &lt;/div&gt;<br>      &lt;/div&gt;<br>      &lt;div className=&quot;search-table-container&quot;&gt;<br>        &lt;input<br>          type=&quot;text&quot;<br>          placeholder=&quot;Search&quot;<br>          className=&quot;search-name&quot;<br>          onChange={e =&gt; setSearchTerm(e.target.value)}<br>        /&gt;<br>        &lt;table&gt;<br>          &lt;thead&gt;<br>            &lt;tr&gt;<br>              &lt;th&gt;Name&lt;/th&gt;<br>              &lt;th&gt;Gender&lt;/th&gt;<br>              &lt;th&gt;Age&lt;/th&gt;<br>              &lt;th&gt;Action&lt;/th&gt;<br>            &lt;/tr&gt;<br>          &lt;/thead&gt;<br>          &lt;tbody&gt;<br>            {paginatedList.map(item =&gt;<br>              isEditIndex === item.id ? (<br>                &lt;tr key={item.id}&gt;<br>                  &lt;td&gt;<br>                    &lt;input<br>                      type=&quot;text&quot;<br>                      name=&quot;name&quot;<br>                      value={editData.name}<br>                      onChange={e =&gt; handleInputChange(e, true)}<br>                    /&gt;<br>                  &lt;/td&gt;<br>                  &lt;td&gt;<br>                    &lt;input<br>                      type=&quot;text&quot;<br>                      name=&quot;gender&quot;<br>                      value={editData.gender}<br>                      onChange={e =&gt; handleInputChange(e, true)}<br>                    /&gt;<br>                  &lt;/td&gt;<br>                  &lt;td&gt;<br>                    &lt;input<br>                      type=&quot;text&quot;<br>                      name=&quot;age&quot;<br>                      value={editData.age}<br>                      onChange={e =&gt; handleInputChange(e, true)}<br>                    /&gt;<br>                  &lt;/td&gt;<br>                  &lt;td className=&quot;actions&quot;&gt;<br>                    &lt;button className=&quot;save&quot; onClick={() =&gt; handleSave(item.id)}&gt;<br>                      Save<br>                    &lt;/button&gt;<br>                    &lt;button className=&quot;cancel&quot; onClick={() =&gt; setIsEditIndex(null)}&gt;<br>                      Cancel<br>                    &lt;/button&gt;<br>                  &lt;/td&gt;<br>                &lt;/tr&gt;<br>              ) : (<br>                &lt;tr key={item.id}&gt;<br>                  &lt;td&gt;{item.name}&lt;/td&gt;<br>                  &lt;td&gt;{item.gender}&lt;/td&gt;<br>                  &lt;td&gt;{item.age}&lt;/td&gt;<br>                  &lt;td className=&quot;actions&quot;&gt;<br>                    &lt;button className=&quot;edit&quot; onClick={() =&gt; handleEdit(item.id)}&gt;<br>                      Edit<br>                    &lt;/button&gt;<br>                    &lt;button className=&quot;delete&quot; onClick={() =&gt; handleDelete(item.id)}&gt;<br>                      Delete<br>                    &lt;/button&gt;<br>                  &lt;/td&gt;<br>                &lt;/tr&gt;<br>              )<br>            )}<br>          &lt;/tbody&gt;<br>        &lt;/table&gt;<br>        &lt;div className=&quot;pagination&quot;&gt;<br>          {Array.from({ length: Math.ceil(filteredData.length / itemsPerPage) }).map(<br>            (_, index) =&gt; (<br>              &lt;button<br>                key={index + 1}<br>                onClick={() =&gt; setCurrentPage(index + 1)}<br>                style={{<br>                  backgroundColor: currentPage === index + 1 ? &#39;lightgreen&#39; : &#39;&#39;,<br>                }}<br>              &gt;<br>                {index + 1}<br>              &lt;/button&gt;<br>            )<br>          )}<br>        &lt;/div&gt;<br>      &lt;/div&gt;<br>    &lt;/div&gt;<br>  );<br>};</pre><pre>/* Container styling */<br>.container {<br>  max-width: 800px;<br>  margin: 0 auto;<br>  padding: 20px;<br>  font-family: Arial, sans-serif;<br>  background-color: #f9f9f9;<br>  border-radius: 8px;<br>  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);<br>}<br><br>/* Add container styling */<br>.add-container {<br>  margin-bottom: 20px;<br>}<br><br>.info-container {<br>  display: flex;<br>  gap: 10px;<br>  margin-bottom: 10px;<br>}<br><br>.info-container input {<br>  padding: 8px;<br>  font-size: 14px;<br>  border: 1px solid #ccc;<br>  border-radius: 4px;<br>  flex: 1;<br>}<br><br>.info-container input:focus {<br>  border-color: #007bff;<br>  outline: none;<br>}<br><br>.add {<br>  padding: 8px 16px;<br>  font-size: 14px;<br>  background-color: #007bff;<br>  color: white;<br>  border: none;<br>  border-radius: 4px;<br>  cursor: pointer;<br>}<br><br>.add:hover {<br>  background-color: #0056b3;<br>}<br><br>/* Search and table container styling */<br>.search-table-container {<br>  margin-top: 20px;<br>}<br><br>.search-name {<br>  width: 100%;<br>  padding: 8px;<br>  font-size: 14px;<br>  margin-bottom: 10px;<br>  border: 1px solid #ccc;<br>  border-radius: 4px;<br>}<br><br>.search-name:focus {<br>  border-color: #007bff;<br>  outline: none;<br>}<br><br>table {<br>  width: 100%;<br>  border-collapse: collapse;<br>  margin-bottom: 20px;<br>}<br><br>table thead {<br>  background-color: #007bff;<br>  color: white;<br>}<br><br>table th,<br>table td {<br>  padding: 12px;<br>  text-align: left;<br>  border-bottom: 1px solid #ddd;<br>}<br><br>table tbody tr:nth-child(even) {<br>  background-color: #f2f2f2;<br>}<br><br>.actions button {<br>  padding: 6px 12px;<br>  font-size: 12px;<br>  border: none;<br>  border-radius: 4px;<br>  cursor: pointer;<br>  margin-right: 5px;<br>}<br><br>.edit {<br>  background-color: #28a745;<br>  color: white;<br>}<br><br>.edit:hover {<br>  background-color: #218838;<br>}<br><br>.delete {<br>  background-color: #dc3545;<br>  color: white;<br>}<br><br>.delete:hover {<br>  background-color: #c82333;<br>}<br><br>.save {<br>  background-color: #007bff;<br>  color: white;<br>}<br><br>.save:hover {<br>  background-color: #0056b3;<br>}<br><br>.cancel {<br>  background-color: #6c757d;<br>  color: white;<br>}<br><br>.cancel:hover {<br>  background-color: #5a6268;<br>}<br><br>/* Pagination styling */<br>.pagination {<br>  display: flex;<br>  justify-content: center;<br>  gap: 5px;<br>}<br><br>.pagination button {<br>  padding: 8px 12px;<br>  font-size: 14px;<br>  border: 1px solid #007bff;<br>  border-radius: 4px;<br>  background-color: white;<br>  cursor: pointer;<br>}<br><br>.pagination button:hover {<br>  background-color: #007bff;<br>  color: white;<br>}<br><br>.pagination button[style*=&quot;background-color: lightgreen&quot;] {<br>  background-color: lightgreen;<br>  color: black;<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c4c05a71b918" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Interview Question: Add Nested Comments]]></title>
            <link>https://medium.com/@pranitapanigrahi50/question-add-nested-comments-849833033389?source=rss-765adebaf43f------2</link>
            <guid isPermaLink="false">https://medium.com/p/849833033389</guid>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Pranita Panigrahi]]></dc:creator>
            <pubDate>Tue, 27 Aug 2024 14:39:36 GMT</pubDate>
            <atom:updated>2024-08-27T14:44:07.055Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/730/1*GdzHIujbr43UrYrqGpGF9w.png" /><figcaption>Nested Comments illustration</figcaption></figure><h3>Dynamically Add Nested Comments in a React Application.</h3><p>Given this scenario, how we can add dynamically nested comments to the existing comment structure and ensure that the latest reply appears immediately under the parent comment</p><pre>// Type for an individual comment<br>export type Comment = {<br>  id: number;<br>  parentId: number | null;<br>  value: string;<br>  children: number[];<br>};<br><br>// Type for the entire comment structure with comments indexed by their IDs<br>export type CommentsById = {<br>  byId: {<br>    [key: string]: Comment;<br>  };<br>  allIds: string[];<br>};</pre><pre><br>const initialComments: CommentsById = {<br>  byId: {<br>    &#39;1&#39;: { id: 1, parentId: null, value: &#39;Hello Folks, How you doing?&#39;, children: [2, 3] },<br>    &#39;2&#39;: { id: 2, parentId: 1, value: &#39;Doing Great&#39;, children: [] },<br>    &#39;3&#39;: { id: 3, parentId: 1, value: &#39;Kaat rahe hai zindagani&#39;, children: [4] },<br>    &#39;4&#39;: { id: 4, parentId: 3, value: &quot;What&#39;s wrong?&quot;, children: [5] },<br>    &#39;5&#39;: { id: 5, parentId: 4, value: &#39;Nothing&#39;, children: [] },<br>    &#39;6&#39;: { id: 6, parentId: null, value: &#39;Another top-level comment&#39;, children: [7] },<br>    &#39;7&#39;: { id: 7, parentId: 6, value: &#39;This is a reply&#39;, children: [] },<br>  },<br>  allIds: [&#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;, &#39;5&#39;, &#39;6&#39;, &#39;7&#39;],<br>};</pre><pre>import React from &#39;react&#39;;<br>import { Comment, CommentsById } from &#39;./types&#39;;<br><br>type CommentListProps = {<br>  comments: CommentsById;<br>  handleReply: (parentId: number) =&gt; void;<br>  parentId: number | null;<br>};<br><br>const CommentList = ({ comments, handleReply, parentId }) =&gt; {<br>  return (<br>    &lt;div&gt;<br>      {comments.allIds<br>        .filter((id) =&gt; comments.byId[id].parentId === parentId)<br>        .map((id) =&gt; {<br>          const comment = comments.byId[id];<br>          return (<br>            &lt;div key={id}&gt;<br>              &lt;p&gt;{comment.value}&lt;/p&gt;<br>              &lt;button onClick={() =&gt; handleReply(comment.id)}&gt;Reply&lt;/button&gt;<br>              {comment.children.length &gt; 0 &amp;&amp; (<br>                &lt;CommentList comments={comments} handleReply={handleReply} parentId={comment.id} /&gt;<br>              )}<br>            &lt;/div&gt;<br>          );<br>        })}<br>    &lt;/div&gt;<br>  );<br>};<br><br>export default CommentList;</pre><pre>import React, { useState } from &#39;react&#39;;<br>import { Comment, CommentsById } from &#39;./types&#39;;<br>import CommentList from &#39;./CommentList&#39;;<br><br>const App = () =&gt; {<br>  const [comments, setComment] = useState&lt;CommentsById&gt;(initialComments);<br><br>  const handleReply = (targetParentId: number) =&gt; {<br>    //currently just pushing hardcoded data<br>    const newCommentId = (comments.allIds.length + 1).toString();<br>    const newComment: Comment = {<br>      id: parseInt(newCommentId),<br>      parentId: targetParentId,<br>      value: &#39;I am newest reply&#39;,<br>      children: [],<br>    };<br><br>    setComment((prev) =&gt; {<br>      const updatedComments = {<br>        ...prev.byId,<br>        [newCommentId]: newComment,<br>      };<br><br>      const updatedParent = {<br>        ...updatedComments[targetParentId.toString()],<br>        children: [parseInt(newCommentId), ...updatedComments[targetParentId.toString()].children],<br>      };<br><br>      return {<br>        byId: { ...updatedComments, [targetParentId.toString()]: updatedParent },<br>        allIds: [...prev.allIds, newCommentId],<br>      };<br>    });<br>  };<br><br>  return (<br>    &lt;div&gt;<br>      &lt;CommentList comments={comments} handleReply={handleReply} parentId={null} /&gt;<br>    &lt;/div&gt;<br>  );<br>};<br><br>export default App;</pre><p><strong>There are other methods and strategies that can be applied to solve this problem, depending on the specific requirements and complexity of your application. This example serves as a basic starting point, but there are many other approaches you could explore.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=849833033389" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode : Trapping rain water : Two pointer pattern]]></title>
            <link>https://medium.com/@pranitapanigrahi50/leetcode-trapping-rain-water-two-pointer-pattern-06bd1af3e0a3?source=rss-765adebaf43f------2</link>
            <guid isPermaLink="false">https://medium.com/p/06bd1af3e0a3</guid>
            <category><![CDATA[two-pointers]]></category>
            <category><![CDATA[100daysofcode]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[ds-algo]]></category>
            <category><![CDATA[leetcode]]></category>
            <dc:creator><![CDATA[Pranita Panigrahi]]></dc:creator>
            <pubDate>Fri, 14 Jun 2024 01:07:15 GMT</pubDate>
            <atom:updated>2024-06-17T04:15:10.954Z</atom:updated>
            <content:encoded><![CDATA[<p>Leetcode : Trapping rain water : Two pointer pattern</p><p><a href="https://leetcode.com/problems/trapping-rain-water">https://leetcode.com/problems/trapping-rain-water</a></p><p>An elevation map where the width of each bar is 1, compute how much water it can trap after raining.</p><p>Lets start by understanding the basic. Water can be only trapped when there is support from both the sides of a bar. The first element and last element wont be able to trap any water. Given an element in the array we have to find the highest bar in both the sides . The height will be determined by the minimum of the two highest bars . Then we can deduct the height of the elevation map from the min height .</p><pre>Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]<br>Output: 6</pre><pre>let trap = function(height) {<br>    let trapped_water = 0;<br>    let left_max_arr = [];<br>    let right_max_arr = [];<br><br>    if (height.length === 0) {<br>        return trapped_water;<br>    }<br><br>    left_max_arr[0] = height[0];<br>    for (let i = 1; i &lt; height.length; i++) {<br>        left_max_arr[i] = Math.max(left_max_arr[i - 1], height[i]);<br>    }<br><br>    right_max_arr[height.length - 1] = height[height.length - 1];<br>    for (let i = height.length - 2; i &gt;= 0; i--) {<br>        right_max_arr[i] = Math.max(right_max_arr[i + 1], height[i]);<br>    }<br><br>    for (let i = 0; i &lt; height.length; i++) {<br>        trapped_water += Math.min(left_max_arr[i], right_max_arr[i]) - height[i];<br>    }<br><br>    return trapped_water;<br>};</pre><p>Time-complexity is O(n)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=06bd1af3e0a3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[LeetCode : Container with most water — Two Pointer Pattern]]></title>
            <link>https://medium.com/@pranitapanigrahi50/leetcode-container-with-most-water-two-pointer-pattern-1aacf340c83e?source=rss-765adebaf43f------2</link>
            <guid isPermaLink="false">https://medium.com/p/1aacf340c83e</guid>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[100daysofcode]]></category>
            <category><![CDATA[ds-algo]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Pranita Panigrahi]]></dc:creator>
            <pubDate>Thu, 13 Jun 2024 04:03:03 GMT</pubDate>
            <atom:updated>2024-06-13T04:03:03.773Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>LeetCode : Container with most water — Two Pointer Pattern</strong></p><p><a href="https://leetcode.com/problems/container-with-most-water/description/">https://leetcode.com/problems/container-with-most-water/description/</a></p><p><em>Brute force approach</em></p><p>We have to find two lines that together with the x-axis form a container, such that the container contains the most water. Lets have two loops running through the entire array . First loop will fix an element and second loop will run till end . We will find the area (height x width) of the container that will be created with first and second element from the respective loops. We will have a max_water variable that will always hold the maximum area(water) in each iteration . <br>Time complexity is O(n²)<br>Now lets code it .</p><pre>Input: height = [1,8,6,2,5,4,8,3,7]<br>Output: 49</pre><pre>var maxArea = function (height) {<br>  // Brute Force<br>  let max_water = 0;<br>  let area = 0;<br><br>  for (let i = 0; i &lt; height.length - 1; i++) {<br>    for (let j = i + 1; j &lt; height.length; j++) {<br>      let cur_height = Math.min(height[i], height[j]);<br>      let width = j - i;<br>      area = cur_height * width;<br>      max_water = Math.max(max_water, area);<br>    }<br>  }<br>  return max_water;<br>};</pre><p><em>Two pointer</em></p><p>Now lets come to the optimised version of the code . Lets not run two loops. Have two pointers pointing to first and last element in the array. The height will be determined by the smaller element. Based on the smaller element we can increment the pointer . The width can be calculated based <br>on the indexes.<br>Time complexity is O(n)<br>Space complexity is O(1)</p><pre>var maxArea = function(height) {<br>    let max_water = 0;<br>    let area = 0;<br><br>    let i = 0;<br>    let j = height.length - 1;<br><br>    while (i &lt; j) {<br>        area = Math.min(height[i], height[j]) * (j - i);<br>        max_water = Math.max(max_water, area);<br><br>        if (height[i] &lt; height[j]) {<br>            i++;<br>        } else {<br>            j--;<br>        }<br>    }<br><br>    return max_water;<br>};</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1aacf340c83e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode problem : longest-consecutive-sequence]]></title>
            <link>https://medium.com/@pranitapanigrahi50/100-days-leetcode-challenge-e6ec7b36715f?source=rss-765adebaf43f------2</link>
            <guid isPermaLink="false">https://medium.com/p/e6ec7b36715f</guid>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[100daysofcode]]></category>
            <dc:creator><![CDATA[Pranita Panigrahi]]></dc:creator>
            <pubDate>Sat, 11 May 2024 01:54:50 GMT</pubDate>
            <atom:updated>2024-06-06T16:13:45.423Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://leetcode.com/problems/longest-consecutive-sequence">https://leetcode.com/problems/longest-consecutive-sequence</a></p><p>First lets approach this problem with Brute Force Solution.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f38565b83c1763d6d4b6ac885d6de41e/href">https://medium.com/media/f38565b83c1763d6d4b6ac885d6de41e/href</a></iframe><p>This is basically taking O(n²) to run the loop. Lets make it better by using set data structure.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6e874ac8f4b61784094ae824d9801a4e/href">https://medium.com/media/6e874ac8f4b61784094ae824d9801a4e/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e6ec7b36715f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JavaScript : What is “this”?]]></title>
            <link>https://medium.com/@pranitapanigrahi50/javascript-what-is-this-d07cc53167ca?source=rss-765adebaf43f------2</link>
            <guid isPermaLink="false">https://medium.com/p/d07cc53167ca</guid>
            <category><![CDATA[this]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[frontend]]></category>
            <dc:creator><![CDATA[Pranita Panigrahi]]></dc:creator>
            <pubDate>Tue, 09 Apr 2024 18:08:38 GMT</pubDate>
            <atom:updated>2024-04-09T18:09:12.159Z</atom:updated>
            <content:encoded><![CDATA[<h3>JavaScript : What is “this”?</h3><blockquote><strong><em>During interviews, frontend developers are commonly asked about this keyword.</em></strong></blockquote><h4><strong><em>&#39;this&#39; is the context of a function invocation.</em></strong></h4><p><strong>Global Scope </strong>: Open your console and just type <strong><em>this or </em></strong><em>define a </em><strong><em>normal function</em></strong> . You will see <strong><em>this</em> </strong>is referring to the<strong><em> window object </em>.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DJfyK1qu6mK69psInUAN_g.png" /><figcaption><strong>In global scope, ‘this’ refers to the window object.</strong></figcaption></figure><p><strong>Function Scope</strong> : We will talk about 4 function invocation types:</p><ul><li><strong><em>function invocation</em></strong></li><li><strong><em>method invocation</em></strong></li><li><strong><em>constructor invocation</em></strong></li><li><strong><em>indirect invocation</em></strong></li></ul><p>Each invocation type defines the context in its way, so this behaves differently than the developer expects.</p><h4><strong><em>function invocation</em></strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/704/1*qtdCJuk9P6wFNszvhx9xQA.png" /><figcaption>At the time mul(5, 6) is called, JavaScript automatically sets this as the global object (window in a browser)</figcaption></figure><h4>this in an inner function</h4><p>⚠️ A common trap with the function invocation is thinking that this is the same in an inner function as in the outer function.</p><p>👍 The context of the inner function (except arrow function) depends only on its own invocation type, but not on the outer function’s context.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/1*RD84uzwX3ls4EP6rvd03Eg.png" /><figcaption>this is pointing to the global object inside calculate function</figcaption></figure><p>To solve the problem, calculate() function must execute with the same context as the numbers.sum() method, to access this.num1 and this.num2 properties.</p><p>One solution is to change manually the context of calculate() to the desired one by calling calculate.call(this) or else we can make use of an arrow function .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/762/1*EDPqD709rU1bfKBF5zFzyQ.png" /><figcaption>arrow function uses this value of numbers.sum() method</figcaption></figure><h4>method invocation</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/648/1*8W4P8ytmQ77V2T8iawahnA.png" /></figure><p>When invoking a method on an object, this is the object that owns the method.</p><p>Let’s create an object with a method that increments a number:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/736/1*Y6lrtvzmQkVo1Bjgll4EcQ.png" /></figure><p>Calling calc.increment() makes the context of increment function to be calc object. So using this.num to increment the number property works well.</p><p>A method can be extracted from an object into a separated variable const alone = myObj.myMethod. When the method is called alone alone(), detached from the original object, you might think that this is the object myObject on which the method was defined.</p><p>Correctly if the method is called without an object, then a function invocation happens, where this is the global object window</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/988/1*un7pviHAA0E1n_8-3UCwdQ.png" /><figcaption>Unfortunately the method is separated from its object when passed as a parameter: setTimeout(pet.logInfo)</figcaption></figure><p>setTimeout(pet.logInfo);</p><p><em>// is equivalent to:</em></p><p>const extractedLogInfo = pet.logInfo;</p><p>setTimeout(extractedLogInfo);</p><p>The above issue can be solved using bind function</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/852/1*FiHvPN4PNrghgkZPgDNcUA.png" /></figure><h4>constructor invocation</h4><p>The context of a constructor invocation is the newly created object. The constructor initializes the object with data that comes from constructor arguments, sets up initial values for properties, attaches event handlers, etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/964/1*b4-XvuGwXl04dGhEDxJtGA.png" /></figure><h4>indirect invocation</h4><p>this in indirect invocation is the value passed as first argument to .call() or .apply().</p><p>this is the first argument of myFunc.bind(thisArg) when invoking a bound function</p><p>myFunction.call(thisArg, arg1, arg2, ...) accepts the first argument thisArg as the context of the invocation and a list of arguments arg1, args2, ... that are passed as arguments to the called function.</p><p>myFunction.apply(thisArg, [arg1, arg2, ...]) accepts the first argument thisArg as the context of the invocation and an array of arguments [arg1, args, ...] that are passed as arguments to the called function.</p><p>myFunc.bind(thisArg[, arg1, arg2, ...) accepts the first argument thisArg as the context and an optional list of arguments arg1, arg2, ... to bound to. .bind() returns a new function which context is bound to thisArg and arguments to arg1, arg2, ....</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/664/1*BsTfSyDL3HShSiaKRYQQ3Q.png" /></figure><p><strong>Note : Call, Apply and Bind doesnot work with arrow functions.</strong></p><p>Thank you for reading :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d07cc53167ca" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JavaScript __proto__ : Explained in a Simple Way]]></title>
            <link>https://medium.com/@pranitapanigrahi50/javascript-proto-explained-in-a-simple-way-77e94b2104dc?source=rss-765adebaf43f------2</link>
            <guid isPermaLink="false">https://medium.com/p/77e94b2104dc</guid>
            <category><![CDATA[beginners-guide]]></category>
            <category><![CDATA[proto]]></category>
            <category><![CDATA[interview]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Pranita Panigrahi]]></dc:creator>
            <pubDate>Mon, 08 Apr 2024 14:52:39 GMT</pubDate>
            <atom:updated>2024-04-09T02:06:03.645Z</atom:updated>
            <content:encoded><![CDATA[<h3>JavaScript __proto__ : Explained in a Simple Way</h3><p>Okay, here’s how I would respond if someone in a JavaScript interview asked me “Can you please tell me what <strong>__proto__</strong> is all about?”</p><p>Lets talk about prototypes. <br><strong><em>let a = { name : ‘pranita’ };</em><br><em>let b = { age : 19 };</em></strong></p><p>Here <strong><em>b</em></strong><em> </em>points at an object without <strong><em>name</em></strong> property . If we will try to log the value of<strong> <em>b.name</em></strong> , it will give undefined as the <strong><em>name</em></strong> property does not exist on<em> </em><strong><em>b </em></strong>object . <br><strong>console.log(b.name); // undefined</strong></p><p>Instead of the default behavior of returning <strong>undefined</strong>, we can instruct JavaScript to <strong><em>continue searching for our missing property on another object</em></strong>. We can do it with one line of code:</p><p><strong><em>let b = { __proto__ : a , age : 19 };</em></strong></p><p>The <strong>__proto__</strong> property establishes a connection between objects b and a. If a property is not found in object b, JavaScript then searches for it in object a, effectively allowing for a fallback mechanism where properties can be accessed across linked objects.</p><p>But how ? <br>This is possible because of the<strong><em> prototype chain.</em></strong></p><p><strong>A prototype is like a relationship . An object may point at another object as its prototype. Now we can think </strong>: what if my object’s prototype has its own prototype? Would that work?</p><p><strong><em>let human = { sleep : true };</em></strong></p><p><strong><em>let girl = { __proto__ : human , age : 19 } ;</em></strong></p><p><strong><em>let rosy = { __proto__ : girl };</em></strong></p><p><strong><em>console.log(rosy.sleep) // true<br>console.log(rosy.age) // 19</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/850/1*jng3cpLw4wIUKD7tr5jvYw.png" /></figure><p>When we <em>read</em> a property that doesn’t exist on our object, then we’ll keep looking for it on the prototype chain. If we don’t find it, we get undefined.</p><p><strong><em>The Object Prototype</em></strong></p><p>Lets define a new object . Try running this in your browser’s console:</p><pre>let obj = {};<br>console.log(obj.__proto__); </pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OJ0422kWzZfaZrgbE2jxcQ.png" /></figure><p>When we create an empty object ,it has a hidden __proto__ wire that points at the Object Prototype by default.</p><p>We can produce an object that doesn’t have a prototype, at all. As a result, it doesn’t even have built-in object methods:<br>let person1 = { __proto__: null };<br> let person2 = Object.create(null)</p><p>You may have another doubt . What is a prototype ?</p><p><em>Don&#39;t confuse Prototype with __proto __ .</em></p><p>The prototype property is only present for functions and is a property that’s set only if you’re using the ‘new’ keyword when creating objects with constructor function.</p><p>function Person() { }<br>Person.prototype.greet = function() { console.log(‘Hello’)}</p><p><strong><em>If you are still confused !!!!!</em></strong></p><p><strong><em>Open the console and start coding . Try small examples and see how things are working .</em></strong></p><p>This is my first article . Hope it helps you :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=77e94b2104dc" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>