<?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: Amit Mehta</title>
    <description>The latest articles on DEV Community by Amit Mehta (@apmfree78).</description>
    <link>https://dev.to/apmfree78</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%2F894783%2Fdfaf2fb2-436f-4169-975c-995b7df70d1d.jpeg</url>
      <title>DEV Community: Amit Mehta</title>
      <link>https://dev.to/apmfree78</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/apmfree78"/>
    <language>en</language>
    <item>
      <title>How to Easily Add and Remove Any Elements from a JavaScript Array</title>
      <dc:creator>Amit Mehta</dc:creator>
      <pubDate>Mon, 08 Aug 2022 19:00:00 +0000</pubDate>
      <link>https://dev.to/apmfree78/how-to-easily-add-and-remove-any-elements-from-a-javascript-array-5796</link>
      <guid>https://dev.to/apmfree78/how-to-easily-add-and-remove-any-elements-from-a-javascript-array-5796</guid>
      <description>&lt;p&gt;In this post I want to discuss a super useful way to add and remove elements from &lt;em&gt;ANY index&lt;/em&gt; in a javaScript array.&lt;/p&gt;

&lt;p&gt;You're probably familiar with &lt;code&gt;push&lt;/code&gt;, &lt;code&gt;pop&lt;/code&gt;, &lt;code&gt;unshift&lt;/code&gt;, and &lt;code&gt;shift&lt;/code&gt;. They come in handy for sure, if you want to add and remove elements from the beginning or the end of the array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;However, there are a TON of different scenarios where you will need to insert and remove array elements from any position.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is worth memorizing &lt;em&gt;cold&lt;/em&gt;!&lt;/p&gt;

&lt;p&gt;Let's start with an array of animals...&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;animals&lt;/span&gt; &lt;span class="o"&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;😺&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;🙉&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;🧞&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;🦊&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;🦁&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;🐯&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;🐵&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wait! There's a genie in the list at index 2. Not sure how that snuck in there 😂. Let's go ahead and remove that array element.&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;genieIndex&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="nx"&gt;animals&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;genieIndex&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;animals&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// =&amp;gt; ['😺' , '🙉' ,'🦊', '🦁', '🐯', '🐵'];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;splice(index,1)&lt;/code&gt; removes the array element located at &lt;code&gt;index&lt;/code&gt;. Very simple.&lt;/p&gt;

&lt;p&gt;Now &lt;code&gt;🐶&lt;/code&gt; is feeling left out, so let's add him into the array at &lt;code&gt;index&lt;/code&gt; equal to 2.&lt;/p&gt;

&lt;p&gt;Again, we can use the splice array method.&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;index&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="nx"&gt;animals&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🐶&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;animals&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// =&amp;gt; ['😺' , '🙉' ,'🐶','🦊', '🦁', '🐯', '🐵'];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;splice(index, 0,'🐶')&lt;/code&gt; inserts the dog emoji at position &lt;code&gt;index&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now there are definitely more sophisticated array manipulations you can do with &lt;code&gt;splice&lt;/code&gt;. However, start by memorizing how to add and remove array elements with &lt;code&gt;splice&lt;/code&gt;. You'll thank me later!&lt;/p&gt;

&lt;p&gt;If you enjoyed this article please check out my blog&lt;br&gt;
&lt;a href="https://indepthjavascript.dev/"&gt;Indepth JavaScript&lt;/a&gt; for more illuminating&lt;br&gt;
content. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>array</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to Solve Palindrome Problem with 1 Line of JavaScript</title>
      <dc:creator>Amit Mehta</dc:creator>
      <pubDate>Wed, 03 Aug 2022 21:45:00 +0000</pubDate>
      <link>https://dev.to/apmfree78/how-to-solve-palindrome-problem-with-1-line-of-javascript-4066</link>
      <guid>https://dev.to/apmfree78/how-to-solve-palindrome-problem-with-1-line-of-javascript-4066</guid>
      <description>&lt;p&gt;Unless you're just learning how to code now, you've probably heard of the famous valid palindrome or 'is palindrome' problem.  It's a common technical interview question.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;I'm going to show you how to knock the socks off your interviewer by solving the palindrome problem in 1 line of JavaScript.&lt;/strong&gt; 😎&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pO8Ts86y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1657641445794/NVn7UEBZ7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pO8Ts86y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1657641445794/NVn7UEBZ7.png" alt="javascript-1-line-surprised.png" width="492" height="351"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Quick Refresher: What is the Palindrome Problem?
&lt;/h3&gt;

&lt;p&gt;A string is a palindrome, if after reversing the order of all the characters, &lt;em&gt;you get the same string.&lt;/em&gt;  &lt;code&gt;const str = 'abba'&lt;/code&gt; is a palindrome, &lt;code&gt;const str = 'abc'&lt;/code&gt; is not.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Here's the challenge:&lt;/em&gt; Given a valid string of letters, can you construct a function that will check if the string is a valid palindrome.  It returns &lt;code&gt;true&lt;/code&gt; if it is, and &lt;code&gt;false&lt;/code&gt; if it's not.&lt;/p&gt;

&lt;h2&gt;
  
  
  IsPalindrome(...) in 1 Line
&lt;/h2&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;isPalindrome&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;first&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="nx"&gt;last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;str&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="mi"&gt;1&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="nx"&gt;first&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;last&lt;/span&gt;&lt;span class="p"&gt;)&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="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;last&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;isPalindrome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;first&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="nx"&gt;last&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Yes, this is 1 line of JavaScript code.&lt;/em&gt; I added a few line breaks and tabs to make it easier to read.&lt;/p&gt;

&lt;p&gt;If the above function looks overwhelming to you, no worries, we're going to walkthrough all the JavaScript and Recursion Jui Jitsu together. 😅&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Let's start with...&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Arrow Function
&lt;/h3&gt;

&lt;p&gt;I'm using a JavaScript arrow function so I can remove &lt;code&gt;return&lt;/code&gt; and taken advantage of implicit return feature the arrow function offers.  Love arrow functions! 😍&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;isPalindrome&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;first&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="nx"&gt;last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;str&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Default values for function arguments
&lt;/h3&gt;

&lt;p&gt;We've initialized our 2 pointers right in the function argument: &lt;code&gt;first = 0&lt;/code&gt; and &lt;code&gt;last = str.length - 1&lt;/code&gt;.  Super cool and  convenient.&lt;/p&gt;

&lt;h3&gt;
  
  
  Ternary operator
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;last&lt;/span&gt;&lt;span class="p"&gt;)&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="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;last&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;isPalindrome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;first&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="nx"&gt;last&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The ternary conditional operator is just a &lt;code&gt;if {...} else {...}&lt;/code&gt; statement condensed onto 1 line.  Here's a simple example:&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="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;x is greater than zero&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;x is less than or equal to zero&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Finally Everyone's Favorite: Recursion!
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;last&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;isPalindrome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;first&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="nx"&gt;last&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fortunately, this is a simple application of recursion that won't give anyone a headache trying to comprehend it.  🤯&lt;/p&gt;

&lt;p&gt;In the above statement: &lt;code&gt;(str[first] === str[last])&lt;/code&gt; checks to make sure the first and last characters of the string are matching. * Then &lt;code&gt;isPalindrome(str, first + 1, last - 1)&lt;/code&gt; recursively checks if the 2nd and 2nd to last characters of the string are matching.*&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For example:&lt;/strong&gt; If we ran &lt;code&gt;str = abcba&lt;/code&gt; through  &lt;code&gt;isPalindrome(str)&lt;/code&gt;, and expanded out the final return statement (after running through each iteration of recursion) it would look like this:&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="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&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="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;str&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="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&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="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;str&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="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&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="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;str&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="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;isPalindrome(str)&lt;/code&gt; is checking each pair of elements starting with &lt;code&gt;str[0]&lt;/code&gt; versus &lt;code&gt;str[4]&lt;/code&gt; (a vs a), until finally it's checking &lt;code&gt;str[2]&lt;/code&gt; against itself when &lt;code&gt;first === last&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;true&lt;/code&gt; statement at the end is tacked on when &lt;code&gt;first &amp;gt; last&lt;/code&gt;, completing the last recursive function call.&lt;/p&gt;

&lt;h3&gt;
  
  
  Wait! What if I have to deal with spaces, uppercase, and weird characters??
&lt;/h3&gt;

&lt;p&gt;OK, yea, in some Palindrome problems, such as &lt;a href="https://leetcode.com/problems/valid-palindrome/"&gt;this leetcode version&lt;/a&gt; you have strings with space, special characters and different cases that you have to remove or somehow deal with.&lt;/p&gt;

&lt;p&gt;Fear not, there's a 1 line fix to cleaning up the string so it contains only lowercase letters.  Run this before running &lt;code&gt;isPalindrome(str)&lt;/code&gt;:&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;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;[&lt;/span&gt;&lt;span class="sr"&gt;a-z&lt;/span&gt;&lt;span class="se"&gt;]&lt;/span&gt;&lt;span class="sr"&gt;/gi&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or we can keep &lt;strong&gt;&lt;em&gt;isPalindrome&lt;/em&gt;&lt;/strong&gt; to 1 line, like I promised:&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;isPalindrome&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;[&lt;/span&gt;&lt;span class="sr"&gt;a-z&lt;/span&gt;&lt;span class="se"&gt;]&lt;/span&gt;&lt;span class="sr"&gt;/gi&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nx"&gt;first&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="nx"&gt;last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;str&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="mi"&gt;1&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="nx"&gt;first&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;last&lt;/span&gt;&lt;span class="p"&gt;)&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="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;last&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;isPalindrome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;first&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="nx"&gt;last&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JOvPuJZe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgflip.com/2hao7l.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JOvPuJZe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgflip.com/2hao7l.jpg" alt="cat palindrome" width="605" height="433"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>algorithms</category>
      <category>leetcode</category>
    </item>
    <item>
      <title>How to Match a Phone Number with Regex and JavaScript</title>
      <dc:creator>Amit Mehta</dc:creator>
      <pubDate>Wed, 27 Jul 2022 21:36:00 +0000</pubDate>
      <link>https://dev.to/apmfree78/how-to-match-a-phone-number-with-regex-and-javascript-od0</link>
      <guid>https://dev.to/apmfree78/how-to-match-a-phone-number-with-regex-and-javascript-od0</guid>
      <description>&lt;p&gt;I'll be honest, the first time I saw a regular expression, it was a scary experience.  &lt;strong&gt;It looks like a weird alien language!&lt;/strong&gt; I thought to myself: "I've spent months learning programming and now i gotta learn this seemly super complex language!?"&lt;/p&gt;

&lt;p&gt;However, once I sat down to actually learn regex, I discovered it's not super hard, once you learn the syntax.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why should I even bother Learning Regex?
&lt;/h3&gt;

&lt;p&gt;As you start coding more, and more, it really comes in handy in all types of situations, and not just to valid phone numbers, and email addresses.  It's very helpful when extract data from logs, messy JSON data from API calls, and many other situations.&lt;/p&gt;

&lt;p&gt;I'm going to teach you how to valid a phone number &lt;em&gt;with 1 line of code&lt;/em&gt;, with 1 regular expression.  *&lt;em&gt;Validating a phone number WITHOUT regex becomes an obnoxious leetcode question. *&lt;/em&gt; 😧&lt;/p&gt;

&lt;h3&gt;
  
  
  Why is validating a phone number so complex?
&lt;/h3&gt;

&lt;p&gt;Let's say you have a form on your website to collect a phone number to spa-, I mean, SMS your subscribers, there are a bunch of different ways they could submit their phone numbers.&lt;/p&gt;

&lt;p&gt;All of these are VALID US based numbers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;202-515-5555&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;202 515 5555&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;(202)515 5555&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 202 515 5555&lt;/code&gt;&lt;/li&gt;
&lt;li&gt; &lt;code&gt;2025155555&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1-202-515-5555&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1202-515-5555&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;etc&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are more valid combinations I didn't list, but you get the idea! Validating every combo because a nasty coding problem.  *But NOT if you're using regex to validate it! * 😉&lt;/p&gt;

&lt;h3&gt;
  
  
  Let's start with the Simplest Case
&lt;/h3&gt;

&lt;p&gt;Let's first validate &lt;code&gt;202-515-5555&lt;/code&gt;.  The basic idea of regex is to build a pattern to match a string.  What is the pattern in &lt;code&gt;202-515-5555&lt;/code&gt; ?&lt;/p&gt;

&lt;p&gt;We start with 3 digits followed by a &lt;code&gt;-&lt;/code&gt; followed by 3 more digits, followed by another &lt;code&gt;-&lt;/code&gt;, then we end with 4 digits.&lt;/p&gt;

&lt;p&gt;Here's what the regex pattern to match &lt;code&gt;202-515-5555&lt;/code&gt; looks like:&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="o"&gt;^&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nx"&gt;d&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="o"&gt;-&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nx"&gt;d&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="o"&gt;-&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nx"&gt;d&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="nx"&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's watch through this...&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;^&lt;/code&gt; just indicates the beginning of the string.  In the above regex, we're stating that the phone number must start with &lt;code&gt;\d{3}&lt;/code&gt; since we have &lt;code&gt;^&lt;/code&gt; in front of &lt;code&gt;\d{3}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now &lt;strong&gt;&lt;code&gt;\d&lt;/code&gt; =&amp;gt; stands for single digit&lt;/strong&gt;, and &lt;strong&gt;&lt;code&gt;{3}&lt;/code&gt; simple means &lt;code&gt;\d&lt;/code&gt; repeats exactly 3 times.&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;So &lt;code&gt;^\d{3}&lt;/code&gt; means our phone number STARTS with 3 digits.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now let's just jump to the end: &lt;code&gt;$&lt;/code&gt; indicates the end of the string match. &lt;strong&gt;&lt;code&gt;\d{4}$&lt;/code&gt; means our phone number must END with  4 digits&lt;/strong&gt;.  &lt;em&gt;Does this make sense?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;-&lt;/code&gt; just means the phone number must have a dash at that spot.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So let's read the entire regex from left to right now:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;^\d{3}&lt;/code&gt; ⇾ start with 3 digits&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-&lt;/code&gt; ⇾ followed by a dash&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\d{3}&lt;/code&gt; ⇾ followed with 3 digits&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-&lt;/code&gt; ⇾ followed by a dash&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\d{4}$&lt;/code&gt; ⇾ ends with 4 digits&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Please read the above section a few times, if necessary, to make sure you fully understand before we move on.&lt;/p&gt;
&lt;h3&gt;
  
  
  How do I match the phone number if dashes are options?
&lt;/h3&gt;

&lt;p&gt;Great question!  How do we match BOTH: &lt;code&gt;202-515-5555&lt;/code&gt; and &lt;code&gt;2025155555&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To make a character match optional, just add a &lt;code&gt;?&lt;/code&gt; after it.&lt;/p&gt;

&lt;p&gt;Here's what our new improved match looks like:&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="o"&gt;^&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nx"&gt;d&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="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nx"&gt;d&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="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nx"&gt;d&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="nx"&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;-?&lt;/code&gt; simply means the &lt;code&gt;-&lt;/code&gt; is optional&lt;/strong&gt;: it may, or may not, be there!&lt;/p&gt;

&lt;p&gt;Let's read the entire regex again:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;^\d{3}&lt;/code&gt; ⇾ start with 3 digits&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-?&lt;/code&gt; ⇾ &lt;em&gt;optionally&lt;/em&gt; followed by a dash&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\d{3}&lt;/code&gt; ⇾ followed with 3 digits&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-?&lt;/code&gt; ⇾ &lt;em&gt;optionally&lt;/em&gt; followed by a dash&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\d{4}$&lt;/code&gt; ⇾ ends with 4 digits&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  How about matching the phone number if there are spaces, instead of dashes?
&lt;/h3&gt;

&lt;p&gt;Now let's work on matching: &lt;code&gt;202-515-5555&lt;/code&gt;, &lt;code&gt;2025155555&lt;/code&gt;, AND &lt;code&gt;202 515 5555&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Instead of optionally just having a &lt;code&gt;-&lt;/code&gt; we can optionally have either &lt;code&gt;-&lt;/code&gt; OR &lt;code&gt;&lt;/code&gt;.  How do we represent this?  Easy, put both &lt;code&gt;-&lt;/code&gt; and &lt;code&gt;&lt;/code&gt; inside of &lt;code&gt;[...]&lt;/code&gt; like this: &lt;code&gt;[ -]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Our new regex looks like this:&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="o"&gt;^&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nx"&gt;d&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="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;]?&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nx"&gt;d&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="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;]?&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nx"&gt;d&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="nx"&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now it's definitely starting to look alien! 😅&lt;/p&gt;

&lt;p&gt;Let's break it down:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;^\d{3}&lt;/code&gt; ⇾ start with 3 digits&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[ -]?&lt;/code&gt; ⇾ &lt;em&gt;optionally&lt;/em&gt; followed by a &lt;em&gt;space OR dash&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\d{3}&lt;/code&gt; ⇾ followed with 3 digits&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[ -]?&lt;/code&gt; ⇾ &lt;em&gt;optionally&lt;/em&gt; followed by a &lt;em&gt;space OR dash&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\d{4}$&lt;/code&gt; ⇾ ends with 4 digits&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  How to Match &lt;code&gt;1&lt;/code&gt; or &lt;code&gt;1&lt;/code&gt; or &lt;code&gt;1-&lt;/code&gt; at the beginning of our phone number
&lt;/h3&gt;

&lt;p&gt;Based on what we've learned, can you figure this out?&lt;/p&gt;

&lt;p&gt;It's a bit tricky once you realize that the &lt;code&gt;1...&lt;/code&gt; in the beginning is optional.&lt;/p&gt;

&lt;p&gt;Let's take it step by step...  &lt;/p&gt;

&lt;p&gt;If you want the phone number to start with &lt;code&gt;1&lt;/code&gt; &lt;em&gt;we add &lt;code&gt;^1&lt;/code&gt; to the beginning of the string match&lt;/em&gt;, correct?  Now we want to optionally add a dash or space after the 1. Luckily, we already know how to do that: &lt;code&gt;[ -]?&lt;/code&gt;.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Combining the 2 we get: &lt;code&gt;^1[ -]?&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Adding this to our previous regex we get:&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="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="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;]?&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nx"&gt;d&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="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;]?&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nx"&gt;d&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="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;]?&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nx"&gt;d&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="nx"&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Can you sense there's something wrong here?  &lt;strong&gt;The above regex string match MUST start with &lt;code&gt;1&lt;/code&gt;, it's not optional.&lt;/strong&gt;  We need to make &lt;code&gt;1[ -]?&lt;/code&gt; optional.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;How do we do that?&lt;/em&gt; Since we're talking about multiple elements here: &lt;code&gt;1&lt;/code&gt; and &lt;code&gt;[ -]?&lt;/code&gt; we need to place the whole thing in &lt;code&gt;(...)&lt;/code&gt; creating a group.  Then add a &lt;code&gt;?&lt;/code&gt; after it to make the entire group optional!  &lt;/p&gt;

&lt;p&gt;I know it's a lot to take in! Here's what it looks like:&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="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="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;]?)?&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nx"&gt;d&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="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;]?&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nx"&gt;d&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="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;]?&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nx"&gt;d&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="nx"&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's break it down again, with an extra step now:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;^(1[ -]?)?&lt;/code&gt; ⇾ &lt;em&gt;optionally&lt;/em&gt; start with 1, which is &lt;em&gt;optionally&lt;/em&gt; followed by a &lt;em&gt;space OR dash&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\d{3}&lt;/code&gt; ⇾ start with 3 digits&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[ -]?&lt;/code&gt; ⇾ &lt;em&gt;optionally&lt;/em&gt; followed by a &lt;em&gt;space OR dash&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\d{3}&lt;/code&gt; ⇾ followed with 3 digits&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[ -]?&lt;/code&gt; ⇾ &lt;em&gt;optionally&lt;/em&gt; followed by a &lt;em&gt;space OR dash&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\d{4}$&lt;/code&gt; ⇾ ends with 4 digits&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;🤯&lt;/p&gt;

&lt;p&gt;If you're still reading, congrats, you now know how to &lt;em&gt;think&lt;/em&gt; 'regex'!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--O_ZhhCJ_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgflip.com/6mzwct.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--O_ZhhCJ_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgflip.com/6mzwct.jpg" alt="Picard Regex" width="656" height="380"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;There's still 1 outstanding issue:&lt;/strong&gt; how to match a phone number like: &lt;code&gt;(202)515 5555&lt;/code&gt;. I'm going to leave that one for the reader (&lt;em&gt;hint:&lt;/em&gt; use the &lt;a href="https://regexone.com/lesson/conditionals"&gt;pipe operator: &lt;code&gt;(...|...)&lt;/code&gt;)&lt;/a&gt;.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Putting it all Together to Test an Actual Phone Number String
&lt;/h3&gt;

&lt;p&gt;Now let's take our regex and turn it into a regular expression in javaScript.  To do that you just add &lt;code&gt;/.../&lt;/code&gt; around it.  Then use a method called &lt;code&gt;test&lt;/code&gt;:&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;regex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/^&lt;/span&gt;&lt;span class="se"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;1&lt;/span&gt;&lt;span class="se"&gt;[&lt;/span&gt;&lt;span class="sr"&gt; -&lt;/span&gt;&lt;span class="se"&gt;]?)?\d{3}[&lt;/span&gt;&lt;span class="sr"&gt; -&lt;/span&gt;&lt;span class="se"&gt;]?\d{3}[&lt;/span&gt;&lt;span class="sr"&gt; -&lt;/span&gt;&lt;span class="se"&gt;]?\d{4}&lt;/span&gt;&lt;span class="sr"&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;phoneNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;1202-515-5555&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// test returns 'true' if there's a match and 'false' if there is not&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;match&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;phoneNumber&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to learn regex properly, here's a EXCELLENT free tutorial: &lt;a href="https://regexone.com/"&gt;RegexOne&lt;/a&gt;.  This is literally how I learned Regex. It's worth going through ALL the exercises.&lt;/p&gt;

&lt;p&gt;If you enjoyed this article please check out my blog&lt;br&gt;
&lt;a href="https://indepthjavascript.dev/"&gt;Indepth JavaScript&lt;/a&gt; for more illuminating&lt;br&gt;
content. 🤓&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>regex</category>
      <category>algorithms</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to Crack the Staircase Problem with JavaScript using Memoization</title>
      <dc:creator>Amit Mehta</dc:creator>
      <pubDate>Fri, 22 Jul 2022 18:23:00 +0000</pubDate>
      <link>https://dev.to/apmfree78/how-to-crack-the-staircase-problem-with-javascript-using-memoization-1hpa</link>
      <guid>https://dev.to/apmfree78/how-to-crack-the-staircase-problem-with-javascript-using-memoization-1hpa</guid>
      <description>&lt;p&gt;I want to write a quick follow up to a previous post on &lt;a href="https://dev.to/apmfree78/how-to-solve-the-staircase-problem-with-5-lines-of-javascript-50ag"&gt;How to Solve the Staircase Problem with 5 Lines of JavaScript&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It's a very elegant solution to the infamous staircase problem, *however, as one commentor noted it's terribly inefficient to the order of &lt;code&gt;O(3^n)&lt;/code&gt;.😖&lt;/p&gt;

&lt;h3&gt;
  
  
  Yikes! So Now What?
&lt;/h3&gt;

&lt;p&gt;There's a cool trick with a funny name to fix this problem:  &lt;em&gt;Memoization&lt;/em&gt;.  Yea, I'm not exactly sure how to pronounce it either! 😂&lt;/p&gt;

&lt;p&gt;Here's what the staircase problem looks like memoitized...&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;function&lt;/span&gt; &lt;span class="nf"&gt;stairSteps&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="c1"&gt;// store repeat values in memo to prevent repeat computations&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;memo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;stepsM&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;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;else&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;&amp;lt;&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="mi"&gt;0&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;memo&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="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;undefined&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;memo&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="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;memo&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="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;stepsM&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;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;stepsM&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="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;stepsM&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;3&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;memo&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="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;stepsM&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The idea is simple:&lt;/strong&gt; the array &lt;code&gt;memo&lt;/code&gt; just stores values that may repeat, so you can reference them later instead of having to calculate them again - &lt;em&gt;which is SUPER expensive in the case of recursion.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  For example...
&lt;/h3&gt;

&lt;p&gt;Once you know the value of &lt;code&gt;stepsM(N-3)&lt;/code&gt; which is calculated on the first round of &lt;code&gt;stepsM(N)&lt;/code&gt;, you do NOT need to calculate its value again when it's called later by &lt;code&gt;stepsM(N-1)&lt;/code&gt; or &lt;code&gt;stepsM(N-2)&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Pretty neat, huh?&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.imgflip.com%2F6m7sja.jpg" 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.imgflip.com%2F6m7sja.jpg" alt="mewoization"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you enjoyed this article please check out my blog&lt;br&gt;
&lt;a href="https://indepthjavascript.dev/" rel="noopener noreferrer"&gt;Indepth JavaScript&lt;/a&gt; for more illuminating&lt;br&gt;
content. 🤓&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>algorithms</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Quick Way to Check if 2 Arrays are Equal in JavaScript</title>
      <dc:creator>Amit Mehta</dc:creator>
      <pubDate>Thu, 21 Jul 2022 22:49:52 +0000</pubDate>
      <link>https://dev.to/apmfree78/quick-way-to-check-if-2-arrays-are-equal-in-javascript-4ldl</link>
      <guid>https://dev.to/apmfree78/quick-way-to-check-if-2-arrays-are-equal-in-javascript-4ldl</guid>
      <description>&lt;p&gt;When learning JavaScript for the first time we've all done this...&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;a&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;b&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="k"&gt;if&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Equal!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you'd tried this, then you KNOW &lt;code&gt;a === b&lt;/code&gt; will return false every time.&lt;/p&gt;

&lt;p&gt;*Why is that? *&lt;/p&gt;

&lt;p&gt;Because array variables are assigned by &lt;strong&gt;reference&lt;/strong&gt; NOT by &lt;strong&gt;value&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So &lt;code&gt;a&lt;/code&gt; is just a pointer to the location of &lt;code&gt;[1,2]&lt;/code&gt; somewhere in memory.&lt;br&gt;
While 'b' is another pointer to a different location in memory, that also happens to be storing &lt;code&gt;[1,2]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When you're checking if &lt;code&gt;a === b&lt;/code&gt;, you're really checking their member locations, and not the values they're pointing to.&lt;/p&gt;
&lt;h3&gt;
  
  
  So what gives?
&lt;/h3&gt;

&lt;p&gt;Is there any easy way to check if the underlying arrays that are represented by &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; are equal, &lt;strong&gt;WITHOUT having to do an element by element comparison?&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;em&gt;Here's the  quick cheat to get it done...&lt;/em&gt;
&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;d&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;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="mi"&gt;6&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;c&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;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="mi"&gt;6&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;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;c&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Equal!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Why does this work?
&lt;/h3&gt;

&lt;p&gt;And what the heck is &lt;code&gt;JSON.stringify(...)&lt;/code&gt; anyway?&lt;/p&gt;

&lt;p&gt;&lt;code&gt;JSON.stringify(...)&lt;/code&gt;  takes an array (or an object)and returns its string representation.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;It's stringified!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For example:&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;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Will just return &lt;code&gt;'[7,8,9]'&lt;/code&gt;.  Since the value is a string, you can compare it with the value of another array, that's also returned as a string with &lt;code&gt;JSON.stringify(...)&lt;/code&gt;. :)&lt;/p&gt;

&lt;p&gt;This trick even works with double array (and multi-dimensional arrays in general).  You can also compare objects this way, &lt;em&gt;as long as properties of each object are in the same order.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Another Tip
&lt;/h3&gt;

&lt;p&gt;If you want to compare if 2 arrays contain the same values, regardless of order, then just use &lt;code&gt;Array.sort()&lt;/code&gt; method to sort both arrays first, THEN use &lt;code&gt;JSON.stringify&lt;/code&gt; to compare then!&lt;/p&gt;

&lt;p&gt;If you enjoyed this article please check out my blog&lt;br&gt;
&lt;a href="https://indepthjavascript.dev/"&gt;Indepth JavaScript&lt;/a&gt; for more insightful&lt;br&gt;
content. 🤓&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>es6</category>
    </item>
    <item>
      <title>4 Simple Ways to Covert a JavaScript String to a Number</title>
      <dc:creator>Amit Mehta</dc:creator>
      <pubDate>Thu, 21 Jul 2022 02:28:51 +0000</pubDate>
      <link>https://dev.to/apmfree78/4-simple-ways-to-covert-a-javascript-string-to-a-number-m5h</link>
      <guid>https://dev.to/apmfree78/4-simple-ways-to-covert-a-javascript-string-to-a-number-m5h</guid>
      <description>&lt;p&gt;Having to check if a string is a number, and then type convert it to a number, comes up a lot when coding in JavaScript. If you're done more than 5 leetcode problems, then you know what I'm talking about! 😅&lt;/p&gt;

&lt;p&gt;&lt;em&gt;It's also super useful when coding applications too.&lt;/em&gt; The classic example is the calculator app, where you're taking in input values from the users as strings, and you need to verify that these values are valid numbers (or operators).&lt;/p&gt;

&lt;h3&gt;
  
  
  How do you check if a String is a Number?
&lt;/h3&gt;

&lt;p&gt;There's a JavaScript method with a funny name called &lt;code&gt;isNaN(str)&lt;/code&gt;.  Which translates to "is Not a Number".  It returns true if &lt;code&gt;str&lt;/code&gt; is not a number, and false if it IS a number.&lt;/p&gt;

&lt;p&gt;If we use &lt;code&gt;!isNaN(str)&lt;/code&gt; we get true if &lt;code&gt;str&lt;/code&gt; is a number....&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="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;isNaN&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;👾123&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; false&lt;/span&gt;
&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;isNaN&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;😺&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; false&lt;/span&gt;
&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;isNaN&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;456&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; true&lt;/span&gt;

&lt;span class="c1"&gt;// works for floats too&lt;/span&gt;
&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;isNaN&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;-2.5&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Great, now how do I Convert the String to a Number?
&lt;/h3&gt;

&lt;p&gt;There are 4 ways to type convert a string to a number (if it is a number). &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;WARNING&lt;/strong&gt;: If the string is NOT a number, do NOT attempt to convert it to a number, because you'll either get &lt;code&gt;NaN&lt;/code&gt; back or sometimes &lt;code&gt;0&lt;/code&gt; if you convert an empty string or space to a number.  Good luck finding that bug in your code!** 🤬&lt;/p&gt;

&lt;p&gt;*So ALWAYS check if it's a number using &lt;code&gt;!isNaN(str)&lt;/code&gt; FIRST. *&lt;/p&gt;

&lt;p&gt;Got it? Cool! Let's type convert:&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;// for integers&lt;/span&gt;
&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;456&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; 456&lt;/span&gt;
&lt;span class="nb"&gt;parseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;456&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; 456 &lt;/span&gt;
&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;456&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; 456&lt;/span&gt;

&lt;span class="c1"&gt;// for floating point numbers&lt;/span&gt;
&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;-2.600&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; -2.6&lt;/span&gt;
&lt;span class="nb"&gt;parseFloat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;-2.600&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; -2.6 &lt;/span&gt;
&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;-2.600&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; -2.6&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Note that &lt;code&gt;parseInt(...)&lt;/code&gt; ONLY works for integers, if you give it a float it will cut off everything after the &lt;code&gt;.&lt;/code&gt;, so &lt;code&gt;parseInt('-2.6')&lt;/code&gt; becomes &lt;code&gt;-2&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to Remove Trailing (and Leading) Zeros from a Floating Point Number
&lt;/h3&gt;

&lt;p&gt;Notice I added some zeros at the end of &lt;code&gt;-2.6&lt;/code&gt; I did that to illustrate something cool: *&lt;em&gt;by type converting a string to a float, all unnecessary floating point zeros that come after AND before will be removed.  *&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This really comes in handy.&lt;/p&gt;

&lt;p&gt;For example:&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="o"&gt;+&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;-000002.600&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; -2.6&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Related trick:&lt;/strong&gt; if you have a float with a lot of trailing zeros you want to get rid of, just convert it to a string THEN back to a number with &lt;code&gt;+&lt;/code&gt;:&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;annoyingNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;1.230000000000&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;cleanNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;annoyingNumber&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toString&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cleanNumber&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; 1.23&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  😎
&lt;/h2&gt;

&lt;h3&gt;
  
  
  My Favorite Option for Converting from String to Number in JavaScript?
&lt;/h3&gt;

&lt;p&gt;Hands down, my favorite is the &lt;code&gt;+&lt;/code&gt; operator.  It's just one character long, gotta love it! 😻&lt;/p&gt;

&lt;p&gt;What's your favorite?&lt;/p&gt;

&lt;p&gt;If you enjoyed this article please check out my blog&lt;br&gt;
&lt;a href="https://indepthjavascript.dev/"&gt;Indepth JavaScript&lt;/a&gt; for more illuminating content. 🤓&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to Solve the Staircase Problem with 5 Lines of JavaScript</title>
      <dc:creator>Amit Mehta</dc:creator>
      <pubDate>Wed, 20 Jul 2022 01:06:39 +0000</pubDate>
      <link>https://dev.to/apmfree78/how-to-solve-the-staircase-problem-with-5-lines-of-javascript-50ag</link>
      <guid>https://dev.to/apmfree78/how-to-solve-the-staircase-problem-with-5-lines-of-javascript-50ag</guid>
      <description>&lt;p&gt;If you're new to algorithms and leetcode, the staircase problem is infamous!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--huT2l8TA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgflip.com/6m6pa9.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--huT2l8TA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgflip.com/6m6pa9.jpg" alt="scared cat" width="620" height="464"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What is the Staircase Programming Challenge?
&lt;/h3&gt;

&lt;p&gt;Stated simply....&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Imagine a staircase with &lt;code&gt;N&lt;/code&gt; steps.  If you can take 1, 2, or 3 steps at a time, how many different ways can you reach the top of the stairs?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Encountering a problem like this for the first time is for sure frightening.  &lt;strong&gt;Even on its own as a math problem, IT'S REALLY HARD.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are a few obvious cases.  You can take 1 step at a time, &lt;code&gt;N&lt;/code&gt; times.  If &lt;code&gt;N&lt;/code&gt; is divisible by 2, you can take 2 steps, &lt;code&gt;N/2&lt;/code&gt; times.  Also, if &lt;code&gt;N&lt;/code&gt; is divisible by 3, you can take 3 steps, &lt;code&gt;N/3&lt;/code&gt; times.&lt;/p&gt;

&lt;p&gt;And then there is everything in between. 🤔&lt;/p&gt;

&lt;p&gt;The good news is there is a really simple way to solve this problem using everyone's favorite algorithm: &lt;strong&gt;recursion.&lt;/strong&gt; 😨&lt;/p&gt;

&lt;h3&gt;
  
  
  Recursion to the Rescue
&lt;/h3&gt;

&lt;p&gt;As promised, here is the solution in 5 lines of JavaScript code...&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;// N is total number of steps in the staircase&lt;/span&gt;
&lt;span class="c1"&gt;// stepTaken is a counter for steps taken in each combination&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;steps&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="nx"&gt;stepsTaken&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stepsTaken&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;N&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;else&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;stepsTaken&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;N&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&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="nx"&gt;steps&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="nx"&gt;stepsTaken&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="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;steps&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="nx"&gt;stepsTaken&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="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;steps&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="nx"&gt;stepsTaken&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;3&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;h3&gt;
  
  
  Yikes! What does this mean??
&lt;/h3&gt;

&lt;p&gt;*&lt;em&gt;Don't panic or feel bad if the above code confuses you. *&lt;/em&gt; I struggled for a long time with recursion since I first learned it in high school CS class. It's tricky for sure. * But once you understand it, it opens up a whole new world of possibilities!*&lt;/p&gt;

&lt;h3&gt;
  
  
  Let's walk through this solution
&lt;/h3&gt;

&lt;p&gt;If you're thinking of just copying the above solution and leaving, you're missing the point. &lt;em&gt;It's super important you understand what's happening.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;function steps(N, stepsTaken = 0)&lt;/code&gt; is just a simple recursive counter.  &lt;/p&gt;

&lt;p&gt;Let's walk through it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We're at the bottom of the stairs, no steps taken.  So &lt;code&gt;stepsTaken = 0&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;You have 3 possibilities in front of you: &lt;em&gt;take 1 step, jump up 2 steps, or leap up 3 steps.&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Now we need to account for ALL 3 possibilities. So imagine you clone the stairs and yourself 2 times. Plus, you each have your own version of &lt;code&gt;stepsTaken&lt;/code&gt; variable your carrying with you. 
You and your clones each go through one of these 'doors' (note each of you must go through a DIFFERENT door):
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;steps&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="nx"&gt;stepsTaken&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="nx"&gt;steps&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="nx"&gt;stepsTaken&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="nx"&gt;steps&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="nx"&gt;stepsTaken&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you go through your chosen door, &lt;strong&gt;your individual &lt;code&gt;stepsTaken&lt;/code&gt; counter will be incremented by 1, 2, or 3&lt;/strong&gt; (depending on the door you went through).   Then after that, you'll immediately see 3 more doors:&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;steps&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="nx"&gt;stepsTaken&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="nx"&gt;steps&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="nx"&gt;stepsTaken&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="nx"&gt;steps&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="nx"&gt;stepsTaken&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again, you'll clone yourself 2 times &lt;em&gt;again&lt;/em&gt; and each go through one of these &lt;code&gt;steps&lt;/code&gt; doors.  Your &lt;code&gt;stepsTaken&lt;/code&gt; counter will increment &lt;em&gt;again&lt;/em&gt; by 1, 2 or 3.  &lt;strong&gt;This will KEEP HAPPENING until :&lt;/strong&gt;&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stepsTaken&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;N&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;else&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;stepsTaken&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;N&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&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;If you overstepped &lt;code&gt;stepsTaken &amp;gt; N&lt;/code&gt;&lt;/strong&gt;, your combination of steps does NOT count towards the total of unique way to go up the stairs.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;However, &lt;code&gt;if (stepsTaken === N)&lt;/code&gt;&lt;/strong&gt;, then bingo 🤩 you found a legit combo of steps to go up the stairs and you &lt;code&gt;return 1&lt;/code&gt; to add to the count of way to go up the stairs.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Now remember the way we count the number of possible combinations of &lt;code&gt;stepsTaken&lt;/code&gt; to reach the top of the &lt;code&gt;N&lt;/code&gt; step staircase, is to simply add all the possibilities:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;steps&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="nx"&gt;stepsTaken&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="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;steps&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="nx"&gt;stepsTaken&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="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;steps&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="nx"&gt;stepsTaken&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember each legitimate combo of steps where &lt;code&gt;if (stepsTaken === N) return 1&lt;/code&gt; and each if path is not valid (overstep) then 0 is returned : else &lt;code&gt;if (stepsTaken &amp;gt; N) return 0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Some of you may be asking: &lt;em&gt;Nice, this is elegant, but isn't it super inefficient in terms of time complexity?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Yea, it's super high time complexity.  However, there's a trick to dramatically improving that ➡️ &lt;a href="https://indepthjavascript.dev/how-to-solve-the-staircase-problem-with-javascript-using-memoization"&gt;How to Solve the Staircase Problem with JavaScript using Memoization&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hope this makes sense, comment below if you have any questions?&lt;/p&gt;

&lt;p&gt;If you enjoyed this article, please check out my blog &lt;a href="https://indepthjavascript.dev/"&gt;Indepth JavaScript&lt;/a&gt; for more illuminating content. 🤓&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>leetcode</category>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
