<?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: Shavon Harris</title>
    <description>The latest articles on DEV Community by Shavon Harris (@shavonharrisdev).</description>
    <link>https://dev.to/shavonharrisdev</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%2F867448%2F88bfb0c3-271a-4b80-a45a-dc962726e3b0.jpeg</url>
      <title>DEV Community: Shavon Harris</title>
      <link>https://dev.to/shavonharrisdev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shavonharrisdev"/>
    <language>en</language>
    <item>
      <title>Building Secure AI Apps: Defending Features, Protecting Costs and Staying Ahead of Attacks</title>
      <dc:creator>Shavon Harris</dc:creator>
      <pubDate>Wed, 09 Jul 2025 20:21:58 +0000</pubDate>
      <link>https://dev.to/shavonharrisdev/building-secure-ai-apps-defending-features-protecting-costs-and-staying-ahead-of-attacks-29a0</link>
      <guid>https://dev.to/shavonharrisdev/building-secure-ai-apps-defending-features-protecting-costs-and-staying-ahead-of-attacks-29a0</guid>
      <description>&lt;h2&gt;
  
  
  Building Secure AI‑Powered Apps
&lt;/h2&gt;

&lt;p&gt;Building secure AI‑powered apps isn’t just a check‑mark exercise.&lt;br&gt;&lt;br&gt;
It directly impacts user trust, brand reputation, and runaway API costs. Here’s what I learned when even simple features opened the door to real economic stranger dangers!&lt;/p&gt;

&lt;p&gt;The other day I decided to add a feature to my business‑card app. I hadn’t touched the code in eight months, so I figured, &lt;em&gt;what the heck&lt;/em&gt;. That “quick change” turned into a comprehensive security overhaul that taught me more about &lt;strong&gt;web application security&lt;/strong&gt; than any tutorial ever has.&lt;/p&gt;

&lt;p&gt;As a mid‑level engineer I know security matters—my first role was at a cybersecurity company. But it wasn’t until I started building AI‑integrated apps that I saw how deep security has to go. It’s a survival strategy as attacks get harder to defend.&lt;/p&gt;


&lt;h2&gt;
  
  
  How I Started
&lt;/h2&gt;

&lt;p&gt;My initial code was a little embarrassing, but if you look at the docs this is what you see—a simple chat endpoint:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// The dangerous approach (don’t do this!)&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/api/chat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;completion&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;openai&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;chat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;completions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt; &lt;span class="na"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;user&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="p"&gt;}],&lt;/span&gt; &lt;span class="c1"&gt;// 🚨 direct injection risk&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;response&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;completion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;choices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unfortunately, every user is &lt;strong&gt;not&lt;/strong&gt; to be trusted. This opens the app to multiple attack vectors that could compromise the application &lt;em&gt;and&lt;/em&gt; the AI model’s behavior. It’s a small app, and these protocols might feel like overkill, but when you’re on enterprise‑level projects you’ll be glad you practiced.&lt;/p&gt;




&lt;h2&gt;
  
  
  Modern Attacks
&lt;/h2&gt;

&lt;p&gt;While researching best practices I found that AI‑powered apps face unique threats:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Prompt injection&lt;/strong&gt; – malicious inputs manipulate AI responses
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data exfiltration&lt;/strong&gt; – attackers extract training data or system prompts
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Resource exhaustion&lt;/strong&gt; – unlimited input length drains API quotas and server resources
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Traditional web vulns&lt;/strong&gt; – XSS and CSRF are still very real
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So, input validation isn’t just about preventing crashes; it’s about protecting AI‑human interactions.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Implementation
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Layer 1 · Input Validation &amp;amp; Sanitization
&lt;/h3&gt;

&lt;p&gt;Input validation is the foundation of any secure app, so I started there—type and existence checks, length constraints (helps control API costs), whitespace normalization, HTML escaping for XSS, suspicious‑pattern detection, and statistical anomaly checks.&lt;br&gt;&lt;br&gt;
&lt;a href="https://github.com/ShavonHarris-Dev/Digital_Business_Card" rel="noopener noreferrer"&gt;See the full function in my repo.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Why it works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Multiple validation layers&lt;/strong&gt; – each check has a purpose
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Statistical analysis&lt;/strong&gt; – special‑character ratios catch clever encoding attacks
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Graceful degradation&lt;/strong&gt; – clear errors aid legit users, frustrate attackers
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Performance&lt;/strong&gt; – regex patterns tuned to avoid ReDoS issues
&lt;/li&gt;
&lt;/ul&gt;


&lt;h3&gt;
  
  
  Layer 2 · Security Headers &amp;amp; Middleware
&lt;/h3&gt;

&lt;p&gt;Modern web security relies on HTTP headers to tell browsers how to handle content safely.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;helmet&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;contentSecurityPolicy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;directives&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;defaultSrc&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;'self'&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
      &lt;span class="na"&gt;styleSrc&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;'self'&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;'unsafe-inline'&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
      &lt;span class="na"&gt;scriptSrc&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;'self'&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
      &lt;span class="na"&gt;imgSrc&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;'self'&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;data:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;}));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Content‑Security‑Policy (CSP)&lt;/strong&gt; – whitelists approved sources, neutralizes XSS
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;X‑Frame‑Options&lt;/strong&gt; – prevents clickjacking
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;X‑Content‑Type‑Options&lt;/strong&gt; – stops MIME sniffing
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Referrer‑Policy&lt;/strong&gt; – controls information leakage
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Layer 3 · CORS Configuration
&lt;/h3&gt;

&lt;p&gt;Cross‑Origin Resource Sharing (CORS) errors can be frustrating, but restrictive CORS is critical for AI apps:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;cors&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NODE_ENV&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;production&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://yourdomainname.netlify.app&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://localhost:5173&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://localhost:8888&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;credentials&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;methods&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;GET&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;OPTIONS&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;allowedHeaders&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Content-Type&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Authorization&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;}));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why restrictive CORS matters&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Origin validation&lt;/strong&gt; – stops unauthorized domains
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Method restriction&lt;/strong&gt; – limits the attack surface
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Credential handling&lt;/strong&gt; – secures tokens and sessions
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Layer 4 · Enhanced Error Handling
&lt;/h3&gt;

&lt;p&gt;Security‑conscious error handling prevents information leaks while keeping usability:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// chat logic&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Chat API error:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Invalid input&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
      &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Message too long&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
      &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Message cannot be empty&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;429&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;429&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Service busy. Try again in a moment.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Unexpected server error.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Information hiding&lt;/strong&gt; – internal details stay in logs
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Specific feedback&lt;/strong&gt; – users get actionable messages
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rate‑limit awareness&lt;/strong&gt; – handles quota hits cleanly
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  The Frontend
&lt;/h2&gt;

&lt;p&gt;Client‑side checks improve UX and cut unnecessary server calls:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;validateInput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;string&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Please enter a valid message&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;trimmed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;trimmed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;              &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Please enter a message&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;trimmed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Message is too long.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;trimmed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Message is too short.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bad&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sr"&gt;/&amp;lt;script/i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sr"&gt;/javascript:/i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sr"&gt;/&amp;lt;iframe/i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sr"&gt;/&amp;lt;object/i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bad&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;some&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;re&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;trimmed&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Invalid characters detected&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Security Dependencies
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;validator helmet express-rate-limit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Validator.js&lt;/strong&gt; – 60+ sanitization and validation helpers
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Helmet.js&lt;/strong&gt; – quick, sane security headers
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Express‑rate‑limit&lt;/strong&gt; – throttles abuse and protects budgets
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Advanced Considerations
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Prompt Injection Prevention
&lt;/h3&gt;

&lt;p&gt;Define clear system prompts, limit data sources, monitor overrides, and provide fallback responses.&lt;/p&gt;

&lt;h3&gt;
  
  
  Dual‑Tier Cost Guard
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;globalLimiter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;rateLimit&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;windowMs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;max&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;chatLimiter&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;rateLimit&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;windowMs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;max&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;  &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/api/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;globalLimiter&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/api/chat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;chatLimiter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* ... */&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Global limits protect the whole API; tighter limits protect expensive endpoints.&lt;/p&gt;




&lt;h2&gt;
  
  
  AI Accelerated My Learning
&lt;/h2&gt;

&lt;p&gt;Instead of weeks in docs, I used AI tools to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Break docs into Q&amp;amp;A chats
&lt;/li&gt;
&lt;li&gt;Get rapid code reviews
&lt;/li&gt;
&lt;li&gt;Generate podcast‑style explanations
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;AI sped research; real testing proved the ideas.&lt;/p&gt;




&lt;h2&gt;
  
  
  Lessons Learned
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Trust nothing&lt;/strong&gt; – validate every input
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Defense in depth&lt;/strong&gt; – layers back each other up
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Protect the budget&lt;/strong&gt; – rate‑limit everything
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security is mindset, not checklist&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;AI accelerates learning, but testing seals it&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Tech &amp;amp; Security Stack
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Node.js · Express · React
&lt;/li&gt;
&lt;li&gt;OpenAI GPT‑4
&lt;/li&gt;
&lt;li&gt;Validator.js · Helmet.js · Express‑rate‑limit
&lt;/li&gt;
&lt;li&gt;Custom CSRF (HMAC‑SHA256)
&lt;/li&gt;
&lt;li&gt;Netlify Functions&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;&lt;a href="https://github.com/ShavonHarris-Dev/Digital_Business_Card" rel="noopener noreferrer"&gt;Repo link&lt;/a&gt;.&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Have you secured an AI‑powered app? What surprised you most? Let’s share stories.&lt;/p&gt;

</description>
      <category>security</category>
      <category>ai</category>
      <category>promptengineering</category>
      <category>developer</category>
    </item>
    <item>
      <title>Building a URL Shortener: A First System Design Test</title>
      <dc:creator>Shavon Harris</dc:creator>
      <pubDate>Wed, 09 Jul 2025 17:38:19 +0000</pubDate>
      <link>https://dev.to/shavonharrisdev/building-a-url-shortener-a-first-system-design-test-118j</link>
      <guid>https://dev.to/shavonharrisdev/building-a-url-shortener-a-first-system-design-test-118j</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is a URL Shortener?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A URL shortener is a tool that takes a long, messy web address and turns it into something short and manageable. Think about Google Drive links or Amazon product pages—they get pretty wild!&lt;/p&gt;

&lt;p&gt;I recently read that designing a URL shortener is a common first system design test, so let’s break it down and see how we can build one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Database Schema&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If we want to keep track of URLs, we need a database. Think of it like a coat check at a restaurant:&lt;/p&gt;

&lt;p&gt;You hand over your coat (long URL)&lt;br&gt;
You get a ticket (short URL)&lt;br&gt;
Later, you return the ticket, and they give you back your exact coat (redirecting to the original URL)&lt;br&gt;
A simple schema might look like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;table&amp;gt;&lt;br&gt;
  &amp;lt;tr&amp;gt;&lt;br&gt;
    &amp;lt;th&amp;gt;Column&amp;lt;/th&amp;gt;&lt;br&gt;
    &amp;lt;th&amp;gt;Type&amp;lt;/th&amp;gt;&lt;br&gt;
    &amp;lt;th&amp;gt;Description&amp;lt;/th&amp;gt;&lt;br&gt;
  &amp;lt;/tr&amp;gt;&lt;br&gt;
  &amp;lt;tr&amp;gt;&lt;br&gt;
    &amp;lt;td&amp;gt;&amp;lt;b&amp;gt;id&amp;lt;/b&amp;gt;&amp;lt;/td&amp;gt;&lt;br&gt;
    &amp;lt;td&amp;gt;UUID / Int&amp;lt;/td&amp;gt;&lt;br&gt;
    &amp;lt;td&amp;gt;Unique identifier for each URL&amp;lt;/td&amp;gt;&lt;br&gt;
  &amp;lt;/tr&amp;gt;&lt;br&gt;
  &amp;lt;tr&amp;gt;&lt;br&gt;
    &amp;lt;td&amp;gt;&amp;lt;b&amp;gt;longURL&amp;lt;/b&amp;gt;&amp;lt;/td&amp;gt;&lt;br&gt;
    &amp;lt;td&amp;gt;String&amp;lt;/td&amp;gt;&lt;br&gt;
    &amp;lt;td&amp;gt;The original long URL&amp;lt;/td&amp;gt;&lt;br&gt;
  &amp;lt;/tr&amp;gt;&lt;br&gt;
  &amp;lt;tr&amp;gt;&lt;br&gt;
    &amp;lt;td&amp;gt;&amp;lt;b&amp;gt;shortURL&amp;lt;/b&amp;gt;&amp;lt;/td&amp;gt;&lt;br&gt;
    &amp;lt;td&amp;gt;String&amp;lt;/td&amp;gt;&lt;br&gt;
    &amp;lt;td&amp;gt;The shortened version&amp;lt;/td&amp;gt;&lt;br&gt;
  &amp;lt;/tr&amp;gt;&lt;br&gt;
  &amp;lt;tr&amp;gt;&lt;br&gt;
    &amp;lt;td&amp;gt;&amp;lt;b&amp;gt;clicks&amp;lt;/b&amp;gt;&amp;lt;/td&amp;gt;&lt;br&gt;
    &amp;lt;td&amp;gt;Integer&amp;lt;/td&amp;gt;&lt;br&gt;
    &amp;lt;td&amp;gt;(Optional) Track number of uses&amp;lt;/td&amp;gt;&lt;br&gt;
  &amp;lt;/tr&amp;gt;&lt;br&gt;
  &amp;lt;tr&amp;gt;&lt;br&gt;
    &amp;lt;td&amp;gt;&amp;lt;b&amp;gt;createdAt&amp;lt;/b&amp;gt;&amp;lt;/td&amp;gt;&lt;br&gt;
    &amp;lt;td&amp;gt;Timestamp&amp;lt;/td&amp;gt;&lt;br&gt;
    &amp;lt;td&amp;gt;(Optional) When the URL was created&amp;lt;/td&amp;gt;&lt;br&gt;
  &amp;lt;/tr&amp;gt;&lt;br&gt;
&amp;lt;/table&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Basic Endpoints (POST to Create, GET to Redirect)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Our app needs two core endpoints:&lt;/p&gt;

&lt;p&gt;POST /shorten – Takes a long URL and returns a shortened version&lt;br&gt;
GET /:shortURL – Redirects users from the short URL to the original long URL&lt;br&gt;
When a user enters a long URL, we POST it to create a short version. Later, when someone clicks the short link, we GET the original and redirect them.&lt;/p&gt;

&lt;p&gt;Simple but effective!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;URL Generation Strategy&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;How do we generate the short URL? There are a few options:&lt;/p&gt;

&lt;p&gt;1️⃣ Hashing the URL&lt;br&gt;
Take the long URL, run it through a hash function, and use part of the hash as the short URL.&lt;br&gt;
Pros: Consistent and deterministic.&lt;br&gt;
Cons: Hash collisions (two URLs creating the same short code), no custom URLs, and inconsistent length.&lt;br&gt;
2️⃣ Random Strings (NanoID)&lt;br&gt;
Generate a random unique string like "xs#$19F".&lt;br&gt;
Pros: Low collision risk, used by companies like Vercel for deployment URLs.&lt;br&gt;
Cons: Doesn’t derive from the original URL.&lt;br&gt;
A better approach? NanoID—a lightweight package that generates collision-resistant unique IDs. Large companies like Vercel use this for their deployment URLs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How It Works in Our App&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User enters a long URL&lt;/li&gt;
&lt;li&gt;We generate a NanoID-based short URL&lt;/li&gt;
&lt;li&gt;We store both in our database&lt;/li&gt;
&lt;li&gt;We return something like mysite.com/xs#$19F&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;** Authentication Needs**&lt;/p&gt;

&lt;p&gt;So far, anyone can create or use our short URLs. But what if we want user accounts so they can manage their links?&lt;/p&gt;

&lt;p&gt;We’d need:&lt;br&gt;
✅ User authentication (login, signup)&lt;br&gt;
✅ Tokens to remember logged-in users&lt;/p&gt;

&lt;p&gt;Where Do We Store Tokens?&lt;br&gt;
LocalStorage? ✅ Good for personal apps but not secure&lt;br&gt;
Access token in memory + refresh token in HTTP-only cookie? ✅ More secure and scalable&lt;br&gt;
If a user logs in and we need multiple pages to know who they are, we have to share state across the app.&lt;/p&gt;

&lt;p&gt;Small app? → Use React's useContext (like a family group chat—everyone gets the message at once).&lt;br&gt;
Larger app? → Use Redux for global state management.&lt;br&gt;
Scaling Up: Handling High Traffic&lt;br&gt;
What if our awesome URL shortener goes viral? &lt;/p&gt;

&lt;p&gt;We don’t want it crashing under heavy traffic, so we use a load balancer to:&lt;br&gt;
🔹 Distribute requests evenly across multiple servers&lt;br&gt;
🔹 Prevent server overload&lt;/p&gt;

&lt;p&gt;Thankfully, cloud providers handle most of this for us.&lt;/p&gt;

&lt;p&gt;Error Handling &amp;amp; Security&lt;br&gt;
Things will go wrong. It’s our job to handle them well.&lt;/p&gt;

&lt;p&gt;Common Errors to Plan For&lt;br&gt;
❌ Invalid URL? → “That’s not a valid address.”&lt;br&gt;
❌ Short URL doesn’t exist? → “We couldn’t find that link.”&lt;br&gt;
❌ Too many requests? → “Slow down, cowboy/cowgirl!” (Rate limiting)&lt;/p&gt;

&lt;p&gt;Security Considerations&lt;br&gt;
Is the user logged in? If not, they can’t see their URL history.&lt;br&gt;
Is this the owner? Only the creator should modify or delete a short URL.&lt;br&gt;
Is this a safe URL? Prevent malicious redirects (phishing links).&lt;br&gt;
Future Features&lt;br&gt;
💡 Custom Short URLs – Let users create something like mysite.com/my-brand&lt;br&gt;
💡 Expiration Dates – Set URLs to expire after a certain time&lt;br&gt;
💡 API Access – Allow developers to generate short URLs programmatically&lt;br&gt;
💡 Advanced Analytics – Track referrers, device types, locations&lt;/p&gt;

&lt;p&gt;Final Thoughts&lt;br&gt;
Building a URL shortener might seem simple, but it’s a great system design test because it touches databases, backend logic, authentication, performance, and security.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>systemdesign</category>
      <category>programming</category>
    </item>
    <item>
      <title>[Boost]</title>
      <dc:creator>Shavon Harris</dc:creator>
      <pubDate>Thu, 20 Feb 2025 21:00:39 +0000</pubDate>
      <link>https://dev.to/shavonharrisdev/-1b5j</link>
      <guid>https://dev.to/shavonharrisdev/-1b5j</guid>
      <description>&lt;div class="ltag__link"&gt;
  &lt;a href="/shavonharrisdev" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F867448%2F88bfb0c3-271a-4b80-a45a-dc962726e3b0.jpeg" alt="shavonharrisdev"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://dev.to/shavonharrisdev/building-a-url-shortener-a-first-system-design-test-d9m" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Building a URL Shortener: A First System Design Test&lt;/h2&gt;
      &lt;h3&gt;Shavon Harris ・ Feb 20&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#webdev&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#systemdesign&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#programming&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>systemdesign</category>
      <category>programming</category>
    </item>
    <item>
      <title>Building a URL Shortener: A First System Design Test</title>
      <dc:creator>Shavon Harris</dc:creator>
      <pubDate>Thu, 20 Feb 2025 21:00:20 +0000</pubDate>
      <link>https://dev.to/shavonharrisdev/building-a-url-shortener-a-first-system-design-test-d9m</link>
      <guid>https://dev.to/shavonharrisdev/building-a-url-shortener-a-first-system-design-test-d9m</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is a URL Shortener?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A URL shortener is a tool that takes a long, messy web address and turns it into something short and manageable. Think about Google Drive links or Amazon product pages—they get pretty wild!&lt;/p&gt;

&lt;p&gt;I recently read that designing a URL shortener is a common first system design test, so let’s break it down and see how we can build one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Database Schema&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If we want to keep track of URLs, we need a database. Think of it like a coat check at a restaurant:&lt;/p&gt;

&lt;p&gt;You hand over your coat (long URL)&lt;br&gt;
You get a ticket (short URL)&lt;br&gt;
Later, you return the ticket, and they give you back your exact coat (redirecting to the original URL)&lt;br&gt;
A simple schema might look like this:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Column&lt;/th&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;id&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;UUID / Int&lt;/td&gt;
&lt;td&gt;Unique identifier for each URL&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;longURL&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;String&lt;/td&gt;
&lt;td&gt;The original long URL&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;shortURL&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;String&lt;/td&gt;
&lt;td&gt;The shortened version&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;clicks&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Integer&lt;/td&gt;
&lt;td&gt;(Optional) Track number of uses&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;createdAt&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Timestamp&lt;/td&gt;
&lt;td&gt;(Optional) When the URL was created&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Basic Endpoints (POST to Create, GET to Redirect)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Our app needs two core endpoints:&lt;/p&gt;

&lt;p&gt;POST /shorten – Takes a long URL and returns a shortened version&lt;br&gt;
GET /:shortURL – Redirects users from the short URL to the original long URL&lt;br&gt;
When a user enters a long URL, we POST it to create a short version. Later, when someone clicks the short link, we GET the original and redirect them.&lt;/p&gt;

&lt;p&gt;Simple but effective!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;URL Generation Strategy&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;How do we generate the short URL? There are a few options:&lt;/p&gt;

&lt;p&gt;1️⃣ Hashing the URL&lt;br&gt;
Take the long URL, run it through a hash function, and use part of the hash as the short URL.&lt;br&gt;
Pros: Consistent and deterministic.&lt;br&gt;
Cons: Hash collisions (two URLs creating the same short code), no custom URLs, and inconsistent length.&lt;br&gt;
2️⃣ Random Strings (NanoID)&lt;br&gt;
Generate a random unique string like "xs#$19F".&lt;br&gt;
Pros: Low collision risk, used by companies like Vercel for deployment URLs.&lt;br&gt;
Cons: Doesn’t derive from the original URL.&lt;br&gt;
A better approach? NanoID—a lightweight package that generates collision-resistant unique IDs. Large companies like Vercel use this for their deployment URLs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How It Works in Our App&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User enters a long URL&lt;/li&gt;
&lt;li&gt;We generate a NanoID-based short URL&lt;/li&gt;
&lt;li&gt;We store both in our database&lt;/li&gt;
&lt;li&gt;We return something like mysite.com/xs#$19F&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;** Authentication Needs**&lt;/p&gt;

&lt;p&gt;So far, anyone can create or use our short URLs. But what if we want user accounts so they can manage their links?&lt;/p&gt;

&lt;p&gt;We’d need:&lt;br&gt;
✅ User authentication (login, signup)&lt;br&gt;
✅ Tokens to remember logged-in users&lt;/p&gt;

&lt;p&gt;Where Do We Store Tokens?&lt;br&gt;
LocalStorage? ✅ Good for personal apps but not secure&lt;br&gt;
Access token in memory + refresh token in HTTP-only cookie? ✅ More secure and scalable&lt;br&gt;
If a user logs in and we need multiple pages to know who they are, we have to share state across the app.&lt;/p&gt;

&lt;p&gt;Small app? → Use React's useContext (like a family group chat—everyone gets the message at once).&lt;br&gt;
Larger app? → Use Redux for global state management.&lt;br&gt;
Scaling Up: Handling High Traffic&lt;br&gt;
What if our awesome URL shortener goes viral? &lt;/p&gt;

&lt;p&gt;We don’t want it crashing under heavy traffic, so we use a load balancer to:&lt;br&gt;
🔹 Distribute requests evenly across multiple servers&lt;br&gt;
🔹 Prevent server overload&lt;/p&gt;

&lt;p&gt;Thankfully, cloud providers handle most of this for us.&lt;/p&gt;

&lt;p&gt;Error Handling &amp;amp; Security&lt;br&gt;
Things will go wrong. It’s our job to handle them well.&lt;/p&gt;

&lt;p&gt;Common Errors to Plan For&lt;br&gt;
❌ Invalid URL? → “That’s not a valid address.”&lt;br&gt;
❌ Short URL doesn’t exist? → “We couldn’t find that link.”&lt;br&gt;
❌ Too many requests? → “Slow down, cowboy/cowgirl!” (Rate limiting)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Security Considerations&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Is the user logged in? If not, they can’t see their URL history.&lt;br&gt;
Is this the owner? Only the creator should modify or delete a short URL.&lt;br&gt;
Is this a safe URL? Prevent malicious redirects (phishing links).&lt;br&gt;
Future Features&lt;br&gt;
💡 Custom Short URLs – Let users create something like mysite.com/my-brand&lt;br&gt;
💡 Expiration Dates – Set URLs to expire after a certain time&lt;br&gt;
💡 API Access – Allow developers to generate short URLs programmatically&lt;br&gt;
💡 Advanced Analytics – Track referrers, device types, locations&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Final Thoughts&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Building a URL shortener might seem simple, but it’s a great system design test because it touches databases, backend logic, authentication, performance, and security.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>systemdesign</category>
      <category>programming</category>
    </item>
    <item>
      <title>Understanding K Element Patterns in LeetCode: The Basics (Part 1)</title>
      <dc:creator>Shavon Harris</dc:creator>
      <pubDate>Mon, 25 Nov 2024 12:15:02 +0000</pubDate>
      <link>https://dev.to/shavonharrisdev/understanding-k-element-patterns-in-leetcode-the-basics-part-1-186p</link>
      <guid>https://dev.to/shavonharrisdev/understanding-k-element-patterns-in-leetcode-the-basics-part-1-186p</guid>
      <description>&lt;p&gt;I finally understand! The best way to learn LeetCode is not by grinding through problem after problem, sometimes taking a whole hour to solve them inefficiently. The key to mastering LeetCode is studying patterns. Let's study a common one!&lt;/p&gt;

&lt;p&gt;Interviewers love asking about finding, maintaining, or manipulating K elements in a string or array. At first, I thought each problem was totally different, but then I started seeing the connections. Let me show you what I mean with two problems that really helped me understand this pattern.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem 1: Find Subsequence of Length K with the Largest Sum
&lt;/h2&gt;

&lt;p&gt;This is technically an 'easy' level question (only asked by one &lt;a href="https://interview-prep-pro.vercel.app/" rel="noopener noreferrer"&gt;company&lt;/a&gt;), but, it teaches you so much about how to think about these k-element problems!&lt;/p&gt;

&lt;h3&gt;
  
  
  What They're Asking
&lt;/h3&gt;

&lt;p&gt;You get an array of numbers and a value k, and you need to find k numbers from the array that add up to the biggest possible sum. BUT (and this is the part that tripped me up at first), you have to keep the numbers in their original order!&lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhyxz3e5u2juoug0dmyxx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhyxz3e5u2juoug0dmyxx.png" alt="Image description" width="800" height="254"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Oh! this one is trickier:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgn1ps049v4rfkxtfghpv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgn1ps049v4rfkxtfghpv.png" alt="Image description" width="800" height="302"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Ok I Get It
&lt;/h3&gt;

&lt;p&gt;At first, I thought "just grab the k biggest numbers, done!" But nope - that order requirement changes everything. Here's what finally clicked:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We need to remember where each number came from, right? So 
I thought, "I should pair every number with it's
position?"&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgys0jul4pugqupl477uf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgys0jul4pugqupl477uf.png" alt="Image description" width="800" height="319"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Then we can sort these pairs by the values (that's the 
first number in each pair), but we're keeping track of 
where they came from (that's the second number)!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1osr0a0kn7obshu11tga.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1osr0a0kn7obshu11tga.png" alt="Image description" width="800" height="295"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Now the cool part - we only need k of these, so grab the 
first k pairs and just keep their positions:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flsmb0bg3j5cz8zsvj763.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flsmb0bg3j5cz8zsvj763.png" alt="Image description" width="800" height="290"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Finally, walk through the original array and only keep 
numbers whose positions are in our Set:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9l6qttotmzv609f560n2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9l6qttotmzv609f560n2.png" alt="Image description" width="790" height="632"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Here's The Code
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnlf920dyqlz7sdb4qxa1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnlf920dyqlz7sdb4qxa1.png" alt="Image description" width="800" height="415"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2: Kth Largest Element in a Stream
&lt;/h2&gt;

&lt;p&gt;Ok, so this one's also labeled 'easy' (asked by five &lt;a href="https://interview-prep-pro.vercel.app/" rel="noopener noreferrer"&gt;companies&lt;/a&gt;), but this one was more confusing to me than any of the more difficult Kth element problems.&lt;/p&gt;

&lt;h3&gt;
  
  
  What They're Asking
&lt;/h3&gt;

&lt;p&gt;Imagine you're working at a university and students keep submitting test scores. Your job is to always know the kth highest score at any time. New scores keep coming in, and you need to keep track.&lt;/p&gt;

&lt;p&gt;They give you k and some initial scores, then they keep throwing new scores at you and want to know the kth highest each time. Let's look at an example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fozyvy41z606rwyx9i0lv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fozyvy41z606rwyx9i0lv.png" alt="Image description" width="800" height="471"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Ok, I Think I Get It
&lt;/h3&gt;

&lt;p&gt;At first, I kept trying to sort the entire array every time a new score came in but I know that sorting can be inefficient. Then I thought why am I keeping track of all the scores when I only care about the top k? &lt;/p&gt;

&lt;p&gt;Here's how I broke it down:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First, sort the initial scores and only keep the top k:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0r2tcwg59gcwhhz17j8v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0r2tcwg59gcwhhz17j8v.png" alt="Image description" width="800" height="282"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Now when a new score comes in:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If it's smaller than our kth highest (first number), ignore it&lt;br&gt;
If it's bigger, it belongs in our list somewhere&lt;/p&gt;

&lt;p&gt;Here's what's happening with each add:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8a11tjwmadf3tcr197x4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8a11tjwmadf3tcr197x4.png" alt="Image description" width="800" height="455"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The Code
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm3ky1z5dk40oq1g3zheq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm3ky1z5dk40oq1g3zheq.png" alt="Image description" width="800" height="886"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Why These Two Problems Are Connected
&lt;/h3&gt;

&lt;p&gt;Both problems taught me something super important about handling k elements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First problem: Sometimes you need to track where elements 
came from&lt;/li&gt;
&lt;li&gt;Second problem: Sometimes you only need to keep k elements 
around&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These k-element problems are all about being clever with what information you keep and what you throw away.&lt;br&gt;
Next time we'll look at two more k-element problems that build on these idea. I hope at the end you see a pattern and these types of problems seem less scary!&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>development</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>How To Train Your Codebase: Enhancing Readability, Reusability and Scalability with Modular Code</title>
      <dc:creator>Shavon Harris</dc:creator>
      <pubDate>Thu, 01 Aug 2024 18:16:55 +0000</pubDate>
      <link>https://dev.to/shavonharrisdev/how-to-train-your-codebase-enhancing-readability-reusability-and-scalability-with-modular-code-476d</link>
      <guid>https://dev.to/shavonharrisdev/how-to-train-your-codebase-enhancing-readability-reusability-and-scalability-with-modular-code-476d</guid>
      <description>&lt;p&gt;It can be easy for new devs or even senior devs to get lost in a codebase. It's crucial to be able to quickly jump in and ground yourself. Writing modular code—coding in a way that maximizes reusability and minimizes complexity—is one way for teams and individual developers to address this issue. I want to explore what modular code is, the benefits, and how to implement it in everyday use cases. To demonstrate, I also built a fun &lt;a href="https://flipdate.netlify.app/" rel="noopener noreferrer"&gt;game&lt;/a&gt; for couples or friends. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is Modular Code
&lt;/h2&gt;

&lt;p&gt;Simply put, modular code involves breaking down a program into separate modules, each handling an important piece of functionality. Imagine you are constructing a building using pre-made blocks, each designed for a different purpose. You have your windows, doors, floors, etc. You don't need to build a new door each time; you can just grab a pre-made block. Similarly, in coding, you might need to modify your pre-made code to fit your needs, but you won’t have to start from scratch. This approach makes code easier to manage and understand, and also facilitates easier testing, debugging, and maintenance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Benefits of Modular Code
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Enhanced Readability&lt;/strong&gt;: Each module is self-contained, making it easier to understand at a glance. This means you can quickly identify the purpose of each part of your code without wading through unrelated logic. For example, in my &lt;strong&gt;flipdate&lt;/strong&gt; game, the &lt;strong&gt;Card&lt;/strong&gt; component handles only the display and flip logic of individual cards, making it easier to comprehend and manage.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Increased Reusability&lt;/strong&gt;: Modules can be reused across different parts of a project or in different projects. This reduces the amount of code you need to write. In my game, the** Card** component can be reused anywhere a similar card-flipping functionality is required.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Simplified Testing&lt;/strong&gt;: Testing becomes more straightforward as each module can be tested independently. By isolating functionality into separate components and hooks, you can write tests for each piece without worrying about the entire app. For example, I could use my &lt;strong&gt;useCardFlip&lt;/strong&gt; hook independently from the &lt;strong&gt;GameBoard&lt;/strong&gt; component. Although I haven't yet written tests for this game, when I do, it will be much easier to test functionality independently.&lt;/p&gt;

&lt;p&gt;For a small app like this, I don't necessarily have to break my application into modules. However, practicing modular architecture on smaller apps will help you see how much easier and more helpful it is when you move on to more complex applications. If you remember nothing else, remember that modular code performs a &lt;strong&gt;specific task&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementing Modular Code with React
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Breaking Code into Small Reusable Components
&lt;/h3&gt;

&lt;p&gt;Imagine you want to create a button that will be used throughout your program multiple times. Instead of coding it directly into the &lt;strong&gt;index.js&lt;/strong&gt; file, you can create a &lt;strong&gt;Button.js&lt;/strong&gt; file and import it into &lt;strong&gt;index.js&lt;/strong&gt;. This way, you can use the Button component throughout the application.&lt;/p&gt;

&lt;p&gt;In our game, we follow this idea by creating a &lt;strong&gt;Card&lt;/strong&gt; component that is used multiple times within the &lt;strong&gt;GameBoard&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faayeslw77a4n078m292j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faayeslw77a4n078m292j.png" alt="Code for Card"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Use React Hooks
&lt;/h2&gt;

&lt;p&gt;Hooks help you write modular code by enabling the reuse of stateful logic across multiple components. In our game, we use the** useCardFlip** hook to manage the state and logic related to flipping cards and checking matches.&lt;/p&gt;

&lt;p&gt;For example, the &lt;strong&gt;useCardFlip&lt;/strong&gt; hook encapsulates the logic for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tracking which cards have been flipped.&lt;/li&gt;
&lt;li&gt;Checking for matches.&lt;/li&gt;
&lt;li&gt;Handling the state of matched and denied cards.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This approach allows us to separate the game logic from the UI components, making the code easier to maintain and test.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fchgy532z2rea7f4k4ljp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fchgy532z2rea7f4k4ljp.png" alt="Logic for useCardFlip hook"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  FlipDate
&lt;/h2&gt;

&lt;p&gt;In our game, we use modular code principles to build a maintainable and scalable application. The game consists of reusable components (&lt;strong&gt;Card&lt;/strong&gt;, &lt;strong&gt;GameBoard&lt;/strong&gt;) and custom hooks (&lt;strong&gt;useCardFlip&lt;/strong&gt;).&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;GameBoard&lt;/strong&gt; component manages the overall game logic, including shuffling the deck, handling player turns, and managing the game state.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp6djhy45538l457qgtib.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp6djhy45538l457qgtib.png" alt="Logic For Gameboard"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Writing modular code might feel tedious, but it is invaluable for creating maintainable and scalable code, as well as enhancing team collaboration. By breaking code into logical blocks, you make your code easier to manage for yourself and easier to collaborate on in large projects. Embrace modular code and watch your projects become more organized and scalable.&lt;/p&gt;

&lt;p&gt;Our game is a great example of modular architecture. By dividing the application into reusable components and hooks, we achieve a clean, maintainable, and scalable codebase. The &lt;strong&gt;Card&lt;/strong&gt; component, &lt;strong&gt;useCardFlip&lt;/strong&gt; hook, and **GameBoard **component each handle distinct responsibilities, illustrating the simplicity of modular coding in a real-world application.&lt;/p&gt;

&lt;p&gt;Check out the &lt;a href="https://github.com/ShavonHarris-Dev/go-fish-date-night.git" rel="noopener noreferrer"&gt;source code&lt;/a&gt; for the &lt;a href="https://flipdate.netlify.app/" rel="noopener noreferrer"&gt;Game&lt;/a&gt; and Happy coding!&lt;/p&gt;

</description>
      <category>react</category>
      <category>module</category>
      <category>developer</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Shuffle The Array</title>
      <dc:creator>Shavon Harris</dc:creator>
      <pubDate>Thu, 27 Jun 2024 02:24:33 +0000</pubDate>
      <link>https://dev.to/shavonharrisdev/shuffle-the-array-444c</link>
      <guid>https://dev.to/shavonharrisdev/shuffle-the-array-444c</guid>
      <description>&lt;p&gt;Today's problem is fairly straightforward. It can be solved with a simple for loop and the &lt;code&gt;push&lt;/code&gt; method. &lt;/p&gt;

&lt;p&gt;Given the array &lt;code&gt;nums&lt;/code&gt; consisting of &lt;code&gt;2n&lt;/code&gt; elements in form &lt;code&gt;[x1,x2,...,xn,y1,y2,...,yn]&lt;/code&gt;. we need to rearrange the array into `&lt;code&gt;[x1,y1,x2,y2,...,xn,yn]&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Function  Signature
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;`&lt;br&gt;
@param {number[]} nums&lt;br&gt;
@param {number} n&lt;br&gt;
@return {number[]}&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Examples
&lt;/h3&gt;

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

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; nums = [2,5,1,3,4,7], n = 3&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [2,3,5,4,1,7] &lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7].&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; nums = [1,2,3,4,4,3,2,1], n = 4&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [1,4,2,3,3,2,4,1]&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; nums = [1,1,2,2], n = 2&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [1,2,1,2]&lt;/p&gt;

&lt;p&gt;The first thing to understand is that &lt;code&gt;2n elements&lt;/code&gt; means the total number of elements in the array is twice the value of &lt;code&gt;n&lt;/code&gt;. The first half of the array consists of elements &lt;code&gt;[x1, x2, ..., xn]&lt;/code&gt; and the second half consists of elements &lt;code&gt;[y1, y2, ..., yn]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The task is to rearrange the array into &lt;code&gt;[x1, y1, x2, y2, ..., xn, yn]&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example Breakdown
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; &lt;code&gt;nums = [2, 5, 1, 3, 4, 7]&lt;/code&gt;, &lt;code&gt;n = 3&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here, &lt;code&gt;nums&lt;/code&gt; has 6 elements (2 * 3 = 6).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The first half &lt;code&gt;[x1, x2, x3]&lt;/code&gt; is &lt;code&gt;[2, 5, 1]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The second half &lt;code&gt;[y1, y2, y3]&lt;/code&gt; is &lt;code&gt;[3, 4, 7]&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We need to combine them like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Take &lt;code&gt;x1&lt;/code&gt; (which is &lt;code&gt;2&lt;/code&gt;) and &lt;code&gt;y1&lt;/code&gt; (which is &lt;code&gt;3&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Take &lt;code&gt;x2&lt;/code&gt; (which is &lt;code&gt;5&lt;/code&gt;) and &lt;code&gt;y2&lt;/code&gt; (which is &lt;code&gt;4&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Take &lt;code&gt;x3&lt;/code&gt; (which is &lt;code&gt;1&lt;/code&gt;) and &lt;code&gt;y3&lt;/code&gt; (which is &lt;code&gt;7&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Resulting in &lt;code&gt;[2, 3, 5, 4, 1, 7]&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dive into the code
&lt;/h2&gt;

&lt;p&gt;So how do we approach this with code. Let's create a plan:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a function &lt;code&gt;shuffle&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The function takes two parameters: &lt;code&gt;nums&lt;/code&gt; and &lt;code&gt;n&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Create an empty array to store the result.&lt;/li&gt;
&lt;li&gt;Loop through the first &lt;code&gt;n&lt;/code&gt; elements of the array.&lt;/li&gt;
&lt;li&gt; Push the current element and the corresponding element from the second half into the result array.&lt;/li&gt;
&lt;li&gt;Return the result array.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F49mymyxci45nqfb127p8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F49mymyxci45nqfb127p8.png" alt="Image description" width="800" height="738"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Conclusion&lt;br&gt;
Today's problem was a great example of how we can use simple loops and basic array operations to solve a seemingly task. &lt;/p&gt;

&lt;p&gt;To recap:&lt;/p&gt;

&lt;p&gt;We identified that the array nums consists of 2n elements, split into two halves.&lt;br&gt;
Our goal was to get two halves into a new array in the specified format.&lt;br&gt;
Using a simple for loop and the push method, we created a solution that is both intuitive and effective.&lt;/p&gt;

&lt;p&gt;I encourage you to try this problem on your own and see how you can further optimize or modify the solution.&lt;/p&gt;

&lt;p&gt;Happy Coding!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>A More Straightforward Guide to Solving Two Sum.</title>
      <dc:creator>Shavon Harris</dc:creator>
      <pubDate>Wed, 26 Jun 2024 17:32:18 +0000</pubDate>
      <link>https://dev.to/shavonharrisdev/a-more-straightforward-guide-to-solving-two-sum-2gbi</link>
      <guid>https://dev.to/shavonharrisdev/a-more-straightforward-guide-to-solving-two-sum-2gbi</guid>
      <description>&lt;p&gt;When approaching the classic two sum problem you can answer in a few different ways. You can attempt a brute method in which you consider every possible number.This would be attempted using a nested for loop. This is not an efficient runtime. Or, you can do the one pass method using a hashmap, which is much more efficient. &lt;/p&gt;

&lt;h2&gt;
  
  
  Brute Force
&lt;/h2&gt;

&lt;p&gt;Given an array lets use&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

let array = [2,1, 5, 3]
let target = 4


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We are look for 2 numbers in this array to equal the target 4.&lt;/p&gt;

&lt;p&gt;2 + 1 = 3&lt;br&gt;
2 + 5 = 7&lt;br&gt;
2 + 3 = 5&lt;/p&gt;

&lt;p&gt;After iterating through the array once using the 0th index we can see that 2 is not going to help us reach the target. So we pass through again using the 1st index.&lt;/p&gt;

&lt;p&gt;1 + 5 = 6&lt;br&gt;
1 + 3 = 4&lt;/p&gt;

&lt;p&gt;Finally, we find two numbers that add up to the target: 1 and 3. In the array, the number 1 is at index 1, and the number 3 is at index 3. Therefore, the answer is [1, 3], indicating that the numbers at indices 1 and 3 of the array sum up to the target value of 4!&lt;/p&gt;

&lt;p&gt;Now imagine the array looked like [2, 5, 1, 3] instead. We would have had to iterate through the array 3 times. That is much too much and there is a more efficient method. Enter hashmaps.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Hashmaps?
&lt;/h2&gt;

&lt;p&gt;A hashmap is a data structure that stores data in pairs of keys and values. It allows for fast retrieval of values based on their associated keys. That's the textbook definition and a bit confusing so let's try to visualize this.&lt;/p&gt;

&lt;p&gt;Imagine you're in a library looking for specific books. Each book has a unique identifier, like a number, known as a 'key.' When you need a particular book, you simply look up its key, and voila, you've found it. This key—whether it's the book title or a number—quickly directs you to the book you're searching for. This method is much faster than searching through every book one by one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Here's the Plan:
&lt;/h2&gt;

&lt;p&gt;Now that we know what a hashmap is we can use one to solve our problem. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Traverse the list once.&lt;/li&gt;
&lt;li&gt;For each number, calculate its complement(target - current number).&lt;/li&gt;
&lt;li&gt;Check if this complement exists in our hashmap.&lt;/li&gt;
&lt;li&gt;If it doesn’t, store the current number with its index.&lt;/li&gt;
&lt;li&gt;If it does, return the current index and the stored 
index from the hashmap.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Given an array of integers and a target integer, your task is to return the indices of two numbers in the array that add up to the target.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;

&lt;p&gt;Input: nums = [2, 7, 11, 15], target = 9&lt;br&gt;
Output: [0, 1]&lt;br&gt;
Explanation: nums[0] + nums[1] = 2 + 7 = 9&lt;br&gt;
Note: you can return the indices in any order and there is only one answer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dive into the code?
&lt;/h2&gt;

&lt;p&gt;Not quite. Let's draw this hashmap out!&lt;/p&gt;

&lt;p&gt;Hashmap&lt;br&gt;
value: index&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: We start with an empty hashmap and we look at each number and we check something very specific:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Does the difference between the target and the current number already exist as a key in our hashmap.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;: For the first number(2) the difference between the target (9) and 2 is 7. If we look in the hashmap:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;is 7 noted down already? Of course not, the hashmap is still empty.&lt;/li&gt;
&lt;li&gt;So, write down 2 and where you found it (index 0)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Current Hashmap&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;2: 0&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;: Move to the next number 7 .&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The difference between the target (9) and the number (7) is 2. Is 2 in the hashmap? Yes, and its noted at being at index 0!&lt;/li&gt;
&lt;li&gt;This means you've found the two numbers that add up to the target:2 (from index 0) and 7 (the number you're currently looking at, at index 1).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Updated Hashmap&lt;/strong&gt;:&lt;br&gt;
2: 0&lt;br&gt;
7: 1&lt;/p&gt;

&lt;p&gt;Because you found that both components of the sum are in the hashmap, you can now return their indices: [0, 1]. These indices tell us where in the array each component of the target sum can be found.&lt;/p&gt;

&lt;p&gt;Yay!&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's dive into the code!
&lt;/h2&gt;

&lt;p&gt;Let's remind ourselves or our steps and problem.&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem
&lt;/h3&gt;

&lt;p&gt;Given an array of integers and a target integer, your task is to return the indices of two numbers in the array that add up to the target.&lt;/p&gt;

&lt;h3&gt;
  
  
  Steps
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Traverse the list once.&lt;/li&gt;
&lt;li&gt;For each number, calculate its complement(target- 
current number).&lt;/li&gt;
&lt;li&gt;Check if this complement exists in our hashmap.&lt;/li&gt;
&lt;li&gt;If it doesn’t, store the current number with its index.&lt;/li&gt;
&lt;li&gt;If it does, return the current index and the stored 
index from the hashmap.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If we are going to traverse a list, we should definitely use some sort of loop. I will utilize a for loop to iterate through the array just like I did in the visualization.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffajj58u63suwitt3pzbg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffajj58u63suwitt3pzbg.png" alt="Image of the twosum walk through"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The two approaches to solving the 2-sum problem—brute force and the one-pass hashmap method—demonstrate the power of choosing the right data structure for the job. While the brute force method is straightforward, it's not efficient, especially with larger arrays. The hashmap approach, on the other hand, optimizes the process by reducing the number of comparisons needed to find the correct indices. This makes it an ideal choice for real-world applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Takeaways:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Efficiency&lt;/strong&gt;: Using hashmaps can drastically improve the efficiency of your code, reducing the time complexity from O(n²) with brute force to O(n) with hashmaps.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Simplicity&lt;/strong&gt;: Despite their power, hashmaps are straightforward to implement and use.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Practicality&lt;/strong&gt;: Understanding when and how to use hashmaps is a valuable skill in computer science, applicable to a wide range of problems beyond just the 2-sum issue.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By now, you should have a solid understanding of how the two-sum problem can be efficiently solved using hashmaps! Hope you enjoyed!&lt;/p&gt;

&lt;p&gt;Happy Coding!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Let's Talk Version Control</title>
      <dc:creator>Shavon Harris</dc:creator>
      <pubDate>Tue, 25 Jun 2024 14:13:37 +0000</pubDate>
      <link>https://dev.to/shavonharrisdev/lets-talk-version-control-25ce</link>
      <guid>https://dev.to/shavonharrisdev/lets-talk-version-control-25ce</guid>
      <description>&lt;p&gt;Understanding Git can be really confusing. I have been using Git for quite some time, but I sometimes found it difficult to explain.  This guide aims to equip you with the essential language and concepts of version control so that you do not have that problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Git?
&lt;/h2&gt;

&lt;p&gt;Imagine you're writing a novel collaboratively. Each contributor makes changes—maybe deleting a comma or adding a whole new chapter. &lt;strong&gt;Git&lt;/strong&gt; is like an attentive editor who keeps track of all these changes. Not only does it help you see what others are doing but it also helps you integrate these changes smoothly to create a final, cohesive manuscript.&lt;/p&gt;

&lt;p&gt;Git is the most popular tool for version control, but it's not the only option. Alternatives like &lt;strong&gt;Mercurial&lt;/strong&gt; or &lt;strong&gt;Subversion&lt;/strong&gt; also offer ways to manage project versions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Subversion (SVN)&lt;/strong&gt; is like a traditional library where a single, central ledger records every change in the books.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mercurial&lt;/strong&gt;, like Git, is something like each writer maintaining their own detailed notebook of changes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, as a developer, I would use Git or Mercurial so that myself and my colleagues can access an identical version of the project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Different Types of Version Control
&lt;/h2&gt;

&lt;p&gt;Version control systems can be classified into three main types:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Local Version Control&lt;/strong&gt;: This is like keeping a personal diary of changes on your own computer.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Centralized Version Control (CVC)&lt;/strong&gt;: Imagine a club where all decisions are recorded in a single register kept at the club's headquarters.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Distributed Version Control (DVC)&lt;/strong&gt;: (This is the most popular version) Each member of the group keeps their own copy of the decision record, allowing operations even when away from the club. Distributed allows for the greatest amount of control and flexibility. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Most people choose Git because it is fast, flexible, and supported by major platforms like GitHub, GitLab, and Bitbucket, which enhance community support.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Three States of Git Files
&lt;/h2&gt;

&lt;p&gt;Understanding the file states in Git is crucial:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Modified&lt;/strong&gt;: Your file has changes that are not yet committed to your database. It's like marking up a draft without finalizing the changes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Staged&lt;/strong&gt;: You're ready to commit the changes. Think of it as wrapping a gift before giving it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Committed&lt;/strong&gt;: The changes are safely stored in your local database, something like storing the wrapped gift in a safe until the party.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After committing, you push your changes to a remote repository.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Concepts and Operations
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Remote Repository&lt;/strong&gt;: A remote repo is like a shared library on the internet or a network where everyone can access the books at the SAME TIME.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Common Operations&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Creating and Switching Branches&lt;/strong&gt;: Developers are encouraged 
to utilize branches such as dev, test, and prod. This 
structure allows you to switch between different 
stages of development. The primary advantage is the ability 
to thoroughly test changes in the test branch before merging 
them into the prod (production) branch. This strategy helps 
prevent the introduction of breaking changes to your main 
deployment. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pulling&lt;/strong&gt; changes: Bringing the latest 
updates from the shared library to 
your personal collection.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pushing changes&lt;/strong&gt;: Sending your latest 
writings to the shared library for 
others to access.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Best Practices
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Commit Early and Often&lt;/strong&gt;: Regular updates help colleagues stay in sync, provide feedback, and avoid complex merging.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Understanding Merging and Rebasing&lt;/strong&gt;: Merging integrates changes from one branch to another to create a unified history. Rebasing is like rearranging the books on your shelf to make it look as if they've always been in that order.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By now, you should have a clearer understanding of how Git functions as a version control system—a digital history for every change, ensuring that collaborations go smoothly and efficiently. &lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

&lt;h2&gt;
  
  
  Extra resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.blog/2024-06-10-top-12-git-commands-every-developer-must-know/"&gt;itsThatLadyDev&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.r-bloggers.com/2024/04/git-gud-version-control-best-practices/"&gt;Git Gud&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>github</category>
      <category>git</category>
      <category>development</category>
      <category>developer</category>
    </item>
    <item>
      <title>To Be Or Not To Be</title>
      <dc:creator>Shavon Harris</dc:creator>
      <pubDate>Wed, 19 Jun 2024 11:13:03 +0000</pubDate>
      <link>https://dev.to/shavonharrisdev/to-be-or-not-to-be-2g1p</link>
      <guid>https://dev.to/shavonharrisdev/to-be-or-not-to-be-2g1p</guid>
      <description>&lt;p&gt;I'm excited to get to my third day of blogging! Covering beginner concepts in Javascript is definitely bolstering my confidence, I hope its helping you as well!&lt;/p&gt;

&lt;p&gt;Today we dive into closures once again, it's an important thing to master, but we are also looking at error handling and comparing values.&lt;/p&gt;

&lt;h2&gt;
  
  
  Today's Journey
&lt;/h2&gt;

&lt;p&gt;In this problem we are writing a function &lt;code&gt;expect&lt;/code&gt; that takes in a value &lt;code&gt;val&lt;/code&gt; and returns an object that holds two functions.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a class="mentioned-user" href="https://dev.to/param"&gt;@param&lt;/a&gt; {string} val&lt;/li&gt;
&lt;li&gt;@return {object}
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const expect = function(val){
    return {
        toBe: function (value) {},
        notToBe: function (value) {}
    };
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  More on Closures
&lt;/h2&gt;

&lt;p&gt;This function looks differently than the ones I wrote out yesterday and the day before &lt;br&gt;
[&lt;a href="https://dev.to/shavonharrisdev/encapsulation-closure-and-a-higher-order-function-walk-into-a-hackathon-403b"&gt;https://dev.to/shavonharrisdev/encapsulation-closure-and-a-higher-order-function-walk-into-a-hackathon-403b&lt;/a&gt;],&lt;br&gt;
&lt;a href="https://dev.to/shavonharrisdev/closures-closures-closures-3o3n"&gt;https://dev.to/shavonharrisdev/closures-closures-closures-3o3n&lt;/a&gt; &lt;br&gt;
but this is still a closure. &lt;/p&gt;

&lt;p&gt;Why? Well because the key characteristic of a closure is its ability to remember and access variables from its parent function or lexical scope in fancy programmer jargon, even when the function is executed outside that scope. Functions &lt;code&gt;toBe&lt;/code&gt; and &lt;code&gt;notToBe&lt;/code&gt; access the val parameter of their parent function even after &lt;code&gt;expect&lt;/code&gt; has finished execution.&lt;/p&gt;
&lt;h2&gt;
  
  
  Comparing Values
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;toBe&lt;/code&gt; function accepts a value and returns &lt;code&gt;true&lt;/code&gt; if the two values === each other. If they are not equal, it should throw an error "Not Equal".&lt;/p&gt;

&lt;p&gt;In other words we will have two values that we are comparing. A value comes from the &lt;code&gt;expect&lt;/code&gt; function (val) and another comes from the &lt;code&gt;toBe&lt;/code&gt; function(value). We ask ourselves, or rather the program, are val and value strictly equal to each other? Are they the same value and the same data type? If yes, wonderful. If no, throw an error!&lt;/p&gt;

&lt;p&gt;I'll also add the reverse logic in a &lt;code&gt;notToBe&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const expect = function (val) {
  return {
toBe: function (value) {
      if (val === value) {
        return true;
      } else {
        throw new Error("Not Equal");
      }
    },
notToBe: function (value) { 
      if (val !== value) {
        return true;
      } else {
        throw new Error("Equal");
      }
    }
  };
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What is a throw new Error 😳
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;throw new Error&lt;/code&gt; is like a special phrase that means hold up, this makes no sense! &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;throw&lt;/code&gt; is just like raising your hand and saying STOP. It stops the normal flow of your program.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;new Error&lt;/code&gt; creates a new "Error" object. If that sounds confusing just think of it as a bucket called Error that holds a note and that note explains the problem.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you say (&lt;code&gt;throw new Error("message")&lt;/code&gt;); This is like stopping the story while someone is telling it and holding up a note that says "THAT DOESN'T MAKE SENSE AND THIS IS WHY". It's important to use clear descriptive error messages for easy understanding.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Throw new Error is really helpful for handling errors and debugging.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try...Catch
&lt;/h2&gt;

&lt;p&gt;The last thing I do is use a try catch block to log the results.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log("toBe", expect(5).toBe(5)); // 
try {
  console.log("notToBe", expect(5).notToBe(5)); 
} catch (e) {
  console.error(e.message); 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why use Try/ catch and not just a console.log or return? &lt;/p&gt;

&lt;p&gt;With a &lt;code&gt;throw new Error&lt;/code&gt; message you're basically saying if this code goes goes wrong stop everything and throw an error. However, you might not want to stop everything in a large application, I mean, one monkey doesn't need to stop the whole show 🤭. So, you wrap the code that might throw an error in a &lt;code&gt;try...catch&lt;/code&gt; block so that you're prepared for the possibility of an error. If an error does occur you can handle it neatly, like perhaps deciding whether to just log the error, continue, or do something else. This way your program will not just stop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const expect = function (val) {
  return {
toBe: function (value) {
      if (val === value) {
        return true;
      } else {
        throw new Error("Not Equal");
      }
    },
notToBe: function (value) { 
      if (val !== value) {
        return true;
      } else {
        throw new Error("Equal");
      }
    }
  };
};

console.log("toBe", expect(5).toBe(5)); // 
try {
  console.log("notToBe", expect(5).notToBe(5)); 
} catch (e) {
  console.error(e.message); 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's all for today, Happy Coding!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>comparingvalues</category>
      <category>developer</category>
      <category>leetcode</category>
    </item>
    <item>
      <title>Closures! Closures! Closures!</title>
      <dc:creator>Shavon Harris</dc:creator>
      <pubDate>Wed, 21 Feb 2024 16:24:24 +0000</pubDate>
      <link>https://dev.to/shavonharrisdev/closures-closures-closures-3o3n</link>
      <guid>https://dev.to/shavonharrisdev/closures-closures-closures-3o3n</guid>
      <description>&lt;p&gt;From what I've seen in these coding challenges, you can never have enough practice with closures. So let's dive into an example of how to tackle them.&lt;/p&gt;

&lt;h2&gt;
  
  
  But First, What's a Closure Again?
&lt;/h2&gt;

&lt;p&gt;Imagine a big robot in a kitchen, whipping up an omelet. It assigns each task—chopping onions, cracking eggs, adding spices—to a fleet of tiny robots. These mini-robots venture off to their stations, but no matter where they are in the kitchen, they never forget their main goal, thanks to a recipe card (closure) that remembers everything the big robot told them.&lt;/p&gt;

&lt;p&gt;In programming terms, a closure is just like that recipe card—a function that remembers the variables from where it was created, no matter where you use it later.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Counter Challenge
&lt;/h3&gt;

&lt;p&gt;We're given a task: start with an integer n, and create a counter function that initially returns this integer. Then, each time you call it, it should return n + 1.&lt;/p&gt;

&lt;h4&gt;
  
  
  What to Do First?
&lt;/h4&gt;

&lt;p&gt;List out what we know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Parameter: an integer n&lt;/li&gt;
&lt;li&gt;Return: a function called counter&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If I'm returning a function, I know that I'm dealing with some sort of closure, so let's set that up.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Create a main function 
function createCounter(n) {
   // Return the counter function
   return function counter() {
      console.log(n);
   };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's test our setup:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const myCounter = createCounter(10); // We've got our recipe card!
myCounter(); // Prints out 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Something is not quite right though. We're not incrementing the integer n at all. Luckily, we can use an operator to easily increment.&lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding n++
&lt;/h3&gt;

&lt;p&gt;Using n++ is like telling one of our mini-robots to add a pinch of salt and then prepare to add another pinch the next time around. It increments n but gives us the value before the increment.&lt;/p&gt;

&lt;p&gt;Let's tweak our function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function createCounter(n) {
    // Return the counter function
    return function counter() {
        return n++;
    };
}

const myCounter = createCounter(10);
console.log(myCounter()); // 10
console.log(myCounter()); // 11
console.log(myCounter()); // 12

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Making It Stick
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Try It Out: Play around with this code in the developer tools or on a site like CodePen? 
Hmmm. What happens if you use ++n instead of n++?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Challenge Yourself: Can you modify the createCounter function to start counting down instead of up?&lt;/p&gt;

&lt;h3&gt;
  
  
  Wrapping Up
&lt;/h3&gt;

&lt;p&gt;Closures might seem confusing at first, I also had a really hard time grasping the concept, but with a bit of practice, they become a really useful tool in JavaScript. They're like having a secret recipe that only your function knows about, allowing you to write more predictable and bug-free code.&lt;/p&gt;

&lt;p&gt;Keep coding!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Encapsulation, Closure, and a Higher Order Function walk into a Hackathon.</title>
      <dc:creator>Shavon Harris</dc:creator>
      <pubDate>Tue, 20 Feb 2024 14:55:47 +0000</pubDate>
      <link>https://dev.to/shavonharrisdev/encapsulation-closure-and-a-higher-order-function-walk-into-a-hackathon-403b</link>
      <guid>https://dev.to/shavonharrisdev/encapsulation-closure-and-a-higher-order-function-walk-into-a-hackathon-403b</guid>
      <description>&lt;h2&gt;
  
  
  How do they work together?
&lt;/h2&gt;

&lt;p&gt;Closures, encapsulation, and higher-order functions are three cornerstone concepts in programming that, though distinct, often work hand in hand within JavaScript to craft more efficient and elegant code.&lt;/p&gt;

&lt;p&gt;Let's delve into a LeetCode problem that, at first glance, appears straightforward but beautifully weaves together these three essential programming concepts.&lt;/p&gt;

&lt;p&gt;So, let's create a function that returns another function! &lt;/p&gt;

&lt;h2&gt;
  
  
  Problem:
&lt;/h2&gt;

&lt;p&gt;Create a function that returns a "Hello World" function. &lt;/p&gt;

&lt;h3&gt;
  
  
  Step One: Define the Outer Function.
&lt;/h3&gt;

&lt;p&gt;We can define our outer function to have only one job. &lt;strong&gt;Encapsulate&lt;/strong&gt; (more on what this means below) the logic for creating another function. This particular function will not take any parameters, it's sole purpose is to return another function that when invoked, will say, "Hello world".&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function createHelloWorld() {
    // This function will return another function.
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step Two: Create the Inner Function
&lt;/h3&gt;

&lt;p&gt;Inside &lt;code&gt;createHelloWorld&lt;/code&gt;, we can define another function called &lt;code&gt;inner function&lt;/code&gt; for simplicity. This function is a one trick pony. It takes no parameters and when invoked, returns the string " Hello World". It's static and does one thing and one thing only, but it does it well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function innerFunction(){
  return "Hello World";
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step Three: Return the inner function
&lt;/h3&gt;

&lt;p&gt;So now &lt;code&gt;createHelloWorld&lt;/code&gt; returns &lt;code&gt;innerFunction&lt;/code&gt;. We are handing over the function itself - &lt;strong&gt;not triggering its execution&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This is where the encapsulation magic happens, neatly packaging the inner function within the outer one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function createHelloWorld() {
   function innerFunction() {
    return "Hello World";
   }
   return innerFunction;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step Four: Utilizing the function
&lt;/h3&gt;

&lt;p&gt;So what do we do now? Well we need to see how we can use this &lt;code&gt;createHelloWorld&lt;/code&gt; function. We invoke it and assign the result to a variable, &lt;code&gt;f&lt;/code&gt; in our case. The variable &lt;code&gt;f&lt;/code&gt; now holds a function, (our inner function) that we can call whenever we like.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const f = createHelloWorld();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(f()); // Outputs: "Hello World"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function createHelloWorld() {
  function innerFunction() {
    return "Hello World";
  }
  return innerFunction;
}
const f = createHelloWorld()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then we do call it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//log variable f
console.log("f", f());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That was a lot of work just to log Hello World. Why is this useful? &lt;/p&gt;

&lt;p&gt;Well turns out we used some pretty powerful concepts.&lt;/p&gt;

&lt;h4&gt;
  
  
  In simple terms
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Encapsulation: Like a secret compartment, it involves tucking away details within a function, revealing only what's necessary. That's the role of our outer function.It not only hides complexity but also protects the integrity of the inner functions operations by not exposing any internal variables or states directly to the other parts of the code. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Closures: Think of it as the inner function holding onto a piece of the outer function like a keepsake.This allows the inner function to access and use variables from the outer function even after the outer function has executed and returned.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Higher-Order Functions: These are the VIPs of the function world, transforming functions by either accepting them as parameters or creating new ones to return. Higher order functions are used in event handlers and array methods. You'll find them everywhere. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Wrapping it up
&lt;/h4&gt;

&lt;p&gt;This is one of the trickier topics to grasp for a beginner programmer but it really shows how flexible and powerful Javascript is as a programming language. Before you know it, you'll see these patterns popping up everywhere!&lt;/p&gt;

&lt;p&gt;Keep at it, and happy coding!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>leetcode</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
