<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Rianna Cleary</title>
    <description>The latest articles on Forem by Rianna Cleary (@rlc900).</description>
    <link>https://forem.com/rlc900</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%2F334258%2F6406e3aa-d105-4059-8417-fa4e65a7504d.jpeg</url>
      <title>Forem: Rianna Cleary</title>
      <link>https://forem.com/rlc900</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/rlc900"/>
    <language>en</language>
    <item>
      <title>Memory Management in JavaScript</title>
      <dc:creator>Rianna Cleary</dc:creator>
      <pubDate>Mon, 24 Aug 2020 17:44:04 +0000</pubDate>
      <link>https://forem.com/rlc900/memory-management-in-javascript-457d</link>
      <guid>https://forem.com/rlc900/memory-management-in-javascript-457d</guid>
      <description>&lt;p&gt;In programming, memory life cycles are almost always the same regardless of which language you are using. Memory management, however, is different between languages. There are other languages that have manual memory management methods, such as C and C++. In JavaScript though, memory is automatically allocated when data types are created, and disposes of them when they aren't needed or used anymore, hence the term &lt;em&gt;garbage collector&lt;/em&gt;. There are other languages besides JavaScript that also fall under the garbage collector term like Java, Python and Ruby.&lt;/p&gt;

&lt;p&gt;Garbage collecting is a form of automatic memory management which monitors all data types that are created and removes the ones that have become unreachable. Let's look at this snippet as an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;edmArr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;addObj&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;dubstep&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;genre&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Dubstep&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="nx"&gt;edmArr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dubstep&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;addObj&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;edmArr&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In this example, an empty array is initialized and a function that simply adds an object to that array. When we &lt;code&gt;console.log(edmArr[0])&lt;/code&gt; after the function call, the output is &lt;code&gt;{genre: 'Dubstep'}&lt;/code&gt;. Even though &lt;code&gt;dubstep&lt;/code&gt; isn't in scope, we are still able to access it through the &lt;code&gt;edmArr&lt;/code&gt;, which means that it needs to say in memory until the reference isn't there (unreachable) anymore. If we were to remove the object from the array, the value of &lt;code&gt;dubstep&lt;/code&gt; won't be needed and will be garbage collected.&lt;/p&gt;

&lt;h2&gt;
  
  
  How is garbage collection actually "collecting the garbage"?
&lt;/h2&gt;

&lt;p&gt;Garbage collection is mainly based on two algorithms, one called &lt;strong&gt;mark-and-sweep&lt;/strong&gt; and another called &lt;strong&gt;reference-counting&lt;/strong&gt;. The reference-counting algorithm reduces the problem of seeing whether an object is still needed to determine if an object has any other data types referencing it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;animal&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;

&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello, World!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Y:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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



&lt;p&gt;If you run this example, you'll see &lt;code&gt;X: 'Hello, World!' Y: {animal: 'cat'}&lt;/code&gt;. Since the value of &lt;code&gt;x&lt;/code&gt; is no longer an object, &lt;code&gt;y&lt;/code&gt; is still a reference to it, so there's no need for garbage collecting. If we were to change the value of &lt;code&gt;y&lt;/code&gt; though, the object would then be collected because it wouldn't be reachable anymore. The 'garbage' will only be collected if there are &lt;strong&gt;zero&lt;/strong&gt; references pointing to it. This collection algorithm isn't preferred in modern browsers today because it has a limitation when it comes to circular referencing. If two objects reference each other, it creates a cycle and the algorithm won't really consider them being unneeded because each object has at least one reference pointing to them. This leads to neither of them being collected as "garbage" and is a common cause to memory leaks.&lt;/p&gt;

&lt;p&gt;The mark-and-sweep algorithm reduces the definition of "an object no longer needed" to "an object that is unreachable". This algorithm finds the &lt;em&gt;root&lt;/em&gt; (global object) and then finds all the references from that root, and then the references of those, etc. It finds all the reachable objects, and collects the ones that are unreachable. This algorithm is used in browsers, other garbage collecting languages today because it handles cycles and cyclic dependencies with improvements that have been made on it over time.&lt;/p&gt;

&lt;p&gt;Here are some documentations that go more in depth into memory cycles and management. Happy coding!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management#:~:text=Some%20high%2Dlevel%20languages%2C%20such,longer%20needed%20and%20reclaim%20it."&gt;MDN Web Docs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://javascript.info/garbage-collection"&gt;JavaScript.info&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>womenintech</category>
    </item>
    <item>
      <title>Using Helper Functions in JavaScript</title>
      <dc:creator>Rianna Cleary</dc:creator>
      <pubDate>Mon, 17 Aug 2020 18:31:39 +0000</pubDate>
      <link>https://forem.com/rlc900/using-helper-functions-in-javascript-5eal</link>
      <guid>https://forem.com/rlc900/using-helper-functions-in-javascript-5eal</guid>
      <description>&lt;p&gt;In this article, I'll be explaining how to solve the anagram algorithm using helper functions in JavaScript! Utilizing helper functions in your code makes tasks less complicated to manage and makes your code more DRY (Don't Repeat Yourself). I found them very useful when I used them to tackle algorithm problems because I noticed myself trying to solve them in just one function. After watching some tutorials and searching through hundreds of stack overflow posts to learn how to come up with easier solutions, I saw several other engineers implementing helper functions in their code which helped expand my thinking process in terms of breaking down the problem into pieces. &lt;/p&gt;

&lt;p&gt;The anagram algorithm is one of the more simple ones out of most. Having said that, it's one of those problems that will trip you up if you don't put enough thought into your solution. If you're new to this algorithm, the goal is to implement a function that takes in two strings and checks to see if the input is an anagram. If yes, the function returns true, if no it returns false. Let's see how we can use helper functions to solve this algorithm.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building our Character Map
&lt;/h2&gt;

&lt;p&gt;We should first think about our edge cases in terms of what the user inputs to the function. What if there are spaces in the strings, or symbols in one string and not the next, or what if one string is in all caps and the other is lowercase? Also, how are we going to compare the strings if the letters are going to be out of order in both inputs? In other words, if you passed in the strings &lt;code&gt;stew&lt;/code&gt; and &lt;code&gt;west&lt;/code&gt; as &lt;code&gt;stringA&lt;/code&gt; and &lt;code&gt;stringB&lt;/code&gt; and had the condition &lt;code&gt;if ('stringA' === 'stringB')&lt;/code&gt; it would return false because they're two different strings. To solve this we could essentially build a helper function that returns a character map of the string, and returns an object of each letter paired with a number value. The number value equals to how many times the letter appears in the string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;buildCharacterMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;characterMap&lt;/span&gt; &lt;span class="o"&gt;=&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;char&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;[^\w]&lt;/span&gt;&lt;span class="sr"&gt;/g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="nx"&gt;characterMap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;char&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;characterMap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;char&lt;/span&gt;&lt;span class="p"&gt;]&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;1&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;characterMap&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;At the top, there's an initialized variable called &lt;code&gt;characterMap&lt;/code&gt; set equal to an empty object. Then a for-of loop is started that iterates over every character of the passed in &lt;code&gt;str&lt;/code&gt; and replaces any extra characters and white space using regex while also making the string lowercase. If you'd like to learn more about regex syntax please check out this &lt;a href="https://www.rexegg.com/regex-quickstart.html"&gt;cheatsheet&lt;/a&gt;! After that is the condition that builds our map, which sets its value to 1 or increases it if its already 1. We're halfway done with our solution!&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementing Main Function
&lt;/h2&gt;

&lt;p&gt;To write our main function, we should first convert both of the strings to character maps. If only we had a helper function that could do that for us...oh wait! We do!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;isAnagrams&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stringA&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;stringB&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;stringAMap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;buildCharMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stringA&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;stringBMap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;buildCharMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stringB&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 that we have our mapped strings, we can iterate through &lt;code&gt;stringAMap&lt;/code&gt; and compare the values from that string to &lt;code&gt;stringBMap&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;isAnagrams&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stringA&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;stringB&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;stringAMap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;buildCharMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stringA&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;stringBMap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;buildCharMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stringB&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;char&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;stringAMap&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;stringAMap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;char&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;stringBMap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;char&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;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;There is one more edge case we need to check before we could call our solution finished. What if our user adds an extra character by accident when inputting the strings? For example, what if they passed in &lt;code&gt;'rail'&lt;/code&gt; and &lt;code&gt;'rails'&lt;/code&gt;? Our function would still return true because both strings each have equal values for &lt;code&gt;'rail'&lt;/code&gt;. To fix this, we could iterate through the objects using &lt;code&gt;Object.keys()&lt;/code&gt; and compare the length of them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ...&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;aCharMap&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="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bCharMap&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="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;As stated, helper functions can really come in handy when you're tackling logical problems like these in the future, and its good practice to utilize them in your projects. Not only does it help you solve problems with a bit more ease and make your code less repetitive, but it shows other people reading it how you think and manage to solve tasks. I hope you enjoyed this tutorial and that it inspired you to see the beauty of helper functions! &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>womenintech</category>
      <category>webdev</category>
      <category>codenewbie</category>
    </item>
  </channel>
</rss>
