<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Fakorede Damilola</title>
    <description>The latest articles on Forem by Fakorede Damilola (@fakorededamilola).</description>
    <link>https://forem.com/fakorededamilola</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%2F437773%2Fd5c76a60-44d6-4fc8-a316-0d5a8991567f.jpg</url>
      <title>Forem: Fakorede Damilola</title>
      <link>https://forem.com/fakorededamilola</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/fakorededamilola"/>
    <language>en</language>
    <item>
      <title>Git flow and github flow</title>
      <dc:creator>Fakorede Damilola</dc:creator>
      <pubDate>Fri, 09 Sep 2022 10:39:00 +0000</pubDate>
      <link>https://forem.com/fakorededamilola/git-flow-and-github-flow-29f2</link>
      <guid>https://forem.com/fakorededamilola/git-flow-and-github-flow-29f2</guid>
      <description>&lt;p&gt;Recently I had the opportunity to learn a concept, that well, looked quite scary whenever I was asked in an interview.&lt;/p&gt;

&lt;p&gt;The interviewer gives a very strong face with no smile at all and asks.. what is &lt;strong&gt;git flow&lt;/strong&gt;.&lt;br&gt;
Me: *&lt;em&gt;sweating already :) *&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So after going through git and GitHub flow, I want to share what I have learned, and help you realize why it is quite an important concept to be familiar with.&lt;/p&gt;

&lt;p&gt;Git flow and GitHub flow are steps you take when building software or app. This is quite common in a company setting. Since in a company, you will be working with other developers, and you have a goal, a timeline, and more often than none, a project manager bugging you :):).&lt;/p&gt;

&lt;p&gt;So in a situation where we have multiple developers working on a single product at different times of the day. There can be errors that can occur if this is not properly handled&lt;/p&gt;

&lt;p&gt;So how does this flow help you?&lt;/p&gt;

&lt;p&gt;As I said, I like to think of &lt;strong&gt;git&lt;/strong&gt; and &lt;strong&gt;git flow&lt;/strong&gt; as processes that define specific ways to work with other developers and make the process easy&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;GitHub flow&lt;/strong&gt; actually looks and feels quite simpler to use than the &lt;strong&gt;git flow&lt;/strong&gt; but I am going to talk about the two and show you how it works&lt;/p&gt;

&lt;h4&gt;
  
  
  GIT FLOW
&lt;/h4&gt;

&lt;p&gt;Here is how the git flow works. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;There is always a branch by default called &lt;strong&gt;main(or master)&lt;/strong&gt; where the live/production code stays (by convention). One file goes missing in that master branch, and the site might come crashing down. So due to the peculiarity and the sensitivity, you usually don't touch this branch&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Another branch is created called the &lt;strong&gt;develop&lt;/strong&gt; branch (by convention) which should contain all the code from main ( basically checked out from main), new changes are added to this branch from the feature branch&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The feature branch contains the different features being worked on. This can be multiple branches with different names, a naming system for example will be feature/notification, feature/api, feature, etc&lt;br&gt;
This honestly can be different anyhow I just want to give you a feel of how the feature branch works&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When a feature branch has been completed and/or tested, it is then merged to the &lt;strong&gt;develop&lt;/strong&gt; branch, since this is where the code currently being worked on stays&lt;/p&gt;

&lt;p&gt;After a few merges to the develop branch (from the feature branches) and the project manager thinks it is time to push this to the master branch for others to see the beautiful updates, a &lt;strong&gt;release&lt;/strong&gt; branch is fork(Git fork operation will create a completely new copy of the target repository) from develop&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A release branch will mean all the feature up to that point is ready to move to live and no new feature will be added to that release branch&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once the release branch has been tested (or whatever trick and hoops it is made to pass through), and it is ready to be released, the release branch gets merged into the main branch and &lt;br&gt;
tagged with a version number. &lt;br&gt;
In addition, it should be merged back to the develop branch in case there are any differences or critical changes made to the release branch. ( just to be safe)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Maintenance/hotfixes branches&lt;/strong&gt; are used to patch production releases, and this is forked directly from the main branch. After which it is merged back to the main branch and the develop branch (to keep it in sync)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now let's do a quick recap of what is needed for the git flow&lt;/p&gt;

&lt;p&gt;1) main (master) branch: This holds the production code&lt;/p&gt;

&lt;p&gt;2) develop branch: This is created from the main branch&lt;/p&gt;

&lt;p&gt;3) feature branch: these are created from the develop branch (e.g feat/notification, feature/buy, etc)&lt;/p&gt;

&lt;p&gt;4) release branch: The branch that will be sent off to the main branch. forked from develop&lt;/p&gt;

&lt;p&gt;5) hotfix branch: crated from the main branch, used to fix production/ emergency error&lt;/p&gt;

&lt;p&gt;honestly, that is a whole lot...let's talk about the GitHub flow&lt;/p&gt;

&lt;h4&gt;
  
  
  GitHub flow
&lt;/h4&gt;

&lt;p&gt;The GitHub flow uses two branches &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;main (or master) branch&lt;/strong&gt; is where the live/ production code stays (similar to the git flow)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;feature branch&lt;/strong&gt; which is every other branch, is where developers work from, which are pulled off from the main branch. Once a feature is completed, a &lt;strong&gt;pull request (PR)&lt;/strong&gt; is opened to the main branch, and after proper testing, etc. It is merged with the main branch (production)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That is pretty simple and straight forward right :):)&lt;/p&gt;

&lt;p&gt;Depending on the culture of the company, they can decide to use git or GitHub flow, but honestly, I prefer the latter.&lt;/p&gt;

&lt;p&gt;Getting familiar with this concept might require working with it on your projects. Since quite many companies will want to use these flows, it is better that you familiarize yourself with them.&lt;/p&gt;

&lt;p&gt;In this article, I hope you have been able to learn the difference between &lt;strong&gt;git flow&lt;/strong&gt; and &lt;strong&gt;GitHub flow&lt;/strong&gt;, and how they work.&lt;/p&gt;

&lt;p&gt;Thank you&lt;/p&gt;

</description>
      <category>github</category>
      <category>git</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Common Questions in Regular Expression</title>
      <dc:creator>Fakorede Damilola</dc:creator>
      <pubDate>Wed, 10 Feb 2021 09:47:05 +0000</pubDate>
      <link>https://forem.com/fakorededamilola/common-questions-in-regular-expression-4l29</link>
      <guid>https://forem.com/fakorededamilola/common-questions-in-regular-expression-4l29</guid>
      <description>&lt;p&gt;In my last article &lt;a href="https://dev.to/fakorededamilola/introduction-to-regular-expression-in-javascript-2o8m"&gt;introduction to regular expression&lt;/a&gt;, I explained what regular expression is, some of the methods involved and so on. In this article I will be going through a few regular expression questions that should help you get comfortable with regex. Note that this questions might not be the interview questions you are expecting but I hope it gives you an edge when it comes to solving questions in regular expressions.&lt;br&gt;
Like the famous saying in programming you can do one thing in a thousand different ways and I, for one will definitely use just one of this different ways. If you feel there is a better way I should have solved a particular problem, let me know in the comment section below.&lt;br&gt;
Let's get down to business.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Email Validation :&lt;/strong&gt;&lt;br&gt;
Create a function that will test if a given input is an email or not. Note that the emails can come is different formats for example &lt;a href="mailto:devto123@gmail.com"&gt;devto123@gmail.com&lt;/a&gt;, &lt;a href="mailto:dev.to@example.com"&gt;dev.to@example.com&lt;/a&gt; and so on. Using the gmail format, that is only letters, number, and periods are allowed. Return a boolean&lt;/p&gt;

&lt;p&gt;Solution &lt;br&gt;
From the question above, we are testing to see if an input is an email or not and it is pretty obvious that we will end up using regex, that is &lt;strong&gt;the test method&lt;/strong&gt;.&lt;br&gt;
Basically we have to write a regex that matches different formats of email. When solving questions like this, it is better you start from ground zero. &lt;strong&gt;What do you know about the question / what were we told&lt;/strong&gt;. Here is a few things we know about emails&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It should start with an alphanumeric character and casing does not matter.&lt;/li&gt;
&lt;li&gt;A dot can be used somewhere in the string but not necessarily. If it is then it must be immediately followed by one or more characters.&lt;/li&gt;
&lt;li&gt;There must be an &lt;strong&gt;@&lt;/strong&gt; after which a few other characters must follow.&lt;/li&gt;
&lt;li&gt;It must end with a &lt;strong&gt;.com&lt;/strong&gt; or &lt;strong&gt;.co&lt;/strong&gt; and so on.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This might look like a really long process for just one question and it actually is. I won't recommend doing this in an examination or interview. But when you are at your comfort, this can really help you understand the question especially for beginners. Although you don't have to write it out like I did but then it won't hurt.&lt;br&gt;
So now that we know what the mail looks like, let's move on and see how this can help us.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Emails &lt;strong&gt;must&lt;/strong&gt; start with letters and casing doesn't matter. In regex, &lt;strong&gt;must start&lt;/strong&gt; is the &lt;strong&gt;^&lt;/strong&gt; and we can easily match alphanumeric characters with &lt;code&gt;\w ===[a-zA-Z1-9_]&lt;/code&gt;. But there is a problem with this, it should not allow underscore. So we have to write it out, that is &lt;code&gt;[a-zA-Z1-9]&lt;/code&gt;. In order to match more than one character, we use the &lt;strong&gt;+&lt;/strong&gt;. All together &lt;code&gt;/^[a-zA-Z1-9]+/&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A period can be somewhere in the string but not necessarily. In regex, a period is a &lt;strong&gt;wild card&lt;/strong&gt;. Making it a normal character, we need the backslash to escape it. To make it optionally we use &lt;strong&gt;?&lt;/strong&gt;. At this point, you have this &lt;code&gt;/^[a-zA-Z1-9]+\.?/&lt;/code&gt;. If there is a period, it should be followed by one or more string, so basically repeating step one. &lt;code&gt;/^[a-zA-Z1-9]+\.?[a-zA-Z1-9]+/&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;@&lt;/strong&gt; should follow. This is pretty straightforward &lt;code&gt;/^[a-zA-Z1-9]+\.?[a-zA-Z1-9]+@/&lt;/code&gt;. After which a few letters should follow, that is &lt;code&gt;/^[a-zA-Z1-9]+\.?[a-zA-Z1-9]+@[a-zA-Z1-9]{3,}/&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Must end with &lt;strong&gt;.com, .ca&lt;/strong&gt; and so on. &lt;code&gt;/(\.\w{2,3})$/&lt;/code&gt;. The parenthesis is used to just group regex together. Summing it all up we have this.
&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;validateEmail&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="kd"&gt;let&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;a-zA-Z1-9&lt;/span&gt;&lt;span class="se"&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-zA-Z1-9&lt;/span&gt;&lt;span class="se"&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-zA-Z1-9&lt;/span&gt;&lt;span class="se"&gt;]{3,}(\.[&lt;/span&gt;&lt;span class="sr"&gt;a-zA-Z0-9&lt;/span&gt;&lt;span class="se"&gt;]{2,3})&lt;/span&gt;&lt;span class="sr"&gt;$/&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;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;str&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;p&gt;I know this is pretty long, and  I won't be doing this for other questions. I just wanted to show you a better way to approach questions especially algorithm. I hope this will help you out when solving other questions.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Date validation :&lt;/strong&gt;&lt;br&gt;
Create a function to test if a string is a valid date format. The format is &lt;strong&gt;DD-MM-YYYY&lt;/strong&gt; or &lt;strong&gt;D-M-YY&lt;/strong&gt;. Note that the separator can be &lt;strong&gt;:,_,- or /&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Solution&lt;br&gt;
Like what we did above, splitting this question will make it easier.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;DD / D :&lt;/strong&gt; From our calendar, the days are always less than or equal to 31. We are basically matching 01-31 or 1-31. &lt;code&gt;/0?[0-9]/&lt;/code&gt; will be used to match numbers less than 10 while making the &lt;strong&gt;0&lt;/strong&gt; optionally, &lt;code&gt;/[12][0-9]/&lt;/code&gt; will match from 10-29 (remember [12] is a character set and it means either 1 or 2) and &lt;code&gt;/3[01]/&lt;/code&gt; since we can't have more than 31 days. All together
&lt;code&gt;/(0?[0-9]|[12][0-9]|3[01])/&lt;/code&gt;. Remember that &lt;strong&gt;|&lt;/strong&gt; stands for &lt;strong&gt;or&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MM / M :&lt;/strong&gt; 12 months in the calendar, matching 0-12 or 01-12.
Basically since we cannot have more than 12 months, we can't match it all at once. So &lt;code&gt;/0?[0-9]/&lt;/code&gt; and &lt;code&gt;/1[0-2]/&lt;/code&gt;. Altogether
&lt;code&gt;/(0?[0-9])|(1[0-2])/&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;YY / YYYY :&lt;/strong&gt; Since this has no specific number it is pretty straightforward. Just remember 4 or 2 digits. That is &lt;code&gt;/[0-9]{2}|[0-9]{4}/&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Separator :&lt;/strong&gt; Piece of cake right &lt;code&gt;/[:\/_-]/&lt;/code&gt;.
All together we have this.
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;validateDate&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="kd"&gt;let&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;0&lt;/span&gt;&lt;span class="se"&gt;?[&lt;/span&gt;&lt;span class="sr"&gt;0-9&lt;/span&gt;&lt;span class="se"&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;12&lt;/span&gt;&lt;span class="se"&gt;][&lt;/span&gt;&lt;span class="sr"&gt;0-9&lt;/span&gt;&lt;span class="se"&gt;]&lt;/span&gt;&lt;span class="sr"&gt;|3&lt;/span&gt;&lt;span class="se"&gt;[&lt;/span&gt;&lt;span class="sr"&gt;01&lt;/span&gt;&lt;span class="se"&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;_-&lt;/span&gt;&lt;span class="se"&gt;](&lt;/span&gt;&lt;span class="sr"&gt;0&lt;/span&gt;&lt;span class="se"&gt;?[&lt;/span&gt;&lt;span class="sr"&gt;0-9&lt;/span&gt;&lt;span class="se"&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;0-2&lt;/span&gt;&lt;span class="se"&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;_-&lt;/span&gt;&lt;span class="se"&gt;][&lt;/span&gt;&lt;span class="sr"&gt;0-9&lt;/span&gt;&lt;span class="se"&gt;]{2}?[&lt;/span&gt;&lt;span class="sr"&gt;0-9&lt;/span&gt;&lt;span class="se"&gt;]{2}&lt;/span&gt;&lt;span class="sr"&gt;$/&lt;/span&gt;
&lt;span class="k"&gt;return&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;str&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;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Vowel count:&lt;/strong&gt; &lt;br&gt;
Return the number of vowels in this string&lt;/p&gt;

&lt;p&gt;Solution&lt;br&gt;
Try it your self‼️‼️‼️&lt;br&gt;
There is quite a number of ways you can do this, a &lt;strong&gt;for loop&lt;/strong&gt; will work just fine but right now you have the power of regex so why not try that.&lt;br&gt;
The closest thing you can use to actually get quantity in regex is the &lt;strong&gt;match method&lt;/strong&gt;(returns an array) and then you can easily call a &lt;strong&gt;.length&lt;/strong&gt; on the array returned.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;vowel&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="k"&gt;return&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;aeiou&lt;/span&gt;&lt;span class="se"&gt;]&lt;/span&gt;&lt;span class="sr"&gt;/ig&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;/code&gt;&lt;/pre&gt;



&lt;p&gt;Don't forget the &lt;strong&gt;i&lt;/strong&gt; and &lt;strong&gt;g&lt;/strong&gt; flag. Piece of cake right.&lt;/p&gt;


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

&lt;p&gt;&lt;strong&gt;Palindrome :&lt;/strong&gt;&lt;br&gt;
Create a function to test if a string is a palindrome. Note that special characters, spaces and so on should not be considered when testing the string for example, &lt;strong&gt;race_-+C ar&lt;/strong&gt; and &lt;strong&gt;m-.um&lt;/strong&gt; are both palindrome.&lt;/p&gt;

&lt;p&gt;Solution&lt;br&gt;
Before we move forward we need to understand what a palindrome is. A palindrome is basically a string that when reversed spells out the same thing. For example &lt;strong&gt;racecar&lt;/strong&gt;.  If there was a special character or space in the string above it might not be a palindrome, for example &lt;strong&gt;ra_-ce car != rac ec-_ar&lt;/strong&gt;. That is why the question says all non alphanumeric character should be removed before testing.&lt;br&gt;
Even though the question says we should test if a string is a palindrome, it is pretty obvious you cannot use any method from regex. I mean what would you be matching or testing against. So that is not an option.&lt;br&gt;
The first thing we can do know is to remove all non alphanumeric character. Regex could come in pretty handy here with the &lt;strong&gt;replace method&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ra c e-_.c;+-a.?).;#r&lt;/span&gt;&lt;span class="dl"&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;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;[\W&lt;/span&gt;&lt;span class="sr"&gt;_&lt;/span&gt;&lt;span class="se"&gt;]&lt;/span&gt;&lt;span class="sr"&gt;/g&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="c1"&gt;//\W matches All non alphanumeric character expect the underscore, which we also need to match, then replace them.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;With this we should have the exact string we are suppose to test alone without the other characters. &lt;br&gt;
Since a palindrome is basically the reverse of the actual string, we can do just that. Convert the string to an array with the &lt;strong&gt;split method&lt;/strong&gt; and call the &lt;strong&gt;reverse method&lt;/strong&gt; on that array. Then simply join the array back with the &lt;strong&gt;join method&lt;/strong&gt; and you have the reverse which you can easily test to see if they are the same.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;palindrome&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;string&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;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;[\W&lt;/span&gt;&lt;span class="sr"&gt;_&lt;/span&gt;&lt;span class="se"&gt;]&lt;/span&gt;&lt;span class="sr"&gt;/g&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;split&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;str2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reverse&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;string2&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;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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;string2&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="kc"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;//shorter version&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;palindrome&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="k"&gt;return&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;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;[\W&lt;/span&gt;&lt;span class="sr"&gt;_&lt;/span&gt;&lt;span class="se"&gt;]&lt;/span&gt;&lt;span class="sr"&gt;/g&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="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;split&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="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;()&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="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="kc"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;




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

&lt;p&gt;&lt;strong&gt;Hexadecimal colors :&lt;/strong&gt;&lt;br&gt;
Create a function to test if the given string is an hexadecimal color for example &lt;strong&gt;#333, #333333&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Solution&lt;br&gt;
So we are back to testing and at this point you should know that we will be using regex. Can you give it a try.&lt;br&gt;
Here is what we know about hexadecimal colors. It can be &lt;strong&gt;three(3)&lt;/strong&gt; or &lt;strong&gt;six(6)&lt;/strong&gt; characters and it must be between &lt;strong&gt;0-9&lt;/strong&gt; or &lt;strong&gt;A-F&lt;/strong&gt; that is, sixteen different characters.&lt;br&gt;
An hexadecimal must start with an &lt;strong&gt;#&lt;/strong&gt; and can be followed by A-F or 0-9 three times so basically &lt;code&gt;/^#([A-Fa-f0-9]){3}/&lt;/code&gt;. But it can also be six alphanumeric characters. That is &lt;code&gt;/^#([A-Fa-f0-9]){6}/&lt;/code&gt;. Since it is three or six together we can do this&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;validateHexadecimal&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="kd"&gt;let&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;A-Fa-f0-9&lt;/span&gt;&lt;span class="se"&gt;]{3}&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-Fa-f0-9&lt;/span&gt;&lt;span class="se"&gt;]{6})&lt;/span&gt;&lt;span class="sr"&gt;$/&lt;/span&gt;
&lt;span class="k"&gt;return&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;str&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;/li&gt;
&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Spinal case :&lt;/strong&gt;&lt;br&gt;
Create a function to convert a string to a spinal case. For example &lt;strong&gt;This Is A JavaScript_String&lt;/strong&gt; = &lt;strong&gt;this-is-a-javascript-string&lt;/strong&gt;, &lt;strong&gt;thisIsJavascript&lt;/strong&gt; = &lt;strong&gt;this-is-javascript&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Solution&lt;br&gt;
Try it out first.&lt;br&gt;
This question is in a way tricky because the strings can come in very different formats. Basically the task is to add an &lt;strong&gt;hyphen&lt;/strong&gt; between words. If this sentences are always separated with &lt;strong&gt;_&lt;/strong&gt; or &lt;strong&gt;-&lt;/strong&gt; it will be pretty easy. But it can also be camelCase like the example above. In situations like this, you will have to split it at all occurrence of a capital letter and then add the &lt;strong&gt;hyphen&lt;/strong&gt;. Note that the string returned should always be in lowercase. &lt;br&gt;
Now that we know what should and shouldn't be there, we can move forward. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The easiest thing to do is to remove all alphanumeric characters first. That is &lt;code&gt;str.replace(/[\W_]/g,"")&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Now that all the special characters are gone, we can easily split the words either with spaces or with capital letters.
That is &lt;code&gt;str.split(/(?=[A-Z])|\s/)&lt;/code&gt;. So basically, when going through the string, it is either it looks ahead to see if the next letter is in uppercase or it checks if there is a space and splits at that point.&lt;/li&gt;
&lt;li&gt;With the array that was returned from the split method, a &lt;strong&gt;map method&lt;/strong&gt; can be called to convert all to lowercase and then join with an hypen.
Summing it altogether we have this
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;spinalCase&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;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;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\W&lt;/span&gt;&lt;span class="sr"&gt;_/g&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="k"&gt;return&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;split&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;|&lt;/span&gt;&lt;span class="se"&gt;\s&lt;/span&gt;&lt;span class="sr"&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;map&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;=&amp;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;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;())&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="s2"&gt;-&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Check HTML :&lt;/strong&gt; &lt;br&gt;
Create a function to test if the given string is an HTML or not. Examples includes &lt;strong&gt;,&amp;lt;&amp;gt; &lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Solution&lt;br&gt;
An HTML string will always have an opening and closing parenthesis with zero or more string, but a backslash is optional&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;validateHTML&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="kd"&gt;let&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;/&amp;lt;&lt;/span&gt;&lt;span class="se"&gt;([&lt;/span&gt;&lt;span class="sr"&gt;A-Za-z&lt;/span&gt;&lt;span class="se"&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; *&lt;/span&gt;&lt;span class="se"&gt;\/?&lt;/span&gt;&lt;span class="sr"&gt;&amp;gt;/&lt;/span&gt;
&lt;span class="k"&gt;return&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;str&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;p&gt;Breaking this down, we are basically saying&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It should start with &lt;strong&gt;&amp;lt;&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Zero or more characters &lt;strong&gt;[A-Za-z]&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Zero or more spaces " *"&lt;/li&gt;
&lt;li&gt;An optional backslash and the final closing tag.&lt;/li&gt;
&lt;/ul&gt;


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

&lt;p&gt;&lt;strong&gt;Password validator :&lt;/strong&gt;&lt;br&gt;
Create a function to check if a given password follows this given format. Above 8 characters, at least a lowercase character, an uppercase character, a digit and a special character.&lt;/p&gt;

&lt;p&gt;Solution&lt;br&gt;
A password validator can be tricky. But let's start from the easier part, which should be making sure the password is 8 characters or above.&lt;br&gt;
&lt;/p&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="err"&gt;\&lt;/span&gt;&lt;span class="nx"&gt;w&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nx"&gt;W&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="sr"&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;w&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;match&lt;/span&gt; &lt;span class="nx"&gt;alphanumeric&lt;/span&gt; &lt;span class="nx"&gt;and&lt;/span&gt; &lt;span class="nx"&gt;underscore&lt;/span&gt; &lt;span class="nx"&gt;and&lt;/span&gt; &lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nx"&gt;W&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;match&lt;/span&gt; &lt;span class="nx"&gt;special&lt;/span&gt; &lt;span class="nx"&gt;character&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Now we need to make sure that at least one of each character actually appears in the password. We have to do this for each of the different characters but it is basically the same thing, so I will explain just one of them. &lt;br&gt;
&lt;strong&gt;Uppercase:&lt;/strong&gt; &lt;br&gt;
To match for uppercase, we need to use a look ahead &lt;strong&gt;(?=...)&lt;/strong&gt;. A look ahead makes sure that a character is followed by another specific character and basically returns true or false based on that. That is &lt;code&gt;/^(?=.*\[A-Z])/&lt;/code&gt;. It goes through the string and checks to see if zero or more characters (the period is a wild card used to match any character) are followed by an uppercase character. It uses &lt;strong&gt;asterisk&lt;/strong&gt; because it is possible the uppercase is the first character.&lt;br&gt;
So we do this for all the other types of characters we want to make sure occurs at least once in the string.&lt;br&gt;
&lt;/p&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="o"&gt;=&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="o"&gt;=&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;a&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;z&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="o"&gt;*&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;Z&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="o"&gt;*&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nx"&gt;W&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;If one or more of this returns false, for example a digit cannot be found it results to false.&lt;br&gt;
But if one or more of this occur in the string, we can then go ahead to match for the quantity. That is, the number of characters in the string. putting it altogether&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;validatePassword&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="kd"&gt;let&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;.*&lt;/span&gt;&lt;span class="se"&gt;\d)(?=&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;.*&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;.*&lt;/span&gt;&lt;span class="se"&gt;\W)[\w\W]{8,}&lt;/span&gt;&lt;span class="sr"&gt;$/&lt;/span&gt;
&lt;span class="k"&gt;return&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;str&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;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So we have come to the end of this rather long article, I really hope you learnt something and you are more comfortable with regular expression now. With this, algorithms and regex&lt;br&gt;
should to a level pose no threat to you again. Just follow the patterns we used to solve some of this questions and you will be fine. If you have any suggestions or questions let me know in the comment section.&lt;br&gt;
If you enjoyed this, smash that like button and share it with your friends. You can also follow me on Twitter &lt;a href="//twitter.com/fakoredeDami"&gt;@fakoredeDami&lt;/a&gt;.&lt;br&gt;
Thank you for reading.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Introduction to Regular Expression in JavaScript</title>
      <dc:creator>Fakorede Damilola</dc:creator>
      <pubDate>Sun, 31 Jan 2021 02:46:58 +0000</pubDate>
      <link>https://forem.com/fakorededamilola/introduction-to-regular-expression-in-javascript-2o8m</link>
      <guid>https://forem.com/fakorededamilola/introduction-to-regular-expression-in-javascript-2o8m</guid>
      <description>&lt;p&gt;Have you ever been in a situation in school or college where your teacher gives you two to three different solutions on how to solve particular problem based on how it is structured, he/she will be like "if the question has an even number use solution a, if it has a prime number use solution c" and so on. Then after dropping about three different approaches, he then drops a fourth one and calls it the almighty formula( no matter the type of number, this solution will solve everything). This happened to me quite a number of times back in school and honestly it was annoying.&lt;/p&gt;

&lt;p&gt;Coming back to JavaScript or programming in general, we tend to have our own almighty formula for &lt;strong&gt;strings&lt;/strong&gt;. This formula can solve almost everything relating to strings from matching to testing and so on. This is called &lt;strong&gt;regular expression&lt;/strong&gt; or &lt;strong&gt;regex&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  So what is a regular expression
&lt;/h2&gt;

&lt;p&gt;Regular expressions are basically patterns used to match character combinations in some part of a string. Different ways of creating a regular expression includes&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A regular expression can be created using a regular expression literal. (A regular expression literal is made of two backslashes for example &lt;code&gt;/regex/&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Calling the constructor function of the regex object for example &lt;code&gt;new regExp("abc+d")&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The first one is best used when you know the character combinations you want to match , while the second one is used if for example you are storing the regex in a variable or passing it from a user input.&lt;br&gt;
Regular expression has a couple of built in methods that are basically used to test strings with the defined pattern. Here are the ones we will be looking at today&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Test&lt;/li&gt;
&lt;li&gt;Match&lt;/li&gt;
&lt;li&gt;Replace and &lt;/li&gt;
&lt;li&gt;Split&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Don't worry about how the expression are created, we will also talk about that.&lt;/p&gt;
&lt;h4&gt;
  
  
  Test
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;test&lt;/code&gt; method is one of the most common method you will be using, and it basically takes a regular expression and tests it with a text you pass in. The test method returns true if a part of the text you pass in matches the regular expression.&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="nx"&gt;abcd&lt;/span&gt;&lt;span class="o"&gt;/&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;abcd&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// returns true. &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Basically a literal expression matches letter for letter with the string passed to it except otherwise stated. So in this case, &lt;code&gt;/abcd/&lt;/code&gt; matches "abcd" exactly, &lt;strong&gt;a for a&lt;/strong&gt; and &lt;strong&gt;b for b&lt;/strong&gt; etc,&lt;/p&gt;

&lt;h4&gt;
  
  
  Match
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;match&lt;/code&gt; method also searches through a string that you pass in, but this returns the exact match found as an array. Note that the match method also returns the &lt;strong&gt;position&lt;/strong&gt; where the match was found and the &lt;strong&gt;text&lt;/strong&gt; passed except you use a &lt;code&gt;g&lt;/code&gt; flag, which you will learn about below. But basically with the &lt;code&gt;g&lt;/code&gt; flag it returns just the matched string alone&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;abcd&lt;/span&gt;&lt;span class="dl"&gt;"&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;/abc/&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// returns ["abc", index: 0, input: "abcd", groups: undefined]&lt;/span&gt;
&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;abcd&lt;/span&gt;&lt;span class="dl"&gt;"&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;/abc/g&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// returns ["abc"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It searches the same way the test method does but instead of returning true/false ,it returns an array.&lt;/p&gt;

&lt;h4&gt;
  
  
  Replace
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;replace&lt;/code&gt; method basically does just that &lt;strong&gt;replace&lt;/strong&gt;. The replace method like the match and test method searches the whole string but instead of returning true if found or an array, it replaced that exact match with another string you pass in.&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Java is awesome&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/Java/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;JavaScript&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// returns "JavaScript is awesome"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Split
&lt;/h4&gt;

&lt;p&gt;If you have worked with JavaScript for a while, then you should be familiar with the &lt;code&gt;split&lt;/code&gt; method. The split basically takes a string or sentence and breaks it into an array based on the value you pass to it. This value is the &lt;strong&gt;separator&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;JavaScript is awesome guys&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&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;// ["JavaScript","is","awesome","guys"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Basically the split method loops through the string and anywhere it finds the separator that was passed (in this case, a space), it breaks it into an array. The split method also accepts regex as a separator which we will see later.&lt;/p&gt;

&lt;h3&gt;
  
  
  Flags
&lt;/h3&gt;

&lt;p&gt;Before we move to constructing various regex, we will take a detour and talk about &lt;strong&gt;flags&lt;/strong&gt; in regex.&lt;br&gt;
Flags are optional when writing regex, but they help us a great deal. we are going to talk about 2 of the most important ones in javascript.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;i&lt;/strong&gt; - The &lt;code&gt;i&lt;/code&gt; flag makes searches case insensitive, that is it makes it such that there is no difference btw &lt;strong&gt;a&lt;/strong&gt; and &lt;strong&gt;A&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;g&lt;/strong&gt; - The &lt;code&gt;g&lt;/code&gt;(global) flag looks through the whole string and get multiple matches. Without it regex finds the first match and stop.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now that we have talked about some methods and flags in regex, we will now write different forms of regex. Note that as we write this regex, we will also be testing it using any of the &lt;strong&gt;methods&lt;/strong&gt; or &lt;strong&gt;flags&lt;/strong&gt; described above and this will be random, but any method or flag can be used based on what you want to do.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Like I said earlier, we can test for the exact string.
&lt;/li&gt;
&lt;/ul&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;regex&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sr"&gt;/Quick/&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;string1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Quick&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;string2&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;quick&lt;/span&gt;&lt;span class="dl"&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;string1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// return true&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;string2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// return false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;From above, the regex matches the exact word with the exact cases (uppercase for uppercase)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can search for multiple strings using the or operator &lt;code&gt;|&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&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;regex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sr"&gt;/quick|brown|lean/&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;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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;the quick fox&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// returns true&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;the quick brown fox&lt;/span&gt;&lt;span class="dl"&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;string&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="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// returns ["quick", index: 4, input: "the quick brown fox", groups: undefined]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This returns the first match found.&lt;br&gt;
With the global flag, it returns all match found&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;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;string&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;/quick|brown/g&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt; &lt;span class="c1"&gt;// returns ["quick", "brown"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The dot/period &lt;code&gt;.&lt;/code&gt; is called a wildcard and it matches any character, number, symbol and so on. But it matches just one character.
&lt;/li&gt;
&lt;/ul&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;regex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sr"&gt;/hu./g&lt;/span&gt; 
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This are words with hu, hug, hum, hub and huh&lt;/span&gt;&lt;span class="dl"&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;string&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="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// returns ["hu,","hug","hum","hub","huh"] &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You remember the flags right, normally &lt;code&gt;regex&lt;/code&gt; should find the first match and stop but because of the &lt;strong&gt;global flag&lt;/strong&gt;, it goes through everything.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Character class &lt;code&gt;[]&lt;/code&gt; lets you define a group of character you want to match. It basically matches any character inside that character class.
&lt;/li&gt;
&lt;/ul&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;string&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;this are b_g strings e.g bOg big bAg bug&lt;/span&gt;&lt;span class="dl"&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;string&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;/b&lt;/span&gt;&lt;span class="se"&gt;[&lt;/span&gt;&lt;span class="sr"&gt;oui_&lt;/span&gt;&lt;span class="se"&gt;]&lt;/span&gt;&lt;span class="sr"&gt;g/gi&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// returns ["b_g","bog","big","bug"] &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without the &lt;code&gt;i&lt;/code&gt; flag &lt;strong&gt;bOg&lt;/strong&gt; and &lt;strong&gt;bAg&lt;/strong&gt; won't be matched because in regex &lt;strong&gt;A&lt;/strong&gt; is different from &lt;strong&gt;a&lt;/strong&gt;. But the &lt;code&gt;i&lt;/code&gt; flag makes it such that regex does not match cases.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The hypen &lt;code&gt;-&lt;/code&gt; when used inside a character set, let's you define a range of characters or numbers instead of listing them out
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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="s2"&gt;bay bby bcy bdy&lt;/span&gt;&lt;span class="dl"&gt;"&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;/b&lt;/span&gt;&lt;span class="se"&gt;[&lt;/span&gt;&lt;span class="sr"&gt;a-c&lt;/span&gt;&lt;span class="se"&gt;]&lt;/span&gt;&lt;span class="sr"&gt;y/g&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// returns ["bay", "bby", "bcy"]&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="s2"&gt;123456789&lt;/span&gt;&lt;span class="dl"&gt;"&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;5-8&lt;/span&gt;&lt;span class="se"&gt;]&lt;/span&gt;&lt;span class="sr"&gt;/g&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// returns ["5", "6", "7", "8"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The caret &lt;code&gt;^&lt;/code&gt; symbol when used inside a character set makes sure that none of the string in that set is matched. It can be used with or without the hypen
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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="s2"&gt;bay bby bcy bdy&lt;/span&gt;&lt;span class="dl"&gt;"&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;/b&lt;/span&gt;&lt;span class="se"&gt;[^&lt;/span&gt;&lt;span class="sr"&gt;a-c&lt;/span&gt;&lt;span class="se"&gt;]&lt;/span&gt;&lt;span class="sr"&gt;y/g&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// returns  ["bdy"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The caret &lt;code&gt;^&lt;/code&gt; symbol when used at the beginning of a regular expression outside the character set basically means that the string passed in must start with that particular word or number.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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="s2"&gt;123456789&lt;/span&gt;&lt;span class="dl"&gt;"&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;5-8&lt;/span&gt;&lt;span class="se"&gt;]&lt;/span&gt;&lt;span class="sr"&gt;/g&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="c1"&gt;// returns null &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From above, we are basically saying the string must start with &lt;strong&gt;5,6,7 or 8&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The dollar &lt;code&gt;$&lt;/code&gt; symbol makes sure that a string ends with a particular character or characters. Similar to the caret, just different positions
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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="sr"&gt;/JavaScript$/i&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I love javascript&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// returns true&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="sr"&gt;/JavaScript$/i&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I love javscript&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;//returns false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;+&lt;/code&gt; symbol allows you to match the same character multiple times
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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="sr"&gt;/huh+/&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;huhhhhhhhhhhh&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// returns true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;*&lt;/code&gt; symbol lets you match the same character multiple times also, but while the &lt;code&gt;+&lt;/code&gt; symbol lets you match one or more times the &lt;code&gt;*&lt;/code&gt; matches zero or more time. Basically with the &lt;code&gt;+&lt;/code&gt;, the character you are matching must appear at least once but with the &lt;code&gt;*&lt;/code&gt; it might or might not appear.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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="sr"&gt;/huh*/&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hu&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// returns true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;?&lt;/code&gt; is used to make a character optional that is, it might exist or not
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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="sr"&gt;/colou&lt;/span&gt;&lt;span class="se"&gt;?&lt;/span&gt;&lt;span class="sr"&gt;r/&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;color&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// returns true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;look head&lt;/strong&gt; looks ahead of the string to check if a particular letter exists.
&lt;strong&gt;Positive look ahead&lt;/strong&gt; makes sure that the string specified exist &lt;code&gt;(?=...)&lt;/code&gt; for example &lt;code&gt;(?=u)&lt;/code&gt; and the &lt;strong&gt;negative look ahead&lt;/strong&gt; makes sure the string is not there &lt;code&gt;(?!...)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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="s2"&gt;yes!&lt;/span&gt;&lt;span class="dl"&gt;"&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;/yes&lt;/span&gt;&lt;span class="se"&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;/g&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// returns ["yes"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above we only want to match &lt;strong&gt;yes&lt;/strong&gt; if it is followed by an exclamation mark.&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;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="s2"&gt;yes?&lt;/span&gt;&lt;span class="dl"&gt;"&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;/yes&lt;/span&gt;&lt;span class="se"&gt;(?=\?)&lt;/span&gt;&lt;span class="sr"&gt;/g&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// returns ["yes"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;?&lt;/code&gt; is a special character in regex like we have seen above, so in order to check for it you need to escape it. The same way you escape quotation inside a string.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We can also check for groups of characters using parentheses &lt;code&gt;()&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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="sr"&gt;/ex&lt;/span&gt;&lt;span class="se"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;pect|cept&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="sr"&gt;/&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;expect&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// returns true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The next set of characters we will be looking at are certain alphabets that have special meaning in regex, but in order to use them for this their special purpose, you use the &lt;code&gt;\&lt;/code&gt; to escape them like we escaped &lt;strong&gt;?&lt;/strong&gt; above.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;\w&lt;/code&gt; is used to match all &lt;strong&gt;uppercase&lt;/strong&gt;, &lt;strong&gt;lowercase&lt;/strong&gt;, &lt;strong&gt;numbers&lt;/strong&gt; and &lt;strong&gt;underscore&lt;/strong&gt;. This is basically the same as &lt;code&gt;[a-zA-Z0-9_]&lt;/code&gt;, just really shorter.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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="s2"&gt;JAVASCRIPT _ react = 5 and 6 :)&lt;/span&gt;&lt;span class="dl"&gt;"&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;\w&lt;/span&gt;&lt;span class="sr"&gt;/g&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// ["J", "A", "V", "A", "S", "C", "R", "I", "P", "T", "_", "r", "e", "a", "c", "t", "5", "a", "n", "d", "6"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;\W&lt;/code&gt; is used to match anything that is not an &lt;strong&gt;alphabet&lt;/strong&gt;, &lt;strong&gt;number&lt;/strong&gt; or &lt;strong&gt;underscore&lt;/strong&gt;, similar to [^a-zA-Z0-9_].
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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="s2"&gt;JAVASCRIPT _ react = 5 and 6 :)&lt;/span&gt;&lt;span class="dl"&gt;"&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;\W&lt;/span&gt;&lt;span class="sr"&gt;/g&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// returns [" ", " ", " ", "=", " ", " ", " ", " ", ":", ")"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It basically matched the &lt;strong&gt;spaces&lt;/strong&gt;, &lt;strong&gt;=&lt;/strong&gt;, &lt;strong&gt;:&lt;/strong&gt; and &lt;strong&gt;)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;\d&lt;/code&gt; is used to match digits or numbers, similar to [0-9]
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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="s2"&gt;JAVASCRIPT _ react = 5 and 6 :)&lt;/span&gt;&lt;span class="dl"&gt;"&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;\d&lt;/span&gt;&lt;span class="sr"&gt;/g&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// returns ["5","6"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;/D&lt;/code&gt; is used to match anything that is not a digit or number, similar to [^0-9].
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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="s2"&gt;JAVASCRIPT _ react = 5 and 6 :)&lt;/span&gt;&lt;span class="dl"&gt;"&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;\D&lt;/span&gt;&lt;span class="sr"&gt;/g&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// returns ["J", "A", "V", "A", "S", "C", "R", "I", "P", "T", " ", "_", " ",...].&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;\s&lt;/code&gt; matches form fields, carriage return level, white line and new lines
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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="s2"&gt;JAVASCRIPT _ react = 5 and 6 :)&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\s&lt;/span&gt;&lt;span class="sr"&gt;/g&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// returns ["JAVASCRIPT", "_", "react", "=", "5", "and", "6", ":)"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;\S&lt;/code&gt; matches anything except from form fields, carriage return level, white line and new lines
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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="s2"&gt;JAVASCRIPT _ react = 5 and 6 :)&lt;/span&gt;&lt;span class="dl"&gt;"&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;\S&lt;/span&gt;&lt;span class="sr"&gt;/g&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// returns ["J", "A", "V", "A", "S", "C", "R"...]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Quantifiers
&lt;/h3&gt;

&lt;p&gt;Quantifiers &lt;code&gt;{}&lt;/code&gt; are used to match specific number of characters. You can specify the values like this, &lt;code&gt;{x,y}&lt;/code&gt; where x is the lower bound and y is the higher bound, so basically the number of characters written must be between x and y&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;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="sr"&gt;/huh&lt;/span&gt;&lt;span class="se"&gt;{2,5}&lt;/span&gt;&lt;span class="sr"&gt;/&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;huhhh&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;//returns true&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="sr"&gt;/huh&lt;/span&gt;&lt;span class="se"&gt;{2,5}&lt;/span&gt;&lt;span class="sr"&gt;/&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;huh&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;//returns false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also decide to use just x. Using only x means the number of character inserted must be from &lt;strong&gt;x to infinity&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="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="s2"&gt;huhhhhhhhhhhhhhhh&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/h&lt;/span&gt;&lt;span class="se"&gt;{2,}&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;g&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;//returns hug&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also use the quantifier to match an exact number of character like below&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;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="s2"&gt;huhhhh&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/h&lt;/span&gt;&lt;span class="se"&gt;{4}&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;g&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 have read it to this point congratulations. I know this is a really long and exhausting article but that is regex for you. I hope you have learnt a lot from this.&lt;br&gt;
There are a few characters and combinations I left out, I felt this are the ones you will mostly use.&lt;br&gt;
It is OK if you feel overwhelmed with this, when I first learnt regex, I was confused and this was mostly because I did not know when to use it  and also because it looked really difficulty to construct.&lt;br&gt;
So in my next article, we are going to go through &lt;strong&gt;a couple of coding problems&lt;/strong&gt; that should help you get comfortable with regex. I am also going to mix it a few other non regex questions so that you can know when you can or cannot use it(I might be wrong about this), see you next time, bye for now.&lt;/p&gt;

&lt;p&gt;Thank you.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Introduction to algorithm and the big O notation</title>
      <dc:creator>Fakorede Damilola</dc:creator>
      <pubDate>Fri, 27 Nov 2020 20:50:29 +0000</pubDate>
      <link>https://forem.com/fakorededamilola/introduction-to-algorithm-and-the-big-o-notation-533</link>
      <guid>https://forem.com/fakorededamilola/introduction-to-algorithm-and-the-big-o-notation-533</guid>
      <description>&lt;p&gt;Algorithms are very important in programming, every programmer will definitely end up writing an algorithm one way or another. In this article, I am going to explain &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The term algorithm&lt;/li&gt;
&lt;li&gt;The best possible solution for an algorithm&lt;/li&gt;
&lt;li&gt;The big-O notation&lt;/li&gt;
&lt;li&gt;Measuring performance (asymoptoic function)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;a&gt;The term algorithm&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;An algorithm is a sequence of steps (instructions) used to solve a clearly defined problem. There are two things you should note here, &lt;strong&gt;a sequence of steps&lt;/strong&gt; and a &lt;strong&gt;clearly defined problem&lt;/strong&gt;. So basically, an algorithm is any piece of code that you write (a line, 20 lines etc), that solves a problem. In as much has the problem follows the same pattern, that program you write should be able to solve it. &lt;br&gt;
Let's look at an example.&lt;br&gt;
write a program that sums up all the numbers from 1 to 10.&lt;br&gt;
This can be easily done with a &lt;strong&gt;for loop&lt;/strong&gt;. I will use JavaScript to solve this question&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sumNumber(){
let sum=0 
for (let i=0;i&amp;lt;=10;i++){ 
sum =sum+i 
} 
return sum 
}
console.log(sumNumber())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function above &lt;strong&gt;sumNumber&lt;/strong&gt; should be an algorithm. Why, because it solves a clearly defined problem (i.e it summed all the number from 1 to 10) which is what I asked for. But notice I said &lt;strong&gt;should be an algorithm&lt;/strong&gt; and not &lt;strong&gt;it is an algorithm&lt;/strong&gt;. This is not because it is a five line code and the bosses in programming write a single line, or because it is a simple for loop and real nerds with use reduce to solve it. But this is not an algorithm simply because this function is solving an exact question. In the definition above, I said it is a sequence of step that solves a clearly defined problem. We got the sequence of steps part (our five liner was awesome and we did not have to move through the whole planet or write 500 lines to get it done), but our algorithm solved the question for a clearly defined and exact problem. As programmers, we solve problems. We write code that helps solve the difficulties our users might have. So imagine a user wants to sum up all the numbers from one to 100 and comes to our code. Luckily our code won't break up, but it will give a devastating result which we don't want. So how can we write this so that it can solve a clearly defined but not exact problem, which is what all algorithms should do. What if instead of assuming that every number a user wants to add up will always be 10, why don't we take the longer route and assume it is unknown and only becomes known when our user inputs that number. That way our function will always loop to the number the user inputs and not &lt;strong&gt;10&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sumNumber(n){ 
let sum=0 
for (let i=0;i&amp;lt;=n;i++){ 
sum =sum+i 
} 
return sum 
}
console.log(sumNumber(100))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So by changing the number in the parenthesis, we can change the value of n, and therefore change our result. Therefore adhering to the definition altogether i.e &lt;strong&gt;sequence of steps&lt;/strong&gt; (This five line is awesome) and &lt;strong&gt;clearly defined&lt;/strong&gt; (no matter the number inputted, has long as the problem is to add up all the numbers from 1 to that number, our algorithm is more than able to solve it.)&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a&gt;The best possible solution for an algorithm&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;There is a popular saying in programming that you can solve one thing in 1000 different ways. A developer could decide to use the reduce higher order array or even a math formula etc. The fact is different people solve the same problem different ways. But then one method of solving a problem should to an extent be considered better than another (i.e the best possible solution). So the question now is what is the criteria for picking the best possible solution. Is it the&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Minimum amount of code (whoa one-liner, this is the best algorithm programmer :) )&lt;/li&gt;
&lt;li&gt;Best performance (the algorithm that takes the least amount of time to finish)&lt;/li&gt;
&lt;li&gt;Least amount of memory (the algorithm that does not take a lot of resources on the computer) or&lt;/li&gt;
&lt;li&gt;Personal preference (you like code A over code B)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Oftentimes, the best solution is the best performance (takes the least amount of time). So with the problem above, let's try to measure the best performance. In the browser, there is a performance object on which we can call the now method and this gives the current time stamp. So, we can easily get the timestamp before and after our program runs and also their differences to see how long the program ran.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sumNumber(n){ 
let sum=0 for (let i=0;i&amp;lt;=n;i++){
 sum =sum+i 
} 
return sum 
} 
let start,end 
start =performance.now() 
sumNumber(3) 
end =performance.now() 
console.log(end-start) //1.0576668876
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I am guessing your result is not the same has mine, and that is OK. The problem with getting the performance this way is that it depends on a lot of factors such has the speed of your laptop, the amount of programs running in the background etc. There are too many variables that can affect your result and this can definitely lead to errors if performance was judged this way. But for now let's stick with this. If you try increasing the number to 5, then 10 you will see that the result is literally the same thing. Same thing goes with 100 and even 1000. But If you try 100000, 1000000 and 10000000 (try not to go too large), you will begin to notice a trend&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;start =performance.now() 
sumNumber(100000) 
end =performance.now() 
console.log(end-start) //20.1 
start =performance.now() 
sumNumber(1000000) 
end =performance.now() 
console.log(end-start) //20.8 
start =performance.now() 
sumNumber(10000000) 
end =performance.now() 
console.log(end-start) //198.2 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It tends to take a longer time for bigger numbers and that is the way it should be, but it is at the rate of &lt;strong&gt;×10&lt;/strong&gt; of the previous number when we add an additional &lt;strong&gt;zero&lt;/strong&gt; to that number. E.g if we double the number, the performance is also doubled and if we multiply the number by 10, the performance is also multiplied by 10. &lt;br&gt;
But for performance, we should not really care about the values gotten, cause like I said earlier, this values depends on a number of factors. But the general trend should be observed, especially for larger numbers.&lt;br&gt;
Considering this on a graph, we would draw a linear graph i.e has the values get larger so does the time and with the same factor. The varying values is generally due to other factors and that is how you judge algorithms, not with numbers but with the general trend. So with this trend, we can write a statement about the performance of an algorithm based on the time it take in relation to the input. This is called time complexity. The function above can be said to have a &lt;strong&gt;linear time complexity&lt;/strong&gt;( has the value increases, the time increases at the same rate i.e linearly). &lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;a&gt;The big O Notation&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;From above, we have seen that has our performance (time) increases by the same factor which our value increases which we called &lt;strong&gt;linear time complexity&lt;/strong&gt;. But that is not the only time complexity we have. There is also the constant time complexity. Can you think of a way the algorithm above can have a constant time complexity?? &lt;br&gt;
What if instead of looping everytime we want to get the sum of numbers we use a simple math formula. So basically instead of our algorithm stopping at the for loop and running that same line for e.g 10,000 times which might take 5s, it just simply uses our formula and run once i.e&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sumNumber(n){
return (n/2)*(n+1) 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now when you try this for all n and calculate the performance with &lt;strong&gt;performance.now()&lt;/strong&gt;, you will get almost the same values and remember, we don't really care about the values because they are polluted by other activities running on our conputer, but instead we care about the general trend which is has the values (n) increases, the time remains the same. No matter the value of n i.e constant time complexity. So from this two algorithms, which one do you feel is the best method to use and solve this problem i.e Is it the linear time complexity (has the value increases the time increases by the same value) or the constant time complexity (has the value increases the time remains the same). I feel it is the constant time complexity. But the issue is, it is not for every algorithm you can find a math formula or a way to get the trend to be a constant time complexity. Sometimes you just have to stick With the linear time complexity. But there are other time complexity such as &lt;strong&gt;quadratic Time complexity&lt;/strong&gt; (has the value increases, the time doubles by that factor), the &lt;strong&gt;cubic Time complexity&lt;/strong&gt; etc. &lt;br&gt;
But when talking to other people, developers especially, there is a way to describe this time complexity using the &lt;strong&gt;big O notation&lt;/strong&gt;. For example, the &lt;strong&gt;linear time complexity&lt;/strong&gt; can be written has o(n) pronounced has (o of n). This is written in terms of the performance that is has n values increase, the time increases by the same value (n). Can you guess constant time complexity :). This will be o(1) has the value increases, the performance remains constant i.e 1. &lt;strong&gt;quadratic Time complexity&lt;/strong&gt; o(n^2), &lt;strong&gt;cubic Time complexity&lt;/strong&gt; o(n^3), logarithmic time complexity o(log n) (i.e has the values increase, the performance increases by a value of log of that number).&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;a&gt;Measuring performance (asymoptoic function)&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Now that we understand big O Notation and how to get the performance, the next question is how can we know the time complexity of a given algorithm. We could follow the route above and calculate for specific values and take note of the general trend (but that will take a while and some trends are not so straight forward e.g logarithmic trend ), or we could try to memorize it for each algorithm (that sounds fine but then we will have to start cramming and look for all possible algorithm etc). &lt;br&gt;
But there is a way we can get the big O via &lt;strong&gt;asymoptoic analysis&lt;/strong&gt;. We can do this through three steps.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Define the function (not the algorithm function but the math function. I Will explain this)&lt;/li&gt;
&lt;li&gt;Find the fastest growing term&lt;/li&gt;
&lt;li&gt;Remove the coefficients&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's take for example the &lt;strong&gt;sumNumber&lt;/strong&gt; algorithm above and talk about this three things in details.&lt;/p&gt;
&lt;h4&gt;
  
  
  Define the function
&lt;/h4&gt;

&lt;p&gt;From above, I said the function I am talking about is not the algorithm sumNumber but the mathematical time complexity function. Now how do we get the mathematical time complexity function?  In this case, that of function sumNumber. We need to find the number of expression execution i.e each expression. Basically, each line of code and we will count how many times it takes that line to run. So let's test for n=1 and n=n.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sumNumber(n){
let sum=0 
for (let i=0;i&amp;lt;=n;i++){ 
sum =sum+i 
} 
return sum 
} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So for n=1,&lt;br&gt;
The first line &lt;strong&gt;let sum=0&lt;/strong&gt; runs once. That is this algorithm defines that line just once and that is all.&lt;br&gt;
The second line &lt;strong&gt;for (let i=0;i&amp;lt;=n;i++){&lt;/strong&gt; also runs once. This defines the condition for the loop.&lt;br&gt;
The third line &lt;strong&gt;sum =sum+i&lt;/strong&gt; is inside the loops and this will keep on running based on the value of &lt;strong&gt;n&lt;/strong&gt;, i.e it runs from &lt;strong&gt;1&lt;/strong&gt; to &lt;strong&gt;n&lt;/strong&gt; which in our case is one, so it runs once.&lt;br&gt;
The fifth line &lt;strong&gt;return sum&lt;/strong&gt; also run once. It returns the answer once.&lt;br&gt;
For n=n,&lt;br&gt;
This is quite similar to n=1 above, the first and second line run once each like above.&lt;br&gt;
The third line &lt;strong&gt;sum =sum+i&lt;/strong&gt; will run from i=1 all through n, and in this case the n is actually n, so it will run n times.&lt;br&gt;
Now, we will add all the values together. &lt;br&gt;
For n=1&lt;br&gt;
That is &lt;strong&gt;1+1+1+1 = 4&lt;/strong&gt;.&lt;br&gt;
For n=n&lt;br&gt;
The sum will be 1+1+n+1 =3+n.&lt;br&gt;
Now remember, since in algorithm we are not solving for an exact problem but for unknown values, it will only make sense to use the result gotten from &lt;strong&gt;n=n&lt;/strong&gt;. I used &lt;strong&gt;n=1&lt;/strong&gt; just to help you understand.&lt;br&gt;
For n values, the math function = &lt;strong&gt;3+n&lt;/strong&gt;. We can rewrite this has &lt;strong&gt;1*n + 3&lt;/strong&gt; (remember 1*n is still n). Like I said earlier, we don't really care about numbers but trends because number tends to be polluted. So we could easily turn that math function into a trend &lt;strong&gt;T =a*n + b&lt;/strong&gt;, i.e the performance(T) for n no of values is this.&lt;/p&gt;

&lt;h4&gt;
  
  
  Fastest growing term
&lt;/h4&gt;

&lt;p&gt;So from above, we already have this function T =a*n + b , now the next thing is to find the fastest growing term.&lt;br&gt;
From the function, it is pretty obvious that &lt;strong&gt;b&lt;/strong&gt; will remain the same no matter the value of n, it is a constant. But not a. As the value of n increases so those the value of a. Therefore a is the fastest growing term and we can reduce our function to &lt;strong&gt;T= a*n&lt;/strong&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Remove the coefficients
&lt;/h4&gt;

&lt;p&gt;We are left with T=a*n, removing the coefficients (a), &lt;strong&gt;T=n&lt;/strong&gt;. Which is our finally statement i.e T increases has n increases with the same factor &lt;strong&gt;o(n)&lt;/strong&gt;.&lt;br&gt;
Now can you try this method for the constant time complexity. Let me know your answer in the comment section below.&lt;/p&gt;

&lt;p&gt;Thank you for reading to this point, you can follow me on twitter &lt;a href="https://twitter.com/fakoredeDami"&gt;@fakoredeDami&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>algorithms</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Building a connect-four game with JavaScript</title>
      <dc:creator>Fakorede Damilola</dc:creator>
      <pubDate>Sat, 14 Nov 2020 21:14:41 +0000</pubDate>
      <link>https://forem.com/fakorededamilola/building-a-connect-four-game-with-javascript-1f45</link>
      <guid>https://forem.com/fakorededamilola/building-a-connect-four-game-with-javascript-1f45</guid>
      <description>&lt;p&gt;The connect four game can be likened to two players building a wall, each player with a different brick(i.e color). The rule is simple, each person drops a brick one at a time. Like an actually wall, a player must drop the brick on top of another cause a brick cannot stand alone in the air. The game is won when one of the two players can lay up their brick in an horizontal / vertical or diagonal manner made up of four bricks (i.e connect-four).&lt;/p&gt;

&lt;p&gt;Here is how you can build your connect four game using HTML, CSS and JavaScript. I will be covering the&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;basic HTML and CSS&lt;/li&gt;
&lt;li&gt;variables and data needed in JS&lt;/li&gt;
&lt;li&gt;board and DOM load in JS&lt;/li&gt;
&lt;li&gt;restart game function&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;a&gt;basic HTML and CSS&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;The structure of this project is straightforward. This skeleton(HTML and CSS) will house data from the JS&lt;/p&gt;

&lt;h4&gt;
  
  
  HTML
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="container"&amp;gt;
&amp;lt;div class="currentPlayer"&amp;gt;The current player is 
&amp;lt;span class="player"&amp;gt;&amp;lt;/span&amp;gt;
&amp;lt;div class="board"&amp;gt;&amp;lt;/div&amp;gt;
&amp;lt;div class="restart"&amp;gt;
&amp;lt;button class="playAgain"&amp;gt;play again&amp;lt;/button&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From above, we have a span with a class &lt;strong&gt;player&lt;/strong&gt; that will tell us whose turn it is to play, The &lt;strong&gt;grid&lt;/strong&gt; class will house the game board and the &lt;strong&gt;restart&lt;/strong&gt; class with house the button to easily renew the game.&lt;/p&gt;

&lt;h4&gt;
  
  
  CSS
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.board { 
border:1px solid black; 
display:flex; 
flex-wrap:wrap; 
height:276px; 
width:330px; 
background:blue 
} 
.board div { 
height:40px; 
width:40px; 
margin:3px;
border-radius:20px 
}
.square{ 
background:white; 
} 
.taken { 
/*background:red !important */
} 
.player-one { 
background:red;
}
.player-two { 
background:yellow; 
}
.restart{ 
background: red; 
width: 200px; 
height: 200px; 
position: fixed; 
top: 100px; 
left: 100px; 
display: none; 
justify-content: center; 
align-items: center; 
flex-direction: column; 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Most of what is going on in the CSS might make little or no sense to you, has we start working with JS, you will begin to understand what is going on.&lt;br&gt;
But from here, we are setting up the board which will house a lot of divs. This are all block elements, but the flex property arranges them horizontally. The flex-wrap moves the div to the next line once it is about to reach the end of the board (remember we gave it a width and height).&lt;br&gt;
The &lt;strong&gt;.board div&lt;/strong&gt; styles each div inside the board. &lt;strong&gt;player-one&lt;/strong&gt; and &lt;strong&gt;player-two&lt;/strong&gt; gives each player a different color of brick. (Note that the commented parts is to help you understand what is going on, and they have no actually effect on the game. Has time goes on, we will comment and uncomment them to show what is actually going on).&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;a&gt;variables and data needed in JS&lt;/a&gt;
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let board=document.querySelector(".board") 
let player=document.querySelector(".player") 
let playAgain=document.querySelector(".playAgain") 
let restart=document.querySelector(".restart") 
let box=0 
let winningArray = [ 
[0, 1, 2, 3], [41, 40, 39, 38],[7, 8, 9, 10], 
[34, 33, 32, 31], [14, 15, 16, 17], [27, 26, 25, 24], 
[21, 22, 23, 24], [20, 19, 18, 17], [28, 29, 30, 31], 
[13, 12, 11, 10], [35, 36, 37, 38], [6, 5, 4, 3], 
[0, 7, 14, 21], [41, 34, 27, 20], [1, 8, 15, 22], 
[40, 33, 26, 19], [2, 9, 16, 23], [39, 32, 25, 18], 
[3, 10, 17, 24], [38, 31, 24, 17], [4, 11, 18, 25], 
[37, 30, 23, 16], [5, 12, 19, 26], [36, 29, 22, 15], 
[6, 13, 20, 27], [35, 28, 21, 14], [0, 8, 16, 24], 
[41, 33, 25, 17], [7, 15, 23, 31], [34, 26, 18, 10], 
[14, 22, 30, 38], [27, 19, 11, 3], [35, 29, 23, 17], 
[6, 12, 18, 24], [28, 22, 16, 10], [13, 19, 25, 31], 
[21, 15, 9, 3], [20, 26, 32, 38], [36, 30, 24, 18], 
[5, 11, 17, 23], [37, 31, 25, 19], [4, 10, 16, 22], 
[2, 10, 18, 26], [39, 31, 23, 15], [1, 9, 17, 25], 
[40, 32, 24, 16], [9, 7, 25, 33], [8, 16, 24, 32], 
[11, 7, 23, 29], [12, 18, 24, 30], [1, 2, 3, 4], 
[5, 4, 3, 2], [8, 9, 10, 11], [12, 11, 10, 9],
[15, 16, 17, 18], [19, 18, 17, 16], [22, 23, 24, 25], 
[26, 25, 24, 23], [29, 30, 31, 32], [33, 32, 31, 30], 
[36, 37, 38, 39], [40, 39, 38, 37], [7, 14, 21, 28], 
[8, 15, 22, 29], [9, 16, 23, 30], [10, 17, 24, 31], 
[11, 18, 25, 32], [12, 19, 26, 33], [13, 20, 27, 34] 
]; 
let currentPlayer=1 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;From above, we get the &lt;strong&gt;board&lt;/strong&gt;, the &lt;strong&gt;player&lt;/strong&gt;, and the &lt;strong&gt;restart&lt;/strong&gt; button from the HTML5 via querySelector. The &lt;strong&gt;box&lt;/strong&gt; variable will be used to check if the board is filled and the &lt;strong&gt;winningArray&lt;/strong&gt; is just an array that houses the different possible way a player can win the game, &lt;strong&gt;currentPlayer&lt;/strong&gt; changes whose turn it is to play.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;a&gt;board and DOM load in JS&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Like my earlier article on &lt;a href="https://dev.to/fakorededamilola/create-a-memory-game-with-js-1l9j/edit"&gt;memory game&lt;/a&gt;, most of the heavy lifting will be done via JS.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.addEventListener("DOMContentLoaded", loadDOM)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On DOM load, we will call a function &lt;strong&gt;loadDOM&lt;/strong&gt; which will set the game and everything&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//load dom function

function loadDOM(){ 
createBoard() 
player.innerHTML=currentPlayer 
let squares =document.querySelectorAll(".board div") 
Array.from(squares).forEach(square=&amp;gt;{ 
square.addEventListener("click",clickBox)
})
playAgain.addEventListener("click",reset) 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first thing here is to call &lt;strong&gt;createBoard&lt;/strong&gt; which will create the gameBoard, after which we will set the player innerHTML to let the user know whose turn it is to play. Finally, we get all the divs inside the board via querySelectorAll and add a click eventListener to watch out for a click.&lt;br&gt;
&lt;/p&gt;

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

function createBoard(){ 
for(let i=0;i&amp;lt;49;i++){ 
let div =document.createElement("div") 
div.setAttribute("data-id",i) 
div.className = "square" 
if (i&amp;gt;=42){ 
div.className="taken" 
} 
board.appendChild(div) 
} 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this game, we are going to need exactly 49 divs. So we loop from 0 to 49 and every time we create a new div, give it a dataset attribute (this will be used later) and add a class of square to each div. This is to give all the divs a generic styling. Moving forward, we put a conditional statement from 42 i.e all divs from no 42 to 49 should have a class taken. Like I earlier stated, a player can only drop a brick on top of another. so this 7 bottom div will act like our ground level. This is similar to a house. A house stands on a ground not on the air. So in this case, the seven brick is serving as our ground. After all this, we simply append all the divs to the board which we got via querySelector.&lt;br&gt;
You can uncomment the CSS class of taken to see the divs we will be using has the ground level.&lt;br&gt;
After setting up the board and all, we are going to add an eventListener for every time we click a div and check if the player is actually dropping a block on top of another.&lt;br&gt;
&lt;/p&gt;

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

function clickBox(){ 
let squares =document.querySelectorAll(".board div") 
let click =parseInt(this.dataset.id) 
if( squares[click+7].classList.contains("taken") &amp;amp;&amp;amp; !squares[click].classList.contains("taken")){ 
if(currentPlayer===1){ 
currentPlayer=2 
player.innerHTML=currentPlayer
this.className="player-one taken" 
checkWon() 
}else if(currentPlayer===2){ 
currentPlayer=1 
player.innerHTML=currentPlayer 
this.className="player-two taken" 
checkWon() 
} 
if(box===42){ 
setTimeout(()=&amp;gt;alert("boxes filled"),300)
setTimeout(()=&amp;gt;restart.style.display="flex",500) 
} 
}else{
alert("You cannot build on an empty space or on a space that has been built on")
} 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is how the &lt;strong&gt;clickBoard&lt;/strong&gt; function works, we first get all the squares (i.e divs created. We could not do that globally at the top because they have not been created then) via querySelectorAll And then get the id of the div clicked with dataset (which is used to hold data in html5 and can then be accessed in js. This actually returns a string, so to turn it into a number you wrap it with parseInt). After which we check if the element below it i.e the seventh square after it (since it is a six by seven grid, the block below it will be seven steps ahead) already has a class &lt;strong&gt;taken&lt;/strong&gt; (i.e it has been clicked) and if also the square you are clicking on does not already have the className of taken. If the former is true (i.e you are dropping a brick on top of another) and the latter is false (i.e you are dropping a brick at an empty spot), then we add one to the &lt;strong&gt;box&lt;/strong&gt; variable and simply check who is dropping the brick player-one or player-two and add a className of taken and player-one (if it was player one that dropped the brick). This will give that div a color(via css) which will fill that space. But if one or any of the two conditions is not what it should be, we simply alert the player that they are doing something they should not. After which we simply change the value of currentPlayer and put it on the screen via innerHTML. Finally after each player clicks on a div and we add the className and change the value of whose turn it is to play, we then check if the game has been won with the &lt;strong&gt;checkWon&lt;/strong&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//the checkWon function

function checkWon(){
let squares =document.querySelectorAll(".board div")
for (let y=0;y&amp;lt;winningArray.length;y++){
let square =winningArray[y]
if(square.every(q=&amp;gt;squares[q].classList.contains("player-one"))){
  setTimeout(() =&amp;gt;alert("player one(red) wins "), 200)
  setTimeout(() =&amp;gt;restart.style.display="flex", 500)
  }else if(square.every(q=&amp;gt;squares[q].classList.contains("player-two"))){
  setTimeout(() =&amp;gt;alert("player two(yellow) wins"), 200)
  setTimeout(() =&amp;gt;restart.style.display="flex", 500)
}
}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The checkWon function like the clickBoard function first gets the squares (div) and then loops over the winningArray. Each value of the array is stored in a variable &lt;strong&gt;square&lt;/strong&gt; and then a conditional statement is called. Note that each value in the winningArray is basically another array with a set of numbers (from 0-42, similar to the value we gave to each div via dataset i.e above). So everytime we get a new array from winningArray, we call an higher order function called &lt;strong&gt;every&lt;/strong&gt; on that array, (this loops through every element in that array and cross check it with the condition stated). So basically, it loops through each element in the array i.e gets the value which is a number, and then it passes that number into squares (remember squares is basically all the divs which we got via querySelctorAll. So to get a div, we access it like an array with the number we got from this current looping) and then we simply check if that particular div has a className of player-two or player-one, meaning there is already a brick there. the &lt;strong&gt;every&lt;/strong&gt; higher order function like I said earlier loops through each element of the array you specified, but it adds a special effect, if the condition you specified in the &lt;strong&gt;every&lt;/strong&gt; parenthesis is true for every element, then it returns true, but even if just one value in that array does not agree with the condition it returns false. E.g if every div selected already has a class of player-two it returns true. So moving forward, the first if statement in the checkWon loop function simply check if any set of the divs in the game board matches any of the pattern defined in the winningArray and if all that set of div contains a class of player-one. If it does, alert the user that player one has one and you can then display the popup div to give players the access to play again. Else you should check if the set of divs has a class of player-two then alert the user that player two has won the game and display the popup div .If no set of div matches any of the defined pattern, we move ahead to check if all the board has been filled (remember we kept adding one to the box variable every time a player clicks on a spot). Since the number of divs on the gameBoard is 42, we check if variable &lt;strong&gt;box&lt;/strong&gt; is equal to 42. If it is, we alert the user that it is gameOver and display the restart button to allow players restart the game. If there is still space on the board, i.e box is not equal to 42, the game will continue and every time a player clicks on a div it checks again.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a&gt;restart game function&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;From above, an eventListener has been added for every time the restart button is clicked, this calls the reset game function. The reset game function is a simply function that clears the game board ,prepares for another game(via the loadDOM function which we defined above) and hides the popup afterward. This is the reset function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function reset(){
board.innerHTML="" 
loadDOM() 
restart.style.display="none" 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is my &lt;a href="https://codepen.io/Fako29/pen/NWroLvp"&gt;codepen link&lt;/a&gt; to the game.&lt;br&gt;
You could also use make this game player VS AI, this uses the minmax algorithm. I think and there are some pretty great articles on it, you can check it out. But I really don't understand it yet :( .&lt;/p&gt;

&lt;p&gt;This is how you create a connect four game. If you have any question or better idea let me know in the comments below. &lt;/p&gt;

&lt;p&gt;Thank you, Adios&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>html</category>
      <category>css</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Create a memory game with JavaScript</title>
      <dc:creator>Fakorede Damilola</dc:creator>
      <pubDate>Thu, 05 Nov 2020 08:04:20 +0000</pubDate>
      <link>https://forem.com/fakorededamilola/create-a-memory-game-with-js-1l9j</link>
      <guid>https://forem.com/fakorededamilola/create-a-memory-game-with-js-1l9j</guid>
      <description>&lt;p&gt;The memory game is a pretty interesting game, and has been around for a while. You are given a set of cards containing images which are turned to the back, when you click on an image, it flips over and reveals the image. If you click on two images that are exactly the same, that is a win for you. &lt;/p&gt;

&lt;p&gt;I would like to walk you through how to build a memory game using plain HTML, CSS and JavaScript.&lt;br&gt;
Here is what we will be doing :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Setting up the basic HTML and CSS&lt;/li&gt;
&lt;li&gt;Defining the variables and data in JS&lt;/li&gt;
&lt;li&gt;Setting up the board on DOM load in JS&lt;/li&gt;
&lt;li&gt;Check if the cards clicked is correct&lt;/li&gt;
&lt;li&gt;Restart the game&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  &lt;a&gt;Setting up the basic HTML and CSS&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;The HTML and CSS for this project is pretty simply. We are creating the skeleton which will house data from the JS.&lt;/p&gt;
&lt;h4&gt;
  
  
  HTML
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="container"&amp;gt;
&amp;lt;div class="data"&amp;gt;
&amp;lt;div class="score"&amp;gt;Score : &amp;lt;span class="scoreBoard"&amp;gt;0&amp;lt;/span&amp;gt;&amp;lt;/div&amp;gt;
&amp;lt;div class="click"&amp;gt;Click : &amp;lt;span class="clickBoard"&amp;gt;0&amp;lt;/span&amp;gt;&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class="grid"&amp;gt;
&amp;lt;div class="popup"&amp;gt;
&amp;lt;button class="playAgain"&amp;gt;play Again&amp;lt;/button&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;From above, we have a div with a class &lt;strong&gt;score&lt;/strong&gt; and &lt;strong&gt;click&lt;/strong&gt; that will be populated via JS, a &lt;strong&gt;grid&lt;/strong&gt; class that will house the images which is the game board and a &lt;strong&gt;popup&lt;/strong&gt; class with a button to easily renew the game.&lt;/p&gt;
&lt;h4&gt;
  
  
  CSS
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.grid { 
width: 90%; 
height: 300px; 
display: flex; 
flex-wrap: wrap; 
margin: 60px auto; 
} 

.grid img { 
margin: 2px; 
transform-style: preserve-3d; 
transition: transform 0.5s; 
} 

.grid img.flip { 
transform: rotateY(180deg); 
} 

.popup { 
background: red; 
width: 200px; 
height: 200px; 
z-index:20;
position: fixed; 
top: 100px;
left: 100px; 
display: flex;
justify-content: center; 
align-items: center; 
flex-direction: column;
}

.data { 
display: flex; 
justify-content: space-between; 
padding: 20px 30px; 
font-size: 23px; 
} 

.data span { 
color: rgb(206, 121, 11);
font-size: 30px; 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;From here, we are setting up the grid (gameboard) and the grid img styling. The grid img has the transform-style and the transition property which allows the card to flip when the flip class is added to it(i.e via JS) and the popup is styled as a fixed div.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;a&gt;Defining the variables and data in JS&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Most of the heavy lifting of this game will be carried out in the JS file, the board will be populated etc&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let cardArray = [ 
{ name: "fries", img: "fries.png", }, 
{ name: "fries", img: "fries.png", },
{ name: "pizza", img: "pizza.png", },
{ name: "pizza", img: "pizza.png", }, 
{ name: "milkshake", img: "milkshake.png", },
{ name: "milkshake", img: "milkshake.png", }, 
{ name: "ice-cream", img: "ice-cream.png", },
{ name: "ice-cream", img: "ice-cream.png", },
{ name: "hotdog", img: "hotdog.png", },
{ name: "hotdog", img: "hotdog.png", },
{ name: "cheeseburger", img: "cheeseburger.png", },
{ name: "cheeseburger", img: "cheeseburger.png", }, 
]; 

//define variables and get DOM element

let grid = document.querySelector(".grid"); 
let scoreBoard = document.querySelector(".scoreBoard"); 
let popup = document.querySelector(".popup"); 
let playAgain = document.querySelector(".playAgain"); 
let clickBoard = document.querySelector(".clickBoard"); 
let imgs; 
let cardsId = []; 
let cardsSelected = []; 
let cardsWon = 0; 
let clicks = 0;

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

&lt;/div&gt;



&lt;p&gt;So we have a &lt;strong&gt;cardArray&lt;/strong&gt;, which is just a list of objects containing images and their names. The names will be used when we want to compare two images that were clicked. You will notice each object is actually double, and that is because you will be trying to compare two images on the board.&lt;br&gt;
The &lt;strong&gt;grid&lt;/strong&gt;, &lt;strong&gt;scoreboard&lt;/strong&gt;, &lt;strong&gt;popup&lt;/strong&gt;, &lt;strong&gt;playAgain&lt;/strong&gt; and &lt;strong&gt;clickBoard&lt;/strong&gt; are elements from the HTML that we are getting into the JS and will inject data into them.&lt;br&gt;
&lt;strong&gt;imgs&lt;/strong&gt; is a variable that we will initialize from here and will hold the images created, &lt;strong&gt;cardsId&lt;/strong&gt; and &lt;strong&gt;cardsSelected&lt;/strong&gt; are arrays that will contain the cards clicked on. While &lt;strong&gt;cardsWon&lt;/strong&gt; and &lt;strong&gt;clicks&lt;/strong&gt; will record the wins and no of clicks respectively&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;a&gt;Setting up the board on DOM load in JS&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;This will be done with an eventListener called &lt;strong&gt;DOMContentLoaded&lt;/strong&gt;, which will be added to the document itself. The eventListener works immediately the DOM loads (hence the name).&lt;br&gt;
This eventListener will house some functions that will start the game&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.addEventListener("DOMContentLoaded", function () {
//define functions 

createBoard(grid, cardArray); 
arrangeCard();
playAgain.addEventListener("click", replay); 

//add a click function for images 

imgs = document.querySelectorAll("img");
Array.from(imgs).forEach(img =&amp;gt; 
img.addEventListener("click", flipCard)
) 
});

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

&lt;/div&gt;



&lt;p&gt;I called a function here (&lt;strong&gt;createBoard&lt;/strong&gt;) that will create the board with the images and all, I scattered the images for difficulty (&lt;strong&gt;arrangeCard&lt;/strong&gt;), and I added an eventListener for each image to watch out for clicks (&lt;strong&gt;flipCard&lt;/strong&gt;)&lt;br&gt;
&lt;/p&gt;

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

function createBoard(grid, array) { 
popup.style.display = "none"; 
array.forEach((arr, index) =&amp;gt; { 
let img = document.createElement("img"); 
img.setAttribute("src", "blank.png");
img.setAttribute("data-id", index); 
grid.appendChild(img); 
})
}

// arrangeCard function

function arrangeCard() { 
cardArray.sort(() =&amp;gt; 0.5 - Math.random())
}

// flip Card function

function flipCard() { 
let selected = this.dataset.id;
cardsSelected.push(cardArray[selected].name); 
cardsId.push(selected); 
this.classList.add("flip"); 
this.setAttribute("src", cardArray[selected].img); 
if (cardsId.length === 2) { 
setTimeout(checkForMatch, 500);
} 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;createBoard&lt;/strong&gt; function removes the popup, loops over the image array with &lt;strong&gt;forEach&lt;/strong&gt;, this receives two arguments. Each object in the array and the index(optional) which is the position of that object in the array and starts from zero. For each object, simply create an image element, set the src attribute to display the image and add a data attribute (data attribute are simply objects that holds values in the html5 which you can use in JS via dataset). The value of the id will be the index i.e from 0 etc.&lt;br&gt;
The &lt;strong&gt;flipCard&lt;/strong&gt; function looks out for click. Once an image is clicked, it gets the id (remember the attribute we set above data-id, we can access it via dataset.id or if it was data-name it would be dataset.name). The &lt;strong&gt;this&lt;/strong&gt; keyword basically tells it to select the image that was clicked. So we get the id of the image clicked to a variable &lt;strong&gt;selected&lt;/strong&gt;, we then use this variable to get the object clicked and then push the name property of that object into the &lt;strong&gt;cardsSelected&lt;/strong&gt; array. The &lt;strong&gt;id&lt;/strong&gt; of the image clicked will also be pushed into the &lt;strong&gt;cardsId&lt;/strong&gt; array. We add a class of &lt;strong&gt;flip&lt;/strong&gt; to the img clicked to create a sort of flipping effect and then change the image to reveal the image beneath.&lt;br&gt;
This process will be repeated when the second image is clicked and then we will check to see if they are the same image&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;a&gt;Check if the cards clicked is correct&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Once two images has been clicked, we wait for about .5s (to give a nice user experience ), and then we call the &lt;strong&gt;checkForMatch&lt;/strong&gt; function&lt;br&gt;
&lt;/p&gt;

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

function checkForMatch() { 
let imgs = document.querySelectorAll("img"); 
let firstCard = cardsId[0];
let secondCard = cardsId[1];
if (cardsSelected[0] === cardsSelected[1] &amp;amp;&amp;amp; firstCard !== secondCard) { 
alert("you have found a match"); 
cardsWon += 1; 
scoreBoard.innerHTML = cardsWon; 
setTimeout(checkWon,500) 
} else { 
imgs[firstCard].setAttribute("src", "blank.png");
imgs[secondCard].setAttribute("src", "blank.png"); alert("wrong, please try again"); imgs[firstCard].classList.remove("flip"); imgs[secondCard].classList.remove("flip"); 
} 
cardsSelected = []; 
cardsId = []; 
clicks += 1; 
clickBoard.innerHTML = clicks; 
}

function checkWon() {
if (cardsWon == cardArray.length / 2) {
alert("You won") 
setTimeout(()=&amp;gt; popup.style.display = "flex" ,300); 
}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;checkForMatch&lt;/strong&gt; function first gets all the images on the board, then gets the Ids of the images clicked from the "cardsId" array ( This will allow us easily get access to their object). We will then checked if the first value in cardsArray is equal to the second and if it is not the same image that was clicked twice.&lt;br&gt;
If the images are the same we tell the person they got it and adds to the number of cardsWon, and then check if all the cards have been selected with the gameWon function. If the images are not the same (else), we simply remove the flip Class and change the image back. After the if/else statement, we want to clear the arrays for the next time the user clicks on an image and add to the number of clicks&lt;br&gt;
The checkWon function simply check if the value of cardsWon is equal to the length of the card divided by 2. If it is tell the person the game is over and display the popup replay button&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;a&gt;Restart the game&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;The restart game function is a simply function that clears the game board and scoreboard and prepares for another game. This is the replay function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// The replay function

function replay() { 
arrangeCard(); 
grid.innerHTML = "";
createBoard(grid, cardArray);
cardsWon = 0;
clicks = 0; 
clickBoard.innerHTML = 0; 
scoreBoard.innerHTML = 0; 
popup.style.display = "none"; 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It basically arranges the cardsArray again, empties the gameBoard (grid), create a new board and resets the scores.&lt;/p&gt;

&lt;p&gt;And that is how you create a memory game. Here is a &lt;a href="https://codepen.io/Fako29/pen/ZEOjyBV"&gt;codepen link&lt;/a&gt; for the finished project.&lt;br&gt;
Like the popular saying in programming, there is a thousand ways to do one thing, so please try creating your memory game and if your method is different let me know in the comments.&lt;/p&gt;

&lt;p&gt;Thank you&lt;/p&gt;

&lt;p&gt;You can follow me on twitter &lt;a href="https://twitter.com/fakoredeDami"&gt;@fakoredeDami&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>css</category>
      <category>html</category>
    </item>
    <item>
      <title>Uploading Images on MongoDB via nodeJS</title>
      <dc:creator>Fakorede Damilola</dc:creator>
      <pubDate>Sat, 31 Oct 2020 11:49:30 +0000</pubDate>
      <link>https://forem.com/fakorededamilola/uploading-images-on-mongodb-via-nodejs-5h1l</link>
      <guid>https://forem.com/fakorededamilola/uploading-images-on-mongodb-via-nodejs-5h1l</guid>
      <description>&lt;p&gt;Images are very important on our websites and gives life to the internet. A website without one or two images will be very close to boring.&lt;br&gt;
Images like content can be uploaded on a database for easy access, and today I am going to show you how to easily upload images to MongoDB via nodeJS.&lt;br&gt;
Today I will explain how to&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Setup the necessary tools&lt;/li&gt;
&lt;li&gt;Upload images to MongoDB&lt;/li&gt;
&lt;li&gt;Get the list of image object (in an array)&lt;/li&gt;
&lt;li&gt;Get a single image object&lt;/li&gt;
&lt;li&gt;Display the actual image&lt;/li&gt;
&lt;li&gt;Delete an image&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  &lt;a&gt;Setup the necessary tools&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Before we move forward, we are definitely going to need some packages from &lt;a href="https://www.npmjs.com/"&gt;NPM&lt;/a&gt; (Node Package Manager), such has&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://expressjs.com/"&gt;Express&lt;/a&gt; : basically a Node.js web application framework&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://mongoosejs.com/"&gt;Mongoose&lt;/a&gt; : Object Data Modeling (ODM) library for MongoDB and Node.js. It basically handles relationship between data&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.npmjs.com/package/multer"&gt;Multer&lt;/a&gt; : Is used for uploading files&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.npmjs.com/package/gridfs-stream"&gt;Gridfs-stream&lt;/a&gt; : Allows streaming of files to and from mongodb&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://docs.mongodb.com/manual/core/gridfs/"&gt;Gridfs&lt;/a&gt; : This is a specification for storing and retriviing files that excess the BSON-document size limit of 16MB
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i express mongoose multer multer-gridfs-storage gridfs-stream
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We will be uploading straight to &lt;a href="https://www.mongodb.com/cloud/atlas"&gt;MongoDB atlas&lt;/a&gt;, which is a remote MongoDB database, you can also use the local instance but the connection string will be different. &lt;/p&gt;

&lt;p&gt;The first thing is to import the required modules, and some core nodeJS modules, and just create the basic server&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express')
const path = require('path')
const crypto = require('crypto')//to generate file name
const mongoose = require('mongoose')
const multer = require('multer')
const GridFsStorage = require('multer-gridfs-storage')
const Grid = require('gridfs-stream')
const app = express()

// other code here

const PORT =5000
app.listen(PORT,()=&amp;gt;console.log(`Server started on port ${PORT}`))

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

&lt;/div&gt;



&lt;p&gt;Next is to add the connection string. If you are using the local instance , yours will probably be 27017...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoURI = "mongodb+srv://fako:fako@nodejspassport-nahp0.mongodb.net"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next thing is to, create a connection via mongoose, initialize a variable for stream(i.e gfs) and once the connection is open, set the &lt;strong&gt;gfs&lt;/strong&gt; variable to Grid(gridfs-stream) and then pass the collection where our images will be stored to gfs :). Note that this collection will be divided into two, &lt;strong&gt;imageUpload.chunk&lt;/strong&gt; and &lt;strong&gt;imageUpload.files&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let conn = mongoose.connection
let gfs
conn.once('open', () =&amp;gt; {
    //initialize our stream
    gfs = Grid(conn.db, mongoose.mongo)
    gfs.collection('imageUpload')
})

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

&lt;/div&gt;



&lt;p&gt;Now, we are going to create a storage object with a given configuration.&lt;br&gt;
The first property will be the &lt;strong&gt;uri&lt;/strong&gt; string which we specified above and the second is called &lt;strong&gt;file&lt;/strong&gt;, a function to control the file storage in the database. It is invoked per file with the parameters &lt;strong&gt;req&lt;/strong&gt; and &lt;strong&gt;file&lt;/strong&gt; in that order and returns an object of a promise that resolves to an object. Some of the property of the object include&lt;br&gt;
&lt;strong&gt;filename&lt;/strong&gt; : The desired filename for the file (default: 16 byte hex name without extension), but you can override this with your given name&lt;br&gt;
&lt;strong&gt;content-type&lt;/strong&gt; : The content type for the file (default: inferred from the request)&lt;br&gt;
&lt;strong&gt;bucketname&lt;/strong&gt; : The GridFs collection to store the file (default: fs)&lt;br&gt;
missing property will use the default&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let storage = new GridFsStorage({
    url: uri,
    file: (req, file) =&amp;gt; {
        return new Promise(
            (resolve, reject) =&amp;gt; {
                       const fileInfo = {
                    filename: file.originalname,
                    bucketName: "imageUpload"
                }
                resolve(fileInfo)

            }
        )
    }
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Set the multer storage engine to the newly created object, we will use this upload variable has our middleware, so that it actually upload to the database&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const upload = multer({ storage })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;a&gt;Upload images to MongoDB&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Now to actually upload an image. The upload variable will be added has a middleware and &lt;strong&gt;.single&lt;/strong&gt; will be called on it (because we are uploading a single file each time. You can upload multiple files has an array). You will then pass the name you specified in your input field i.e in the frontend (e.g input type="file" name="upload"&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.post("/upload",upload.single("upload"),(req,res)=&amp;gt;{
res.json({file:req.file})
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I am not really going to deal with the frontend in this article, but you should have a basic html file with an input file field that on submit will make an AJAX request to &lt;strong&gt;localhost:5000/upload&lt;/strong&gt;, and if you try it out, that should work :). If you were to check atlas or your local database, and you should see the file uploaded.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a&gt;Get the list of image object (in an array)&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;To get the list of image object is pretty straight forward,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.get('/files', (req, res) =&amp;gt; {
    gfs.files.find().toArray((err, files) =&amp;gt; {
        //check if files exist
        if (!files || files.length == 0) {
            return res.status(404).json({
                err: "No files exist"
            })
        }
        // files exist
        return res.json(files)
    })
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are basically using gridfs-stream(gfs) like we will use mongoose. Go to the url with &lt;strong&gt;/files&lt;/strong&gt; and you will see an array of the uploaded files&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a&gt;Get a single image object&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;To get a single file, all we need is the filename and we can call a findOne on gfs i.e&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.get('/files/:filename', (req, res) =&amp;gt; {
    gfs.files.findOne({ filename: req.params.filename }, (err, file) =&amp;gt; {
        //check if files exist
        if (!file || file.length == 0) {
            return res.status(404).json({
                err: "No files exist"
            })
        }
        //file exist
        return res.json(file)
    })
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;a&gt;Display the actual image&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;To get the image itself,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.get('/image/:filename', (req, res) =&amp;gt; {
    gfs.files.findOne({ filename: req.params.filename }, (err, file) =&amp;gt; {
        //check if files exist
        if (!file || file.length == 0) {
            return res.status(404).json({
                err: "No files exist"
            })
        }
        //check if image
        if (file.contentType === 'image/jpeg' || file.contentType === "img/png") {
            //read output to browser
            const readStream = gfs.createReadStream(file.filename)
            readStream.pipe(res)
        } else {
            res.status(404).json({
                err: "Not an image"
            })
        }
    })
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first thing to do is check if the file actually exists, if it does, go ahead and check if it is actually an image by looking at it contentType. If is actually an image, then read it to the browser by creating a readStream.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a&gt;Delete an image&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Deleting an image is just as easy, all you have to do is make a delete request i.e&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.delete("/files/:id", (req, res) =&amp;gt; {
    gfs.remove({ _id: req.params.id, root: 'imageUpload' }, (err, gridStore) =&amp;gt; {
        if (err) {
            return res.status(404).json({ err: err })
        }
        res.redirect("/")
    })
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and that is how you upload an image to MongoDB via NodeJS. Thank you&lt;/p&gt;

&lt;p&gt;You can follow me on twitter &lt;a href="https://twitter.com/fakoredeDami"&gt;@fakoredeDami&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>mongodb</category>
    </item>
    <item>
      <title>Creating a custom cursor using CSS?</title>
      <dc:creator>Fakorede Damilola</dc:creator>
      <pubDate>Fri, 11 Sep 2020 12:47:51 +0000</pubDate>
      <link>https://forem.com/fakorededamilola/creating-a-custom-cursor-using-css-1io6</link>
      <guid>https://forem.com/fakorededamilola/creating-a-custom-cursor-using-css-1io6</guid>
      <description>&lt;p&gt;Cascading Style Sheet (CSS) offers you the opportunity to create pretty awesome designs. from SVGs to animation to the box model concept, CSS let's you go wild with your imagination acting has the only barrier.&lt;/p&gt;

&lt;p&gt;One of such awesome designs is creating your custom cursor.&lt;br&gt;
There are over 30 different kinds of cursor, from &lt;em&gt;pointer&lt;/em&gt; to &lt;em&gt;help&lt;/em&gt;. this &lt;a href="https://codepen.io/benlinton/pen/JXzZBp"&gt;codepen&lt;/a&gt; shows you the different kind of cursor available.&lt;/p&gt;

&lt;p&gt;Creating a custom cursor is fairly simple, I will show you the easy way which uses an SVG then we will move from there.&lt;/p&gt;
&lt;h2&gt;
  
  
  CREATING A CUSTOM CURSOR USING AN SVG IMAGE
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Create a simply html page with just the body tag.&lt;/li&gt;
&lt;li&gt;Create your style sheet and just give the body tag a background color of red.&lt;/li&gt;
&lt;li&gt;Get a cool SVG that you want to use and just link it to your CSS. Note that for this to work you also have to pass a second value of auto. This will be the default cursor in case the image is not available.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;body {
background:red;
cursor:url('cursor.svg'),auto;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Try it out. It worked I hope.&lt;/p&gt;

&lt;p&gt;Now that is awesome. But we want to go further than that. We wanna add cool animation, such that when you click, it does this and that etc.&lt;br&gt;
It might actually be possible to do that with the svg, but that might get out of hand. So we will create another type of cursor.&lt;/p&gt;
&lt;h2&gt;
  
  
  CREATING A CUSTOM CURSOR USING ELEMENTS
&lt;/h2&gt;

&lt;p&gt;Now let's first get rid of everything we have done earlier&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Then create a simple div with a class of cursor.&lt;/li&gt;
&lt;li&gt;Style the div. You can style this div anyhow, make it a triangle, square etc. I am going with a circle and also, give the body a couple other styles
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;body {
margin: 0;
padding: 0;
height: 100vh;
background:black;
} 
.cursor {
width:20px
height:20px
border:2px solid white;
border-radius:50%;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;So moving on, the next thing to do is give our div a position that is relative to the body tag. &lt;strong&gt;position:absolute;&lt;/strong&gt; will work just fine. I will not go in-depth about positioning, just know that this will place the div at the top left corner of the parent tag in this case that is the body tag
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.cursor {
//other styles
position:absolute;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now to make it a functional cursor we will add a few scripts&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a script file and link it to your html, get the cursor tag and add an eventListener of mousemove to the whole body
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let cursor =document.querySelector(".cursor")
document.addEventListener("mousemove",function(e){
console.log(e)
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;mousemove&lt;/code&gt; basically gives you the position of your mouse has you hover the screen. it returns an object which contains a lot of properties. But what we need is the pageX and pageY. This two values is basically the position of the cursor from the left and top of your browser window respectively and this is what we are going to use to change the position of the div element
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// MouseEvent {isTrusted: true, screenX: 470, screenY: 219, clientX: 470, clientY: 148, …}
document.addEventListener("mousemove",function(e){
cursor.setAttribute("style",
`top: ${e.pageY}px;left:${e.pageX}px`
)
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Now, you should have a cursor that moves with your default cursor. Nice. Note that you could totally do this a different way,you can change the values respectively using JS with &lt;strong&gt;cursor.style.left&lt;/strong&gt; and &lt;strong&gt;cursor.style.top&lt;/strong&gt;. That also works.
Like the popular saying there is about a thousand ways to do one thing.&lt;/li&gt;
&lt;li&gt;But there is a slight issue, the div is actually not at the exact point the cursor is, there is kind of a small difference. This is the same thing that happens when you try to position an element to the center of a page with position absolute. It is always slightly off and that is because it takes the whole element to the center which is not suppose to be so. Half of the element has to be on one side and the other half on the other side. So in this case, all you need to do is every time you position it with pageX and PageY, subtract exactly half of the width and height of that element from this two values i.e
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.addEventListener("mousemove",function(e){
cursor.setAttribute("style",
`top: ${e.pageY-10}px;left:${e.pageX-10}px`
)
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;One other problem with custom cursor is the fact that you cannot click on anything. You can actually, it just won't work. Button, anchor tags etc :( . But thankfully there is a solution. In your cursor class just add this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pointer-events:none;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And you are good to go. To make it more awesome, you can go ahead and remove the custom cursor with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cursor:none;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;in your body tag.&lt;/p&gt;

&lt;p&gt;Now, you can totally stop here, that is cool. But I want to add some cool animation to this. So let do that next&lt;/p&gt;

&lt;p&gt;So let add an effect for every time we click. So every time I click something in the document, it should expand and disappear for about .5s.&lt;br&gt;
So to do this, we will work with CSS first. A class 'expand' with an animation&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.expand {
animation: cursorAnim 0.5s forwards;
border: 1px solid red;
}
@keyframes cursorAnim {
0% {transform: scale(1) }
50% { transform: scale(3) }
100% { transform: scale(1); opacity: 0 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So this is basically straight forward, expand has an animation property, at 50% it scales to 3x it value and at 100%, it disappears.&lt;br&gt;
Now we need JavaScript to add this class to the cursor i.e every time you click the document or anywhere, add the &lt;code&gt;expand&lt;/code&gt; class and after .5s remove it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.addEventListener("click", function () {
cursor.classList.add("expand");
setTimeout(() =&amp;gt; {
cursor.classList.remove("expand");
}, 500);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that is how you create your custom mouse.&lt;br&gt;
I hope you enjoyed reading it.&lt;/p&gt;

&lt;p&gt;In this article, I talked about:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;How to create your custom cursor using an image,(don't forget to add a second value of auto)&lt;/li&gt;
&lt;li&gt;How to create your custom cursor using a div element (don't forget position:absolute and pointer-events:none)&lt;/li&gt;
&lt;li&gt;And how to add animation to your custom div cursor.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>css</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Publishing a package on NPM</title>
      <dc:creator>Fakorede Damilola</dc:creator>
      <pubDate>Mon, 24 Aug 2020 10:40:50 +0000</pubDate>
      <link>https://forem.com/fakorededamilola/publishing-a-package-on-npm-4h9h</link>
      <guid>https://forem.com/fakorededamilola/publishing-a-package-on-npm-4h9h</guid>
      <description>&lt;p&gt;One of the things that makes a web developer's job interesting is the fact that a random person can just type a word on google and your website pops up. But it gets better, You can have someone use your work in their code.&lt;/p&gt;

&lt;p&gt;I recently learnt how to publish packages on NPM for other people to use and I want to explain how you can too. But first, I want to talk about PACKAGES and MODULES in node because this two things really confused me and I hope it can help someone out there get clarity.&lt;/p&gt;

&lt;h2&gt;
  
  
  PACKAGE
&lt;/h2&gt;

&lt;p&gt;From the  &lt;a href="https://nodejs.org/en/"&gt;node JS&lt;/a&gt; docs, &lt;strong&gt;A package is a file or directory that is described by a package.json file&lt;/strong&gt;. Basically, a &lt;strong&gt;package&lt;/strong&gt; is a piece of code that you write and has a package.json file to describe it. So, this file below is a package why?? because of the fact that it has a package.json file with it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//index.js file
function checkNumber(val){
if(val%2===0){
return "is Even"
}else{
return "is Odd"
}
}

//package.json file
{
  "name": "check_number",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1"
  },
  "author": "Fako",
  "license": "MIT"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see from the code above the package.json file is simply a file that describes another file in this case the index.js file. You can create a package.json file by typing &lt;strong&gt;npm init&lt;/strong&gt; in your terminal and answer the questions. Any answer that you don't like can easily be replaced by typing it right beside that question.&lt;/p&gt;

&lt;h2&gt;
  
  
  Modules
&lt;/h2&gt;

&lt;p&gt;Also from the node JS docs, &lt;strong&gt;A module is any file or directory in the node_modules directory that can be loaded by the Node.js require() function.&lt;/strong&gt;&lt;br&gt;
But to be loaded by the Node.js require() function, a module must be one of the following&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A folder with a package.json file containing a "main" field.&lt;/li&gt;
&lt;li&gt;A folder with an index.js file in it.&lt;/li&gt;
&lt;li&gt;A JavaScript file.
Note that modules are not required to have a package.json file. I.e not all modules are packages (only those that have a package.json file) but all packages are modules.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now let's create a package.&lt;/p&gt;
&lt;h3&gt;
  
  
  Description
&lt;/h3&gt;

&lt;p&gt;This is a package which will take in an array of numbers and return the average of those numbers. Sounds funny right, but that is packages. Simply your reusable code that you make available for everyone.&lt;br&gt;
Note: you will need node JS and NPM installed on your system.&lt;br&gt;
Start with creating a new folder, I will name mine &lt;strong&gt;avgNumber&lt;/strong&gt; inside it create a file &lt;strong&gt;index.js&lt;/strong&gt;. This has to be named specifically that because &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;This is the convention&lt;/li&gt;
&lt;li&gt;That is the first place your system will look at when you try to use this folder/package anywhere. Remember that was what we described in the package.json file above
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"main": "index.js"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;although you can change this, but I would strongly advice against that.&lt;br&gt;
In your index.js file, this is the code you need&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let sum = 0,
  avg;
module.exports = function avgNumber(arr) {
  for (let i = 0; i &amp;lt; arr.length; i++) {
    sum += arr[i];
  }
  avg = sum / arr.length;
  return avg;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we are simply exporting a function that takes in an array of numbers and returns the average.&lt;br&gt;
We can try what we have now, outside this &lt;strong&gt;avgNumber&lt;/strong&gt; folder, create a new file &lt;strong&gt;main.js&lt;/strong&gt;. I am creating the file outside the folder because I want to keep my package contained. Just my package and nothing else.&lt;br&gt;
Now in main.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const avg = require("./avg_number");
console.log(avg([1, 2, 3, 4, 5]));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since it is not yet an official 'npm package', I have to require it like I would require a folder. Notice the way I am not going &lt;code&gt;./avg_number/index.js&lt;/code&gt;, that is because JS by default already knows to look for an index file inside avg_number folder.&lt;br&gt;
And from here, open up your terminal and type&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node main.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and voila it runs....... I hope :):).&lt;br&gt;
So now let publish the package for the whole world to see you are an awesome developer.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create an account with &lt;a href="https://www.npmjs.com/"&gt;npmjs.org&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;In your root directory terminal, run &lt;code&gt;npm login&lt;/code&gt; and fill in the required details&lt;/li&gt;
&lt;li&gt;From your terminal, cd into avgNumber with &lt;code&gt;cd avgNumber&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Create a package.json file from your terminal with &lt;code&gt;npm init&lt;/code&gt;. &lt;strong&gt;Note&lt;/strong&gt;, Like every other product around the world, the name of your product must be special and this is no difference. The name written in your package.json file is what other people will use to install your package so you have to look for a catchy and fine name. I am not a name wizard like &lt;strong&gt;cisco&lt;/strong&gt; (from flash) so I will just tag my name with the package. That adds a nice ring to it.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package name: (package) fako_average
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that by default the package name is the name of your root directory.&lt;br&gt;
Press enter to see the other options and change them based on what you want.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now run
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;/div&gt;



&lt;p&gt;Your package is now available for download from anywhere around the world.&lt;br&gt;
Note that running &lt;strong&gt;npm publish&lt;/strong&gt; will extract every file or folder from the directory you are running it and publish. So make sure you run npm publish inside the folder you want to make available.&lt;br&gt;
Now, try to install it &lt;code&gt;npm i fako_average&lt;/code&gt;, and you will immediately get a node modules which should contain your package and now you can go back to the &lt;code&gt;main.js&lt;/code&gt; file and remove the ./ and note that you will have to change the name too what is written in your package.json file dependencies and run it again.&lt;/p&gt;

&lt;p&gt;Before I end this session, I quickly want to add the fact that when you download packages like express etc, you tend to get a lot of other folders in your node_modules. This is because express itself depends on this other folders/modules to work and so installing express will install them also. But note that when you check your package.json, the dependency part, you will only see express meaning others are just modules (from our definition above).&lt;/p&gt;

&lt;p&gt;Thank you for reading. You can follow me on twitter &lt;a href=""&gt;@fakoredeDami&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>npm</category>
      <category>node</category>
    </item>
    <item>
      <title>Beginner's guide to LESS</title>
      <dc:creator>Fakorede Damilola</dc:creator>
      <pubDate>Sun, 09 Aug 2020 17:35:38 +0000</pubDate>
      <link>https://forem.com/fakorededamilola/beginner-s-guide-to-less-3a6a</link>
      <guid>https://forem.com/fakorededamilola/beginner-s-guide-to-less-3a6a</guid>
      <description>&lt;p&gt;CSS is annoying and can get frustrating. As a developer, it is really annoying having to write cool functions in JS and then turn to CSS and start with &lt;strong&gt;background:yellow;&lt;/strong&gt;, &lt;strong&gt;color:yellow;&lt;/strong&gt;, &lt;strong&gt;padding:yellow&lt;/strong&gt;, :). Although CSS is getting better with new features like variables etc, but I feel LESS is a better option. So this is a brief introduction to LESS based on what I have learnt. &lt;/p&gt;

&lt;h1&gt;
  
  
  So, what is LESS
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;LESS is not bootstrap&lt;/strong&gt;, or anything close to that. It definitely does not let you write classes and give you magical styles. In my opinion, I feel using LESS might even increase your CSS, but you will end up with a beautiful and comfortable CSS.&lt;br&gt;&lt;br&gt;
LESS makes writing of CSS way easier, introducing syntax like functions, variables etc.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;h3&gt;
  
  
  INSTALLING LESS
&lt;/h3&gt;

&lt;p&gt;You can either install it on your system, or use the LESS CDN. I prefer installing it, cause using the CDN might give unexpected behaviors.&lt;br&gt;
So to install LESS, &lt;strong&gt;npm install -g less&lt;/strong&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h3&gt;
  
  
  WORKING WITH LESS
&lt;/h3&gt;

&lt;p&gt;Create two files. styles.less and styles.css. We will work in the LESS file which will then be converted to CSS using &lt;strong&gt;lessc styles.less styles.css&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Note:Everything here should be written on the less file. Run lessc styles.less styles.css to see the changes in your css file&lt;/strong&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  variables
&lt;/h4&gt;

&lt;p&gt;To create a variable in LESS, you use the @ symbol e.g @width:50px;. and then you can use it anywhere e.g&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@width:50px;
@primaryColor:red;
body{width:@multipler;}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and then run &lt;code&gt;lessc styles.less styles.css&lt;/code&gt; in your terminal, and nice, CSS has been transformed.&lt;/p&gt;

&lt;h4&gt;
  
  
  Maps
&lt;/h4&gt;

&lt;p&gt;Like JS objects or maps, this is a data-type in LESS. You can define values like breakpoints etc&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@breakpoints: {
  desktop: 900px;
  tablet: 760px;
  phone: 480px;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can then use this for your media queries&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@media (min-width: @breakpoints[phone]) {
body{
width:@breakpoints[desktop];
}
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Math functions
&lt;/h4&gt;

&lt;p&gt;You can use some Math functions like round, floor etc and Math operations like *, /. For example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;h3{
 height: floor(@breakpoints[tablet] / 3);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember to run &lt;code&gt;lessc styles.less styles.css&lt;/code&gt; to see the changes in your styles.css&lt;/p&gt;

&lt;h4&gt;
  
  
  Color operations
&lt;/h4&gt;

&lt;p&gt;LESS provides color operations like &lt;strong&gt;lighten&lt;/strong&gt; and &lt;strong&gt;darken&lt;/strong&gt;, which takes your color and increases or decreases the lightness of the color based on the percentage specified&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;body{
 background:lighten(@primaryColor,60%);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Guards
&lt;/h4&gt;

&lt;p&gt;Similar to &lt;strong&gt;if statement in js&lt;/strong&gt;, guards is used to evaluate a style using &lt;strong&gt;when&lt;/strong&gt;. For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;body:hover when (lightness(@primaryColor)&amp;lt;70%){
background:green;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;note that this will check the lightness of "red", not what we specified for the background.&lt;/p&gt;

&lt;h4&gt;
  
  
  Variable interpolation
&lt;/h4&gt;

&lt;p&gt;This is simply using variables to set name or value. For example, I love naming my classes based on what they are used for e.g section-1-data-img,section-1-data-content,section-1-data-heading.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@section1:section-1-data;
.@section1-img{}
.@section1-content{}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Scoping in LESS
&lt;/h4&gt;

&lt;p&gt;LESS also has scoping. For example, defining another value for @primaryColor inside a media query won't change the value outside the media query e.g&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@media (min-width: @breakpoints[phone]) {

  @primaryColor: green;
  h2 {
   background:@primaryColor;
    height: @breakpoints[phone];
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Nested CSS rules
&lt;/h4&gt;

&lt;p&gt;Nesting CSS rules with LESS is basically specificity e.g if this is our HTML&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div&amp;gt;
&amp;lt;ul&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a&amp;gt;this is a link&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to style the &lt;strong&gt;a&lt;/strong&gt; tag in LESS&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;div ul li {
background:@primaryColor;
 @secondaryColor: violet;
  border: 2px solid @secondaryColor;
a{
color:blue;
font-size:25px;
}
&amp;amp;:hover{
background:black;
}
&amp;amp;{
padding:30px;
}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Basically, the &lt;strong&gt;&amp;amp;&lt;/strong&gt; is used to get the whole selector. In this case &lt;strong&gt;div ul li:hover&lt;/strong&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Imports in LESS
&lt;/h4&gt;

&lt;p&gt;It is actually quite easy to import in LESS. You can import LESS files, CSS,PHP etc.Note that every other import require an extension except from LESS.&lt;br&gt;
You can create a file called variable.less which will contain all your variables and import into styles.less&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//variable.less
@primaryColor:red;
@secondaryColor:yellow;

//styles.less
@import "variable";
body{
background:@primaryColor;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To import css e.g fonts&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@import (css)
  url("https://fonts.googleapis.com/css2?family=Roboto&amp;amp;display=swap");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can even go wild and move a whole style to another file and import it into styles.less. This will import it at the exact place you specified it.&lt;/p&gt;

&lt;h4&gt;
  
  
  Detached rule set
&lt;/h4&gt;

&lt;p&gt;Detached rule set is basically writing a property in one place and using it in other places. E.g you can define a box-shadow in one place and use it elsewhere. Detached rule set uses the @ symbol&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@box-shadow:{
box-shadow:3px 3px 5px blue;
font-size:30px;
}
div{
@box-shadow();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The issue with &lt;strong&gt;Detached rule set&lt;/strong&gt; is simply the fact that redefining a value will override every former value e.g&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@box-shadow:{
background:blue;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will be the only value defined in box-shadow.&lt;/p&gt;

&lt;h4&gt;
  
  
  Mixins
&lt;/h4&gt;

&lt;p&gt;Mixins, :) this is the big fish.&lt;br&gt;
Mixins is like &lt;strong&gt;detached rule set&lt;/strong&gt; but better such that adding a new value will not override the former value. Mixins uses period&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.font(){
font-size:40px;
}
.font(){
font-family:Arial;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will merge the two together.&lt;/p&gt;

&lt;h4&gt;
  
  
  Parameterizing Mixins
&lt;/h4&gt;

&lt;p&gt;Mixins also allow arguments and default value for arguments&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.box(@width:30px;@color:@primaryColor,@style:dashed){
  border: @width @color @style;

}
// or
.box(@width:30px;@color:@primaryColor,@style:dashed){
  border: @arguments;
border-radius:@width;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I passed default values, just in case you decide not to put any value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.div{
.box()
}
// or
.div{
.box(30px,brown,solid)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Mixins as functions
&lt;/h4&gt;

&lt;p&gt;Mixins can act as functions also , receiving arguements and returning values&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.arithmetic(@x,@y){
@add:@x+@y;
@multiply:@x*@y;
@divide:@x/@y;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using it elsewhere&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.div{
width:.arithmetic(200px,300px) [@add];
height:.arithmetic(15px,6px) [@multiply];
font-size:.arithmetic(15em,5) [@divide];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Personally, I feel mixins should be used in other files and then imported into the main less files for easy readability but that is me.&lt;/p&gt;

&lt;p&gt;So....... that is LESS from my point of view with some of the major things I learnt. You can know more about LESS by reading the documentation.&lt;/p&gt;

&lt;p&gt;Till then &lt;strong&gt;Adios&lt;/strong&gt; &lt;/p&gt;

</description>
      <category>css</category>
    </item>
    <item>
      <title>Introduction to HTML5 web components</title>
      <dc:creator>Fakorede Damilola</dc:creator>
      <pubDate>Wed, 22 Jul 2020 11:13:38 +0000</pubDate>
      <link>https://forem.com/fakorededamilola/introduction-to-html5-web-components-56ac</link>
      <guid>https://forem.com/fakorededamilola/introduction-to-html5-web-components-56ac</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JkzDbsfS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/y0ws69e39u6iwd6yscfy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JkzDbsfS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/y0ws69e39u6iwd6yscfy.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
Have you ever tried opening maybe, twitter or stackoverflow, and you found your way to the source code in the console, and you find a big soup of divs and divs and divs, ooh hey is that a paragraph no :( div, divs, divssssss. Web developers have a problem. Sorry lot of problems actually&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;h4&gt;
  
  
  Undescriptive Markup
&lt;/h4&gt;

&lt;p&gt;I mean, just take a look at that picture,some websites will try a litte by adding classnames so that you can understanding what is going on, but everywhere is still.....you know the word DIVSSSSS&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h4&gt;
  
  
  Style Conflicts
&lt;/h4&gt;

&lt;p&gt;I don't know about you but most times, my styles don't work. CSS can get really frustrating. It either I&lt;br&gt;
-avoid style conflict using highly css selectors&lt;br&gt;
-or use !important to force styles.&lt;br&gt;
But hey, there is no guarantee my style won't conflict. I mean, my parents always picture a well behaved family photo with us children but never quite get that :):)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h4&gt;
  
  
  No native templates
&lt;/h4&gt;

&lt;p&gt;There is no way I can import a inert html for use in my html. I mean, I can import js with script of css with style, but no native way to import html. What do I do,&lt;br&gt;
-I slap html in script tags e.g &amp;lt;script type="text/html"&lt;br&gt;
-I store HTML in hidden DOM elements and manually extract&lt;br&gt;
-I use iframes to get seperate scopes and styling.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h4&gt;
  
  
  No bundling
&lt;/h4&gt;

&lt;p&gt;What if I want to bundle some css,js and html together?&lt;br&gt;
I have to add the links line by line.&lt;br&gt;
Have you tried using bootstrap&lt;br&gt;
&lt;a href="https://i.giphy.com/media/jquDWJfPUMCiI/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/jquDWJfPUMCiI/giphy.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h4&gt;
  
  
  No Standards
&lt;/h4&gt;

&lt;p&gt;We have no standard for creating html5, javascript and css projects today. Yes we have lot of solution providers out there, like jquery, bootstrap, kendoui etc. But all this solves problems in different ways.&lt;br&gt;
Try to think of a car. e.g BMW, although the car might be a new model, with lot of cooler stuffs etc, but some things might never change e.g the radio, the gear etc. I mean they already have this already why do they need to create it again. They already have a template which can be shipped and use anywhere they want.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So 5 problems in all :(, but finally a solution&lt;/p&gt;

&lt;h1&gt;
  
  
  Web Components
&lt;/h1&gt;

&lt;p&gt;Web components offer four main things&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;h4&gt;
  
  
  The use of templates
&lt;/h4&gt;

&lt;p&gt;Like the radio of a BMW car, why should you always build or write out the same thing from the scratch, when you can just reuse what you already have. I mean, it even gets better,why rewrite what someone has already written. you can define inert, reusable standards with web markups&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h4&gt;
  
  
  Custom elements
&lt;/h4&gt;

&lt;p&gt;I don't know about you, but I like the idea of been able to create my own elements&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h4&gt;
  
  
  Shadow DOM
&lt;/h4&gt;

&lt;p&gt;This provides encapsulation of our markup and styling&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Major browsers now support web components and it is pretty stable, so you should not have a problem with that. They include Opera, chrome,edge, safari and firefox&lt;/p&gt;

&lt;p&gt;There are lot of abstraction for web components out there like polymer. But like JS and Jquery, it is very important to learn the foundation first before abstractions.&lt;/p&gt;

&lt;p&gt;So in summary, why should you use web components&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;h5&gt;
  
  
  Descriptive markup
&lt;/h5&gt;

&lt;p&gt;Web components will make our markup, easier to read, navigate and understand&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h5&gt;
  
  
  Don't start at ground zero
&lt;/h5&gt;

&lt;p&gt;BMW does not create a new radio from scratch for every car, so why should you.&lt;br&gt;
&lt;a href="https://i.giphy.com/media/3otPoxOOMn4ci9LprW/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/3otPoxOOMn4ci9LprW/giphy.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h5&gt;
  
  
  Easily replaced and upgrade
&lt;/h5&gt;

&lt;p&gt;I don't have to proof read 500 lines of css to know why it is white instead of brown.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;h5&gt;
  
  
  Fewer integration mistakes
&lt;/h5&gt;&lt;/li&gt;
&lt;li&gt;&lt;h5&gt;
  
  
  Clearer interface / API
&lt;/h5&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So, thank you for coming to my talk show :) I hope you are has excited has I am about web components. Watch out for my next article has we dissect each of it one by one. from templates to Imports.&lt;/p&gt;

</description>
      <category>html</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
