<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Raja71</title>
    <description>The latest articles on Forem by Raja71 (@raja71).</description>
    <link>https://forem.com/raja71</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3735668%2F5511bc62-bbb1-4087-bfc9-78de97204f3f.png</url>
      <title>Forem: Raja71</title>
      <link>https://forem.com/raja71</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/raja71"/>
    <language>en</language>
    <item>
      <title>Rain water trapping</title>
      <dc:creator>Raja71</dc:creator>
      <pubDate>Sat, 31 Jan 2026 07:22:36 +0000</pubDate>
      <link>https://forem.com/raja71/rain-water-trapping-35b2</link>
      <guid>https://forem.com/raja71/rain-water-trapping-35b2</guid>
      <description>&lt;p&gt;To calculate how much rainwater can be trapped between bars, we use a two-pointer technique that runs in O(n) time and O(1) space.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Core Idea&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Water trapped at any index depends on:&lt;br&gt;
min(max height on left, max height on right) - current height&lt;br&gt;
Instead of precomputing arrays, we walk inward from both ends, keeping track of:&lt;/p&gt;

&lt;p&gt;leftMax → tallest bar seen from the left&lt;/p&gt;

&lt;p&gt;rightMax → tallest bar seen from the right&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const trapping = height =&amp;gt; {
let left = 0, right = height.length - 1;
let leftMax = 0, rightMax = 0, water = 0;

while (left &amp;lt; right) {
    if (height[left] &amp;lt; height[right]) {
        leftMax = Math.max(leftMax, height[left]);
        water += leftMax - height[left];
        left++;
    } else {
        rightMax = Math.max(rightMax, height[right]);
        water += rightMax - height[right];
        right--;
    }
}

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

&lt;/div&gt;



&lt;p&gt;1️⃣ &lt;strong&gt;Initialize pointers&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;left starts at beginning&lt;br&gt;
right starts at end&lt;br&gt;
leftMax and rightMax track the tallest bars seen so far&lt;br&gt;
water stores total trapped water&lt;/p&gt;

&lt;p&gt;2️⃣ &lt;strong&gt;Why compare height[left] &amp;lt; height[right]?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We always move the smaller height side because:&lt;br&gt;
The trapped water is limited by the shorter boundary&lt;br&gt;
If left bar is smaller → water depends on leftMax&lt;br&gt;
If right bar is smaller → water depends on rightMax&lt;br&gt;
This guarantees correctness without checking both sides every time.&lt;/p&gt;

&lt;p&gt;3️⃣ &lt;strong&gt;When moving left pointer&lt;/strong&gt;&lt;br&gt;
leftMax = Math.max(leftMax, height[left]);&lt;br&gt;
water += leftMax - height[left];&lt;br&gt;
left++;&lt;/p&gt;

&lt;p&gt;Update highest wall seen from left&lt;br&gt;
Add water trapped at current index&lt;br&gt;
Move inward&lt;/p&gt;

&lt;p&gt;4️⃣ &lt;strong&gt;When moving right pointer&lt;/strong&gt;&lt;br&gt;
rightMax = Math.max(rightMax, height[right]);&lt;br&gt;
water += rightMax - height[right];&lt;br&gt;
right--;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>algorithms</category>
      <category>javascript</category>
      <category>interview</category>
    </item>
    <item>
      <title>Difference Between Regular Functions and Arrow Functions</title>
      <dc:creator>Raja71</dc:creator>
      <pubDate>Tue, 27 Jan 2026 17:10:37 +0000</pubDate>
      <link>https://forem.com/raja71/difference-between-regular-functions-and-arrow-functions-4i18</link>
      <guid>https://forem.com/raja71/difference-between-regular-functions-and-arrow-functions-4i18</guid>
      <description>&lt;p&gt;“What is the difference between regular functions and arrow functions?” is one of the most commonly asked JavaScript interview questions.&lt;/p&gt;

&lt;p&gt;While both look similar, they behave very differently when it comes to this, arguments, constructors, and more. In this blog, we’ll understand these differences with easy examples and real-world scenarios.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Regular Functions&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Syntax&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Regular functions must use the function keyword.&lt;/p&gt;

&lt;p&gt;const add = function (num1, num2) {&lt;br&gt;
  return num1 + num2;&lt;br&gt;
};&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;arguments object&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Regular functions automatically get a special object called arguments.&lt;/p&gt;

&lt;p&gt;You can access all passed values even without parameters.&lt;/p&gt;

&lt;p&gt;const add = function () {&lt;br&gt;
  console.log(arguments);&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;add(1, 2, 3, 4);&lt;/p&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;{0: 1, 1: 2, 2: 3, 3: 4}&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;No implicit return&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Regular functions do NOT return values automatically.&lt;/p&gt;

&lt;p&gt;You must explicitly use return.&lt;/p&gt;

&lt;p&gt;const sum = function (a, b) {&lt;br&gt;
  a + b;   // nothing returned&lt;br&gt;
};&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;this keyword &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In regular functions, this depends on how the function is called.&lt;/p&gt;

&lt;p&gt;let learnLng = {&lt;br&gt;
  lang: "JavaScript",&lt;br&gt;
  learnLang: function () {&lt;br&gt;
    console.log("Will start learning", this.lang);&lt;br&gt;
  }&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;learnLng.learnLang();&lt;br&gt;
// Will start learning JavaScript&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Arrow Functions&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Syntax (short &amp;amp; clean)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Arrow functions are more concise.&lt;/p&gt;

&lt;p&gt;const multiply = (num1, num2) =&amp;gt; num1 * num2;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;No arguments object&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Arrow functions do NOT have their own arguments.&lt;/p&gt;

&lt;p&gt;const add = () =&amp;gt; {&lt;br&gt;
  console.log(arguments);&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;add(1, 2, 3);&lt;/p&gt;

&lt;p&gt;ReferenceError: arguments is not defined&lt;/p&gt;

&lt;p&gt;If you need arguments in arrow functions, use rest parameters:&lt;/p&gt;

&lt;p&gt;const add = (...args) =&amp;gt; {&lt;br&gt;
  console.log(args);&lt;br&gt;
};&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Implicit return&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If the function has only one statement, arrow functions return it automatically.&lt;/p&gt;

&lt;p&gt;const square = x =&amp;gt; x * x;&lt;/p&gt;

&lt;p&gt;No return needed &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;this keyword (lexical)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Arrow functions do NOT have their own this.&lt;/p&gt;

&lt;p&gt;Instead, they borrow this from where they are written, not from how they are called.&lt;/p&gt;

&lt;p&gt;let learnLng = {&lt;br&gt;
  lang: "JavaScript",&lt;br&gt;
  learnLang1: () =&amp;gt; {&lt;br&gt;
    console.log("Will start learning", this.lang);&lt;br&gt;
  },&lt;br&gt;
  learnLang2: function () {&lt;br&gt;
    console.log("Will start learning", this.lang);&lt;br&gt;
  }&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;learnLng.learnLang1(); // undefined&lt;br&gt;
learnLng.learnLang2(); // JavaScript&lt;/p&gt;

&lt;p&gt;Why does this happen?&lt;br&gt;
learnLang1 (arrow function):&lt;/p&gt;

&lt;p&gt;this comes from outer scope&lt;/p&gt;

&lt;p&gt;NOT from learnLng&lt;/p&gt;

&lt;p&gt;So this.lang is undefined&lt;/p&gt;

&lt;p&gt;learnLang2 (regular function):&lt;/p&gt;

&lt;p&gt;this points to the object calling it (learnLng)&lt;/p&gt;

&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Regular functions use the &lt;code&gt;function&lt;/code&gt; keyword; arrow functions use &lt;code&gt;=&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Regular functions have their own &lt;code&gt;this&lt;/code&gt;; arrow functions inherit &lt;code&gt;this&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Regular functions support &lt;code&gt;arguments&lt;/code&gt;; arrow functions do not&lt;/li&gt;
&lt;li&gt;Arrow functions allow implicit return for single expressions&lt;/li&gt;
&lt;li&gt;Arrow functions cannot be used as constructors&lt;/li&gt;
&lt;li&gt;Use regular functions for object methods&lt;/li&gt;
&lt;li&gt;Use arrow functions for callbacks and short functions&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>react</category>
    </item>
  </channel>
</rss>
