<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Adriana DiPietro</title>
    <description>The latest articles on DEV Community by Adriana DiPietro (@am20dipi).</description>
    <link>https://dev.to/am20dipi</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%2F471509%2Fd09034a8-e705-494d-a2b1-d4ecf7ef069d.jpg</url>
      <title>DEV Community: Adriana DiPietro</title>
      <link>https://dev.to/am20dipi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/am20dipi"/>
    <language>en</language>
    <item>
      <title>Introduction to RSpec</title>
      <dc:creator>Adriana DiPietro</dc:creator>
      <pubDate>Mon, 20 Feb 2023 18:54:05 +0000</pubDate>
      <link>https://dev.to/am20dipi/what-i-have-learned-about-rspec-186a</link>
      <guid>https://dev.to/am20dipi/what-i-have-learned-about-rspec-186a</guid>
      <description>&lt;p&gt;I've been using RSpec to test my Ruby code for about 8 months now and have learned a lot. So, in this post, I want to share some of the best practices that I've learned about using RSpec for testing. This content is targeted toward beginners who want to get familiar with RSpec, but also can totally benefit more experienced developers who need a quick refresh.&lt;/p&gt;

&lt;p&gt;Whether you are just getting started or not, here are some things that I wish I knew back 8 months ago:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Testing is as important as writing code&lt;/li&gt;
&lt;li&gt;RSpec is very feature-rich&lt;/li&gt;
&lt;li&gt;Abide by formatting rules&lt;/li&gt;
&lt;li&gt;Practice makes progress&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;li&gt;Resources&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Testing is as important as writing code &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Testing is as important as writing code, if not more so. It is one of the aspects of software development that I both underestimated and dreaded early in my career. &lt;/p&gt;

&lt;p&gt;Testing allows you to find bugs before your code gets released. Testing also helps with understanding complex systems and finding out where there is room for improvement. For example: what happens when someone enters the wrong data type into a field? How do different inputs affect your output? Does this piece of logic &lt;em&gt;always&lt;/em&gt; work as intended? We can answer these questions through testing.&lt;/p&gt;

&lt;p&gt;In my personal opinion, I think testing has made me better at coding because in order to write successful RSpec tests, you need to understand your code. There have been times where I have written tests and then realized my code could be refactored to be more efficient or more DRY.&lt;/p&gt;




&lt;h3&gt;
  
  
  RSpec is very feature-rich &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Mocking
&lt;/h4&gt;

&lt;p&gt;In a colloquial sense, to mock something is to replicate. Remember this definition as we proceed with rspec mocks.&lt;/p&gt;

&lt;p&gt;Mocking is mirroring or replicating your Ruby code into rspec in order to test it properly. It is extremely helpful in perceiving return values or method implementations. Given an edgecase, does your expected return value stand? Given an edgecase, does your method get called? Given an edgecase, how do two objects interact? Let's look at an example.&lt;/p&gt;

&lt;p&gt;Here we have a simple Ruby class with a class method that calls an instance method to update a user's city.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UpdateUserInfo&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update_city&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:,&lt;/span&gt; &lt;span class="n"&gt;city&lt;/span&gt;&lt;span class="p"&gt;:)&lt;/span&gt;
   &lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update_city&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;user: &lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;city: &lt;/span&gt;&lt;span class="n"&gt;city&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;update_city&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:,&lt;/span&gt; &lt;span class="n"&gt;city&lt;/span&gt;&lt;span class="p"&gt;:)&lt;/span&gt;
    &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;city: &lt;/span&gt;&lt;span class="n"&gt;city&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, here is our rspec file. First, I mock out what my user object &lt;em&gt;should&lt;/em&gt; look like in reality.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;let&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="ss"&gt;first_name: &lt;/span&gt;&lt;span class="s1"&gt;'Frances'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="ss"&gt;email: &lt;/span&gt;&lt;span class="s1"&gt;'frances.dev@example.com'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="ss"&gt;hobbies: &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="s1"&gt;'writing'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="s1"&gt;'coding'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="s1"&gt;'reading'&lt;/span&gt;
    &lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="ss"&gt;city: &lt;/span&gt;&lt;span class="s1"&gt;'New York City'&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;let&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:updated_city&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s1"&gt;'Philadelphia'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;describe&lt;/span&gt; &lt;span class="s1"&gt;'.update_city'&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;subject&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;described_class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update_city&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;user: &lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;city: &lt;/span&gt;&lt;span class="n"&gt;updated_city&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; 

  &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="s1"&gt;'updates the user'&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="n"&gt;city&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;receive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:update!&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;city: 
    &lt;/span&gt;&lt;span class="n"&gt;updated_city&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;and_return&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;


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

&lt;/div&gt;



&lt;p&gt;I then mock out my call to &lt;code&gt;#update!&lt;/code&gt; using an expectation. I include what I would normally include: the attribute name and the value I want to pass in. I also want to mock out the expected return or response. A successful call to &lt;code&gt;#update!&lt;/code&gt; on the user object should return true. If it were to fail, &lt;code&gt;#update!&lt;/code&gt; would raise an error. In this example, the spec will pass. &lt;/p&gt;

&lt;p&gt;You may notice that this doesn't look to different than our Ruby code in the &lt;code&gt;UpdateUserInfo&lt;/code&gt; class. That is the whole point: readability and mirroring. Just by solely reading this test, I know what that class should accomplish. &lt;/p&gt;

&lt;p&gt;Mocking out both the object and the expected behavior allows for an additional perspective on their interaction. How does this object react to this method? Keep this in mind while building out your spec files — maintain the integrity of your code through replication. If your spec doesn't mirror your code, then your spec is not testing truly.&lt;/p&gt;

&lt;h4&gt;
  
  
  Matchers
&lt;/h4&gt;

&lt;p&gt;What is a matcher? A matcher allows us to create an assertion and expectation within our spec. This is vague, I know. Let's look at a simple example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;let&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:random_num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;14&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;is_expected&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;is_expected&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;  &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code snippet, I declare a variable &lt;code&gt;random_num&lt;/code&gt; and assign it to the value of &lt;code&gt;14&lt;/code&gt;. Then I assert two (2) expectations. I assert that &lt;code&gt;it&lt;/code&gt; (meaning our declared variable &lt;code&gt;random_num&lt;/code&gt;) should pass two different cases: &lt;code&gt;random_num&lt;/code&gt; is less than 20 and &lt;code&gt;random_num&lt;/code&gt; is more than 1. &lt;/p&gt;

&lt;p&gt;In this example, both tests would pass. So, if the object is the variable I declared and the expectation is the test, then what is the matcher? The matcher is &lt;code&gt;.to be&lt;/code&gt;. This is only one of many, many matchers. Let's look at a more complex example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;let&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:fruits&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'orange'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'mango'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'strawberry'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fruits&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="kp"&gt;include&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'blueberry'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fruits&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="kp"&gt;include&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'mango'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'strawberry'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fruits&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="kp"&gt;include&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'blueberry'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'mango'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'banana'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After taking a second to look at the above code snippet, can you point out the following?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What is the name of the declared variable?&lt;/li&gt;
&lt;li&gt;What is the value of the declared variable?&lt;/li&gt;
&lt;li&gt;What data type is the value of the declared variable?&lt;/li&gt;
&lt;li&gt;What is the matcher of the expectations?&lt;/li&gt;
&lt;li&gt;What tests pass? What tests fail?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Look at Resources to discover more types of rspec matchers.  &lt;/p&gt;




&lt;h3&gt;
  
  
  Abide by formatting rules &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Just like programming languages, it is best practice to abide by formatting rules and syntax. If there is a clear, defined standard to your specs, then you will eventually be able to understand others' tests and write yours more efficiently. And then on the flip side, this will help other people understand what your code is to achieve. &lt;/p&gt;

&lt;p&gt;Some formatting examples include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Test one thing per example block.&lt;/li&gt;
&lt;li&gt;Make your shared examples, context blocks and it blocks readable and read-like-english.&lt;/li&gt;
&lt;li&gt;Like Ruby, use snake_case.&lt;/li&gt;
&lt;li&gt;One test suite per class.&lt;/li&gt;
&lt;li&gt;If the class file is 'cat_controller.rb', then the spec file for that class should be 'cat_controller_spec.rb'&lt;/li&gt;
&lt;li&gt;Keep all your RSpec files in a &lt;code&gt;/spec&lt;/code&gt; directory.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are just a few formatting expectations. However, they are a great jumping off point and will set a sound foundation for your test development.&lt;/p&gt;




&lt;h3&gt;
  
  
  Practice makes progress &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Taking a look at RSpec as a whole, you may be confused at first – I sure was. You may be confused after weeks of writing with RSpec – guilty, again! Just keep practicing — this stuff is not easy! Remember: practice makes things easier for you in the future. Don't give up.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How can you practice?&lt;/strong&gt; Write your tests first before your code. Think Test Driven Development (TDD). Offer to shadow/pair with your peers on writing their specs. Even better, review your peers' specs and see if and how they can be written better. Offer a solution!&lt;/p&gt;




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

&lt;p&gt;Writing tests is a great way to make sure you are building the right thing – in the right way. RSpec syntax, while tricky at first, is simple and expressive. So once you get used to it, writing in RSpec will feel like second nature. &lt;/p&gt;

&lt;p&gt;I hope this post has helped you learn a little bit more about RSpec. As I mentioned at the start, it can be a bit overwhelming at first, but don't let that stop you from working with this awesome tool. &lt;/p&gt;

&lt;p&gt;If you have any questions about RSpec or want to share your own experiences with it, please leave a comment.&lt;/p&gt;

&lt;p&gt;Remember: we are all learning as we go :) so be kind.&lt;/p&gt;




&lt;h4&gt;
  
  
  Resources &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://relishapp.com/rspec/rspec-expectations/v/3-12/docs"&gt;relishapp documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://rspec.info/"&gt;rspec official documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://rubydoc.info/gems/rspec-rails#installation"&gt;rubydoc rspec installation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.codewithjason.com/rspec-mocks-stubs-plain-english/"&gt;Mocks and Stubs in Plain English&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>rspec</category>
      <category>ruby</category>
      <category>rails</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Introduction to GraphQL</title>
      <dc:creator>Adriana DiPietro</dc:creator>
      <pubDate>Thu, 07 Apr 2022 19:58:22 +0000</pubDate>
      <link>https://dev.to/am20dipi/introduction-to-graphql-2bkd</link>
      <guid>https://dev.to/am20dipi/introduction-to-graphql-2bkd</guid>
      <description>&lt;p&gt;Hi everyone! &lt;/p&gt;

&lt;p&gt;Today we are learning about &lt;strong&gt;GraphQL&lt;/strong&gt;! GraphQL is a new tool for me that I am lucky enough to learn about and use in my internship. &lt;em&gt;So&lt;/em&gt;, I figured together we can learn what GraphQL is, why it is important and how we can use it. &lt;/p&gt;

&lt;p&gt;Let's get started! &lt;/p&gt;




&lt;h2&gt;
  
  
  Learning Goals
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;GraphQL is a Query Language&lt;/li&gt;
&lt;li&gt;How It Works&lt;/li&gt;
&lt;li&gt;Differences Between GQL + REST&lt;/li&gt;
&lt;li&gt;
Elements of GraphQL

&lt;ul&gt;
&lt;li&gt;Queries / Types&lt;/li&gt;
&lt;li&gt;Data Types&lt;/li&gt;
&lt;li&gt;Scalars&lt;/li&gt;
&lt;li&gt;Enums&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Summary + Recap&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  GraphQL is a Query Language &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;As the title of this section suggests, &lt;strong&gt;GraphQL is a Query language&lt;/strong&gt;. What does this mean? It means that GraphQL is a &lt;em&gt;programming&lt;/em&gt; language used to &lt;strong&gt;get&lt;/strong&gt; and &lt;strong&gt;post&lt;/strong&gt; data. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;It is important to acknowledge what GraphQL is because of its name. While many databases end with "QL", GraphQL is NOT a database! Do not get confused by this.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A "query", as you may have guessed, is a request usually for information (in programming: data!). In life, I can query how old you are by asking you "Hey, how old are you?". In return, you may answer: "25" "I am not telling!" "I am turning 30 in July." &lt;/p&gt;

&lt;p&gt;Using GraphQL, we create queries to get data from our database. We ask our database: "How many users have brown eyes?" "How many pets does user 129i04u83 have?" "What stores are located in NYC?"&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Recap&lt;/strong&gt;: GraphQL is a QUERY language that we use to get and post data from our database. &lt;/p&gt;

&lt;p&gt;Now, let's learn how it &lt;em&gt;really&lt;/em&gt; works. &lt;/p&gt;




&lt;h2&gt;
  
  
  How It Works &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;The client (the frontend of an application) uses GraphQL API to communicate with the database. Think of GraphQL as the bridge between the client and the database -- it does all the talking -- it is the middleman! &lt;/p&gt;

&lt;p&gt;How does the client use GraphQL API? Answer: through the &lt;code&gt;'/graphql'&lt;/code&gt; endpoint. The client makes a request with a query to this endpoint (and this endpoint &lt;em&gt;only&lt;/em&gt;) and the query determines what data is received or posted. Ultimately, the query &lt;em&gt;describes&lt;/em&gt; to the endpoint what it wants. &lt;/p&gt;

&lt;p&gt;The &lt;u&gt;flow&lt;/u&gt; is as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The client makes a request with a query to the GraphQL endpoint =&amp;gt;&lt;/li&gt;
&lt;li&gt;The query is sent to GraphQL API =&amp;gt;&lt;/li&gt;
&lt;li&gt;GraphQL parses (JSON) the query =&amp;gt; &lt;/li&gt;
&lt;li&gt;GraphQL sends the desired data back to the client&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Differences Between GQL + REST &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;In the section above, we learned that the client only has to make requests to a single endpoint: &lt;code&gt;'/graphql'&lt;/code&gt;. Which means GraphQL throws out the idea of REST (and its many, many endpoints) in order to use the single endpoint. Why?&lt;/p&gt;

&lt;p&gt;With GraphQL, queries determine &lt;em&gt;exactly&lt;/em&gt; what data is to be received or posted. These queries completely diminish the need to specify endpoints. Queries also reduce the amount of &lt;strong&gt;overfetching&lt;/strong&gt; and &lt;strong&gt;underfetching&lt;/strong&gt; of data in an application. &lt;/p&gt;

&lt;p&gt;Overfetching occurs when the client receives more data than is needed from a specific REST endpoint. &lt;/p&gt;

&lt;p&gt;For example, if I am building an application that takes a user's age and outputs their expected high school graduation year, I only really need the user's age. But say within my database, in addition to the user age data, I also have their address, height and eye color. None of that data is necessary for my application. Graphql querying allows a pinpoint level of specification as to not receive unnecessary data that may slow down the application or cost a lot of money. &lt;/p&gt;

&lt;p&gt;Underfetching is quite the opposite: it occurs when a specific REST endpoint does not provide all the data required/desired for an application. &lt;/p&gt;

&lt;p&gt;Check out this diagram below for a visual representation:&lt;/p&gt;

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




&lt;h2&gt;
  
  
  Elements of GraphQL &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Let's look into some of the elements of GraphQL to get a better understanding: &lt;/p&gt;

&lt;h3&gt;
  
  
  Data Types &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;GraphQL has five (5) data types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ID &lt;strong&gt;=&amp;gt;&lt;/strong&gt; a unique identifier&lt;/li&gt;
&lt;li&gt;String &lt;strong&gt;=&amp;gt;&lt;/strong&gt; a UTF-8 character sequence&lt;/li&gt;
&lt;li&gt;Int &lt;strong&gt;=&amp;gt;&lt;/strong&gt; a signed 32-bit integer&lt;/li&gt;
&lt;li&gt;Boolean &lt;strong&gt;=&amp;gt;&lt;/strong&gt; true or false&lt;/li&gt;
&lt;li&gt;Float &lt;strong&gt;=&amp;gt;&lt;/strong&gt; a decimal&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Queries / Types &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Now that we know what a query is, we can look into how GraphQL defines them.&lt;/p&gt;

&lt;p&gt;GraphQL describes a query using the &lt;strong&gt;type&lt;/strong&gt; keyword. For example, to create a query for "cat" with fields "name", "age" "color", "breed", we would code it out like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Cat {
   name: String
   age: Int
   color: String
   breed: String
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Within each field, we declare the field name (or a &lt;em&gt;key&lt;/em&gt;) and the data type of its value. So the name, color and breed of a Cat is a string, while the age is an integer. &lt;/p&gt;

&lt;p&gt;While querying, we can non-nullify the values of a field using an exclamation point(!) -- meaning the value of a cat's name cannot be "null". Like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Cat {
   name: String!
   age: Int!
   color: String!
   breed: String!
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the basic, universal syntax for defining a query in GraphQL. &lt;/p&gt;




&lt;h3&gt;
  
  
  Scalars &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;While the above, five (5) data types are scalars in GraphQL, GraphQL also allows for custom scalars. What does this mean? It means we can create and initialize our own "data types" to be used to query. &lt;/p&gt;

&lt;p&gt;For example, let's say we have to query for website data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Website {
    id: ID
    name: String
    url: Url
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This query returns the website's id, name and url. We could possible store the value of the url field as a string, but then any ol' string could be assigned to "url". Such as "ww.google.com" or "hey hey hey". And those are not valid urls. So, instead, we could just create our own scalar called "Url". &lt;/p&gt;

&lt;p&gt;In order to create our own scalar, we need to consider a few things:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; How the scalar's value is represented in the backend&lt;br&gt;
   &lt;strong&gt;2.&lt;/strong&gt; If and how the value's backend representation is JSON-compatible&lt;/p&gt;

&lt;p&gt;So, a Url would be represented as a string in the backend, but a &lt;em&gt;regexed&lt;/em&gt; string. Meaning, it has to fulfill some requirements, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;starts with "&lt;a href="https://www"&gt;https://www&lt;/a&gt;."&lt;/li&gt;
&lt;li&gt;includes ".com", ".edu", ".org"... etc&lt;/li&gt;
&lt;li&gt;does not have any spaces&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With the scalar's value represented as a regexed string in the backend, we can ensure it is JSON-compatible in order to be sent to the frontend successfully: Strings are indeed very JSON-compatible!&lt;/p&gt;

&lt;p&gt;There are many more custom scalars to be created:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Date&lt;/li&gt;
&lt;li&gt;Time&lt;/li&gt;
&lt;li&gt;Email&lt;/li&gt;
&lt;li&gt;RGB color hex&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What else can you think of?&lt;/p&gt;


&lt;h3&gt;
  
  
  Enums &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Enums are basically a "special" data type we can use to catalog all the possible values or options for a given field. In short, the specified values of an enum are the &lt;em&gt;only&lt;/em&gt; &lt;em&gt;accepted&lt;/em&gt; options. &lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;enum BoxState {
    SHIPPED
    DELIVERED
    PENDING
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When querying an object "box" and we want to know the state of the box, BoxState can only be provided the values of: SHIPPED, DELIVERED, or PENDING. The value cannot be "12", true/false, or "Awaiting truck for pickup".&lt;/p&gt;

&lt;p&gt;Enums provide a specificity that keeps data values contained and limits bugs. &lt;/p&gt;




&lt;h2&gt;
  
  
  Summary + Recap &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;GraphQL is a QUERY language.&lt;/li&gt;
&lt;li&gt;A query is a request for information/data. &lt;/li&gt;
&lt;li&gt;With GraphQL, the client uses a single endpoint to make requests: &lt;code&gt;'/graphql'&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;GraphQL removes the need for many REST endpoints because of its queries. &lt;/li&gt;
&lt;li&gt;A "type" is a query.&lt;/li&gt;
&lt;li&gt;An "enum" defines all possible, accepted values for a field.&lt;/li&gt;
&lt;li&gt;Custom scalars can be defined and implemented as values for fields. &lt;/li&gt;
&lt;li&gt;Overfetching =&amp;gt; receiving too much data that is not necessary for the application.&lt;/li&gt;
&lt;li&gt;Underfetching =&amp;gt; an endpoint not providing the desired data necessary for the application. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;🍵Make sure to check out GraphQL's official website &lt;a href="https://graphql.org/"&gt;here&lt;/a&gt; for documentation and more information.&lt;/p&gt;

&lt;p&gt;🍵Thank you for reading along and learning with me. Please feel free to share, like and comment below. But, please remember, everyone is always learning and the "correct" way is not always the best way.  &lt;/p&gt;

</description>
      <category>graphql</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>A Quick Guide To: Closures</title>
      <dc:creator>Adriana DiPietro</dc:creator>
      <pubDate>Wed, 16 Mar 2022 18:45:05 +0000</pubDate>
      <link>https://dev.to/am20dipi/a-quick-guide-to-closures-2o0</link>
      <guid>https://dev.to/am20dipi/a-quick-guide-to-closures-2o0</guid>
      <description>&lt;p&gt;Hi programmers! Today we will discussing &lt;strong&gt;closures&lt;/strong&gt; in JavaScript. Every heard of them? Whether you have or haven't, I am positive you have used them. &lt;/p&gt;

&lt;p&gt;Without giving too much away, closures are an undeniably necessary concept in order to understand your code and JavaScript as a whole.&lt;/p&gt;

&lt;p&gt;Curious to know why? Let's get started. &lt;/p&gt;




&lt;h3&gt;
  
  
  Learning Goals
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
What is a Closure?

&lt;ul&gt;
&lt;li&gt;Lexical Scope&lt;/li&gt;
&lt;li&gt;Functions as First-Class Citizens&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
The Benefits of using Closures

&lt;ul&gt;
&lt;li&gt;Encapsulation&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
Closure Examples

&lt;ul&gt;
&lt;li&gt;Example 1&lt;/li&gt;
&lt;li&gt;Example 2&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Summary + Recap&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  What is a Closure &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;A closure is the combination of a function and the environment in which it was declared.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Considered one of the pillars of JavaScript, &lt;strong&gt;closures&lt;/strong&gt; allow a function to access variables from an &lt;em&gt;enclosing&lt;/em&gt; scope. &lt;/p&gt;

&lt;p&gt;In other words, a closure gives you access to an outer function's scope from an inner function.&lt;/p&gt;

&lt;p&gt;When I said that I was &lt;em&gt;positive&lt;/em&gt; that you have used closures before, it is because every function in JavaScript forms a closure.&lt;/p&gt;

&lt;h3&gt;
  
  
  Lexical Scope &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;You may have noticed I used the term "enclosing" in my description of closure -- let's unravel this by learning what &lt;strong&gt;lexical scope&lt;/strong&gt; is. &lt;/p&gt;

&lt;p&gt;Simply, lexical scope is the accessibility of variables dependant on where the function is declared; NOT where the function is invoked (or called). &lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;// declare function myFunc()&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;myFunc&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="c1"&gt;// declare and assign variable myName&lt;/span&gt;
    &lt;span class="c1"&gt;// myName is in the scope of myFunc()&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Adriana&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

    &lt;span class="c1"&gt;// declare printName()&lt;/span&gt;
    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;printName&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
      &lt;span class="c1"&gt;// log the value of variable myName to the console&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;printName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="c1"&gt;// invoke myFunc()&lt;/span&gt;
&lt;span class="nf"&gt;myFunc&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above snippet, the function &lt;strong&gt;printName()&lt;/strong&gt; is &lt;strong&gt;lexically scoped&lt;/strong&gt;. printName() has access to the variable "myName" because we invoke and return printName() within the myFunc()'s scope. &lt;/p&gt;

&lt;p&gt;So, when we invoke myFunc(), the console outputs "Adriana".&lt;/p&gt;

&lt;p&gt;JavaScript is cool in the way that we can access a variable declared outside of a function within that function. A lot of languages actually do not allow this behavior. &lt;/p&gt;

&lt;h3&gt;
  
  
  Functions as First-Class Citizens &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Want to know what else is cool about functions in JavaScript?&lt;/p&gt;

&lt;p&gt;Functions in JavaScript are &lt;strong&gt;First-Class Citizens&lt;/strong&gt;. What does this mean? It is a fancy way to say that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Functions can be passed as a parameter to another function. &lt;/li&gt;
&lt;li&gt;Functions can be the return value of a function. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ultimately, functions are flexible and extremely worth utilizing!&lt;/p&gt;




&lt;h2&gt;
  
  
  The Benefits of using Closures &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Now that we got the idea of what a closure is and how they work -- what are the benefits? &lt;/p&gt;

&lt;p&gt;Because of closures, the JavaScript Engine (V8) ensures that the function has access to all of the variables by keeping all the variables declared outside of the innermost function. &lt;/p&gt;

&lt;p&gt;JavaScript is lexically and statically scoped -- so when JavaScript compiles and looks through your code the first time, the JavaScript Engine (V8) catalogs what needs to be closured even before the code is executed or ran. &lt;/p&gt;

&lt;p&gt;So, the benefits?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Closures save memory space.&lt;/li&gt;
&lt;li&gt;Closures allow for encapsulation. &lt;/li&gt;
&lt;li&gt;Closures reduce bugs. &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Encapsulation &lt;a&gt;
&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Encapsulation is the hiding of information so as to not be manipulated by the "outside" world or global scope. &lt;/p&gt;

&lt;p&gt;We use encapsulation because we do &lt;strong&gt;NOT&lt;/strong&gt; want to give access to certain functions to users nor allow certain variables (such as something that stores state) to be modified. &lt;/p&gt;

&lt;p&gt;By nesting an inner function within another function, like our example above, we make sure it is not totally accessible. &lt;/p&gt;

&lt;p&gt;Basically, encapsulation ensures that some data is not exposed. Encapsulation promotes the idea of privilege in terms of access to data (or state) or the functions that have access to that data.&lt;/p&gt;




&lt;h2&gt;
  
  
  Closure Examples&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Now that we have acknowledged a textbook definition of closures, let's code this out (we are programmers, aren't we?).&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 1 &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;First, let's declare a function "a" and within "a" declare a variable "cat" set to a string "Charlie":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;a&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;cat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Charlie&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;Since, we are not returning anything, if we invoked a() we would get undefined. However, our "cat" variable was declared within the function's scope and thus, it is in our memory heap (thanks V8 engine!). &lt;/p&gt;

&lt;p&gt;In order to be a closure, we must &lt;strong&gt;return ** and **declare&lt;/strong&gt; a function within another function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;a&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;cat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Charlie&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;b&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;dog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Zoie&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's declare AND return function "b" below our cat variable declaration. Within function "b" we declare another variable "dog" and set it to the string "Zoie". Function "b"'s scope now contains access to variable "dog" while also containing access to variable "cat".&lt;/p&gt;

&lt;p&gt;So, if we wanted to return the value of variables "cat" and "dog" within the scope of function "b", we could do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;a&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;cat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Charlie&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;b&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;dog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Zoie&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;cat&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; - &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In your console or sandbox, paste the above code snipped and then invoke/call the functions in this order:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;a()&lt;/li&gt;
&lt;li&gt;b()&lt;/li&gt;
&lt;li&gt;a()()&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;What did your console output?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Invoking a() returns function b().&lt;/li&gt;
&lt;li&gt;Invoking b() returns an error "b is not defined".&lt;/li&gt;
&lt;li&gt;Invoking a()() returns our string literal.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 2 &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Let's code out another example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;outer&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;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here I have declared a function "outer" and declared and assigned a variable "state" to the value of 0. &lt;/p&gt;

&lt;p&gt;If we declare and return another function within outer(), can we access "state"?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;outer&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;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;inner&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`Our state is currently at: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;.`&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yes! If we paste this code snippet into our sandbox and call outer() and inner(), what is our output?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Invoking outer() returns the function inner().&lt;/li&gt;
&lt;li&gt;Invoking inner() returns an error "inner is not defined".&lt;/li&gt;
&lt;li&gt;Invoking outer()() returns our string literal with the value of "state."&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In both examples, we are able to access the inner function by invoking the outer function. When we double-invoke the outer function, we are able to access the string literals. Why? By enclosing the inner function in the outer function's scope, we can only invoke it internally. So, we must use the outer scope as a bridge to receive the return value of the inner scope. &lt;/p&gt;

&lt;p&gt;So, how were we able to access the variables in the return statement? JavaScript's Engine (V8) reads through the code from outer function to inner function and shoves the variables into the closure (think of a box). The variables are stored for later use and are never removed because V8 sees that the variables are referenced in another function within the outer function's scope. &lt;/p&gt;




&lt;h2&gt;
  
  
  Summary + Recap &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;From this article, let's bundle and condense some of the takeaways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript's Engine is &lt;strong&gt;V8&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;V8 runs and compiles our code. &lt;/li&gt;
&lt;li&gt;Functions in JavaScript are &lt;em&gt;lexically scoped&lt;/em&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lexical Scope&lt;/strong&gt;: the accessibility of variables dependant on where the function is declared; NOT where the function is invoked (or called).&lt;/li&gt;
&lt;li&gt;Functions in JavaScript are &lt;strong&gt;First-Class Citizens&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;Variables declared in the global context OR within an enclosing scope are stored in the memory heap. &lt;/li&gt;
&lt;li&gt;Closures allow a function to access variables from an enclosing scope even after it leaves the scope in which the function is declared.&lt;/li&gt;
&lt;li&gt;Because of closures, the JavaScript Engine ensures that an inner function has access to all of the variables within its enclosing scope. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Encapsulation&lt;/strong&gt; = privacy or reduced accessibility of data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Still not sure about closures? Here are some resources I used to help me further understand:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://medium.com/javascript-scene/encapsulation-in-javascript-26be60e325b4"&gt;Encapsulation in JavaScript article&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.youtube.com/watch?v=sNKKxc4QHqA"&gt;What is encapsulation in programming?&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=vKJpN5FAeF4"&gt;Closures Explained in 100 Seconds&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=1JsJx1x35c0"&gt;Closures - Beau teaches JavaScript&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thank you for reading along and coding with me :)&lt;br&gt;
Have any questions, comments or suggestions? Leave them below. &lt;/p&gt;

&lt;p&gt;Remember: always code in the way that is best for your learning.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>codenewbie</category>
      <category>webdev</category>
    </item>
    <item>
      <title>LeetCode WalkThru: 'Reverse Integer'</title>
      <dc:creator>Adriana DiPietro</dc:creator>
      <pubDate>Tue, 01 Mar 2022 17:15:50 +0000</pubDate>
      <link>https://dev.to/am20dipi/leetcode-walkthru-reverse-integer-1fjm</link>
      <guid>https://dev.to/am20dipi/leetcode-walkthru-reverse-integer-1fjm</guid>
      <description>&lt;p&gt;Hi programmers! Today we will be walking through how to solve LeetCode's &lt;strong&gt;Reverse Integer&lt;/strong&gt; challenge. Click &lt;a href="https://leetcode.com/problems/reverse-integer/"&gt;here&lt;/a&gt; to pull up the challenge on your end.  &lt;/p&gt;

&lt;p&gt;After going over the instructions and the examples provided by LeetCode, we will go through two (2) different methods to come to a solution, including a &lt;strong&gt;brute force&lt;/strong&gt; attempt and a &lt;strong&gt;optimized&lt;/strong&gt; attempt. &lt;/p&gt;

&lt;p&gt;It is important to see how a single challenge can be solved in multiple ways to understand how different methodologies achieve efficiency, speed and optimization.  &lt;/p&gt;




&lt;h3&gt;
  
  
  Itinerary
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; Instructions + Examples&lt;br&gt;
&lt;strong&gt;2.&lt;/strong&gt; Brute Force Solution&lt;br&gt;
&lt;strong&gt;3.&lt;/strong&gt; Optimized Solution&lt;br&gt;
&lt;strong&gt;4.&lt;/strong&gt; Summary + Recap&lt;/p&gt;


&lt;h2&gt;
  
  
  Instructions + Examples &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Given&lt;/span&gt; &lt;span class="nx"&gt;an&lt;/span&gt; &lt;span class="nx"&gt;integer&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;x&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;x&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="kd"&gt;with&lt;/span&gt; 
&lt;span class="nx"&gt;its&lt;/span&gt; &lt;span class="nx"&gt;digits&lt;/span&gt; &lt;span class="nx"&gt;reversed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nx"&gt;If&lt;/span&gt; &lt;span class="nx"&gt;reversing&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="nx"&gt;causes&lt;/span&gt; 
&lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;go&lt;/span&gt; &lt;span class="nx"&gt;outside&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;signed&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;bit&lt;/span&gt; 
&lt;span class="nx"&gt;integer&lt;/span&gt; &lt;span class="nx"&gt;range&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mi"&gt;31&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;then&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;In my opinion, LeetCode does not do a great job with these instructions. (Ahem, super vague!) So, in addition to returning the reversed version of the given integer, we have to consider a few more things. &lt;/p&gt;

&lt;p&gt;Let's look at the examples to examine what else we should consider when solving this challenge:&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;Example&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

&lt;span class="nx"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;
&lt;span class="nx"&gt;Output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;321&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example 1 is pretty straight forward. We see that the output is the reversed integer of the input. Simple enough.&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;Example&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; 

&lt;span class="nx"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;123&lt;/span&gt;
&lt;span class="nx"&gt;Output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;321&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, Example 2 introduces the possible hiccup (or edge case) of negative integers. We need to consider that the reversal of the integer results in the appropriate placing of the &lt;strong&gt;-&lt;/strong&gt; sign.&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;Example&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

&lt;span class="nx"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;120&lt;/span&gt;
&lt;span class="nx"&gt;Output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;21&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Example 3, the 0 is completely popped off from the output. While the presence of the zero does not affect the actual value of the output, &lt;em&gt;semantically&lt;/em&gt; we do not present numbers &lt;em&gt;usually&lt;/em&gt; with a zero at the beginning. So, we need to also consider removing the zero when necessary. &lt;/p&gt;

&lt;p&gt;After looking at both the instructions and the examples, we get a pretty good idea of what we need to accomplish:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; Reverse the integer and return it. &lt;br&gt;
&lt;strong&gt;2.&lt;/strong&gt; Maintain the positive or negative value of the integer.&lt;br&gt;
&lt;strong&gt;3.&lt;/strong&gt; Remove any zeroes at the beginning of the reversed integer. &lt;br&gt;
&lt;strong&gt;4.&lt;/strong&gt; Return 0 if the given input is larger than 2^31. (If the number is too large, just return 0!)&lt;/p&gt;

&lt;p&gt;Let's get started.&lt;/p&gt;


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

&lt;p&gt;Thanks to JavaScript's built-in methods, the easiest way to reverse data is to use the &lt;strong&gt;reverse()&lt;/strong&gt; method. However, the reverse() method is dedicated only to arrays. So in order to use reverse() let's convert our integer 'x' to an array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;reverseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="c1"&gt;// 1. Convert integer to string.&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;intToString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="c1"&gt;// 2. Convert string to array.&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stringToArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;intToString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;stringToArray&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;reverseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In the above snippet, I broke down the conversion of 'x' from an &lt;strong&gt;integer&lt;/strong&gt; to a &lt;strong&gt;string&lt;/strong&gt; to an &lt;strong&gt;array&lt;/strong&gt;. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;toString()&lt;/strong&gt; converts a data value to a string. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;split()&lt;/strong&gt; converts a data value to an array. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;split()&lt;/strong&gt; takes an optional parameter called a &lt;strong&gt;separator&lt;/strong&gt;. The separator sits between each array item separating the array item from both its preceding item and succeeding item. In this code challenge, we need the separator.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If we run the above code snippet in our console or sandbox, our output should look like this: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;['1', '2', '3']&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, we have a nice array with each digit of our integer as its own array item. Cool. &lt;/p&gt;

&lt;p&gt;Let's keep moving and reverse the array using the &lt;strong&gt;reverse()&lt;/strong&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;reverseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="c1"&gt;// 1. Convert integer to string.&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;intToString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="c1"&gt;// 2. Convert string to array.&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stringToArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;intToString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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="c1"&gt;// 3. Reverse array using .reverse().&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;reversedArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;stringToArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reverse&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;reversedArray&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;reverseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// output =&amp;gt; ['3', '2', '1']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our output would now be &lt;code&gt;['3', '2', '1']&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;With an integer like 123, without a negative sign or any zeroes in the beginning of the array, we could now potentially convert the array back to an integer and we would have the solution. &lt;/p&gt;

&lt;p&gt;However, we need to consider those edge cases we saw in the examples provided by LeetCode because in reality integers include negative numbers and zeroes. &lt;/p&gt;

&lt;p&gt;First, let's consider if the integer ends with a 0. When we reverse the array the 0 would then be in the first position of the array. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;reverseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;120&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// output =&amp;gt; 021&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can use &lt;strong&gt;shift()&lt;/strong&gt; to remove the first element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;reverseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="c1"&gt;// 1. Convert integer to string.&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;intToString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="c1"&gt;// 2. Convert string to array.&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stringToArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;intToString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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="c1"&gt;// 3. Reverse array using .reverse().&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;reversedArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;stringToArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

   &lt;span class="c1"&gt;// 4. If 0 is in the first position of the array, use shift() to remove it.&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;reversedArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;0&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nx"&gt;reversedArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="c1"&gt;// ['0', '3', '2', '1'] =&amp;gt; ['3', '2', '1']&lt;/span&gt;
      &lt;span class="c1"&gt;// ['0', '0', '3', '2', '1'] =&amp;gt; ['3', '2', '1']&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;reverseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1230&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;reverseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12300&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Simple enough, right? Our next edge case we have to consider is the positive or negative value of the integer we pass to our function. &lt;/p&gt;

&lt;p&gt;We want to maintain this value, but when we reverse our array the negative sign will end up in the last position or index which semantically does not work.&lt;/p&gt;

&lt;p&gt;According to our current code, this would be the output:&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="nf"&gt;reverseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; 321-&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But, we want this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;reverseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; -321&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just like reverse(), JavaScript has great built-in functionality to compensate for this edge case. &lt;strong&gt;Math.sign()&lt;/strong&gt; returns either a positive or negative value based on the sign(+/-) of the number passed into it. &lt;/p&gt;

&lt;p&gt;Here are some examples:&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sign&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="c1"&gt;// output =&amp;gt; 1&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sign&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;321&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="c1"&gt;// output =&amp;gt; -1&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sign&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="c1"&gt;// output =&amp;gt; 0&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sign&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;-321&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="c1"&gt;// output =&amp;gt; -1&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Math.sign() will return either -1, 0 or 1. So, how can we apply this to our code to return the reversed integer? We can multiply our reversed integer by Math.sign() passing in our original integer.&lt;/p&gt;

&lt;p&gt;First, let's convert our reversed array back to an integer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;reverseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="c1"&gt;// 1. Convert integer to string.&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;intToString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="c1"&gt;// 2. Convert string to array.&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stringToArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;intToString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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="c1"&gt;// 3. Reverse array using .reverse().&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;reversedArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;stringToArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

   &lt;span class="c1"&gt;// 4. If 0 is in the first position of the array, use shift() to remove it.&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;reversedArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;0&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nx"&gt;reversedArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="c1"&gt;// ['0', '3', '2', '1'] =&amp;gt; ['3', '2', '1']&lt;/span&gt;

   &lt;span class="c1"&gt;// 5. Convert array back into string.&lt;/span&gt;
       &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;backToString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;reversedArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
       &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;backToString&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

   &lt;span class="c1"&gt;// 6. Convert string back into integer.&lt;/span&gt;
       &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;backToInteger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;parseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;backToString&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
       &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;backToInteger&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;reverseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// output =&amp;gt; 321-&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;** I use 'typeof' to check the data type of a given variable. It is a great tool to get in the habit of using! **&lt;/p&gt;

&lt;p&gt;Now we can apply &lt;strong&gt;Math.sign()&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;reverseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="c1"&gt;// 1. Convert integer to string.&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;intToString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="c1"&gt;// 2. Convert string to array.&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stringToArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;intToString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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="c1"&gt;// 3. Reverse array using .reverse().&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;reversedArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;stringToArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

   &lt;span class="c1"&gt;// 4. If 0 is in the first position of the array, use shift() to remove it.&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;reversedArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;0&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nx"&gt;reversedArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="c1"&gt;// ['0', '3', '2', '1'] =&amp;gt; ['3', '2', '1']&lt;/span&gt;

   &lt;span class="c1"&gt;// 5. Convert array back into string.&lt;/span&gt;
       &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;backToString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;reversedArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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="c1"&gt;// 6. Convert string back into integer and check to see if the integer is larger than 2^31.&lt;/span&gt;
       &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;backToInteger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;parseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;backToString&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
       &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;backToInteger&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="mi"&gt;31&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
           &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
       &lt;span class="p"&gt;}&lt;/span&gt;

   &lt;span class="c1"&gt;// 7. Using Math.sign(), multiply the value of the original integer by our reversed integer.&lt;/span&gt;
     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;backToInteger&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sign&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;reverseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// output =&amp;gt; -321&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Paste all this code into your console or sandbox. Our output should be -321! Yes, it worked!&lt;/p&gt;

&lt;p&gt;Try it with a few different integers as well: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Input: 0&lt;/li&gt;
&lt;li&gt;Input: 1400&lt;/li&gt;
&lt;li&gt;Input: -50000&lt;/li&gt;
&lt;li&gt;Input: 200000000000&lt;/li&gt;
&lt;li&gt;Input: -0&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Optimized Solution&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;In our brute force attempt, we have a completely valid solution. However, it is messy because it is not DRY. We repeat ourselves quite a bit and overuse variable declarations. Let's optimize our code and reduce it from 15+ lines of code down to a few. &lt;/p&gt;

&lt;p&gt;First, we can convert our original integer to a string, then to an array, reverse it, back to a string and then finally back into an integer in &lt;strong&gt;one&lt;/strong&gt; line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;optimizedReverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;reversed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;parseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&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="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&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;reversed&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;optimizedReverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;345&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// output =&amp;gt; 543 &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That cleans up a lot of code! Down to two lines already! &lt;/p&gt;

&lt;p&gt;Now, we can borrow some code from our brute force attempt to correctly apply the positive or negative value to our reversed integer as well as check to see if the reversed value is greater than 2^31:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;optimizedReverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;reversed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;parseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&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="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&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;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;reverse&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="mi"&gt;31&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;reversed&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sign&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;optimizedReverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;345&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// output =&amp;gt; -543 &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Done. &lt;/p&gt;

&lt;p&gt;We achieved a more optimized solution. With less variable declarations, we save memory during compilation of the code because in JavaScript each variable receives its own space in memory. So, less variables means more free space in memory. &lt;/p&gt;

&lt;p&gt;We also save time because the JavaScript engine only has to compile over a few lines of code instead of 15+. This is major in the grand scheme of things. If we can save a few lines of code in each file of a project, we ultimately save a whole lot of time and memory. &lt;em&gt;We want to be efficient and optimized everywhere possible.&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Summary + Recap&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Thank you for coding along with me. Before you leave let's look back on what this challenge signified: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use console.log() often in your code to check the value of things.&lt;/li&gt;
&lt;li&gt;Use 'typeof' before a variable to figure out what data type you are working with. &lt;/li&gt;
&lt;li&gt;Clean up your code where you can. &lt;/li&gt;
&lt;li&gt;Too many variable declarations =&amp;gt; slower code.&lt;/li&gt;
&lt;li&gt;Too many variable declarations =&amp;gt; more memory space is being used up. &lt;/li&gt;
&lt;li&gt;JavaScript has a plethora of great built-in methods: use them!&lt;/li&gt;
&lt;li&gt;Optimized means your code uses up less space in memory and takes less time to run. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Remember&lt;/strong&gt;: code in the way that is best for you. If the optimized solution is confusing and not clear, then don't use it. Keep practicing coding by breaking down your code like we did in the brute force attempt. As you learn, the optimized solution will begin to make sense. Take your time. &lt;/p&gt;




&lt;p&gt;Have any questions? Comments? Or suggestions? Leave a comment below :) Feel free to follow me + connect with me on LinkedIn. &lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How To: Build a Linked List in JavaScript Part 3</title>
      <dc:creator>Adriana DiPietro</dc:creator>
      <pubDate>Tue, 25 Jan 2022 21:10:45 +0000</pubDate>
      <link>https://dev.to/am20dipi/how-to-build-a-linked-list-in-javascript-part-3-77m</link>
      <guid>https://dev.to/am20dipi/how-to-build-a-linked-list-in-javascript-part-3-77m</guid>
      <description>&lt;p&gt;🖤 Hi Programmers! 🐱&lt;/p&gt;

&lt;p&gt;Today we will be walking through the third installment of the series on &lt;strong&gt;How To: Build A Linked List&lt;/strong&gt;. Here are the links to the first two installments: &lt;a href="https://dev.to/am20dipi/how-to-build-a-linked-list-in-javascript-4kn1"&gt;1&lt;/a&gt; and &lt;a href="https://dev.to/am20dipi/how-to-build-a-linked-list-in-javascript-part-2-9ec"&gt;2&lt;/a&gt;. Please feel free to read those if you haven't or reread to refresh your mind. &lt;/p&gt;

&lt;p&gt;We will be focusing on how to add insertion functionality via an insert() and traverse() method to our class LinkedList.&lt;/p&gt;

&lt;p&gt;These two methods are definitely more challenging than the previous but together we are going to make them fully comprehensible and readable. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Let's get started!&lt;/em&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Goals
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; Mapping Out insert()&lt;br&gt;
&lt;strong&gt;2.&lt;/strong&gt; Checking Parameters&lt;br&gt;
&lt;strong&gt;3.&lt;/strong&gt; Creating a New Node&lt;br&gt;
&lt;strong&gt;4.&lt;/strong&gt; Build traverse()&lt;br&gt;
&lt;strong&gt;5.&lt;/strong&gt; Traverse the Linked List&lt;br&gt;
&lt;strong&gt;6.&lt;/strong&gt; Recap + Summary&lt;/p&gt;


&lt;h2&gt;
  
  
  Mapping Out insert()&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Our insert() method is going to have 'index' and 'value' parameters. We need both parameters, because unlike append() or prepend() that produce a value at a fixed spot in the linked list, insert() will insert the value at any specified index. &lt;/p&gt;

&lt;p&gt;Therefore, inserting a node at a specific index is a lot more complicated than just appending or prepending. Let's consider what we would need to do in order to successfully insert a new node:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; Check the parameters for edge cases. &lt;br&gt;
&lt;strong&gt;2.&lt;/strong&gt; Create a new node. &lt;br&gt;
&lt;strong&gt;3.&lt;/strong&gt; Traverse through the existing nodes in the linked list until we get to the specific index we pass a parameter.&lt;br&gt;
&lt;strong&gt;4.&lt;/strong&gt; Update the 'next' property of the node that comes before the new node; set it to the new node object. &lt;br&gt;
&lt;strong&gt;5.&lt;/strong&gt; Update the new node's 'next' property.&lt;br&gt;
&lt;strong&gt;8.&lt;/strong&gt; Increase the length of the linked list. &lt;/p&gt;

&lt;p&gt;Whoa -- this is a lot. But &lt;strong&gt;we can do it&lt;/strong&gt; don't worry. &lt;/p&gt;


&lt;h2&gt;
  
  
  Checking Parameters&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;What happens if we call &lt;code&gt;insert(1000, 4)&lt;/code&gt; -- inserting the value of 4 at the index of 1000 -- on our instance of LinkedList, but our instance only has five (5) nodes? Or what happens if we call &lt;code&gt;insert(0, 999)&lt;/code&gt;? &lt;/p&gt;

&lt;p&gt;Realistically, we could still follow through with insert(), but that complicates things for no reason. If our index is greater than or equal to our instance of LinkedList's length, we should just append it using the append() method we created. The same thing goes if we want to insert a value at the index of 0. Since the index of 0 always represents our first node in the linked list we can prepend the node using prepend(). &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Checking parameters is a great thing to think about and implement when coding. It shows that we consider edge cases and we think a little outside of the box.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Here is what the code would look like checking the index parameter:&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="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;prepend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Creating a New Node&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Now, let's create a new node using the ES6 object syntax. This is nothing new if you have been following along with the series:&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="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;prepend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
     &lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We declare an object 'newNode' and set its 'value' property to the value we pass into insert() and we set its 'next' property to null.&lt;/p&gt;




&lt;h2&gt;
  
  
  Build traverse()&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;What is &lt;strong&gt;traversal&lt;/strong&gt;? You may not have heard of it before. Do you recognize the terms "looping" or "iterating"? They are all &lt;em&gt;somewhat&lt;/em&gt; interchangeable. Just think of traversing through a linked list as stepping on stones on a path: you start at the first stone and continue to step onto each stone (one at a time) until you reach the last stone. &lt;/p&gt;

&lt;p&gt;We need to traverse through the linked list because our instance of class LinkedList is &lt;strong&gt;unidirectional&lt;/strong&gt;. Like reading a sentence, going left to right. &lt;/p&gt;

&lt;p&gt;In our traverse() method, we will pass it a parameter of 'index':&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="nf"&gt;traverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we want to traverse the list &lt;strong&gt;until&lt;/strong&gt; we get to the index we passed in. We can achieve this using a &lt;strong&gt;while loop&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="nf"&gt;traverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;currentNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;
  &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
   &lt;span class="c1"&gt;// do something here&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;We declare and assign a 'counter' variable to 0. &lt;/li&gt;
&lt;li&gt;We declare and assign a 'currentNode' variable to our linked list's head node - because we want to start at the beginning. &lt;/li&gt;
&lt;li&gt;While the counter does NOT equal our index -- keep executing the code block in the while loop until the counter does equal our index. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So what should we do to our currentNode as long as the counter does not equal its index?&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="nf"&gt;traverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;currentNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;
  &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;currentNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;currentNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;
    &lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="o"&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;currentNode&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;While the counter does NOT equal our index, reassign the value of the currentNode to the currentNode's 'next' property AND increment the counter. &lt;/li&gt;
&lt;li&gt;By doing this, we are able to skip through from one node to the next. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We continue through the linked list, stopping at each node to check its index. When the counter &lt;em&gt;finally&lt;/em&gt; equals the value of the index, the while loop will stop executing and we will be at the index we passed in (via returning the currentNode). &lt;/p&gt;




&lt;h2&gt;
  
  
  Traverse the Linked List&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;With our parameters checked, our new node created, and a working traverse() method, we can now do a few things like:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; Update the 'next' property of the node that comes &lt;strong&gt;before&lt;/strong&gt; the new node; set it to the new node object. &lt;br&gt;
&lt;strong&gt;2.&lt;/strong&gt; Update the new node's 'next' property.&lt;/p&gt;

&lt;p&gt;How can we do this? We use traverse() to arrive at the node that comes before it. &lt;/p&gt;

&lt;p&gt;Lexically, the node that comes before our index has an index of 'index - 1':&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="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;prepend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
     &lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;beforeNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;traverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we have decided to store the index of the node that comes before our inserted node in a constant 'beforeNode' for reference and later use. &lt;/p&gt;

&lt;p&gt;Now, we can grab the beforeNode's next property and store it also in a constant for reference and memory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;beforeNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;traverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;beforePointer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;beforeNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Then, let's update the 'next' value of beforeNode and set it to the newNode object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;beforeNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;traverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;beforePointer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;beforeNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;
&lt;span class="nx"&gt;beforeNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Right now our newNode's 'next' value is 'null'. Yet, we want it to point to the node that the beforeNode &lt;em&gt;used to point to...&lt;/em&gt; Good thing we stored its value in memory as a constant!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;beforeNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;traverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;beforePointer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;beforeNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;
&lt;span class="nx"&gt;beforeNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt;
&lt;span class="nx"&gt;newNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;beforePointer&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, very quickly and very suddenly we have achieved a few things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We inserted the newNode into the linked list at the index we passed in as a parameter.&lt;/li&gt;
&lt;li&gt;We were able to do so because we updated the beforeNode's 'next' property and updated newNode's 'next' property. &lt;/li&gt;
&lt;li&gt;We shifted the indices of all pre-existing nodes that came after the newNode.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, we just have to increment the length because our instance of LinkedList is now longer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LinkedList&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;pointer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nf"&gt;prepend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nf"&gt;traverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;currentNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;
      &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nx"&gt;currentNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;currentNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;
        &lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="o"&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;currentNode&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;prepend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;beforeNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;traverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;beforePointer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;beforeNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;
      &lt;span class="nx"&gt;beforeNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt;
      &lt;span class="nx"&gt;newNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;beforePointer&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Recap + Summary &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;That was a lot! But, we now have an even more functional Class LinkedList built in JavaScript. We are getting to a point where our code provides functionality to instances instantiated from the class. A functional linked list is great for efficient coding, an introduction to Trees, and predictable data rendering.&lt;/p&gt;

&lt;p&gt;For the next part in the series, I want to focus on traversing linked lists in order to remove a node in a specific location on the linked list.&lt;/p&gt;

&lt;p&gt;Stay tuned! And thank you for reading + coding along with me :)&lt;/p&gt;

&lt;p&gt;🖤🖤🖤&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>beginners</category>
      <category>algos</category>
    </item>
    <item>
      <title>How To: Build a Linked List in JavaScript Part 2</title>
      <dc:creator>Adriana DiPietro</dc:creator>
      <pubDate>Sun, 16 Jan 2022 03:05:34 +0000</pubDate>
      <link>https://dev.to/am20dipi/how-to-build-a-linked-list-in-javascript-part-2-9ec</link>
      <guid>https://dev.to/am20dipi/how-to-build-a-linked-list-in-javascript-part-2-9ec</guid>
      <description>&lt;p&gt;Hi everyone! Today we will continuing our exploration into Linked Lists by building a few methods into our Class LinkedList from &lt;a href="https://dev.to/am20dipi/how-to-build-a-linked-list-in-javascript-4kn1"&gt;this&lt;/a&gt; previous lesson. &lt;/p&gt;

&lt;p&gt;Let's get started! &lt;/p&gt;




&lt;h3&gt;
  
  
  Goals
&lt;/h3&gt;

&lt;p&gt;Here are our goals for today:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Build append()&lt;/li&gt;
&lt;li&gt;Build prepend()&lt;/li&gt;
&lt;li&gt;Recap + Summary&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By implementing these methods, we will expand the utilization and effectiveness of our Class LinkedList and each instance instantiated thereafter. &lt;/p&gt;




&lt;h2&gt;
  
  
  Build append()&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;First off, let's figure out what 'append' means. Append means to &lt;strong&gt;add&lt;/strong&gt; a new node at the &lt;strong&gt;end&lt;/strong&gt; of the linked list. &lt;/p&gt;

&lt;p&gt;Therefore, to add something to the end of the linked list, we need to do a few things:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; Pass in a value to append() as a parameter.&lt;br&gt;
&lt;strong&gt;2.&lt;/strong&gt; Create a new node constant.&lt;br&gt;
&lt;strong&gt;3.&lt;/strong&gt; Set the new node's &lt;strong&gt;value&lt;/strong&gt; property.&lt;br&gt;
&lt;strong&gt;4.&lt;/strong&gt; Set the new node's &lt;strong&gt;next&lt;/strong&gt; property.&lt;br&gt;
&lt;strong&gt;5.&lt;/strong&gt; Update the &lt;strong&gt;tail&lt;/strong&gt; of our instance of LinkedList.&lt;br&gt;
&lt;strong&gt;6.&lt;/strong&gt; Increment the length of our instance of LinkedList.&lt;/p&gt;

&lt;p&gt;So, underneath our constructor method, let's create an append() method and pass in 'value':&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="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;


&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How do we create a new node? We can declare and assign a variable called "newNode" and establish the properties that make up a node (a "value" and a "next"):&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="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The "value" property is going to be set to the value we pass in as a parameter. The "next" property will be set to null. &lt;/p&gt;

&lt;p&gt;Instead of the tail pointing to null, we are now pointing to the newNode we created.&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="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We also want establish that our appended newNode is now the tail itself:&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="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt;
   &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, we want to increment (or increase) the length to accommodate for the newly created appended node:&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="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt;
   &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt;
   &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Remember: "this" represents the instance of Class LinkedList.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's test this out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myLinkedList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;LinkedList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;myLinkedList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;myLinkedList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our console, we should receive something like this: &lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Our head node is made up of a 'value' of 10 and a 'next' of the appended 5. &lt;/li&gt;
&lt;li&gt;Our second node has a value of 5 and a 'next' of the appended 7.
&lt;/li&gt;
&lt;li&gt;Our tail node has a value of 7 and a 'next' of null (because it is the last node in the list). &lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Build prepend()&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;"Prepend", similarly to "append" adds a new node to a linked list. However, prepend adds the node to the &lt;strong&gt;beginning&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Having built append(), we can do something very similar for prepend():&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; Pass in a value to prepend() as a parameter.&lt;br&gt;
&lt;strong&gt;2.&lt;/strong&gt; Create a new node constant.&lt;br&gt;
&lt;strong&gt;3.&lt;/strong&gt; Set the new node's &lt;strong&gt;value&lt;/strong&gt; property.&lt;br&gt;
&lt;strong&gt;4.&lt;/strong&gt; Set the new node's &lt;strong&gt;next&lt;/strong&gt; property.&lt;br&gt;
&lt;strong&gt;5&lt;/strong&gt;. Update the &lt;strong&gt;head&lt;/strong&gt; of our instance of LinkedList.&lt;br&gt;
&lt;strong&gt;6.&lt;/strong&gt; Increment the length of our instance of LinkedList.&lt;/p&gt;

&lt;p&gt;Beneath our append() method, let's create a prepend() method and pass in "value". We are also going to declare a new constant "newNode" with its "value" property set to the value we 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="nf"&gt;prepend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;___&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since this new node is going to the &lt;strong&gt;beginning&lt;/strong&gt; of the linked list, its "next" value must be the &lt;em&gt;previous&lt;/em&gt; head node of the linked list:&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="nf"&gt;prepend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, we have to set the head of our instance of LinkedList to this newly created node AND increment the length:&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="nf"&gt;prepend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt;
   &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we run an example, like this, in our console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;LinkedList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;myLinkedList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;prepend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;44&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We should get returned something like this:&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Our head node is made up of the prepended 'value' of 44 and a 'next' of the node with a value of 10 and a next of null. &lt;/li&gt;
&lt;li&gt;Our tail node has a 'value' of 10 and a 'next' of null (because it is the last node in the list).&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Recap + Summary &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Here we now have a Class LinkedList built in JavaScript! We are getting to a point where our code provides functionality to instances instantiated from the class. A functional linked list is great for efficient coding, an introduction to Trees, and predictable data rendering. &lt;/p&gt;

&lt;p&gt;For the next part in the series, I want to focus on traversing linked lists in order to &lt;strong&gt;remove&lt;/strong&gt; and &lt;strong&gt;insert&lt;/strong&gt; a node in a specific location on the linked list. &lt;/p&gt;

&lt;p&gt;Stay tuned! And thank you for reading + coding along with me :)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>algorithms</category>
      <category>datastructures</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How To: Build a Linked List in JavaScript</title>
      <dc:creator>Adriana DiPietro</dc:creator>
      <pubDate>Mon, 03 Jan 2022 19:16:11 +0000</pubDate>
      <link>https://dev.to/am20dipi/how-to-build-a-linked-list-in-javascript-4kn1</link>
      <guid>https://dev.to/am20dipi/how-to-build-a-linked-list-in-javascript-4kn1</guid>
      <description>&lt;p&gt;☁️ Hi Programmers! 🐱&lt;/p&gt;

&lt;p&gt;Today we will be building a Linked List from scratch using JavaScript. If you are not familiar with Linked Lists, please head over to &lt;a href="https://dev.to/am20dipi/what-is-a-linked-list-4p1d"&gt;this&lt;/a&gt; article first and then join us after :)&lt;/p&gt;

&lt;p&gt;Let's get started. &lt;/p&gt;




&lt;h3&gt;
  
  
  Goals
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Classes in JavaScript&lt;/li&gt;
&lt;li&gt;Class Syntax &amp;amp; the Constructor&lt;/li&gt;
&lt;li&gt;Declaring the LinkedList Class&lt;/li&gt;
&lt;li&gt;Building LinkedList's Constructor Method&lt;/li&gt;
&lt;li&gt;Instantiating a New Instance of LinkedList&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Classes in JavaScript &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;JavaScript actually does not have linked lists built in.&lt;/strong&gt; However, JavaScript, being as powerful and flexible as it is, is capable of building linked lists using ES6 &lt;strong&gt;class&lt;/strong&gt; syntax. &lt;/p&gt;

&lt;p&gt;As you may or may not know, JavaScript classes are &lt;strong&gt;syntactic sugar&lt;/strong&gt;: meaning classes are not necessarily something &lt;em&gt;brand new&lt;/em&gt;. Classes should be thought of as a &lt;strong&gt;blueprint&lt;/strong&gt; for a JavaScript object. So, when you hear the term "syntactic sugar," think Classes make the code cleaner, more accessible and more readable. &lt;/p&gt;

&lt;p&gt;Since classes are "blueprints," classes do a lot of describing. Classes will describe what the object should do (methods) and what the object has (properties). &lt;/p&gt;

&lt;p&gt;Classes are sweet (like &lt;em&gt;syntactic sugar&lt;/em&gt;) because each instance of the JavaScript object created out of the class will have &lt;strong&gt;the same properties and methods!&lt;/strong&gt; This is amazing. This reduces a whole lot of code needed to be written and needed to be considered. &lt;/p&gt;

&lt;p&gt;Ok - you may have guessed by now... today we will be using Class syntax to build our Linked List!&lt;/p&gt;




&lt;h2&gt;
  
  
  Class Syntax &amp;amp; the Constructor() &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;One way to define a class is using a &lt;strong&gt;class declaration&lt;/strong&gt;. To declare a class, you use the &lt;strong&gt;class keyword&lt;/strong&gt; with the name of the class.&lt;/p&gt;

&lt;p&gt;Here is an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Post&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it! That's a class declaration. Now, there are some important AND essential aspects we need to include in our class to make it functional as a blueprint of an object: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;constructor method&lt;/li&gt;
&lt;li&gt;properties&lt;/li&gt;
&lt;li&gt;methods&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While the first one may be unrecognizable to you, both &lt;strong&gt;properties&lt;/strong&gt; and &lt;strong&gt;methods&lt;/strong&gt; should be familiar since JavaScript objects have both properties and methods. &lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;constructor method&lt;/strong&gt; is used to create a new instance of our class. (THINK: The constructor method &lt;em&gt;constructs&lt;/em&gt; a new instance.) While instances of a class can be instantiated with no properties or methods, that would be silly. Classes are amazing for just that! So, the constructor method holds the class's properties and methods and when a new instance is created by the constructor method, that instance has access to those properties and methods.&lt;/p&gt;

&lt;p&gt;Let's see this in our example class "Post":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Post&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;author&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;author&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;author&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;created_at&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An instance of our Post class when instantiated will have a title property, an author property and a created_at property. And every instance of Post instantiated from here on out will too!&lt;/p&gt;

&lt;p&gt;Now we are familiar with class syntax and the functionality of a constructor method, let's start this linked list build!&lt;/p&gt;




&lt;h2&gt;
  
  
  Declaring the LinkedList Class &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Knowing what we know, let's declare a class called "LinkedList" since that is what we are building:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LinkedList&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Class names will start with a capital letter and designate a new word by using another capital letter. No spaces!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Building LinkedList's Constructor Method &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Onto the constructor method, we need each instance of a LinkedList to have specific things that are characteristic of a linked list:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; Head (consisting of data, pointer)&lt;br&gt;
&lt;strong&gt;2.&lt;/strong&gt; Tail (consisting of data, pointer)&lt;br&gt;
&lt;strong&gt;3.&lt;/strong&gt; Length&lt;/p&gt;

&lt;p&gt;Every linked list has a &lt;strong&gt;head&lt;/strong&gt; representing the first node; a &lt;strong&gt;tail&lt;/strong&gt; representing the last node; and a &lt;strong&gt;length&lt;/strong&gt; representing how many nodes are in the linked list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LinkedList&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;&lt;strong&gt;This&lt;/strong&gt; keyword refers to the instance instantiated.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Since both the head and the tail are nodes, and we know that &lt;strong&gt;nodes consist of both data and a pointer to the next node&lt;/strong&gt;, we need to show that in the constructor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LinkedList&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
            &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;___&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;pointer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;___&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
            &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;___&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;pointer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;___&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;___&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By passing in "data" into the constructor, we can set a node's data to the data passed 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="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;pointer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;___&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Regarding the pointer of the head node, its value will be &lt;em&gt;null&lt;/em&gt;. &lt;strong&gt;The reason is that when we instantiate a new instance of a LinkedList, there is only one node&lt;/strong&gt; AND the tail node &lt;strong&gt;always points to null&lt;/strong&gt;! &lt;/p&gt;

&lt;p&gt;Therefore, technically, our tail node is our head node and our length is 1.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LinkedList&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;pointer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Instantiating a New Instance of LinkedList &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Let's see this in action. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; In your console, paste the above code snippet from the above section and click 'enter'. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.&lt;/strong&gt; Then declare and assign a new instance of LinkedList to a constant called 'newLinkedList' and pass a string ('I love data structures') to LinkedList -- this string represents our data. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.&lt;/strong&gt; Call 'newLinkedList'. &lt;/p&gt;

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

&lt;p&gt;It works! If we expand 'newLinkedList', we should see the head, the tail and the length: &lt;/p&gt;

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

&lt;p&gt;Nice! &lt;/p&gt;




&lt;h2&gt;
  
  
  Recap + Summary &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;You may notice our instance of LinkedList only has one (1) node. That is true. I plan on writing another blog soon focusing on how to append, prepend and delete nodes from a linked list. So, stay tuned. &lt;/p&gt;

&lt;p&gt;Today's linked list is a simple one. But it is a great starting point for understanding the foundation of a what a linked list is and also understanding how powerful JavaScript is. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;So, let's continue to learn together and soon enough, we can teach others.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;As always, please feel free to comment, ask questions or make suggestions. &lt;/p&gt;

&lt;p&gt;Follow for more articles and befriend me on LinkedIn too :) &lt;/p&gt;

&lt;p&gt;🤍🤍🤍&lt;br&gt;
🐱🐱🐱&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>algorithms</category>
      <category>datastructures</category>
      <category>beginners</category>
    </item>
    <item>
      <title>A Quick Guide To: jQuery</title>
      <dc:creator>Adriana DiPietro</dc:creator>
      <pubDate>Wed, 29 Dec 2021 04:10:20 +0000</pubDate>
      <link>https://dev.to/am20dipi/a-quick-guide-to-jquery-44l8</link>
      <guid>https://dev.to/am20dipi/a-quick-guide-to-jquery-44l8</guid>
      <description>&lt;p&gt;&lt;strong&gt;Hi Programmers!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Today we will be discussing and exploring jQuery! Have you ever heard of it? Whether you said yes or no, you have arrived at the right article because we will be going through the fundamentals of jQuery and deciding whether or not we should use it. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's get started.&lt;/strong&gt;   &lt;/p&gt;




&lt;h2&gt;
  
  
  Outline
&lt;/h2&gt;

&lt;p&gt;We will be breaking jQuery down into a few components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is jQuery?&lt;/li&gt;
&lt;li&gt;Syntax&lt;/li&gt;
&lt;li&gt;Selectors&lt;/li&gt;
&lt;li&gt;Attributes&lt;/li&gt;
&lt;li&gt;Methods&lt;/li&gt;
&lt;li&gt;Should We Use jQuery?&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  What is jQuery? &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;jQuery is a fast, small, feature-filled JavaScript library. It makes things like HTML manipulation, CSS manipulation and event handling simple and readable. &lt;/p&gt;

&lt;p&gt;jQuery is used alongside JavaScript and can be downloaded from &lt;a href="//www.jquery.com"&gt;here&lt;/a&gt; OR you can include the script tag below in the head section of your application's HTML file:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;script src=”https://code.jquery.com/jquery-3.1.1.js”&amp;gt;&amp;lt;/script&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Notice how the source attribute has a &lt;strong&gt;.js&lt;/strong&gt; extension!&lt;/em&gt;&lt;/p&gt;




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

&lt;p&gt;Here is the base formula for jQuery syntax:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$("selector").action()&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;$&lt;/strong&gt; represents access to jQuery.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;selector&lt;/strong&gt; finds a part of HTML either by class name, id or HTML element type.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;action()&lt;/strong&gt; is the &lt;em&gt;action&lt;/em&gt; performed on the part of the HTML. Thus, the action() is a jQuery method. &lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Selectors &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Like I mentioned previously, the selector finds a part of the HTML based on either &lt;strong&gt;class name, id or HTML element type&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Let's take a look at some examples:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$("div") // =&amp;gt; selects all div elements&lt;/code&gt;&lt;br&gt;
&lt;code&gt;$(".menu") // =&amp;gt; selects element with class name of "menu"&lt;/code&gt;&lt;br&gt;
&lt;code&gt;$("#header") // =&amp;gt; selects element with id of "header"&lt;/code&gt;&lt;br&gt;
&lt;code&gt;$("form.search") // =&amp;gt; selects &amp;lt;form&amp;gt; element with class name of "search"&lt;/code&gt;&lt;br&gt;
&lt;code&gt;$("p:first") // =&amp;gt; selects the first &amp;lt;p&amp;gt; element&lt;/code&gt;&lt;br&gt;
&lt;code&gt;$(“div p”) =&amp;gt; selects all &amp;lt;p&amp;gt; elements that are children of a &amp;lt;div&amp;gt; element&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The main purpose of the selector is to retrieve an HTML element from the HTML file to be referenced or manipulated in the JavaScript file. &lt;/p&gt;

&lt;p&gt;If you are familiar with JavaScript, then you may realize this is not too different than using &lt;code&gt;document.querySelector()&lt;/code&gt; or &lt;code&gt;document.getElementById()&lt;/code&gt;. If you realized this, good job! You are making connections and that is amazing. &lt;/p&gt;


&lt;h2&gt;
  
  
  Attributes &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Since jQuery helps to manipulate both HTML and CSS, jQuery uses various methods to access attributes of an HTML element to make these changes. &lt;/p&gt;

&lt;p&gt;The most common attributes we may see include &lt;strong&gt;href&lt;/strong&gt;, &lt;strong&gt;src&lt;/strong&gt;, &lt;strong&gt;class&lt;/strong&gt;, &lt;strong&gt;styles&lt;/strong&gt;, &lt;strong&gt;id&lt;/strong&gt; etc. And using jQuery, we can add, remove or edit these attributes. &lt;/p&gt;

&lt;p&gt;This is how we would go about adding a &lt;strong&gt;href&lt;/strong&gt; attribute to an 'a' tag:&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="nf"&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;a&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;attr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;href&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;www.google.com&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;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;a&lt;/strong&gt; represents the 'a' tag; the selector. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;.attr()&lt;/strong&gt; is the method to set an attribute; it takes in two (2) parameters: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;the type of attribute&lt;/li&gt;
&lt;li&gt;the value of the attribute&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;"href" is the type of attribute and "&lt;a href="http://www.google.com"&gt;www.google.com&lt;/a&gt;" is the value. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now if we were to inspect our 'a' tag we would see something like this: &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;         `&amp;lt;a href="www.google.com"&amp;gt;&amp;lt;/a&amp;gt;`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;To remove an attribute, we can use &lt;strong&gt;removeAttr()&lt;/strong&gt; which takes in the type of attribute as a parameter. &lt;/p&gt;

&lt;p&gt;Let's put what we have learned so far together and try it out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`
  &amp;lt;input id="input"
  placeholder="search your saved items"&amp;gt;
`&lt;/span&gt;
&lt;span class="c1"&gt;// add a class attribute&lt;/span&gt;
&lt;span class="nf"&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;input&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;attr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;class&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;search&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// remove the id attribute&lt;/span&gt;
&lt;span class="nf"&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;input&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;removeAttr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;id&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;Try this out in your console! What do you get? Something like this?&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;input class="search" placeholder="search your saved items"&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Cool! Now we know that a big part of jQuery is the ability to access attributes of a given HTML element to be able to manipulate HTML from our JavaScript file. &lt;/p&gt;




&lt;h2&gt;
  
  
  Methods &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;So far, we have seen some methods regarding the attributes of HTML elements. However, there are &lt;strong&gt;so many more&lt;/strong&gt; to discover. Today let's go over a few:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;.html()&lt;/strong&gt; method is used to retrieve OR change the content of the selected element, including the HTML markup. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;.text()&lt;/strong&gt; method is used to retrieve OR change the text content of the selected element.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The &lt;strong&gt;.css()&lt;/strong&gt; method can be used to get and set CSS properties. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To set multiple CSS properties, the .css() method uses JSON syntax. &lt;/li&gt;
&lt;li&gt;Example: $(“p”).css({“color”:”red”, “font-size”:”16px”})&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;.val()&lt;/strong&gt; method allows us to get AND set the values of form fields, such as textboxes, dropdowns and inputs.  &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;.append()&lt;/strong&gt; method inserts content at the end of the selected element(s). &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;.prepend()&lt;/strong&gt; method inserts content at the beginning of the selected element(s). &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;.after()&lt;/strong&gt; method inserts content with HTML markup after the selected element(s). &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;.before()&lt;/strong&gt; method inserts content before with HTML markup  the selected element(s). &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;The &lt;strong&gt;.addClass()&lt;/strong&gt; method adds a class to the element called on.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When specifying multiple class names, separate them using spaces. &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;.removeClass()&lt;/strong&gt; method removes the class of the element called on.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;.toggleClass()&lt;/strong&gt; method toggles between adding and removing classes from selected elements. If the specified class exists already, it is then removed. If the specified class does not exist, it is added. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;.width()&lt;/strong&gt; and &lt;strong&gt;.height()&lt;/strong&gt; methods can be used to get and set the width and height of HTML elements. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;** I recommend you try a few of these on your own. Then, I recommend finding other methods used with jQuery and trying those as well. ** &lt;/p&gt;




&lt;h2&gt;
  
  
  Should We Use jQuery? &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Now, this may be a VERY simple and understated guide to jQuery, but it is &lt;strong&gt;important&lt;/strong&gt;! We need to understand the base fundamentals of certain languages, libraries, packages... to best understand how to use them, why we use them AND if we should use them. &lt;/p&gt;

&lt;p&gt;jQuery is a great library to use if you want to DRY your code and work on readability. It is also a great tool that shows the bridge between multiple files (.html, .css, .js), especially in beginner projects and applications. &lt;/p&gt;

&lt;p&gt;However, with the introduction to and the heavy use of JavaScript frameworks such as ReactJS, Vue.js, and AngularJS, jQuery is not &lt;em&gt;totally&lt;/em&gt; necessary anymore. Its syntax does not belong within the inner workings of these frameworks. &lt;/p&gt;

&lt;p&gt;But let's beg the question: "Should we still use it?"&lt;/p&gt;

&lt;p&gt;Yes, it is worth learning jQuery even in 2022: jQuery is used by many existing and efficiently-operating websites! While new web applications created this year may not utilize jQuery, jQuery was once one of the most widely used JavaScript libraries before the introduction to frameworks such as React, Angular and Vue.&lt;/p&gt;







&lt;p&gt;Let's continue to learn, grow and teach as we continue on our personal journeys. 🤍🤍&lt;/p&gt;

&lt;p&gt;Please feel free to leave comments, questions and suggestions below. And follow me for more content on JavaScript, ReactJS, HTML and CSS. 🤍🤍&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>html</category>
      <category>css</category>
      <category>jquery</category>
    </item>
    <item>
      <title>LeetCode WalkThru: 'First Unique Character in a String'</title>
      <dc:creator>Adriana DiPietro</dc:creator>
      <pubDate>Tue, 28 Dec 2021 21:34:06 +0000</pubDate>
      <link>https://dev.to/am20dipi/leetcode-walkthru-first-unique-character-in-a-string-1lh5</link>
      <guid>https://dev.to/am20dipi/leetcode-walkthru-first-unique-character-in-a-string-1lh5</guid>
      <description>&lt;p&gt;Hi Programmers! &lt;/p&gt;

&lt;p&gt;Today we will be solving LeetCode's 'First Unique Character in a String'. Click &lt;a href="https://leetcode.com/problems/first-unique-character-in-a-string/" rel="noopener noreferrer"&gt;here&lt;/a&gt; to pull up the challenge on your end and let's get started!&lt;/p&gt;




&lt;h2&gt;
  
  
  Instructions
&lt;/h2&gt;

&lt;p&gt;LeetCode provides these instructions: &lt;/p&gt;

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

Given a string 's', find the first non-repeating character in it and return its index. If it does not exist, return -1.

* 's' consists of only lowercase English letters.


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

&lt;/div&gt;

&lt;p&gt;From this simple set of instructions, we can figure out a few things about what this challenge is asking of us:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Input is a string&lt;/li&gt;
&lt;li&gt;Output is an integer (either the index of the non-repeating character or -1)&lt;/li&gt;
&lt;li&gt;There could be multiple non-repeating characters, but we must return the index of the first&lt;/li&gt;
&lt;li&gt;And lastly, the non-repeating character must be an a letter a-z (no numbers, symbols)&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Examples
&lt;/h2&gt;

&lt;p&gt;Let's look at some of the examples LeetCode provides:&lt;/p&gt;

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

Example 1:

Input: s = "leetcode"
Output: 0


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

&lt;/div&gt;

&lt;p&gt;Where the input is 's' assigned to the string 'leetcode', the index of 0 is returned as the output. Why? The index 0, represents the first character of the string 'l'. The letter 'l' is the first non-repeating character in the string. As you can see, there are other non-repeating characters, such as 't', 'c', 'o' and 'd'. However, we only care about the &lt;strong&gt;first&lt;/strong&gt; non-repeating character for this challenge. &lt;/p&gt;

&lt;p&gt;Let's look at example where no letters of a string are non-repeating: &lt;/p&gt;

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

Example 3:

Input: s = "aabb"
Output: -1


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

&lt;/div&gt;

&lt;p&gt;Here, we are given an output of '-1'. The string &lt;em&gt;s&lt;/em&gt; provides no non-repeating characters, so according to the instructions, we return the integer of '-1'. &lt;/p&gt;

&lt;p&gt;Seems simple enough right? Let's try to code this out. &lt;/p&gt;




&lt;h2&gt;
  
  
  Approach + Solution
&lt;/h2&gt;

&lt;p&gt;We will start by declaring a function called 'firstUniqCharacter()' which will take in 's' representing a string:&lt;/p&gt;

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

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;firstUniqCharacter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Now, we will build the foundation of an if-else statement because at this point we know &lt;strong&gt;if&lt;/strong&gt; there are no non-repeating characters, we must return '-1':&lt;/p&gt;

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

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;firstUniqCharacter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="sr"&gt;/non-repeating character exists/&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
       &lt;span class="c1"&gt;// do something here&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Cool -- the foundation is laid. Now we have to consider a few important goals:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Check to see if non-repeating character exists&lt;/li&gt;
&lt;li&gt;Retrieve the index of the non-repeating character&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Given a string, that we must comb through to check for something specific, iteration is our best bet. Let's utilize a &lt;strong&gt;for loop&lt;/strong&gt;.&lt;/p&gt;

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

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;firstUniqCharacter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
   &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="sr"&gt;/non-repeating character exists/&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
         &lt;span class="c1"&gt;// do something here&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;A for loop basically says "while i is less than our input's length, continue to iterate and execute what is in the code block." The code block is represented by what is between the curly braces of our for loop. The 'i' represents the index of each letter of the string. So we can assume that 's[i]' equates to the value at that index. Let's see this in action:&lt;/p&gt;

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

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Cats&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; 'C'&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; 'a'&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; 't'&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; 's'&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Now that we have this in mind, we can use the index of each letter of the string to our advantage.&lt;/p&gt;

&lt;p&gt;JavaScript has some great built-in methods regarding the indices of a string. For today's reasons, we will utilize &lt;code&gt;indexOf()&lt;/code&gt; and &lt;code&gt;lastIndexOf()&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf" rel="noopener noreferrer"&gt;indexOf()&lt;/a&gt; =&amp;gt; &lt;em&gt;returns the first index at which a given element can be found in the string&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf" rel="noopener noreferrer"&gt;lastIndexOf()&lt;/a&gt; =&amp;gt; &lt;em&gt;returns the last index at which a given element can be found in the string&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We know that for a character to be unique, its first index must &lt;strong&gt;equal&lt;/strong&gt; its last index -- &lt;em&gt;because it only appears once!&lt;/em&gt; Let's code this out:&lt;/p&gt;

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

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;firstUniqCharacter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
   &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lastIndexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])){&lt;/span&gt;
         &lt;span class="c1"&gt;// return the index of that unique character&lt;/span&gt;
         &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; 
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;In our if statement, our condition states "if the index of a character in the given string equals the last known index of that character, it must be unique". Then inside the code block, we return 'i' which is the index at which that unique character occurs. &lt;/p&gt;

&lt;p&gt;Try this out in your console:&lt;/p&gt;

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

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Pigeon&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;P&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lastIndexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;P&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;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0v8ods3qvxv8qyfh9qiq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0v8ods3qvxv8qyfh9qiq.png" alt="pigeon"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Nice! This checks out. &lt;/p&gt;

&lt;p&gt;We found a great solution that not only works, but is readable AND scalable.&lt;/p&gt;




&lt;h2&gt;
  
  
  Testing Our Code
&lt;/h2&gt;

&lt;p&gt;Here are some examples you can try in your console:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let s = 'yyyyyyyyyyk'&lt;br&gt;
let s = 'ello there'&lt;br&gt;
let s = 'abcdefghijklmnop'&lt;br&gt;
let s = 'aaamyyj'&lt;/code&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Recap + Summary
&lt;/h2&gt;

&lt;p&gt;Today we discovered a bunch of really great stuff that will help with our journey to becoming coding masters:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;JavaScript index begins at &lt;strong&gt;0&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;When you have to access every element in an array or string, &lt;strong&gt;iteration is your best friend&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;A **for loop **is a type of iterator. &lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;for loop&lt;/strong&gt; executes only while the condition is still true.&lt;/li&gt;
&lt;li&gt;The code block represents what will be executed on each string item while the condition is still true. &lt;/li&gt;
&lt;li&gt;Strings, like arrays in JavaScript, are indexed; each letter has its own index. &lt;/li&gt;
&lt;li&gt;JavaScript has so many built-in methods waiting to be used -- please utilize them!!!&lt;/li&gt;
&lt;li&gt;Keep your code &lt;em&gt;readable&lt;/em&gt; AND &lt;strong&gt;scalable&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Code in a way that makes sense to you&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;Write notes or comments above your lines of code explaining what the code is doing. &lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;Thank you for coding along with me :)&lt;br&gt;
Please feel free to like, follow, and share. &lt;br&gt;
Likewise, feel free to ask questions &amp;amp; leave comments or suggestions. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>algorithms</category>
      <category>datastructures</category>
      <category>beginners</category>
    </item>
    <item>
      <title>What is a Linked List?</title>
      <dc:creator>Adriana DiPietro</dc:creator>
      <pubDate>Tue, 21 Dec 2021 15:19:47 +0000</pubDate>
      <link>https://dev.to/am20dipi/what-is-a-linked-list-4p1d</link>
      <guid>https://dev.to/am20dipi/what-is-a-linked-list-4p1d</guid>
      <description>&lt;p&gt;Hello Programmers!✨ Let's learn about &lt;strong&gt;Linked Lists&lt;/strong&gt;! Today's discussion will be an introductory, informational lesson on Linked Lists, so we will be hitting a few points:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What is a Linked List?&lt;/li&gt;
&lt;li&gt;What is a Node?&lt;/li&gt;
&lt;li&gt;What is a Pointer?&lt;/li&gt;
&lt;li&gt;What does a Linked List look like?&lt;/li&gt;
&lt;li&gt;Are Linked Lists similar to Arrays?&lt;/li&gt;
&lt;li&gt;What can Linked Lists be used for?&lt;/li&gt;
&lt;li&gt;Summary + Recap&lt;/li&gt;
&lt;li&gt;Additional Resources&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  What is a Linked List?&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Linked List&lt;/strong&gt; is a series of nodes connected to each other in which each node references the next node in succession. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Node?&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;node&lt;/strong&gt; is a segment or piece of code that has its own value, properties.. etc. In Linked Lists each node has two main components: (1) data and (2) a pointer. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Pointer?&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Eternally thankful for its name, &lt;strong&gt;the pointer&lt;/strong&gt; &lt;em&gt;always points&lt;/em&gt; to the &lt;strong&gt;next&lt;/strong&gt; node of the linked list. If the pointer is NULL, then that tells us that it is the last node in the list. &lt;/p&gt;

&lt;h2&gt;
  
  
  What does a Linked List look like?&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;In the simplest of possible comparisons, a Linked List resembles a caterpillar with each segment on a caterpillar representing a node. &lt;/p&gt;

&lt;p&gt;Another fun way of thinking about a linked list is its resemblance to a conga line at party. You know -- the hilarious line of people where each member holds the shoulders of the person in front of them. We can use this comparison to think of each person's arms representing the reference to the next person, also known as &lt;strong&gt;the pointer&lt;/strong&gt;. &lt;/p&gt;

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

&lt;p&gt;Now, let's look at a Google-search generated image of a Linked List:&lt;/p&gt;

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

&lt;p&gt;In this unadorned rendering, we still manage to see the main components present: each node consisting of data and a pointer and the last node pointing to "NULL". These components are the basic foundation and thus, the main priorities to learn and fully understand as we continue to learn more about linked lists!&lt;/p&gt;

&lt;h2&gt;
  
  
  Are Linked Lists similar to Arrays?&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Unlike arrays, nodes of a linked list are not stored in a particular memory location or index. This makes for slower, more difficult traversal, insertion, deletion and lookup functionality. With an array, we can easily access an array item by index; &lt;strong&gt;linked lists do not have indices&lt;/strong&gt;.  &lt;/p&gt;

&lt;p&gt;On the other hand, like an array, linked lists are super efficient at appending or prepending nodes. Since the 'head' node and the 'tail' node have distinguishing attributes (the head having nothing pointing at it and the tail pointing to NULL) inserting an element at the front or the end is quite easy. &lt;/p&gt;

&lt;h2&gt;
  
  
  What can Linked Lists be used for?&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Like we recently discovered, Linked Lists are recognized and used because of their efficient appending and prepending. Therefore, they are great to implement stacks, queues, etc.&lt;/p&gt;

&lt;p&gt;However, let's think of an applicable, concrete example of a linked list in use on a website. We may see this in the form of an image viewer: the ability to click forward and backward through a series of images. We can also see this similarly in a music player. Another example we all probably use on a daily basis: the 'back' and 'forward' buttons in the browser.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary + Recap&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A Linked List is a series of nodes. &lt;/li&gt;
&lt;li&gt;A node is a segment containing two (2) components: data and a pointer. &lt;/li&gt;
&lt;li&gt;A pointer references the next node in succession. &lt;/li&gt;
&lt;li&gt;A Linked List must be traversed to access all nodes; meaning  the beginning (the first node) is our point of access. &lt;/li&gt;
&lt;li&gt;Linked Lists take up some memory because nodes contain both data AND pointers. &lt;/li&gt;
&lt;li&gt;Linked Lists do not have indices. &lt;/li&gt;
&lt;li&gt;The HEAD of the Linked List is the first node; nothing is pointing to the head. &lt;/li&gt;
&lt;li&gt;The TAIL of the Linked List is the last node; the tail points to NULL.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Additional Resources&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Here is a great resource to play with to understand the structure and operations of Linked Lists: &lt;a href="https://visualgo.net/en/list"&gt;VisuAlgo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And here is another solid resource to understand how Linked Lists are implemented in JavaScript:  &lt;a href="https://www.freecodecamp.org/news/implementing-a-linked-list-in-javascript/"&gt;freeCodeCamp&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;✨ Thank YOU for reading along and learning with me. Please feel free to leave any comments, questions or suggestions below. And please remember to be kind to everyone because we are ALL learning here! ✨&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>algorithms</category>
      <category>datastructures</category>
    </item>
    <item>
      <title>LeetCode WalkThru: 'Sqrt(x)' and 'Valid Perfect Square'</title>
      <dc:creator>Adriana DiPietro</dc:creator>
      <pubDate>Tue, 07 Dec 2021 19:02:36 +0000</pubDate>
      <link>https://dev.to/am20dipi/leetcode-walkthru-sqrtx-and-valid-perfect-square-3ep8</link>
      <guid>https://dev.to/am20dipi/leetcode-walkthru-sqrtx-and-valid-perfect-square-3ep8</guid>
      <description>&lt;p&gt;☁️☁️☁️&lt;br&gt;
Hi programmers + coders alike ! &lt;/p&gt;

&lt;p&gt;Today, we will be walking through two (2) LeetCode challenges: &lt;strong&gt;Sqrt(x)&lt;/strong&gt; and &lt;strong&gt;Valid Perfect Square&lt;/strong&gt;. I decided to pair these challenges together in a walkthrough because they relate to each other AND show the various ways to approach a solution. &lt;/p&gt;

&lt;p&gt;Here are the links to the challenges: &lt;a href="https://leetcode.com/problems/sqrtx/"&gt;Sqrt(x)&lt;/a&gt; and &lt;a href="https://leetcode.com/problems/valid-perfect-square/"&gt;Valid Perfect Square&lt;/a&gt;. Go ahead and pull the first challenge up on your end + let's get started :)&lt;/p&gt;


&lt;h1&gt;
  
  
  Sqrt(x)
&lt;/h1&gt;
&lt;h2&gt;
  
  
  Instructions + Examples
&lt;/h2&gt;

&lt;p&gt;Let's take a look at the instructions provided by LeetCode:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Given a non-negative integer 'x', compute and return the square root of 'x'.

Since the return type is an integer, the decimal digits are truncated, and only the integer part of the result is returned.

Note: You are not allowed to use any built-in exponent function or operator, such as pow(x, 0.5) or x ** 0.5.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the instructions, we can resolve a few things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Input is a positive integer. &lt;/li&gt;
&lt;li&gt;Output is a positive integer; decimal points must be truncated (taken off). &lt;/li&gt;
&lt;li&gt;We cannot use any exponent operator.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The instructions are pretty straight-forward. However, the truncated decimal might be something new for you. (It was for me!) Growing up, in math class we always &lt;em&gt;rounded&lt;/em&gt; decimals to the nearest whole number. For this assignment, we are going to &lt;em&gt;truncate&lt;/em&gt; the decimal.&lt;/p&gt;

&lt;p&gt;If we look to the example LeetCode provides, we can see exactly what this looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Example 2
  Input: x = 8
  Output: 2
  Explanation: The square root of 8 is 2.82842..., and since the decimal part is truncated, 2 is returned.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The square root of 8 is not rounded to the nearest whole number; instead, the decimals are just chopped off. To me, this is strange, but hey, I do not make the instructions here. &lt;/p&gt;




&lt;h2&gt;
  
  
  Approach + Solution
&lt;/h2&gt;

&lt;p&gt;Since this is a math-based question, we can consider a wide slew of built in Math methods provided in JavaScript. These built-in Math methods are both life and time savers. Here is a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math"&gt;link&lt;/a&gt; to a good chunk of Math methods. &lt;strong&gt;Bookmark or save this link!!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If we return to our goals of this challenge, we need to achieve two (2) things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Return the square root any positive integer.&lt;/li&gt;
&lt;li&gt;Return the output as a positive integer (no floats!)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To achieve the first goal, JavaScript provides 'Math.sqrt(x)'. According to the link above, this method 'returns the positive square root of the given input'. Wow! Exactly what we need. &lt;/p&gt;

&lt;p&gt;So, let's build this out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;// Starting by building the foundation of a  function that takes in a parameter of 'x'&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;mySqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="c1"&gt;// Then, declare and assign a variable to the result of Math.sqrt(x) method. &lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;squareRoot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&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;squareRoot&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point, if we were to pass in an integer of '100', we would receive an output of '10'. Nice! That works! However, if we were to pass in an integer of '200', we would receive an output of '14.142135....' Well, yea that is correct, but remember the instructions: we need to return a truncated decimal. So, we need to return solely '14'. &lt;/p&gt;

&lt;p&gt;If we continue to look at that trusty goldmine of built-in Math methods, we may come across 'Math.trunc(x)'. This method 'returns the integer portion of the given input, removing any fractional digits.' Actually perfect! Let's add this functionality to our code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Starting by building the foundation of a  function that takes in a parameter of 'x'&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;mySqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="c1"&gt;// Then, declare and assign a variable to the result of Math.sqrt(x) method. &lt;/span&gt;
  &lt;span class="c1"&gt;// Then, we wrap Math.trunc() around Math.sqrt(x).&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;squareRoot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&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;squareRoot&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Try this out in your console with an input of x = 250. &lt;/p&gt;

&lt;p&gt;Did you receive an output of '15'? If you did, good! Our code works.&lt;/p&gt;




&lt;h1&gt;
  
  
  Valid Perfect Square
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Instructions + Examples
&lt;/h2&gt;

&lt;p&gt;Onto our next challenge, let's look at the instructions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Given a positive integer 'num', write a function which returns 'True' if 'num' is a perfect square. Otherwise returns 'False'.

*Follow up: Do not use any built-in library function such as sqrt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From these instructions, we can resolve a few things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Input is a positive integer. &lt;/li&gt;
&lt;li&gt;Output is a boolean: true or false.&lt;/li&gt;
&lt;li&gt;We CANNOT use any built-in JavaScript methods, like we did in the previous challenge. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's look at the examples provided:&lt;br&gt;
&lt;/p&gt;

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

Input: num = 16
Output: true


Example 2:

Input: num = 14
Output: false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alright -- simple enough. However, the instructions &lt;em&gt;specifically&lt;/em&gt; stated to NOT use any built-in Math methods. This is our time to shine and revert back to seventh grade algebra! Let's get it. &lt;/p&gt;

&lt;h2&gt;
  
  
  Approach + Solution
&lt;/h2&gt;

&lt;p&gt;What is a &lt;em&gt;perfect&lt;/em&gt; square? It is an integer, when taken the square root of, returns a positive integer. We know a few: 4, 9, 16, 25, 100...&lt;/p&gt;

&lt;p&gt;But how do we formulate a test (ahem, our code) to figure this out? We can start by creating a formula that outputs the square root of any given positive integer. &lt;/p&gt;

&lt;p&gt;What is the math formula equivalent of the square root operator? It's the exponent of one-half. Try it out on a calculator. &lt;/p&gt;

&lt;p&gt;Using the exponent operator (**), here is what we can code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;isPerfectSquare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;squareRoot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="mf"&gt;0.5&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;squareRoot&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we pass the number 100 into the function, we should be returned the integer 10. Ok - but what if we pass in 12? We receive 3.464101.... Yes - that is the square root of 12, but remember we want to receive an output of either true or false based on num's validity as a perfect square. &lt;/p&gt;

&lt;p&gt;If we compare our two outputs: 10 and 3.464101, one is an integer and the other is a float (or decimal). &lt;/p&gt;

&lt;p&gt;We can use the remainder operator (%). If our squareRoot produces a remainder when divided by 1, then we know it is NOT a perfect square. &lt;/p&gt;

&lt;p&gt;Let's code this out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;isPerfectSquare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;squareRoot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;squareRoot&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&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;/div&gt;



&lt;p&gt;Using our ol' faithful "if" statement, we can decipher that if our squareRoot variable does NOT produce a remainder, 'num' must be a perfect square. While if it does produce a remainder, 'num' is NOT a perfect square. &lt;/p&gt;




&lt;h2&gt;
  
  
  Summary + Recap
&lt;/h2&gt;

&lt;p&gt;Within these two (2) examples, we can see the multiple approaches to solve a Math-related JavaScript question. Given specific instructions that inhibit our use of either built-in Math methods or arithmetic operators, we can resolve to do the opposite. &lt;/p&gt;

&lt;p&gt;Go ahead and try to solve each question again, except switch the rules this time. A solution is always available -- we may just have to dig a little first. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;REMEMBER&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Keep your code readable.&lt;/li&gt;
&lt;li&gt;Code in a way that makes sense to you. &lt;/li&gt;
&lt;li&gt;Approach code a few different ways to come to a solution. &lt;/li&gt;
&lt;li&gt;Ask questions. &lt;/li&gt;
&lt;li&gt;Keep coding!&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;☁️☁️☁️&lt;br&gt;
Thank you for reading + coding along with me. &lt;br&gt;
Please feel free to leave questions, suggestions or comments below. &lt;/p&gt;

&lt;p&gt;Please be kind to everyone as we are all trying to learn.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>algorithms</category>
      <category>datastructures</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Tech &amp; English Lit?</title>
      <dc:creator>Adriana DiPietro</dc:creator>
      <pubDate>Fri, 03 Dec 2021 17:29:52 +0000</pubDate>
      <link>https://dev.to/am20dipi/tech-english-lit-54mi</link>
      <guid>https://dev.to/am20dipi/tech-english-lit-54mi</guid>
      <description>&lt;h1&gt;
  
  
  &lt;em&gt;Why I Believe My English Literature Degree is Important in the World of Tech&lt;/em&gt;
&lt;/h1&gt;




&lt;h2&gt;
  
  
  My Education + Background
&lt;/h2&gt;

&lt;p&gt;I graduated from Siena College (Loudonville, New York) in May 2019. I graduated reluctantly with a degree I both loved and despised: English Literature. (I know what you are thinking: "English Literature degree??? Yikessss!" We'll get back to this later.) I love English literature and writing and reading and analyzing as much as the next book twerp. If I could have stayed in school and just continued to thumb through old, musty books, I would have. On the other end of the spectrum, the loathing came from a few aspects: undergraduate degrees eventually (and never fail to) come to a close and master degrees cost an arm, a leg, and a spine and are intensely competitive to be accepted into. So, sitting adjacently to your "Yikes!" I despised my English Literature degree. Utterly despised.&lt;/p&gt;

&lt;p&gt;It is not true that you cannot get a job with a liberal arts/humanities degree -- but it is true that having that degree does not reduce the amount of paths waiting to be considered and ultimately, chosen. Therefore, I found myself stuck in a time warp: I felt as confused and hopeless as I did going into college in September of 2015. Lost and abridged. &lt;/p&gt;

&lt;p&gt;So, now, let me reiterate: I was four years older and no closer to understanding what to do with my knowledge of Milton's "Paradise Lost", Wordsworth's "Strange fit of passion have I known", nor Shakespeare's "Richard III".&lt;/p&gt;

&lt;p&gt;Through an also reluctantly chosen gap year, I realized I needed to get out of my comfort zone and dive into something deeply unexpected and challenging. Hence: Software Engineering. Do not ask me why I chose this. I had no previous experience or exploration into the subject. I do not even remember how I came across Flatiron School's home page. I do know that it felt dangerous and it felt like something I should do. &lt;/p&gt;

&lt;p&gt;So I did it. Now, here I am. Having finished the program in just under a year, I am a Flatiron alum with a certificate in Software Engineering. &lt;/p&gt;




&lt;p&gt;I realize now that I have vaguely told you why I am writing this: "Why I Believe My English Literature Degree is Important in the World of Tech". In a sharper sense, I am writing this article because &lt;em&gt;I am too&lt;/em&gt; trying to figure out how to market my English Literature background into landing a job. &lt;/p&gt;

&lt;p&gt;I am sure there are plenty of employees in the tech world that have English Literature degrees... but let me remind you that there are approximately 7.3 million people working in tech in America (I just googled this). So, the maybe 50,000 (?) book-twerps-turned-tech-twerps may just be a rarity. I kind of want to change that. &lt;/p&gt;

&lt;p&gt;So, together, let's go through my reasons as to why I believe my English Degree will help me in tech. &lt;/p&gt;




&lt;h2&gt;
  
  
  Soft Skills That Should Not Be Deemed 'Soft'
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The ability to communicate ideas + executions (of code) to non-tech people at your job (i.e the sales dept).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The ability to enact UX flow + personal(human)touches to websites/apps. Thus, creating an UX that relates to the user and makes sense. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The ability to understand the short term + long term goals (or tasks) assigned to you. Likewise, being able to execute those goals correctly + efficiently.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The ability to introduce a conversation when a problem occurs to your team and/or how the problem could be solved.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The ability to take a overtly simple concept from a client and make it a tangible, brilliant thing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The ability to write + communicate ideas/suggestions/questions clearly through email, phone, and meetings.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The ability to comb through code (not written by you) and understand its functionality and/or purpose. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The ability to understand the difference in syntax + context.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The ability to understand the importance between prioritizing readability versus efficiency/speed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In general, interpersonal skills (aka 'soft' skills) -- critical thinking, research, analysis, empathy, problem solving...&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The ability to bring forth cultural awareness + significance to a project. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;I hope this quick article has brought you some insight. If you are in the similar circumstances as me, I would love to connect and continue the conversation. Accordingly, if you were once in the similar circumstance and now are thriving, feel free to leave your own insight below in the comments. &lt;/p&gt;

&lt;p&gt;Here is my &lt;a href="https://www.linkedin.com/in/adriana-dipietro/"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thank you for reading along :) &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>javascript</category>
      <category>html</category>
    </item>
  </channel>
</rss>
