<?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: Davinderpal Singh Rehal</title>
    <description>The latest articles on DEV Community by Davinderpal Singh Rehal (@davinderpalrehal).</description>
    <link>https://dev.to/davinderpalrehal</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%2F211641%2Fac2a2c24-e303-49cf-8a2f-0dccbe974001.jpeg</url>
      <title>DEV Community: Davinderpal Singh Rehal</title>
      <link>https://dev.to/davinderpalrehal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/davinderpalrehal"/>
    <language>en</language>
    <item>
      <title>1 Year of Consistent LeetCoding</title>
      <dc:creator>Davinderpal Singh Rehal</dc:creator>
      <pubDate>Thu, 24 Oct 2024 16:31:29 +0000</pubDate>
      <link>https://dev.to/davinderpalrehal/1-year-of-consistent-leetcoding-26d0</link>
      <guid>https://dev.to/davinderpalrehal/1-year-of-consistent-leetcoding-26d0</guid>
      <description>&lt;p&gt;It was September 2023, a time when change was in the air. I found myself between jobs, and as luck would have it, Google had just kicked off a hiring wave in Nairobi, Kenya. I came across the Google Foobar challenge, a coding gauntlet I'd heard whispers about. With a decade of coding behind me, I felt it was time to give it a shot.&lt;/p&gt;

&lt;p&gt;The first challenge was like a warm-up—a binary search problem that felt like a gentle jog. The second and third problems followed suit, manageable and engaging. But then, the fourth challenge appeared, and it was like hitting a steep hill. This was my introduction to graphs, a concept that had somehow eluded me in my career.&lt;/p&gt;

&lt;p&gt;Feeling a bit overwhelmed, I turned to YouTube for guidance. As I watched the videos, explaining the algorithms and concepts, I couldn't help but feel a touch of imposter syndrome. How had I worked as a software engineer for so long without encountering these problems?&lt;/p&gt;

&lt;p&gt;To cut to the chase: a Google recruiter did reach out. It led to a phone screen and three rounds of interviews. Unfortunately, I didn't make it through. But this experience kindled a fire in me to master data structures and algorithms (DSA).&lt;/p&gt;

&lt;p&gt;Determined to improve, I found the LeetCode 75 question set. I tackled it with zeal, making notes and learning heaps. Halfway through, I landed my first contract of the year, which meant less time for practice. To keep pushing myself, I started doing LeetCode’s daily challenges. This eventually led to a 365+ day streak.&lt;/p&gt;

&lt;p&gt;I tracked every question in a (Google Sheet)[&lt;a href="https://docs.google.com/spreadsheets/d/14GXh2RLulTo2vLeLzOuOqqCm5DQmCI-OK8MJvi9yJz8/edit?usp=sharing" rel="noopener noreferrer"&gt;https://docs.google.com/spreadsheets/d/14GXh2RLulTo2vLeLzOuOqqCm5DQmCI-OK8MJvi9yJz8/edit?usp=sharing&lt;/a&gt;]. My system was simple, if I can't solve it within the time I mark it as a fail:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Easy: Solve within 25 minutes&lt;/li&gt;
&lt;li&gt;Medium: Solve within 45 minutes&lt;/li&gt;
&lt;li&gt;Hard: Solve within 1 hour&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every two weeks, I'd revisit a failed question and give it another shot. Here’s what I learned along the way:&lt;/p&gt;

&lt;h2&gt;
  
  
  Some Topics Pop Up More Than Others
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Arrays (Lists)
&lt;/h3&gt;

&lt;p&gt;Following (PrimeAgen)[&lt;a href="https://www.youtube.com/@ThePrimeTimeagen" rel="noopener noreferrer"&gt;https://www.youtube.com/@ThePrimeTimeagen&lt;/a&gt;]? Then you know arrays are fundamental to DSA. A year in, I can vouch for it. Efficient array manipulation is essential.&lt;/p&gt;

&lt;h3&gt;
  
  
  Strings
&lt;/h3&gt;

&lt;p&gt;Strings are like arrays, but with a twist. They’re immutable in most languages, meaning any change requires creating a new string. This was a lesson learned the hard way.&lt;/p&gt;

&lt;h3&gt;
  
  
  Hash Tables
&lt;/h3&gt;

&lt;p&gt;When mapping data, hash tables are a go-to due to O(1) insertion and deletion. Initially, I thought hash tables were complex, but I learned they’re essentially objects.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sorting
&lt;/h3&gt;

&lt;p&gt;Know at least two sorting algorithms. Bubble Sort is classic and quick to code. Merge Sort is powerful but takes practice. And there’s Bucket Sort—mind-blowingly fast but space-intensive.&lt;/p&gt;

&lt;h3&gt;
  
  
  Greedy Algorithms
&lt;/h3&gt;

&lt;p&gt;Greedy problems are intriguing. They allow early exits once conditions are met. These problems still trip me up sometimes, but they’re rewarding to solve.&lt;/p&gt;

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

&lt;p&gt;DSA can become addictive. Once you start spotting patterns, solving problems becomes almost mechanical. I’m hoping for another shot at Google. Until then, I’ll keep grinding LeetCode and getting better at this craft.&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>learning</category>
      <category>datastructures</category>
    </item>
    <item>
      <title>100-Day Daily Streak on LeetCode</title>
      <dc:creator>Davinderpal Singh Rehal</dc:creator>
      <pubDate>Wed, 31 Jan 2024 18:16:01 +0000</pubDate>
      <link>https://dev.to/davinderpalrehal/100-day-daily-streak-on-leetcode-1lfa</link>
      <guid>https://dev.to/davinderpalrehal/100-day-daily-streak-on-leetcode-1lfa</guid>
      <description>&lt;p&gt;I almost can't believe it's been 100 days of me grinding LeetCode! There was a point when I thought I completely lost my streak, and that point was 28th Jan 2024. &lt;/p&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%2Fdippyahe42l3tnmiyhx2.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%2Fdippyahe42l3tnmiyhx2.png" alt="Image description" width="694" height="242"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You see that gap in my calendar, that was the day our house was broken into and a lot of things were stolen, vandalised and ransacked. Before anyone complains about the semantics of that sentence I know ransacking is the act of stealing and vandalising, I am still in trauma so let me vent. The only way I could get my streak back was by using the time travel tickets.&lt;/p&gt;

&lt;p&gt;As much as I would love to take you guys through all the details of the robbery, I doubt anyone is interested, so let's talk about what I learned during these 96 days (the last couple of days rolled into 1).&lt;/p&gt;

&lt;h2&gt;
  
  
  1. ALWAYS MAKE NOTES
&lt;/h2&gt;

&lt;p&gt;LeetCode has this amazing feature that lets you take notes, especially when you submit a solution the notes panel pops up. This has been so useful for me to make my own notes as soon as I read the problem statement.&lt;/p&gt;

&lt;p&gt;If it's an easy problem and the solution is obvious then I normally don't bother making notes, but for a bit more complicated problems my notes start off as a speculation of what approach I should use, then when a solution has been arrived I add to my notes what has changed. Some of my notes look like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A DFS approach would probably make sense here since we are looking for diff between ancestor and nodes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This note would evolve to something like.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[ Time taken: 24 m 46 s ] had the right idea but had a hard time converting it to code.

A DFS approach would probably make sense here since we are looking for diff between ancestor and nodes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Don't Rush Into Code
&lt;/h2&gt;

&lt;p&gt;I would usually start the timer as soon as I started reading the problem. This put some pressure on me to read faster and start coding faster, but the more I got comfortable with the timer being on the more time I took to actually understand the problem better, go over the inputs and results until I could form some sort of a mental image. There have been so many times that it took me 10 min just to understand what the question needed and once that clicked, it would take me another 10 min to convert the idea into code.&lt;/p&gt;

&lt;p&gt;Writing the code is usually not the hardest part, it's usually coming up with an approach that is hard. This is especially true for Medium problems, Hard problems are just there so you have suicidal thoughts.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Draw a Line in the Sand
&lt;/h2&gt;

&lt;p&gt;In the beginning, I would spend more than an hour at a problem, write some code and keep on iterating on it until I passed more and more test cases. This is normally the approach most of us take when working on a feature request but it's not the best approach for LeetCode. A lot of times there is a trick that you are missing, or you are lacking a better understanding of the problem itself. At some point in time, its time to try a new approach or to watch a NeetCode video (no affiliation). My suggestion is about the 50 min mark you should probably change approaches and add that to your notes.&lt;/p&gt;

&lt;p&gt;Don't let your ego get the best of you, be humble enough to know that your approach isn't working.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Know Your Language
&lt;/h2&gt;

&lt;p&gt;I come from a JavaScript background, and although I have used Python at work for some reason I never enjoyed it as much as I did JavaScript. That is till I started the LeetCode75 study plan, and at that point I found out how many data structures JS is missing.&lt;/p&gt;

&lt;p&gt;There were many times that I had the right approach in mind and managed to code the correct solution and then I discovered that Python has a much better way of doing something.&lt;/p&gt;

&lt;p&gt;One thing I use all the time is &lt;code&gt;defaultdict&lt;/code&gt; which when I discovered made coding up solutions much faster. 90-something days in and I am still discovering new ways in which Python makes writing algorithms easier. The more I use Python for LeetCode the more I enjoy it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;While I am very far from being able to solve any problem that LeetCode throws my way, every time I do a problem I feel slightly more confident. The biggest issue I seem to have is in Dynamic Programming problems. Looking forward to making a new post with a 200-day streak.&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>Why I moved from JavaScript to Python for LeetCode</title>
      <dc:creator>Davinderpal Singh Rehal</dc:creator>
      <pubDate>Tue, 02 Jan 2024 18:52:38 +0000</pubDate>
      <link>https://dev.to/davinderpalrehal/why-i-moved-from-javascript-to-python-for-leetcode-3pm1</link>
      <guid>https://dev.to/davinderpalrehal/why-i-moved-from-javascript-to-python-for-leetcode-3pm1</guid>
      <description>&lt;p&gt;Cover image generated using Microsoft Designer.&lt;/p&gt;

&lt;p&gt;In December 2023 I started a &lt;a href="https://www.youtube.com/channel/UCKKsRVUL1QVFi3hDIeaj4Sw"&gt;YouTube channel&lt;/a&gt; more for the purpose of learning how to tackle coding interview and getting a better understanding of Data Structures and Algorithms. I started the LeetCode 75 study plan and since my strongest language is JavaScript that's what I started to use. After creating about 9 videos and going through more than 70% of the study plan I am ditching JavaScript for Python and here is why.&lt;/p&gt;

&lt;h2&gt;
  
  
  Data Structures
&lt;/h2&gt;

&lt;p&gt;A major difference between JavaScript and Python is the built-in data structures. JavaScript has arrays and objects, which are versatile but sometimes inefficient. Python has lists, tuples, sets, and dictionaries, which are more specialized and optimized for different purposes. For example, sets are useful for checking membership and removing duplicates, while dictionaries are useful for mapping keys to values. Python also has some data structures that JavaScript does not have, such as stacks, queues, heaps, and dequeues. These data structures are often needed for solving LeetCode problems, especially those involving graphs, trees, and priority queues. For example, here is how to implement a queue using a list in JavaScript and using a dequeue in Python:&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;// JavaScript&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;queue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
&lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// enqueue 1&lt;/span&gt;
&lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// enqueue 2&lt;/span&gt;
&lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// dequeue 1&lt;/span&gt;
&lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// dequeue 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Python
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;collections&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;deque&lt;/span&gt;
&lt;span class="n"&gt;queue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;deque&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# enqueue 1
&lt;/span&gt;&lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# enqueue 2
&lt;/span&gt;&lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;popleft&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# dequeue 1
&lt;/span&gt;&lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;popleft&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# dequeue 2
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, Python has a built-in dequeue class that supports efficient appending and popping from both ends. JavaScript has to use a list and shift the elements every time a dequeue operation is performed, which is costly in terms of time and space.&lt;/p&gt;

&lt;h2&gt;
  
  
  Syntax
&lt;/h2&gt;

&lt;p&gt;I have been using Python professionally for the past 6+ years and JavaScript for probably 10, that being said everytime I move from Python to JavaScript I kind of miss how less code I have to write for Python. JavaScript uses curly braces {} to define code blocks, such as functions, loops, and conditionals. Python uses indentation to define code blocks, which makes the code more readable and concise. For example, here is how a function to check if a number is even looks like in JavaScript and Python:&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;// JavaScript&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;isEven&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&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;n&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&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="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&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;false&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Python
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;isEven&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, Python has less boilerplate code and does not require semicolons or parentheses around the condition. Python also has some syntactic features that JavaScript does not have, such as list comprehensions, multiple assignment, and tuple unpacking. These features can make the code more elegant and expressive. For example, here is how to swap two variables in JavaScript and Python:&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;// JavaScript&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Python
&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, Python can swap two variables in one line, while JavaScript needs three lines and a temporary variable. This can save time and space when coding.&lt;/p&gt;

&lt;p&gt;Before I piss off the rest of my JavaScript brothers and sisters, newer JS syntax does let us do swapping of values in 1 line as well, but I have not had the most consistent results, especially in the context of LeetCode. If you are using modern JS you can do.&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;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Libraries
&lt;/h2&gt;

&lt;p&gt;Another difference between JavaScript and Python is their libraries. JavaScript has a standard library that provides some basic functionality, such as math, string, and date operations. However, it does not have many advanced modules or packages that are useful for solving LeetCode problems, such as data structures, algorithms, or testing frameworks. Python has a rich and comprehensive standard library that covers a wide range of topics, such as data structures, algorithms, math, statistics, random, itertools, collections, functools, and unittest. Python also has many third-party libraries that can be easily installed and imported, such as numpy, scipy, pandas, matplotlib, and sklearn. These libraries can provide more functionality and performance for solving LeetCode problems, especially those involving numerical computation, data analysis, or machine learning. For example, here is how to calculate the mean and standard deviation of a list of numbers in JavaScript and Python:&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;// JavaScript&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;sumOfSquares&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;sumOfSquares&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;mean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;variance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sumOfSquares&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;nums&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;-&lt;/span&gt; &lt;span class="nx"&gt;mean&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;mean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;std&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;variance&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;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;std&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Python
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;numpy&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;
&lt;span class="n"&gt;nums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;mean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;std&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, Python can use the numpy library to calculate the mean and standard deviation of a list of numbers in one line, while JavaScript has to use a loop and some math formulas.&lt;/p&gt;

&lt;h2&gt;
  
  
  Video Support
&lt;/h2&gt;

&lt;p&gt;Everytime I get stuck in a problem, which happens more often than I admit, it is as soon as I search for the problem on YouTube the first few that results that come up solve the problem with Python, though it is not super hard to translate the code to JavaScript given that my primary goal is to get a deeper understanding of the Data Structures and Algorithms it felt a bit counter-intuitive to spend the limited time I have translating code, I would rather attach a debugger to the code and step through with different inputs.&lt;/p&gt;

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

&lt;p&gt;If you follow me on YouTube, LinkedIn and Dev.to expect to see more Python content coming soon :).&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Taking on the JavaScriptmas Challenge</title>
      <dc:creator>Davinderpal Singh Rehal</dc:creator>
      <pubDate>Mon, 28 Dec 2020 11:40:27 +0000</pubDate>
      <link>https://dev.to/davinderpalrehal/taking-on-the-javascriptmas-challenge-4a6h</link>
      <guid>https://dev.to/davinderpalrehal/taking-on-the-javascriptmas-challenge-4a6h</guid>
      <description>&lt;p&gt;Taking on the JavaScriptmas Challenge&lt;/p&gt;

&lt;p&gt;December 2020 started off a little bit stressfully, on about 14th I got an email from Scrimba about a new challenge that focused on front-end web development. Below are all the scrims from the challenges.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a rel="noreferrer noopener" href="https://scrimba.com/scrim/coc78466cb2db27e302592ba2"&gt;Candies&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel="noreferrer noopener" href="https://scrimba.com/scrim/coe71477fbc703b2b2a9af899"&gt;Deposit Profit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel="noreferrer noopener" href="https://scrimba.com/scrim/co98f4f0bb0530e689565d501"&gt;Chunky Monkey&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel="noreferrer noopener" href="https://scrimba.com/scrim/co447409fb98279dfc04d1419"&gt;Century From Year&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel="noreferrer noopener" href="https://scrimba.com/scrim/cofa54dc8b96649a80bba7490"&gt;Reverse a String&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel="noreferrer noopener" href="https://scrimba.com/scrim/co1734745b00be46b47340bfc"&gt;Sort by Length&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel="noreferrer noopener" href="https://scrimba.com/scrim/codbd4583ab07ad302e662af3"&gt;Count Vowel Consonant&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel="noreferrer noopener" href="https://scrimba.com/scrim/codae40dbb68dea1ab47894f8"&gt;The Rolling Dice&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a rel="noreferrer noopener" href="https://scrimba.com/scrim/codfc49c8ae4000f3753190fc"&gt;Sum Od&lt;/a&gt;&lt;a href="https://scrimba.com/scrim/codfc49c8ae4000f3753190fc" rel="noreferrer noopener"&gt;d&lt;/a&gt;&lt;a rel="noreferrer noopener" href="https://scrimba.com/scrim/codfc49c8ae4000f3753190fc"&gt; Fibonacci Numbers&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://scrimba.com/scrim/co420468a9f27e19cc8c3484a" rel="noreferrer noopener"&gt;Adjacent Elements Product&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://scrimba.com/scrim/co58e4880b039c1e24f389d6c" rel="noreferrer noopener"&gt;Avoid Obstacles&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://scrimba.com/scrim/co16c4f918ebec4df8e3a1e80" rel="noreferrer noopener"&gt;Valid Time&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://scrimba.com/scrim/co0364c8f8b80164e288e5e5c" rel="noreferrer noopener"&gt;Extract Each Kth&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://scrimba.com/scrim/coe0e4a48814fdb15ca0fd08d" rel="noreferrer noopener"&gt;Maximal Adjacent Difference&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://scrimba.com/scrim/co23348c4a8fdf75221afc357" rel="noreferrer noopener"&gt;Carousel&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://scrimba.com/scrim/codaa4cf797c3d1d57e09dcdd" rel="noreferrer noopener"&gt;Insert Dashes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://scrimba.com/scrim/co93f464382192d4838be37bd" rel="noreferrer noopener"&gt;Different symbols naive&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://scrimba.com/scrim/co01a446782e88d79889b0cbe" rel="noreferrer noopener"&gt;Array previous less&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://scrimba.com/scrim/co7d449deb5b1e61285bb1aa5" rel="noreferrer noopener"&gt;Alphabet subsequence&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://scrimba.com/scrim/co1a843ee8763980a4be7dcf4" rel="noreferrer noopener"&gt;Domain Type&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://scrimba.com/scrim/co38f4492b5dafec0d2de74ae" rel="noreferrer noopener"&gt;Sum of two&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://scrimba.com/scrim/cod5b45c4933ce6c9ab5c6bd4" rel="noreferrer noopener"&gt;Extract matrix column&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://scrimba.com/scrim/co1b94748926728f17507b27a" rel="noreferrer noopener"&gt;Social Media input&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://scrimba.com/scrim/coeea402cb2496918d006f3f2" rel="noreferrer noopener"&gt;Test your agility&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
    </item>
    <item>
      <title>Web Components - Part 1</title>
      <dc:creator>Davinderpal Singh Rehal</dc:creator>
      <pubDate>Sat, 05 Sep 2020 15:42:48 +0000</pubDate>
      <link>https://dev.to/davinderpalrehal/web-components-part-1-4do5</link>
      <guid>https://dev.to/davinderpalrehal/web-components-part-1-4do5</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2_xTD7Ek--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AfOqlfB6UV7lqmAKg.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2_xTD7Ek--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AfOqlfB6UV7lqmAKg.jpg" alt="" width="512" height="400"&gt;&lt;/a&gt;Web components logo&lt;/p&gt;

&lt;p&gt;Front-end web development has become incredibly complex over the years. Back in the day the only things you needed to become a front-end web developer was &lt;code&gt;HTML&lt;/code&gt;, &lt;code&gt;CSS&lt;/code&gt; and some basic &lt;code&gt;JavaScript&lt;/code&gt; or if you were fancy then you would learn &lt;code&gt;jQuery&lt;/code&gt;. But the issue ‘back in the day’ was that browsers would rendering of elements would be different so as web developer we would have to use hacks to make things look acceptable.&lt;/p&gt;

&lt;p&gt;Today, we have a number of front-end libraries/frameworks that offer front-end developers some incredible super powers, the problem with them is that most of us don’t utilise all the features that we import with the libraries/frameworks. To make things even worse depending on the preference of the developers working on a project you end up using different libraries for different projects. Wouldn’t it be nice if we could have a set of components that could be used across projects and across libraries/frameworks?&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;NB: Henceforth when I &lt;code&gt;libraries&lt;/code&gt; I am referring to &lt;code&gt;libraries/frameworks&lt;/code&gt;&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;Enter&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SOnDzajz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/1%2A0S9pD4xQe-OSGiS13v0DzQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SOnDzajz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/1%2A0S9pD4xQe-OSGiS13v0DzQ.png" alt="" width="500" height="300"&gt;&lt;/a&gt;Web Components title surrounded by fireworks&lt;/p&gt;

&lt;p&gt;[Insert Bollywood Explosions here]&lt;/p&gt;

&lt;p&gt;Web Components were originally introduced in 2011 and browsers started implementing them much later.&lt;/p&gt;








&lt;h3&gt;What are Web Components?&lt;/h3&gt;





&lt;p&gt;Web components are a set of web APIs that allow developers to make encapsulated components that have their own set of functions and styling.&lt;/p&gt;

&lt;p&gt;Web components are made up of&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Custom Elements&lt;/li&gt;
&lt;li&gt;Shadow DOM&lt;/li&gt;
&lt;li&gt;ES Modules&lt;/li&gt;
&lt;li&gt;HTML Templates&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For this article I will be focusing on Custom Elements.&lt;/p&gt;








&lt;h3&gt;Custom Elements&lt;/h3&gt;





&lt;h4&gt;The need for Custom Elements&lt;/h4&gt;





&lt;p&gt;HTML provides a number of tags that have been serving the web well but the issue comes in when you try to get a consistent look and feel for various HTML tags. CSS tries to solve this by having a global style sheet that houses all the UI styles, however, this gets very difficult to manage with multiple developers since most developers would end up writing styles at the bottom of CSS files which makes it difficult to manage the styling.&lt;/p&gt;

&lt;p&gt;To address this various frameworks and libraries have come up such as React and VueJS that let you encapsulate/scope styles. This solution of course works well when you have the freedom of importing libraries but what if you would like to achieve the same without using a particular library or framework. This is where &lt;code&gt;Custom Elements&lt;/code&gt; in. As a PoC lets look at how an ecommerce front-end would look like with &lt;code&gt;Custom Elements&lt;/code&gt; .&lt;/p&gt;

&lt;h4&gt;The Design&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QKUA_5KO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/1200/1%2A98CJQ4f42SWlPMYTMNS1SQ.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QKUA_5KO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/1200/1%2A98CJQ4f42SWlPMYTMNS1SQ.jpeg" alt="" width="800" height="486"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Very simple design of an e-commerce store that we will be implementing to figure out how &lt;code&gt;Custom Elements&lt;/code&gt; work.&lt;/p&gt;

&lt;p&gt;Basic HTML Implementation&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codepen.io/davinderpal/pen/abNwoBp"&gt;https://codepen.io/davinderpal/pen/abNwoBp&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;CodePen showing implementation using basic HTML&lt;/p&gt;

&lt;h4&gt;Converting To Custom Elements&lt;/h4&gt;

&lt;p&gt;We have tried as much as possible to provide semantic meaning to the markup but there are places that the tag chosen doesn’t make sense. If a different developer was to continue this project they would would probably use different mark up, e.g. instead of using a &lt;code&gt;p&lt;/code&gt; tag for the price they could use a &lt;code&gt;div&lt;/code&gt; , this won’t cause any issues in styling since we are using the classes to do the styling but it does make our codebase very inconsistent, therefore, harder to manage.&lt;/p&gt;

&lt;p&gt;Instead we could convert the mark up to a custom element and abstract the implementation. So having a custom element such as&lt;/p&gt;

&lt;pre&gt;&amp;lt;shop-product&lt;br&gt;  image='&lt;a href="https://source.unsplash.com/200x200/?shoes%27" rel="noreferrer noopener"&gt;https://source.unsplash.com/200x200/?shoes'&lt;/a&gt;&lt;br&gt;  name='Nikey Shoes'&lt;br&gt;  price='$40'&lt;br&gt;  rating='1'&lt;br&gt;  variants='100'&lt;br&gt;&amp;gt;&amp;lt;/shop-product&amp;gt;&lt;/pre&gt;

&lt;p&gt;would ensure that any developer creating a new shop page would have a consistent way of describing the elements. There is an added advantage of making our code base slightly more readable.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codepen.io/davinderpal/pen/bGpaNpG"&gt;https://codepen.io/davinderpal/pen/bGpaNpG&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see from the Pen above both implementations look exactly the same to the end-user, but the below one has a more consistent codebase with less chances of a different developer changing markup in a different page. This also makes the code reusable across pages and even sites.&lt;/p&gt;








&lt;h3&gt;The Implementation&lt;/h3&gt;





&lt;p&gt;To register a custom element you have to &lt;code&gt;JS&lt;/code&gt; with the &lt;code&gt;window.customElements.define&lt;/code&gt; (or &lt;code&gt;customElements.define&lt;/code&gt;)function which takes in 2 required arguments and 1 optional argument.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The first argument is the &lt;code&gt;name&lt;/code&gt; of the custom element that you are creating. The docs require that the &lt;code&gt;name&lt;/code&gt; of the custom element needs to include a hyphen &lt;code&gt;—&lt;/code&gt; in its name. This is usually a string.&lt;/li&gt;
&lt;li&gt;The second argument is the &lt;code&gt;constructor&lt;/code&gt; that defines how the element will actually be implemented. This is usually a class.&lt;/li&gt;
&lt;li&gt;The third argument is an &lt;code&gt;options&lt;/code&gt; object. This is out of scope for this article but in the coming articles I will be covering this more.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The way &lt;code&gt;JS&lt;/code&gt; is structured you will need to declare the implementation class before trying to register the custom element.&lt;/p&gt;

&lt;p&gt;The implementation class extends the &lt;code&gt;HTMLElement&lt;/code&gt; class and as such inherits all the functions and events that a normal HTML element would have. Within the new class we only require to provide the &lt;code&gt;constructor&lt;/code&gt; which starts by calling the &lt;code&gt;super()&lt;/code&gt; function.&lt;/p&gt;

&lt;pre&gt;class Product extends HTMLElement {&lt;br&gt;  constructor () {&lt;br&gt;    super()&lt;br&gt;  }&lt;br&gt;}&lt;/pre&gt;

&lt;p&gt;Within the &lt;code&gt;constructor&lt;/code&gt; you would create the HTML elements that are required for the implementation of the custom element. This code can be seen within the CodePen above.&lt;/p&gt;

&lt;h3&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;Creating custom elements really helps have consistent and reusable code for web applications. This also lets you hide some of the inner workings which let developers who are not very familiar with front-end development still write front-end code.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Moving from Apache2 to Nginx Webserver</title>
      <dc:creator>Davinderpal Singh Rehal</dc:creator>
      <pubDate>Sun, 03 Nov 2019 13:38:47 +0000</pubDate>
      <link>https://dev.to/davinderpalrehal/moving-from-apache2-to-nginx-webserver-2n6a</link>
      <guid>https://dev.to/davinderpalrehal/moving-from-apache2-to-nginx-webserver-2n6a</guid>
      <description>&lt;p&gt;Recently at work I had to move our dev, staging and production web servers from Apache2 to Nginx. The reason for this move is that in the rest of the company we are using Nginx for all the backend services but when the web sites were set up for some reason the consultants set up Apache2. This became a problem mostly for management. In this article, I will be going step-by-step through the process. I will be using a vagrant box for all the screenshots but the steps I did were the same for the actual servers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Server
&lt;/h2&gt;

&lt;p&gt;For my test server I am using &lt;code&gt;vagrant&lt;/code&gt; and &lt;code&gt;ubuntu/bionic64&lt;/code&gt;. On this server I had &lt;code&gt;apache2.4&lt;/code&gt;, &lt;code&gt;php7.2&lt;/code&gt; and &lt;code&gt;mysql5.7&lt;/code&gt; a basic &lt;code&gt;LAMP&lt;/code&gt; stack. Which I will be moving to a &lt;code&gt;LEMP&lt;/code&gt; stack replacing Apache with Nginx.&lt;/p&gt;

&lt;p&gt;This server has a WordPress site running on it, with WordPress 5.2.4 running. We will have achieved our goal once we can see this WordPress site running on Nginx and PHP 7.3. Oh and we have a bunch of &lt;code&gt;envvars&lt;/code&gt; set up of the Apache2 instance that will also have to transfer to the Nginx installation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing Nginx
&lt;/h2&gt;

&lt;p&gt;Pretty straight forward. Start off by checking for updates.&lt;/p&gt;

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

&lt;span class="nb"&gt;sudo &lt;/span&gt;apt update
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt upgrade



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

&lt;/div&gt;
&lt;p&gt;Actually install Nginx.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

sudo apt install nginx


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

&lt;/div&gt;
&lt;p&gt;Nginx is now installed but is not running, since we have Apache2 already installed and configured on the system, Apache2 will be using port &lt;code&gt;80&lt;/code&gt; which means that to avoid conflicts we should run Nginx on a different port to test that everything is running well. I choose the port &lt;code&gt;8080&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Configure Nginx with Port 8080
&lt;/h3&gt;

&lt;p&gt;There is a default website that is set up with Nginx (just like Apache) just to make sure that things work. The config can be seen at.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

sudo vim /etc/nginx/sites-available/default


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

&lt;/div&gt;
&lt;p&gt;This should open up a file that looks like.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;On line &lt;code&gt;22&lt;/code&gt; and &lt;code&gt;23&lt;/code&gt; we are gonna change &lt;code&gt;80&lt;/code&gt; to &lt;code&gt;8080&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 nginx
listen 8080 default_server;
listen [::]:8080 default_server;


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

&lt;/div&gt;
&lt;p&gt;Let's test the config quickly before we start the Nginx service.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 bash
sudo nginx -t


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

&lt;/div&gt;
&lt;p&gt;If all is well, let's write a small &lt;code&gt;HTML&lt;/code&gt; page that will just let us know that the server is up. Line &lt;code&gt;41&lt;/code&gt; shows me the root of the server. I changed server root to make sure that am not using the same directory as Apache.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

root /srv/www/html&lt;span class="p"&gt;;&lt;/span&gt;


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

&lt;/div&gt;
&lt;p&gt;That's the directory I will be adding the &lt;code&gt;index.html&lt;/code&gt; file. Now we can start the server.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nb"&gt;sudo &lt;/span&gt;service nginx start


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

&lt;/div&gt;
&lt;p&gt;To test out the new config.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

curl http://localhost:8080


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

&lt;/div&gt;
&lt;p&gt;Which should return the new &lt;code&gt;HTML&lt;/code&gt; file that I had created. Moving on to install &lt;code&gt;PHP&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Installing PHP
&lt;/h2&gt;

&lt;p&gt;Right now we have &lt;code&gt;PHP7.2&lt;/code&gt; installed on the system, since there has been a bit of a scare with the whole &lt;code&gt;PHP7.2&lt;/code&gt; and &lt;code&gt;Nginx&lt;/code&gt; config, will be moving to &lt;code&gt;PHP7.3&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;First off, Ubuntu doesn't know where to get &lt;code&gt;PHP7.3&lt;/code&gt; from so we need to add the repository.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nb"&gt;sudo &lt;/span&gt;add-apt-repository ppa:ondrej/php
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt update
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt upgrade


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

&lt;/div&gt;
&lt;p&gt;Actually installing &lt;code&gt;PHP7.3&lt;/code&gt; and some extensions. These are just the extensions I need, feel free to add or remove extensions as required.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;php7.3
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;php7.3-cli php7.3-fpm php7.3-pdo php7.3-mysql php7.3-zip  php7.3-mbstring php7.3-curl php7.3-xml php7.3-bcmath php7.3-json


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

&lt;/div&gt;
&lt;p&gt;Once all the extensions have been installed, its time to edit the &lt;code&gt;Nginx&lt;/code&gt; config file again to tell it that the website we have uses &lt;code&gt;PHP&lt;/code&gt;. The config file is below, with all the comments stripped out and comments only on the lines that have been changed.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Test the config to make sure there are no syntax errors.&lt;/p&gt;

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

&lt;span class="nb"&gt;sudo &lt;/span&gt;nginx &lt;span class="nt"&gt;-t&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;You will probably get an error because there is no env file at &lt;code&gt;/srv/config/default.nginx.conf&lt;/code&gt;. Just add a blank file there, for now, will explain what it does later.&lt;/p&gt;

&lt;p&gt;Hopefully, that solved your error. Now let's change the &lt;code&gt;index.html&lt;/code&gt; file to &lt;code&gt;index.php&lt;/code&gt; and add some basic &lt;code&gt;PHP&lt;/code&gt; code to see if things work. Also, be sure to set the appropriate permissions to the &lt;code&gt;index.php&lt;/code&gt; file. I like going with.&lt;/p&gt;

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

&lt;span class="nb"&gt;sudo chmod &lt;/span&gt;755 index.php


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

&lt;/div&gt;

&lt;p&gt;Restart the &lt;code&gt;Nginx&lt;/code&gt; server&lt;/p&gt;

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

&lt;span class="nb"&gt;sudo &lt;/span&gt;service nginx restart


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

&lt;/div&gt;

&lt;p&gt;And you should see the &lt;code&gt;PHP&lt;/code&gt; file you wrote. I printed out the &lt;code&gt;phpinfo()&lt;/code&gt; function to make sure configurations are good.&lt;/p&gt;

&lt;h2&gt;
  
  
  Server Env Variables
&lt;/h2&gt;

&lt;p&gt;I like having some of my app configurations saved in the &lt;code&gt;$_SERVER&lt;/code&gt; superglobal. This comes in handy when your moving your app between &lt;code&gt;dev&lt;/code&gt;, &lt;code&gt;stage&lt;/code&gt; and &lt;code&gt;prod&lt;/code&gt; servers and don't want to keep on changing your db passwords or API endpoints.&lt;/p&gt;

&lt;p&gt;The file that we touched &lt;code&gt;/srv/config/default.nginx.conf&lt;/code&gt;, I will be adding the following lines of code in it.&lt;/p&gt;

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

&lt;span class="k"&gt;fastcgi_param&lt;/span&gt;   &lt;span class="s"&gt;APP_ENV&lt;/span&gt;         &lt;span class="s"&gt;dev&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;fastcgi_param&lt;/span&gt;   &lt;span class="s"&gt;APP_ENDPOINT&lt;/span&gt;    &lt;span class="s"&gt;https://dev.server.com&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;fastcgi_param&lt;/span&gt;   &lt;span class="s"&gt;DB_HOST&lt;/span&gt;         &lt;span class="s"&gt;localhost&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;fastcgi_param&lt;/span&gt;   &lt;span class="s"&gt;DB_USER&lt;/span&gt;         &lt;span class="s"&gt;root&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;fastcgi_param&lt;/span&gt;   &lt;span class="s"&gt;DB_PASS&lt;/span&gt;         &lt;span class="s"&gt;password&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;The format is usually&lt;/p&gt;

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

&lt;span class="k"&gt;fastcgi_param&lt;/span&gt;  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="kn"&gt;VAR_NAME&lt;/span&gt;&lt;span class="err"&gt;}&lt;/span&gt;      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="kn"&gt;VAR_VALUE&lt;/span&gt;&lt;span class="err"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Feel free to add any other env variables. Once your happy test config and restart &lt;code&gt;Nginx&lt;/code&gt;. You should see your new variables in the &lt;code&gt;phpinfo()&lt;/code&gt; function output as below.&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%2Fi.ibb.co%2FnbMyr3n%2FScreenshot-2019-11-03-at-16-33-14.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%2Fi.ibb.co%2FnbMyr3n%2FScreenshot-2019-11-03-at-16-33-14.png" alt="PHP Server variables"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Pay close attention to the last 5 values.&lt;/p&gt;

&lt;p&gt;And thats it, just move all your files to the relevant folder to do a final test. When ready you can stop the &lt;code&gt;Apache&lt;/code&gt; service.&lt;/p&gt;

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

&lt;span class="nb"&gt;sudo &lt;/span&gt;service apache2 stop


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

&lt;/div&gt;

&lt;p&gt;Change the port number from &lt;code&gt;8080&lt;/code&gt; in the &lt;code&gt;Nginx&lt;/code&gt; config, to &lt;code&gt;80&lt;/code&gt; and restart &lt;code&gt;Nginx&lt;/code&gt;, and we are done.&lt;/p&gt;

&lt;p&gt;Well done!&lt;/p&gt;

</description>
      <category>nginx</category>
      <category>php</category>
      <category>apache</category>
      <category>webserver</category>
    </item>
  </channel>
</rss>
