<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Zeyad Eissa</title>
    <description>The latest articles on DEV Community by Zeyad Eissa (@zeyadeissa).</description>
    <link>https://dev.to/zeyadeissa</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%2F3303026%2Fb5d0375a-57a8-424c-b0e4-adbb565b1e1d.png</url>
      <title>DEV Community: Zeyad Eissa</title>
      <link>https://dev.to/zeyadeissa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zeyadeissa"/>
    <language>en</language>
    <item>
      <title>Kadane's Algorithm</title>
      <dc:creator>Zeyad Eissa</dc:creator>
      <pubDate>Fri, 26 Sep 2025 18:55:26 +0000</pubDate>
      <link>https://dev.to/zeyadeissa/kadanes-algorithm-3om7</link>
      <guid>https://dev.to/zeyadeissa/kadanes-algorithm-3om7</guid>
      <description>&lt;p&gt;Kadane's Algorithm – Efficient Maximum Subarray Sum&lt;br&gt;
 The Problem&lt;/p&gt;

&lt;p&gt;Imagine you're playing a game where you score positive or negative points each round.&lt;br&gt;
You want to find the maximum total score you can get by choosing a sequence of consecutive rounds.&lt;/p&gt;

&lt;p&gt;This is known as the:&lt;/p&gt;

&lt;p&gt;Maximum Subarray Problem&lt;/p&gt;

&lt;p&gt;Naive (Brute Force) Solution&lt;/p&gt;

&lt;p&gt;You could solve it by checking all possible subarrays and summing each one.&lt;br&gt;
Example:&lt;/p&gt;

&lt;p&gt;[2, -3, 6]&lt;/p&gt;

&lt;p&gt;All possible consecutive subarrays:&lt;/p&gt;

&lt;p&gt;Subarray    Sum&lt;br&gt;
[2] 2&lt;br&gt;
[2, -3] -1&lt;br&gt;
[2, -3, 6]  5&lt;br&gt;
[-3]    -3&lt;br&gt;
[-3, 6] 3&lt;br&gt;
[6] 6 ✅&lt;/p&gt;

&lt;p&gt;The maximum sum is 6.&lt;/p&gt;

&lt;p&gt;But this method becomes very slow for long arrays → O(n²) time complexity.&lt;/p&gt;

&lt;p&gt;Kadane’s Algorithm (Efficient O(n) Solution)&lt;/p&gt;

&lt;p&gt;Kadane’s Algorithm solves this problem in linear time → O(n)&lt;br&gt;
It's based on Dynamic Programming, but it’s extremely space-efficient (uses only two variables).&lt;/p&gt;

&lt;p&gt;Key Concepts&lt;/p&gt;

&lt;p&gt;We use two variables:&lt;/p&gt;

&lt;p&gt;Variable    Purpose&lt;br&gt;
currentSum  The running sum of the current subarray (candidate for max subarray)&lt;br&gt;
maxSum  The best (maximum) sum found so far&lt;/p&gt;

&lt;p&gt;We iterate through the array once, updating these two values at each step.&lt;/p&gt;

&lt;p&gt;Algorithm Logic&lt;/p&gt;

&lt;p&gt;At each item x in the array:&lt;/p&gt;

&lt;p&gt;Add x to currentSum → means we’re trying to extend the current subarray.&lt;/p&gt;

&lt;p&gt;If currentSum &amp;gt; maxSum, update maxSum.&lt;/p&gt;

&lt;p&gt;If currentSum &amp;lt; 0, reset currentSum to 0 → we start fresh, because a negative sum will hurt any future subarray.&lt;/p&gt;

&lt;p&gt;JavaScript Implementation&lt;/p&gt;

&lt;p&gt;function maxSubArray(arr) {&lt;br&gt;
  let currentSum = 0;&lt;br&gt;
  let maxSum = -Infinity; // works even if all values are negative&lt;br&gt;
  for (let x of arr) {&lt;br&gt;
    currentSum += x;&lt;br&gt;
    if (currentSum &amp;gt; maxSum) {&lt;br&gt;
      maxSum = currentSum;&lt;br&gt;
    }&lt;br&gt;
    if (currentSum &amp;lt; 0) {&lt;br&gt;
      currentSum = 0;&lt;br&gt;
    }&lt;br&gt;
  }&lt;br&gt;
  return maxSum;&lt;br&gt;
}&lt;br&gt;
Step-by-step Example&lt;/p&gt;

&lt;p&gt;Let’s walk through the array:&lt;/p&gt;

&lt;p&gt;const arr = [2, -1, 3, -4, 5, -2, 2];&lt;br&gt;
Index   Value   currentSum Before   currentSum After    maxSum&lt;br&gt;
0   2   0   2   2&lt;br&gt;
1   -1  2   1   2&lt;br&gt;
2   3   1   4   4 ✅&lt;br&gt;
3   -4  4   0 (reset)   4&lt;br&gt;
4   5   0   5   5 ✅&lt;br&gt;
5   -2  5   3   5&lt;br&gt;
6   2   3   5   5&lt;/p&gt;

&lt;p&gt;Final result: maxSum = 5&lt;/p&gt;

&lt;p&gt;Special Case: All Negative Numbers&lt;/p&gt;

&lt;p&gt;If the array has only negative numbers, Kadane’s algorithm still works because we initialize:&lt;/p&gt;

&lt;p&gt;let maxSum = -Infinity;&lt;/p&gt;

&lt;p&gt;So it will still return the maximum negative value instead of returning 0.&lt;/p&gt;

&lt;p&gt; Summary&lt;/p&gt;

&lt;p&gt;Kadane’s algorithm solves the Maximum Subarray Problem in O(n) time and O(1) space.&lt;/p&gt;

&lt;p&gt;It uses only two variables: currentSum and maxSum.&lt;/p&gt;

&lt;p&gt;You reset currentSum to 0 when it becomes negative, because any negative prefix will reduce your future total.&lt;/p&gt;

&lt;p&gt;Kadane’s Algorithm is a clean, fast, and optimal way to find the largest sum of a contiguous subarray.&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>maximum</category>
      <category>subarray</category>
    </item>
    <item>
      <title>Caching in Node.js with TypeScript using globalThis and declare global</title>
      <dc:creator>Zeyad Eissa</dc:creator>
      <pubDate>Fri, 26 Sep 2025 17:07:24 +0000</pubDate>
      <link>https://dev.to/zeyadeissa/caching-in-nodejs-with-typescript-using-globalthis-and-declare-global-12e2</link>
      <guid>https://dev.to/zeyadeissa/caching-in-nodejs-with-typescript-using-globalthis-and-declare-global-12e2</guid>
      <description>&lt;p&gt;When working in a Node.js environment, especially with TypeScript, you might come across situations where you need a simple caching mechanism — something lightweight to store a variable that stays alive as long as the server is running.&lt;/p&gt;

&lt;p&gt;One of the most efficient and straightforward ways to achieve this is by using globalThis and declare global.&lt;/p&gt;

&lt;p&gt;Let me walk you through how and why I used this approach in a real-world scenario, and break down the concepts from start to finish.&lt;/p&gt;

&lt;p&gt;The Problem I Faced&lt;br&gt;
In one of my projects, I was managing bookings for workshops. Each booking record included a vehicle_id, which pointed to a vehicle stored in another system.&lt;/p&gt;

&lt;p&gt;We had a third-party system that needed to fetch booking data along with vehicle details in the same API call.&lt;/p&gt;

&lt;p&gt;To support this, I created a hook in Directus that runs before every bookings fetch operation. This hook was responsible for:&lt;/p&gt;

&lt;p&gt;Detecting if the vehicle_id exists.&lt;/p&gt;

&lt;p&gt;Fetching the full vehicle data from the external system.&lt;/p&gt;

&lt;p&gt;Attaching the vehicle object to the booking result.&lt;/p&gt;

&lt;p&gt;The Real Challenge: Avoiding Duplicate API Calls&lt;br&gt;
I didn’t want to fetch the same vehicle data multiple times, especially since vehicle data doesn't change frequently. That would waste time and network resources.&lt;/p&gt;

&lt;p&gt;So I needed a simple caching layer to store the vehicle data after the first fetch, and reuse it for future requests — as long as the server is still running.&lt;/p&gt;

&lt;p&gt;What is globalThis?&lt;br&gt;
In JavaScript (and TypeScript), globalThis is an object that gives you access to the global scope, no matter the environment (Node.js, browser, etc).&lt;/p&gt;

&lt;p&gt;That means any variable you attach to globalThis will be accessible from anywhere in your code, and will live as long as the server process is running.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;globalThis.myAppName = "DriveMotive"; console.log(globalThis.myAppName); // Output: DriveMotive&lt;br&gt;
Using globalThis with TypeScript&lt;br&gt;
In a TypeScript project, if you try to write:&lt;/p&gt;

&lt;p&gt;globalThis.__vehicleCache = new Map();&lt;br&gt;
You’ll get a TypeScript error:&lt;/p&gt;

&lt;p&gt;❌ Property '__vehicleCache' does not exist on type 'typeof globalThis'.&lt;/p&gt;

&lt;p&gt;That’s because TypeScript is type-safe and doesn’t know about this property unless you explicitly tell it.&lt;/p&gt;

&lt;p&gt;How to Declare a Global Variable in TypeScript&lt;br&gt;
Step 1: Let TypeScript know this is a module&lt;br&gt;
At the top of the file (usually types.d.ts), add:&lt;/p&gt;

&lt;p&gt;export {}; // This makes the file a module&lt;br&gt;
Step 2: Declare the global variable&lt;/p&gt;

&lt;p&gt;declare global { var __vehicleCache: Map | undefined; }&lt;br&gt;
This tells TypeScript:&lt;/p&gt;

&lt;p&gt;"Hey, there is a global variable called __vehicleCache, and it's either a Map or undefined."&lt;/p&gt;

&lt;p&gt;Step 3: Initialize the Cache&lt;br&gt;
In your actual logic, you need to initialize the cache if it's not already created:&lt;/p&gt;

&lt;p&gt;const vehicleCache = (globalThis.__vehicleCache ??= new Map());&lt;br&gt;
This single line does three things:&lt;/p&gt;

&lt;p&gt;Checks if __vehicleCache already exists in globalThis&lt;/p&gt;

&lt;p&gt;If yes, assigns it to vehicleCache&lt;/p&gt;

&lt;p&gt;If not, creates a new Map() and assigns it&lt;/p&gt;

&lt;p&gt;I Hope This Post was Helpful, See you in the Next One.&lt;/p&gt;

</description>
      <category>node</category>
      <category>typescript</category>
      <category>caching</category>
    </item>
    <item>
      <title>JavaScript Quick Tips</title>
      <dc:creator>Zeyad Eissa</dc:creator>
      <pubDate>Mon, 22 Sep 2025 14:18:51 +0000</pubDate>
      <link>https://dev.to/zeyadeissa/javascript-quick-tips-4df5</link>
      <guid>https://dev.to/zeyadeissa/javascript-quick-tips-4df5</guid>
      <description>&lt;p&gt;Var vs Let and Const&lt;/p&gt;

&lt;p&gt;Consider the following code:&lt;br&gt;
&lt;code&gt;&lt;br&gt;
function sayHi() {&lt;br&gt;
console.log(name);&lt;br&gt;
console.log(age);&lt;br&gt;
var name = "Alex";&lt;br&gt;
let age = 24; }&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
If you run this function, the output will be:&lt;/p&gt;

&lt;p&gt;Consider the following code:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;function sayHi() {&lt;br&gt;
console.log(name);&lt;br&gt;
console.log(age);&lt;br&gt;
var name = "Alex";&lt;br&gt;
let age = 24; }&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you run this function, the output will be:&lt;br&gt;
undefined&lt;br&gt;
ReferenceError&lt;br&gt;
Why does this happen?&lt;/p&gt;

&lt;p&gt;Variables declared with var are hoisted to the top of their scope during the compilation phase.&lt;br&gt;
Hoisting means JavaScript creates a memory space for the variable before the code executes, but it does not assign a value yet. Instead, the variable is initialized with undefined.&lt;br&gt;
That’s why when we log name before its assignment, we see undefined instead of an error.&lt;/p&gt;

&lt;p&gt;Variables declared with let and const are also hoisted, but the key difference is that they are not initialized automatically.&lt;br&gt;
They remain in a special state called the Temporal Dead Zone (TDZ) from the start of their scope until the line where they are actually assigned a value.&lt;br&gt;
If you try to access them in this zone (before initialization), JavaScript throws a ReferenceError.&lt;/p&gt;

&lt;p&gt;In short:&lt;/p&gt;

&lt;p&gt;var → hoisted and initialized with undefined immediately.&lt;/p&gt;

&lt;p&gt;let &amp;amp; const → hoisted but stay uninitialized (TDZ) until the code execution reaches their declaration.&lt;br&gt;
Arrow Functions vs Regular Functions in JavaScript&lt;/p&gt;

&lt;p&gt;Let’s look at this code example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const square = {&lt;br&gt;
sideOfSquare: 10,&lt;br&gt;
// Regular function area() {&lt;br&gt;
  return this.sideOfSquare * this.sideOfSquare;&lt;br&gt;
},&lt;br&gt;
// Arrow function&lt;br&gt;
perimeter: () =&amp;gt; this.sideOfSquare * 4 };&lt;br&gt;
console.log(square.area()); // 100&lt;br&gt;
console.log(square.perimeter()); // NaN&lt;/code&gt;&lt;br&gt;
Why does this happen?&lt;/p&gt;

&lt;p&gt;Regular function (area)&lt;/p&gt;

&lt;p&gt;When you use a regular function inside an object, this refers to the object itself (square).&lt;/p&gt;

&lt;p&gt;That means this.sideOfSquare correctly points to 10, so the result of area() is 100.&lt;/p&gt;

&lt;p&gt;Arrow function (perimeter)&lt;/p&gt;

&lt;p&gt;Arrow functions don’t have their own this.&lt;/p&gt;

&lt;p&gt;Instead, they use the this value from the outer scope (the place where the function was created).&lt;/p&gt;

&lt;p&gt;In this case, the arrow function is created inside the object literal, but the this it captures is not the object itself — it’s usually the global scope (window in browsers or undefined in strict mode).&lt;/p&gt;

&lt;p&gt;Since there’s no sideOfSquare in the global scope, the result is NaN.&lt;/p&gt;

&lt;p&gt;In Short:&lt;/p&gt;

&lt;p&gt;Use regular functions when you need this to refer to the object.&lt;br&gt;
Use arrow functions when you want to preserve the this from the outer scope (for example, inside callbacks).&lt;br&gt;
Dot notation vs Bracket notation&lt;/p&gt;

&lt;p&gt;Consider this code:&lt;br&gt;
&lt;code&gt;&lt;br&gt;
const bird = { size: "small" }&lt;br&gt;
const mouse = {&lt;br&gt;
 name: "Mickey",&lt;br&gt;
 small: true&lt;br&gt;
};&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
We can perform something like this:&lt;br&gt;
mouse[bird.size] → because bird.size gives us "small", which will be evaluated as mouse["small"] = true. So this is valid.&lt;/p&gt;

&lt;p&gt;Another option is:&lt;br&gt;
mouse[bird["size"]] → because the inner operation also gives "small", leading to mouse["small"].&lt;/p&gt;

&lt;p&gt;What is wrong is trying:&lt;br&gt;
mouse.bird.size → here mouse.bird is undefined, so accessing .size will throw an error (undefined.size).&lt;/p&gt;

&lt;p&gt;In short:&lt;/p&gt;

&lt;p&gt;Dot notation: The key must literally exist as a property.&lt;/p&gt;

&lt;p&gt;Bracket notation: The key can be a variable or expression that resolves to a property name.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
