<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Carlie Anglemire on Medium]]></title>
        <description><![CDATA[Stories by Carlie Anglemire on Medium]]></description>
        <link>https://medium.com/@carlie.anglemire?source=rss-726f287a8b5e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*OiFOXXDFOReY4y7G.jpg</url>
            <title>Stories by Carlie Anglemire on Medium</title>
            <link>https://medium.com/@carlie.anglemire?source=rss-726f287a8b5e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 04:38:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@carlie.anglemire/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[They Have Me Pegged?]]></title>
            <link>https://medium.com/@carlie.anglemire/they-have-me-pegged-4ac122515a10?source=rss-726f287a8b5e------2</link>
            <guid isPermaLink="false">https://medium.com/p/4ac122515a10</guid>
            <dc:creator><![CDATA[Carlie Anglemire]]></dc:creator>
            <pubDate>Tue, 07 Nov 2023 20:49:05 GMT</pubDate>
            <atom:updated>2023-11-07T20:51:44.295Z</atom:updated>
            <content:encoded><![CDATA[<p>A couple of weeks ago, I came across an issue with a couple of images not rendering when I redeployed my portfolio website after making updates to it. When I was developing the React application locally, all of the images were displaying fine, so I was not anticipating a problem. After some trial and error, renaming the files ended up solving the problem.</p><p>This is how I was including one of the photos that displayed fine locally but then was lost when deployed on AWS Amplify:</p><pre>  &lt;img<br>      className=&quot;profile-photo&quot;<br>      src={require(&quot;./Images/profilePhoto.jpeg&quot;)}<br>      width={500}<br>      height={500}<br>      alt=&quot;Carlie Anglemire&quot;<br>   /&gt;</pre><p>At first, I thought maybe it would be worth trying to import it at the top of the file instead of using ‘require’ in the src. However, when I made that change, it did not fix the issue. My picture still showed up locally but did not show up when deployed.</p><p>Then, when I looked at the other photo file that was not displaying when the app was deployed, I noticed that the two shared something in common. This was the path to the other file that was not displaying:</p><pre>&quot;./Images/three-cheers.jpeg&quot;</pre><p>Although from what the internet tells me ‘jpeg’ and ‘jpg’ mean the same thing, changing the file names so that they ended in ‘.jpg’ instead of ‘.jpeg’ actually solved the problem.</p><p>I hope this is helpful for someone! I am still not sure why making that change made a difference, but I believe it is an example of how pattern-recognition can improve one’s life. Thank you, Sesame Street!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fm6keZIUJBsQ%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dm6keZIUJBsQ&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fm6keZIUJBsQ%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/11c2662190b112314dd4622bc485efff/href">https://medium.com/media/11c2662190b112314dd4622bc485efff/href</a></iframe><p>Pegs:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FgGCu0ezL4-U%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DgGCu0ezL4-U&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FgGCu0ezL4-U%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/846eb659539e43506a890b53b97ee998/href">https://medium.com/media/846eb659539e43506a890b53b97ee998/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4ac122515a10" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ordering Strings For the Good of the People]]></title>
            <link>https://medium.com/@carlie.anglemire/ordering-strings-for-the-good-of-the-people-11d36dbf52e5?source=rss-726f287a8b5e------2</link>
            <guid isPermaLink="false">https://medium.com/p/11d36dbf52e5</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[string-manipulation]]></category>
            <category><![CDATA[while-loop]]></category>
            <category><![CDATA[for-loop]]></category>
            <category><![CDATA[object-oriented]]></category>
            <dc:creator><![CDATA[Carlie Anglemire]]></dc:creator>
            <pubDate>Wed, 03 Feb 2021 20:32:32 GMT</pubDate>
            <atom:updated>2021-02-05T02:18:06.599Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2eaWxsdpGkrWoNzy8x9-iw.png" /></figure><p>During a Mock Technical Interview session yesterday, I got a chance to practice string and array manipulation, and afterward I tried optimizing the solution. The solution given was a nested for loop, and I wanted to see if I could improve the run time, even if there would be memory trade-offs.</p><p>Below is what I came up with, but if anyone else has thoughts on this, I would love to discuss!</p><h4>The Problem</h4><p>The problem asked for us to reorder an input string that had words with numbers mashed up with them. The output was supposed to order the mashed up words so that the string displayed a sentence with the numbers in order. As an example, an input string of “4you he1llo are3 h2ow” should be reordered to read “he1llo h2ow are3 4you”. Or in the example that was given &quot;4of Fo1r pe6ople g3ood th5e the2&quot; becomes “Fo1r the2 g3ood 4of th5e pe6ople”. Note that the words are not “indexed” from 0 but start at 1.</p><p>Below is my solution to the problem. Shout out to Franklin Bado (<a href="https://www.linkedin.com/in/franklin-bado/">https://www.linkedin.com/in/franklin-bado/</a> ) who I worked on the problem with yesterday.</p><pre>function sortString (str){<br></pre><pre>let positionForStrings = {}</pre><pre>let currentKey = &quot;&quot;;</pre><pre>let currentNumber = 0;<br></pre><pre>for (let i = 0; i &lt;= str.length; i++){</pre><pre>if (i === str.length){</pre><pre>positionForStrings[currentKey] = currentNumber</pre><pre>}</pre><pre>else{</pre><pre>if (str[i] === &quot; &quot;){</pre><pre>positionForStrings[currentKey] = currentNumber</pre><pre>currentKey = &quot;&quot;</pre><pre>currentNumber = 0</pre><pre>}</pre><pre>else if (parseInt(str[i])%1 == 0){</pre><pre>currentNumber = parseInt(str[i])</pre><pre>currentKey += str[i]</pre><pre>}</pre><pre>else {</pre><pre>currentKey += str[i]</pre><pre>}</pre><pre>}</pre><pre>}<br></pre><pre>let arrayOfStrings = str.split(&quot; &quot;)</pre><pre>let finalArray = []</pre><pre>let count = 1<br></pre><pre>while (count &lt;= arrayOfStrings.length){</pre><pre>for (each in positionForStrings){</pre><pre>if (positionForStrings[each] === count){</pre><pre>finalArray.push(each)</pre><pre>}</pre><pre>}</pre><pre>count+=1</pre><pre>}</pre><pre>return finalArray.join(&quot; &quot;)</pre><pre>}</pre><p>In the first loop, what I am doing is creating an object with keys of each of the words in the sentence and values of the number embedded in that word. To do this, I loop over the input string. There is a variable to keep track of the current key and a variable to set the value for that key. If the character is an empty space separating the words, then at that point, I set the key-value pair in the variable for the object that is being created, and the value for currentKey and currentValue is reset. The first condition stipulates that if we are on the last character in the string, the current key-value pair is set before the loop exits (since there is no space after it to signal that it should be set as it is for the other words).</p><p>The second condition in the wrapping else statement of the for loop checks whether the character is a number by using parseInt(str[i])%1 == 0. If the character is a letter, then that expression will evaluate to NaN, not 0. Another way to check whether it is a number would be to use Number.isInteger(). If it is a number, then it is both added to the currentKey and is set as the value of currentNumber. If the character is not a number, then it is just added to the currentKey.</p><p>After the loop exits, I split the input string to get an array of strings. This allows me to get the target length for an array that is initialized in the next line. I set up a count that starts at one, and then I begin a while loop that exits once the finalArray is filled with the target number of strings. Inside of the while loop a for loop runs to check each of the key-value pairs in the positionForStrings object to see if its value matches the current count value. If it matches, then the key is pushed into finalArray. Each of the key-value pairs is checked, and then at the end of that loop, the count is incremented by 1, so that the words are pushed into the array in order.</p><p>In the end, we end up with an array of words in the target order. Then, they are joined to form one string that is returned, to give us a nice message like “Fo1r the2 g3ood 4of th5e pe6ople”.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=11d36dbf52e5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Are We There Yet?]]></title>
            <link>https://medium.com/@carlie.anglemire/are-we-there-yet-531660d6fd41?source=rss-726f287a8b5e------2</link>
            <guid isPermaLink="false">https://medium.com/p/531660d6fd41</guid>
            <category><![CDATA[for-loop]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[recursion]]></category>
            <dc:creator><![CDATA[Carlie Anglemire]]></dc:creator>
            <pubDate>Thu, 08 Oct 2020 23:36:42 GMT</pubDate>
            <atom:updated>2020-10-08T23:36:42.511Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/630/1*DvGutinbmFnjyPmyCjL-5g.png" /></figure><p>Last week, I did a mock technical interview with someone on Pramp who suggested that I practice recursion by writing a function such that when the input is an array of nested arrays such as [[1],[[2]],[[[3]]]], it returns an array of numbers, such as [1, 2, 3]. Thanks, buddy, wherever you are! Here is what I came up with.</p><h4>The .flat() Method</h4><p>My first instinct was to use the .flat() method, but that only collapses the first layer of nesting. It would work on an array like [[1], [2], [3]], but it would not break through all of the layers of a more deeply nested array. That is why this problem is a good candidate for recursion; we have to keep calling the .flat() method on the item in an input array until it has shed all of its unnecessary layers to live a more unencumbered life…sort of like a butterfly emerging from a cocoon.</p><p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat">Array.prototype.flat() - JavaScript | MDN</a></p><h4>Solution</h4><p>Below is my solution. I will let you look over it and meet you again afterward to discuss!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FW2RrY1KhTS05G%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Ftrip-W2RrY1KhTS05G&amp;image=https%3A%2F%2Fmedia4.giphy.com%2Fmedia%2FW2RrY1KhTS05G%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="246" frameborder="0" scrolling="no"><a href="https://medium.com/media/846c7ef6af80fec7564fdaaa41805f08/href">https://medium.com/media/846c7ef6af80fec7564fdaaa41805f08/href</a></iframe><pre>function flatten_arrays (array){</pre><pre>let i;</pre><pre>let flatten_array = array.flat();</pre><pre>let length = flatten_array.length;</pre><pre>let changedElement;</pre><pre>let newArray = [];</pre><pre>for (i=0; i&lt;length; i++){</pre><pre>if(typeof flatten_array[i]=== &quot;object&quot;){</pre><pre>if (!(typeof flatten_array[i][0] === &quot;number&quot;)){</pre><pre>changedElement = flatten_array[i].flat()</pre><pre>flatten_arrays(changedElement)</pre><pre>}</pre><pre>else{</pre><pre>changedElement = flatten_array[i].flat();</pre><pre>}</pre><pre>}</pre><pre>else{</pre><pre>changedElement = flatten_array[i];</pre><pre>}</pre><pre>newArray.push(changedElement)</pre><pre>}</pre><pre>let finalArray = newArray.flat();</pre><pre>return finalArray;</pre><pre>}</pre><h4>Explanation</h4><p>This problem makes use of a for loop, and it sets up a number of variables right before it starts. First, I flatten the input array to cut through the initial barrier between us and the number we want to retrieve, and it is saved to a variable since the .flat() method is non-destructive.</p><p>The first condition in the for loop states that if the item we are currently on in the loop is of data type “object” (if you call typeof on an array, it will return “object”), then this block of code will execute. Then, there is another if statement nested inside of this one stating that the next block of code should execute if the data type of array[i][0] is not a number. I included this condition, because if the item in the array is an object, I will want to treat it in one of two ways depending on how deeply nested it is.</p><p>If it is only one layer deep, like [1], then I can just call the .flat() method on it. If flatten_array[i] is [1], and I call typeof on flatten_array[i][0], or typeof [1][0], it will return “number.” So, that is the condition I am controlling for in this step. If flatten_array[i][0] === “number” then the function will move to the else statement where changedElement is set equal to flatten_array[i].flat().</p><p>However, if flatten_array[i] is more deeply nested than that, like the poor unfortunate [[[1]]], then it will have to be given the recursion treatment. In that case changedElement is set equal to flatten_array[i].flat(), but then a recursive call is made, passing in changedElement:</p><pre>flatten_arrays(changedElement)</pre><p>This will run until .flat() cannot be called on the element anymore. The variable ‘changedElement’ will be updated at every turn to reveal a less and less nested number. Because .flat() returns an array, we still will not be down to the bare number yet, but at this point we have gotten as close as we need to.</p><p>If the current element in the array is not an object, then I am assuming for the sake of argument that it is a number and can be pushed as is into newArray. Therefore, in that case, I set changedElement equal to the element flatten_array[i] without having to transform it.</p><p>The value of the variable ‘changedElement’ is pushed into newArray before the loop moves on to the next item. Then, after the for loop is done running, finalArray is set equal to newArray.flat(). That should get rid of the final layer of nesting to produce one array of numbers.</p><p>And that is the story of how butterflies are born.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FBM5Xa1FvNQQ%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DBM5Xa1FvNQQ&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FBM5Xa1FvNQQ%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/1b532fb22289332f3e463a4883f38c59/href">https://medium.com/media/1b532fb22289332f3e463a4883f38c59/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=531660d6fd41" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Simpler Times?]]></title>
            <link>https://medium.com/@carlie.anglemire/simpler-times-c7d8818e14dd?source=rss-726f287a8b5e------2</link>
            <guid isPermaLink="false">https://medium.com/p/c7d8818e14dd</guid>
            <category><![CDATA[while-loop]]></category>
            <category><![CDATA[string-manipulation]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Carlie Anglemire]]></dc:creator>
            <pubDate>Thu, 24 Sep 2020 23:36:58 GMT</pubDate>
            <atom:updated>2020-09-25T12:44:39.293Z</atom:updated>
            <content:encoded><![CDATA[<p>This week I am going to return to simpler times — which reminds me of the beer my husband and I used to get at Trader Joe’s — and write about a string manipulation problem that I did for a Hackerrank coding challenge.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cqZJQpH4pg5at2rWQao1rQ.jpeg" /><figcaption>Not Simpler Times, but another beer I wouldn’t mind drinking right now.</figcaption></figure><h4><strong>The Problem</strong></h4><p>The instructions were to write a function that takes in a string, rotates it a total number of times equal to one less than the length of the string, then return how many resulting strings start and end with the same letter. The first string is the input string, and then the function forms additional strings by moving the first letter of the input to the end of it. For example, if the input string is “abgha”, that string is rotated four times to produce the resulting strings: “bghaa”, “ghaab”, “haabg”, and “aabgh”. The return value would be 1, because only the original string starts and ends with the same letter.</p><h4>My Solution</h4><p>Below is my solution; I am not sure if it passes every edge case, but it appears to work when I test it in my terminal. The first condition could be different depending on how you would like to handle input strings that have a length of 0 or 1. I signaled that the function should return 1 in those cases. My reasoning for that was that a string like “a” has the same first and last character. The string “” does not really have any characters, so what you would like to return in that situation might be more debatable. Like with so many other things in life it is contextual and depends what you are trying to accomplish by using this function. And as usual, I do not know what this sort of function would actually be useful for other than torturing — I mean training — emerging software engineers.</p><pre>function countStrings(s) {</pre><pre>let lengthOfString = s.length;</pre><pre>if(lengthOfString === 1 || lengthOfString === 0){</pre><pre>return 1</pre><pre>}</pre><pre>else{</pre><pre>let i = 1;</pre><pre>let count = 0;</pre><pre>if(s[0]===s[lengthOfString-1]){</pre><pre>count+=1;</pre><pre>}</pre><pre>while (i &lt; lengthOfString){</pre><pre>s= s.slice(1, lengthOfString)+s[0]</pre><pre>if(s[0]===s[lengthOfString-1]){</pre><pre>count+=1;</pre><pre>}</pre><pre>i++</pre><pre>}</pre><pre>return count;</pre><pre>}</pre><pre>}</pre><h4>Explanation</h4><p>In the first if statement, I am handling the case of input strings that have a length of 0 or 1. If the function meets that condition, it exits, returning 1. Otherwise, the next part of the function in the else statement runs. First, I set a ‘count’ variable for adding up the number of strings that start and end with the same letter. Then, the function looks at the input string; if it starts and ends with the same letter, the count goes up by one. Next, a while loop runs while the variable i is less than the length of the input string. In the while loop, the additional strings are produced using the slice method, which cuts out the slice of s from the 1 index position until the end of the string and then adds the character in the 0 index position to the end of that slice. The new string is reassigned as the value of ‘s’. Then that string is checked to see whether the first and last characters are the same. If they are, the count goes up, and the i variable is incremented. The while loop runs again until the exit condition is met. At the end the count is returned.</p><p>And that is all there is to it. Have a nice weekend!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c7d8818e14dd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Python]]></title>
            <link>https://medium.com/@carlie.anglemire/python-e6f268ea9387?source=rss-726f287a8b5e------2</link>
            <guid isPermaLink="false">https://medium.com/p/e6f268ea9387</guid>
            <category><![CDATA[for-loop]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[conditional-statements]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Carlie Anglemire]]></dc:creator>
            <pubDate>Thu, 17 Sep 2020 21:01:19 GMT</pubDate>
            <atom:updated>2020-09-18T01:17:54.792Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XHGOir-W9cxW8sDmwLUyew.jpeg" /><figcaption>S is for snake</figcaption></figure><p>This week I have been learning Python thanks to <a href="https://www.coursera.org/learn/python-crash-course?skipBrowseRedirect=true">Google’s Crash Course on Python</a> that is offered on Coursera. So far, I have found that there are a lot of similarities between Python and other languages I have worked with like JavaScript and Ruby. For this blog post, I am going to compare for loops and conditionals in JavaScript and Python to review part of what I have learned.</p><h4>For Loops and Conditionals</h4><p>Python and JavaScript both have for and while loops, and they work in a similar way, though the syntax is a little bit different.</p><p>For example, I could write a for loop in JavaScript that looks like this to check how many even numbers there are between a given starting point and ending point. The way it is written right now, it is not inclusive of the ending number.</p><pre>function checkForEvens(startingNum, endingNum){</pre><pre>  let i;</pre><pre>  let numberOfEvens = 0;</pre><pre>  for (i = startingNum; i &lt; endingNum ; i++){</pre><pre>    if (i % 2 == 0){</pre><pre>      numberOfEvens+=1;</pre><pre>    }</pre><pre>  }</pre><pre>  return numberOfEvens</pre><pre>}</pre><pre>checkForEvens(0, 10) </pre><pre>//returns 5</pre><p>The function above checks all of the numbers from the first parameter until (but not inclusive of) the second parameter. The for loop runs to check each of the values in the range of numbers and increments the count of even numbers if the number the for loop is on is divisible by 2 with no remainder.</p><p>In Python, a lot of things would be the same, but it would look different. Here is how I would write the same function in Python.</p><pre>def checkForEvens(startingNum, endingNum):</pre><pre>  numberOfEvens = 0</pre><pre>  for i in range(startingNum, endingNum):</pre><pre>    if i % 2 == 0:</pre><pre>    numberOfEvens+=1</pre><pre>    else: <br>      continue</pre><pre>  return numberOfEvens</pre><pre>checkForEvens(0, 10)</pre><pre>//returns 5</pre><p>Something that I like about Python is that we do not have to keep track of curly braces. Instead of different parts of the function being wrapped in curly braces, there are colon marks followed by indented blocks of code. I imagine that keeping track of indentations could become just as complicated, but right now it is something that I appreciate! What I learned from the Google course is that the number of times the block of code is indented does not matter as long as it is the same for each line of code in the block.</p><p>Other differences include the for loop syntax. It is understood that the for loop will increment up until the number in the second parameter but not including it. The first number sets the counter. There is a third, optional parameter that indicates a pattern for skipping through numbers. For example, instead of looping through each of the numbers in the range, we can have the loop stop at every fifth or tenth number in the interval by entering a third parameter of 5 or 10. To check every fifth number, the for loop would be set up like this:</p><pre>for i in range(startingNum, endingNum, 5):</pre><p>In the checkForEvens function, I only used an if statement in JavaScript and if else statements in Python. If I had wanted to add more conditions in JavaScript, I would have used else if statements, and in Python elif does the same thing. To break out of a loop in JavaScript before it completes its cycle, I would return some value, and the function would exit. To exit a loop in Python, you can type break in the body of the loop, and it will exit the loop without exiting the whole function. You would enter ‘break’ just as I entered ‘continue’ in the code above. Which brings me to…</p><h4>Continue</h4><p>Something else that is different in Python is the continue statement. That just means to skip the number that we are on in the loop and move on to the next one. The for loop in JavaScript above implicitly does this by only giving an if condition. If the number is not even, then the for loop will move on to the next number. The equivalent of typing out ‘continue’ in JavaScript might be having an empty else statement or something inside of the curly braces of an else statement that effectively does nothing. But why write unnecessary code? I already do that all the time inadvertently and can’t think of a good reason to do it on purpose.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F5iln6fkwJOU%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D5iln6fkwJOU&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F5iln6fkwJOU%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/235f3f4bc41fdbc0fd52acc8119fe3f5/href">https://medium.com/media/235f3f4bc41fdbc0fd52acc8119fe3f5/href</a></iframe><p><a href="https://en.wikipedia.org/wiki/Shari_Lewis">Shari Lewis - Wikipedia</a></p><p>Fun fact: once when I was little, I asked my grandmother how she chose my mother’s name, and she said she was named after Shari Lewis. I don’t know if that is true, but I hope it is!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e6f268ea9387" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Depth First Search]]></title>
            <link>https://medium.com/@carlie.anglemire/depth-first-search-782fe4e21a34?source=rss-726f287a8b5e------2</link>
            <guid isPermaLink="false">https://medium.com/p/782fe4e21a34</guid>
            <category><![CDATA[depth-first-search]]></category>
            <category><![CDATA[for-loop]]></category>
            <category><![CDATA[object-oriented-design]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[while-loop]]></category>
            <dc:creator><![CDATA[Carlie Anglemire]]></dc:creator>
            <pubDate>Thu, 10 Sep 2020 13:15:41 GMT</pubDate>
            <atom:updated>2020-09-10T13:22:00.639Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/760/1*kKuBWPzTzoleNR_G8P6H3w.jpeg" /></figure><p>In the last week, I have been working on doing breadth-first and depth-first searches of graphs. In my own life outside of coding, I use exactly neither of these methods to get to the bottom of problems, so it was a good learning opportunity! The labs that I did were on The Flatiron School’s Learn.co website, and the following function was written to pass the <a href="https://learn.co/tracks/software-engineering-post-work/computer-science/data-structures/depth-first-search-lab">Depth First Search Lab</a>.</p><h4>Vertices and Edges</h4><p>The lab prompt gives us a set of vertices and edges, vertices being objects that are meant to represent points on a graph with a ‘name’ and ‘discovered’ attribute, and edges being arrays of the names of two vertices that represent a path between them. In other words, a line could be drawn between the two vertices that form an ‘edge.’ The ‘discovered’ attribute of the vertices is set to a boolean of true, false, or null. Once the vertex has been ‘visited’ by running the function and entering the sets of vertices and edges as input, that value is set to true.</p><p>These are the edges and vertices that Learn.co gives us as example input:</p><pre><strong>let</strong> edges <strong>=</strong> [[‘14th&amp;6th’, ‘23rd&amp;6th’], [‘23rd&amp;6th’, ‘34th&amp;6th’], [‘34th&amp;6th’, ‘28th&amp;Bwy’], [‘28th&amp;Bwy’, ‘23rd&amp;Bwy’], [‘23rd&amp;Bwy’, ‘14th&amp;Lex’],[‘14th&amp;Lex’, ‘23rd&amp;Lex’]]</pre><pre><strong>let</strong> vertices <strong>=</strong> [{name: ‘34th&amp;6th’, discovered: <strong>null</strong>}, {name: ‘23rd&amp;6th’, discovered: <strong>null</strong>}, {name: ‘14th&amp;6th’, discovered: <strong>null</strong>}, {name: ‘28th&amp;Bwy’, discovered: <strong>null</strong>}, {name: ‘23rd&amp;Bwy’, discovered: <strong>null</strong>}, {name: ‘14th&amp;Lex’, discovered: <strong>null</strong>}, {name: ‘23rd&amp;Lex’, discovered: <strong>null</strong>}]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pSOyxmgvkFaAT4m0x1pGKA.png" /><figcaption>Two Paths</figcaption></figure><p>The goal of a depth-first search is to go all the way down one path at a time, rather than going down one level of each of the paths branching from a particular vertex at a time. So, this is the function that I came up with to return an array of the names of the vertices in the order that they are visited in a depth-first search.</p><h4>Solution</h4><pre>function depthFirstSearch (rootNode, vertices, edges){</pre><pre>function findAdjacentNodes(rootNode, vertices, edges){</pre><pre>let adjacentNodeNames = [];</pre><pre>let i;</pre><pre>for (i = 0; i &lt; edges.length; i++){</pre><pre>if(edges[i].includes(rootNode)){</pre><pre>if(edges[i][0] === rootNode){</pre><pre>adjacentNodeNames.push(edges[i][1])</pre><pre>}</pre><pre>else{</pre><pre>adjacentNodeNames.push(edges[i][0])</pre><pre>}</pre><pre>}</pre><pre>}</pre><pre>let adjacentNodes = [];</pre><pre>for (let t = 0; t &lt; vertices.length; t++){</pre><pre>if (adjacentNodeNames.includes(vertices[t].name)){</pre><pre>if(vertices[t].discovered === null)</pre><pre>adjacentNodes.push(vertices[t]);</pre><pre>vertices[t].discovered = true;</pre><pre>}</pre><pre>else if (vertices[t].name === rootNode){</pre><pre>vertices[t].discovered = true;</pre><pre>}</pre><pre>}</pre><pre>return adjacentNodes;</pre><pre>}</pre><pre>let theQueue = [];</pre><pre>let finalArray = [];</pre><pre>theQueue.push(rootNode);</pre><pre>finalArray.push(rootNode);</pre><pre>let i;</pre><pre>while(theQueue.length &gt; 0){</pre><pre>let arrayForQueue = findAdjacentNodes(theQueue[theQueue.length - 1].name, vertices, edges);</pre><pre>theQueue.pop();</pre><pre>for(i = 0; i &lt; arrayForQueue.length; i++){</pre><pre>theQueue.push(arrayForQueue[i]);</pre><pre>finalArray.push(arrayForQueue[i])</pre><pre>}</pre><pre>}</pre><pre>return finalArray;</pre><pre>}</pre><h4>The Nested Function</h4><p>First, there is a function nested inside of depthFirstSearch called findAdjacentNodes that finds the vertices that branch off of the rootNode. In the first step of that function, there is a for loop that searches the array of edges. If the rootNode’s name appears in an edge, then the name of the node that it shares the edge with is pushed into adjacentNodeNames, which was initialized as an empty array.</p><p>Next, there is a for loop to search through the vertices. If a vertex has a name matching one of the names in adjacentNodeNames, two things happen. First, if it has not yet been marked as ‘discovered’, then that attribute is changed to ‘true.’ It is also pushed into adjacentNodes if it is being discovered for the first time. If it has already been discovered, then nothing happens. If the name of the vertex that the loop is on matches the rootNode, then it is marked as ‘discovered’ without being pushed into the array.</p><p>Finally, at the end of the function, the adjacentNodes array is returned.</p><h4>The Rest of the Function</h4><p>Next, I set up two empty arrays, one theQueue and the other the finalArray. The variable theQueue will have a value of different names of nodes to be visited, with the nodes to be visited first coming last in the array. The first node name that enters theQueue is the rootNode that was given as input. That name is also immediately pushed into finalArray.</p><p>Then, there is a while loop that will run as long as theQueue has a length greater than 0. In the while loop, the findAdjacentNodes function runs, with the name of the last node in theQueue being entered as the first input value. The return value of findAdjacentNodes is set to a variable called arrayForQueue, and the last item in theQueue is removed from that array using the pop() method.</p><p>Since arrayForQueue will be an array of items, there is then a for loop that will run to push the values in that array into theQueue.</p><p>In the end, the function returns the finalArray, which is the names of the nodes in the order they were visited. That should start with the rootNode (the one given as input as a starting point for the exploration of the graph), then include any vertices that branch off of that node, then all of the nodes down one branch, then all of the nodes down another branch, until all of the nodes that can be visited have been.</p><h4>Le Voyage</h4><p>The only thing I have against this method as a way of life is that it is not always worth it to turn over every stone to find what you are looking for. Sometimes you also do not need the breadth-first search model to go one level deep in everything out there to know either that you are where you already need to be or that you know where it is you want to go.</p><p>But that is not to discourage the intrepid travelers!</p><ul><li><a href="https://fleursdumal.org/poem/231">Le Voyage by Charles Baudelaire</a></li><li><a href="https://www.poetryfoundation.org/poets/charles-baudelaire">Charles Baudelaire | Poetry Foundation</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=782fe4e21a34" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Grid Lock]]></title>
            <link>https://medium.com/swlh/grid-lock-ea9246f22b8c?source=rss-726f287a8b5e------2</link>
            <guid isPermaLink="false">https://medium.com/p/ea9246f22b8c</guid>
            <category><![CDATA[multidimensional-arrays]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[for-loop]]></category>
            <dc:creator><![CDATA[Carlie Anglemire]]></dc:creator>
            <pubDate>Fri, 04 Sep 2020 03:41:44 GMT</pubDate>
            <atom:updated>2020-09-08T19:42:46.763Z</atom:updated>
            <content:encoded><![CDATA[<h3>Gridlock</h3><p>For a coding challenge that I did recently, I wrote a function that took in an input of a matrix of size N x N and output whether or not it was valid. In order for the matrix to be valid, it had to have each of the numbers up until N in both the columns and the rows. For example, this is one possible grid, which would be submitted as an array of arrays: [[1, 2, 3, 4], [2, 3, 4, 1], [3, 4, 1, 2], [4, 1, 2, 3]].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/694/1*KhCV6GOp5p7xdfrlupxjkw.png" /></figure><p>Since this satisfies the conditions, the function would return “VALID”.</p><h4>Sudoku</h4><p>Though I have never played it before, this problem was supposed to be designed to check sub-grids in a Sudoku game. Here are a couple of resources about Sudoku in case you would like to learn more.</p><ul><li><a href="https://www.nytimes.com/puzzles/sudoku/easy">Sudoku - New York Times Number Puzzles</a></li><li><a href="https://en.wikipedia.org/wiki/Sudoku">Sudoku - Wikipedia</a></li></ul><h4>Solution</h4><p>This is my solution to the problem, which I will explain below!</p><pre>function checkSubSudoku(matrix) {</pre><pre>let lengthOfMatrix = matrix.length;</pre><pre>let i;</pre><pre>let n;</pre><pre>let theIntegers = [];</pre><pre>let copyOfMatrix = [];</pre><pre>let areTheRowsValid;</pre><pre>let areArraysInMatrixSameSize;</pre><pre>let flattenedMatrix = matrix.flat();</pre><pre>let allAreIntegers;</pre><pre>//check to see whether all of the items in the arrays are positive integers</pre><pre>for(let a = 0; a &lt; flattenedMatrix.length; a++){</pre><pre>if (Math.sign(flattenedMatrix[a])===1){</pre><pre>if (!allAreIntegers || allAreIntegers === true){</pre><pre>allAreIntegers = true;</pre><pre>}</pre><pre>else {</pre><pre>allAreIntegers = false;</pre><pre>}</pre><pre>}</pre><pre>else {</pre><pre>allAreIntegers = false;</pre><pre>}</pre><pre>}</pre><pre>if(!allAreIntegers){</pre><pre>return &quot;INVALID&quot;</pre><pre>}</pre><pre>else{</pre><pre>//check that the length of each of the arrays within the array are the same size</pre><pre>for(let w = 0; w &lt; lengthOfMatrix; w++){</pre><pre>if(matrix[w].length === lengthOfMatrix){</pre><pre>if(!areArraysInMatrixSameSize || areArraysInMatrixSameSize === true){</pre><pre>areArraysInMatrixSameSize = true;</pre><pre>}</pre><pre>else{</pre><pre>areArraysInMatrixSameSize = false;</pre><pre>}</pre><pre>}</pre><pre>else{</pre><pre>areArraysInMatrixSameSize = false;</pre><pre>}</pre><pre>}</pre><pre>if(!areArraysInMatrixSameSize){</pre><pre>return &quot;INVALID&quot;}</pre><pre>else{</pre><pre>//set the variable theIntegers to see what numbers must be in each row and column of the matrix</pre><pre>for(n = 0; n &lt; lengthOfMatrix; n++){</pre><pre>theIntegers.push(n+1);</pre><pre>}</pre><pre>theIntegers = parseInt(theIntegers.join(&quot;&quot;))</pre><pre>//create a copy of the matrix in order to be able to sort without modifying the original array</pre><pre>for(let i = 0; i &lt; lengthOfMatrix; i++){</pre><pre>let sliceOfMatrix = matrix.slice(i, i+1).flat();</pre><pre>let sortedSlice = sliceOfMatrix.sort((a, b)=&gt;{</pre><pre>return a-b})</pre><pre>copyOfMatrix.push(sortedSlice);</pre><pre>}</pre><pre>//check each row of the copy to make sure it includes each number</pre><pre>for(let h = 0; h &lt; lengthOfMatrix; h++){</pre><pre>if(parseInt(copyOfMatrix[h].join(&quot;&quot;))===theIntegers){</pre><pre>if(!areTheRowsValid || areTheRowsValid === true){</pre><pre>areTheRowsValid = true;</pre><pre>}</pre><pre>else{</pre><pre>areTheRowsValid = false;</pre><pre>}</pre><pre>}</pre><pre>else{</pre><pre>areTheRowsValid = false;</pre><pre>}</pre><pre>}</pre><pre>//create an array of the columns</pre><pre>let copyForColumns = [];</pre><pre>for(let g = 0; g &lt; lengthOfMatrix; g++){</pre><pre>let arrayToPush = [];</pre><pre>for (h = 0; h &lt; lengthOfMatrix; h++){</pre><pre>arrayToPush.push(matrix[h][g])</pre><pre>}</pre><pre>copyForColumns.push(arrayToPush.sort((a, b)=&gt; {return a-b}))</pre><pre>}</pre><pre>//check each column to make sure it includes all of the numbers</pre><pre>let areTheColumnsValid;</pre><pre>for(let d = 0; d &lt; lengthOfMatrix; d++){</pre><pre>if(parseInt(copyForColumns[d].join(&quot;&quot;))===theIntegers){</pre><pre>if(!areTheColumnsValid || areTheColumnsValid === true){</pre><pre>areTheColumnsValid = true;</pre><pre>}</pre><pre>else{</pre><pre>areTheColumnsValid = false;</pre><pre>}</pre><pre>}</pre><pre>else{</pre><pre>areTheColumnsValid = false;</pre><pre>}</pre><pre>}</pre><pre>//   return either &quot;VALID&quot; or &quot;INVALID&quot;</pre><pre>if(areTheRowsValid &amp;&amp; areTheColumnsValid){</pre><pre>return &quot;VALID&quot;;</pre><pre>}</pre><pre>else{</pre><pre>return &quot;INVALID&quot;;</pre><pre>}</pre><pre>}</pre><pre>}</pre><pre>}</pre><h4>Damage Control</h4><p>In the first couple of steps, I am doing damage control, anticipating edge cases. First, I check to see whether all of the items in the arrays are positive integers. I flatten the nested array to make one long array of items; then I check each of these items and use Math.sign() to determine whether they are positive integers. Math.sign() returns 1 if what is put in between the parentheses is a positive integer, -1 if what is placed there is a negative number, 0 if 0 is put there, and NaN if the value is anything else put in between quotation marks, for example “,”.</p><p>Fun fact: whether you put a number such as 91 in between quotation marks or not, like this: Math.sign(“91”) or this: Math.sign(91), it will return 1. The same goes for negative numbers, which will cause Math.sign to return -1. However, Math.sign(,) will throw an error, while Math.sign(“,”) will return NaN.</p><p>One of many example of why coding is a good time!</p><p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign#:~:text=sign()%20function%20returns%20either,(%2B)%20will%20not%20be%20returned.">Math.sign()</a></p><p>If the current item in the loop through the flattened matrix passes the test, and if the variable areAllIntegers either has not been set yet or has been set to true, then it remains true, being set to true again. If areAllIntegers has been set to false, though, it remains false, since one non-positive integer or other value like a punctuation mark means that the whole grid is invalid. If the current item in the for loop does not pass the test of Math.sign(), then whether areAllIntegers has not been set yet, has been set to true, or has already been set to false, it will be set to false. Another way I could have done this would have been to use a while loop, so that once the variable is set to false, the loop exits, as none of the other values need to be checked.</p><p>Next, I see whether the input arrays within the matrix are all the same size. This follows a similar pattern to the first test, though it is on the non-flattened matrix. We don’t have to check the values within the arrays at this point; we just want to take the length of them. This follows the same pattern as the one above as far as setting the variable areArraysInMatrixSameSize to true or false goes.</p><p>If either of those tests fail (if either areArraysInMatrixSameSize or areAllIntegers have a value of false), the function exits, returning “INVALID”. Otherwise, it goes on to the next step.</p><p><a href="https://en.wikipedia.org/wiki/Damage_control">Damage control</a></p><h4>The Next Step: Checking the Rows</h4><p>After that, I set the value of theIntegers by running a for loop that goes through the length of the input matrix starting from the counter of n = 0 and pushes n+1 into an array that is then joined and parsed into a number with parseInt. The result of this is that if the input matrix is [[1, 2, 3, 4], [2, 3, 4, 1], [3, 4, 1, 2], [4, 1, 2, 3]], then theIntegers is 1234. This will be used as the point of comparison for what is in each of the rows and columns later on.</p><p>Next, I create a copy of the matrix. The reason that I do this is because I am going to be sorting each of the arrays in the matrix to check them against theIntegers to make sure that all of the numbers are included in each row. I do not want to alter the original input matrix, because I will also be checking the column in a separate step and can’t do that if the original input matrix is changed.</p><p>To create a copy of the matrix, I implemented another for loop starting at 0 and incrementing up until the length of the matrix. At each step of the for loop, I took a slice of the matrix — one of the arrays in the wrapping array, starting from the beginning of the matrix and going until the end.* I sorted it and pushed it into copyOfMatrix. So, copyOfMatrix ended up being not an exact replica of the original input matrix but a sorted version of it. This was so that in the next step of the function, I could check each sorted array to see if it included all of the requisite numbers.</p><p>*To review how to make a shallow copy of the matrix, I consulted a few websites. I am not sure which of them convinced me to try using the slice method, so here are links to all of them.</p><ul><li><a href="https://medium.com/@ziyoshams/deep-copying-javascript-arrays-4d5fc45a6e3e">Deep copying Javascript Arrays</a></li><li><a href="https://www.samanthaming.com/tidbits/35-es6-way-to-clone-an-array/">ES6 Way to Clone an Array 🐑 | SamanthaMing.com</a></li><li><a href="https://www.freecodecamp.org/news/how-to-clone-an-array-in-javascript-1d3183468f6a/">How to clone an array in JavaScript</a></li></ul><p>Because the coding challenge ended up being an exercise in my over-attachment to the for loop, I ran another for loop with a counter of zero incrementing up until the length of the matrix. Here is where I join the sorted array at the current location in the loop and parse it with parseInt before comparing it to theIntegers. If it matches theIntegers, then I go through the same true/false process of setting the variable areTheRowsValid as I did earlier in the first two conditions of the function. At the end of the loop, if areTheRowsValid is false, I could (and should) exit and return “INVALID”.</p><p>However, I did it a little less efficiently, going ahead and checking the columns before making any final determinations one way or another about the validity of this poor matrix which has already been through a lot.</p><h4>The Columns</h4><p>So! On to the columns!</p><p>Next, I create an array of arrays of columns. This is an act of imagination, since we don’t actually have a grid and have to imagine the nested arrays stacked on top of each other.</p><p>I return to the friendly for loop, but unfortunately for those of you keeping track of time complexity, this time it is a nested for loop. At the first level of the for loop, I initialize an empty array called arrayToPush. Then in the inner loop, I push matrix[h][g] into arrayToPush. What that means is that with a grid that looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*KhCV6GOp5p7xdfrlupxjkw.png" /></figure><p>matrix[h] will be the column, and [g] will represent the row. So, on the first loop through, matrix[h][g] will be matrix[0][0], then matrix[0][1], matrix[0][2], and matrix[0][3], or [1, 2, 3, 4]. After that first nested loop through, arrayToPush is pushed into the empty array copyForColumns, though it is sorted first.</p><p>The next nested loop through would be matrix[1][0], matrix[1][1], matrix[1][2], and matrix[1][3], or [2, 3, 4, 1]. At this point, you might notice that the rows and columns with the same index position are actually the same (e.g. row one is the same as column one, row two is the same as column two, etc.). I did not let that observation deter me, because, not being familiar with this type of problem beforehand, it was not clear to me whether that is the way it always ends up as a rule. If it is, though, then checking the columns would be unnecessary.</p><p>Report back to me if you would like to discuss or tell me what is what.</p><p>In the next step, I do the same thing that I did to check whether the rows were valid, looping through copyForColumns, joining each array, and parsing the array with parseInt before comparing that value to theIntegers.</p><p>In the final step, if both areColumnsValid and areRowsValid are true, then the function returns “VALID”. Otherwise, it returns “INVALID”.</p><p>And that is the end of my story about grids.</p><p><a href="https://en.wikipedia.org/wiki/Gridlock">Gridlock</a></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FsnsTmi9N9Gs%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DsnsTmi9N9Gs&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FsnsTmi9N9Gs%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/044f869912805bb716b1ccf373157033/href">https://medium.com/media/044f869912805bb716b1ccf373157033/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ea9246f22b8c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/grid-lock-ea9246f22b8c">Grid Lock</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Binary Search Tree and Me & Recursion Again (again)]]></title>
            <link>https://medium.com/@carlie.anglemire/binary-search-tree-and-me-recursion-again-again-c60814f868d8?source=rss-726f287a8b5e------2</link>
            <guid isPermaLink="false">https://medium.com/p/c60814f868d8</guid>
            <category><![CDATA[binary-search-tree]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[while-loop]]></category>
            <dc:creator><![CDATA[Carlie Anglemire]]></dc:creator>
            <pubDate>Fri, 28 Aug 2020 00:33:35 GMT</pubDate>
            <atom:updated>2020-09-04T00:27:22.591Z</atom:updated>
            <content:encoded><![CDATA[<h3>Binary Search Tree and Me</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/668/1*jvUvbSgk7hC5Po8M5t2EtA.png" /></figure><p>This week, I have been working on binary search trees, so this is an explanation of a problem that I did on Learn.co’s Software Engineering Post-Work track. It builds on the concept of Linked Lists, which I wrote about a couple of weeks ago. Just as a caveat, this is still something I am learning about and getting comfortable with!</p><h4>Binary Search Tree Examples</h4><p>When using a binary search tree in JavaScript, numbers higher than the root go to the right of it, while numbers less than the root go to the left, as in the picture above. Below is a picture of another tree on <a href="https://learn.co/tracks/software-engineering-post-work/computer-science/data-structures/tree-methods-lab">the Flatiron School’s Learn.co</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/404/1*UWUov4K7tfTWGw1YZdwA7g.png" /></figure><p>The pictures are just a heuristic. The code for the first tree pictured above looks like this, as we can see in the lab’s test file:</p><pre>{data: 5, left:</pre><pre>{data: 3, left: null, right: null},</pre><pre>right: {data: 7, left: null,</pre><pre>right: {data: 9, left: null, right: null}</pre><pre>}}</pre><p>Great — now let’s do a problem!</p><h4>With a While Loop</h4><p>One of the parts of the lab I did asked me to write a function that takes in the root node and one other node; if the other node is found to be in the tree already, then the function returns ‘true.’ Otherwise, it adds the node in the appropriate place on the tree.</p><p>The first approach I took to the problem uses a while loop. The recursive solution is in the works, so we’ll go with this one for now!</p><pre>function findOrAdd (currentNode, newNode) {</pre><pre>let quitLoop = 0;</pre><pre>while(quitLoop &lt; 1){</pre><pre>if(currentNode.data === newNode.data){</pre><pre>return true</pre><pre>}</pre><pre>else if(newNode.data &lt; currentNode.data){</pre><pre>if (!currentNode.left){</pre><pre>currentNode.left = newNode;</pre><pre>quitLoop = 1;</pre><pre>}</pre><pre>else{</pre><pre>currentNode = currentNode.left;</pre><pre>}</pre><pre>}</pre><pre>else {</pre><pre>if (!currentNode.right) {</pre><pre>currentNode.right = newNode;</pre><pre>quitLoop = 1;</pre><pre>}</pre><pre>else {</pre><pre>currentNode = currentNode.right;</pre><pre>}</pre><pre>}</pre><pre>}</pre><pre>return false;</pre><pre>}</pre><p>Before walking through the code, I’d like to direct your attention back to our friend the test file.</p><h4>Look at the Test File</h4><p>What is currentNode.data and newNode.data? If you look in the test file or comment out the rest of the code from the function and just return them, you can see what type of data the problem is expecting as inputs. Below is what we can learn from looking in the test file.</p><pre>let rootNode = {data: 5, left: null, right: null}</pre><pre>let firstNewNode = {data: 3, left: null, right: null}</pre><pre>let secondNewNode = {data: 7, left: null, right: null}</pre><pre>let thirdNewNode = {data: 9, left: null, right: null}</pre><p>The rootNode is what would be passed in as currentNode, or the head of the binary tree. The other three nodes are all examples of nodes that could be passed in as the second input. The test also shows us what it is expecting if the rootNode is entered as the first input and the firstNewNode is entered as the second input.</p><pre>findOrAdd(rootNode, firstNewNode)</pre><pre>expect(rootNode.left).toEqual(firstNewNode)</pre><p>The data for firstNewNode is 3, and since that is less than 5, it would go to the left. The test is actually pretty simple, since the left and right of the rootNode are both null. If the data of the second argument is larger, it goes to the right, and if it is smaller it goes to the left. Though my solution works with the current tests, I am not 100% sure that my solution would work the way that it is supposed to with a binary tree that has been built up more before using the function to add additional nodes. I did more testing myself — and though it seems to work, I have to think about it some more and get more practice.</p><p>But for a first try with something new, let’s start small!</p><h4>Explanation of the While Loop</h4><p>If currentNode.data is equal to newNode.data, then the function can return ‘true’ right away. Otherwise, if newNode.data is less than currentNode.data, if currentNode does not have a value to the left, the newNode can be inserted there. The variable quitLoop is set to 1 so the while loop will exit. However, if newNode.data is less than currentNode.data but there is already a value to the left of currentNode, then the currentNode variable is set to the value of currentNode.left and the while loop runs again until either a place on the tree is found for the newNode or it is discovered that the newNode is actually an old node already on the binary search tree — in which case the function will return true to signify that the tree already contains that node.</p><p>If newNode.data is greater than currentNode.data, then the same process occurs, except it starts looking along the right side of what I am going to start imagining to be a Christmas tree. If the function makes it through the tree, finds a place for the newNode, and does not find data equal to the data in newNode, then it will return false and update the binary search tree to include the newNode in it.</p><p>So, that was my adventure with binary search trees. Don’t get lost in the branches, everyone!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FVNR72YHWVCk%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DVNR72YHWVCk&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FVNR72YHWVCk%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/1043773e2889f323b02a6bae571dd0cb/href">https://medium.com/media/1043773e2889f323b02a6bae571dd0cb/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FDA_dExJjywk%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DDA_dExJjywk&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FDA_dExJjywk%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/3b1e687c04b69acf4095eb53a69a4371/href">https://medium.com/media/3b1e687c04b69acf4095eb53a69a4371/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c60814f868d8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Recursion (Again)]]></title>
            <link>https://medium.com/@carlie.anglemire/recursion-again-f577bee3379e?source=rss-726f287a8b5e------2</link>
            <guid isPermaLink="false">https://medium.com/p/f577bee3379e</guid>
            <category><![CDATA[recursion]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Carlie Anglemire]]></dc:creator>
            <pubDate>Thu, 20 Aug 2020 02:26:33 GMT</pubDate>
            <atom:updated>2020-08-20T03:30:43.150Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently, I have been working on recursion, a topic I became newly acquainted with last week. While I am looking forward to using it in different ways in the future, below is a problem that I did as I got started. It is part of a lab on the Software Engineering Post-Work Track on Learn.co for the <a href="https://www.accesslabs.org/">Flatiron School</a>. Though, I don’t have a job yet, this is how much I like the Flatiron School — I wear their Access Labs t-shirt on beautiful summer days.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a_iAmh7Ubi6QaHk7CPSkCg.jpeg" /></figure><h4>Plugging Other Posts</h4><p>When learning something new, there are a lot of things that help me to understand it. It is nice to have a theoretical overview, to try to solve problem sets, to see multiple examples, and to get explanations of how a concept or tool works in different contexts. While the read-me and lab on Learn.co was a great place to start, examples I found elsewhere on the internet, particularly for doing string manipulation using recursion, made it easier for me to jump in. Below are the two posts that best helped me to get a foothold at the beginning of the lab so that I could then solve the few remaining parts of the problem set on my own. Thanks, guys!</p><ul><li><a href="https://davidtang.io/2019/04/11/learning-recursion-in-javascript-part-4.html">Learning Recursion in JavaScript Part 4 - Palindromes - David Tang</a></li><li><a href="https://learnersbucket.com/examples/algorithms/reverse-a-string-using-recursion/">Reverse a string using recursion | LearnersBucket</a></li></ul><h4>Adding Up To An Index</h4><p>One of the functions I wrote adds up the numbers in an array, stopping at a specified index position and returning the sum. Often there is more than one answer to a problem. That is part of what makes life interesting. Below is my first solution.</p><h4>Solution 1</h4><pre>function addUpTo (arr, index) {</pre><pre>if (arr.length &lt; 1) {<br>    return 0;<br>  }</pre><pre>else {<br>    let reducedArray = arr.slice(0, index + 1);</pre><pre>return reducedArray.pop() + addUpTo(reducedArray, reducedArray.length);<br>  }<br>}</pre><p>If the length of the array is 0, then the function just returns 0. Otherwise, it takes a slice of the array from the beginning through the specified index position. Since when you take a slice it does not alter the array in place, the result of the slice is stored in the variable reducedArray.</p><p>For example, if the input index is 1, and the input array is [5, 6, 2, 4, 1], the reducedArray would be [5, 6]. Below is a link to the MDN explanation of the slice method.</p><p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice">Array.prototype.slice() - JavaScript | MDN</a></p><p>Then, once we have an array from which all of the values we will not be adding are eliminated, we can go ahead and start adding together the numbers that are left. In the next step, the function removes the last item in the array. Using the pop method returns the eliminated item and alters the array in place. So, [5, 6].pop() returns the number 6, and the value of reducedArray is now just [5]. We do not have to save the altered array to a new variable, unlike when we used the slice method.</p><p>Now, we can finally use recursion! In addition to returning the number that is deleted from the array, we call the function with the new input values of reducedArray and reducedArray.length.</p><p>This is still like magic to me, but what happens next is that the function keeps calling on itself until the array is empty, and there are no more numbers to add together. Though it looks like the function would return several times (and doesn’t a function exit upon the first return?!), the function does not actually return until it has met its base condition and is ready to exit. That means that all of the numbers are popped off of the array and added together. Then the sum of them is returned.</p><h4>Solution 2</h4><p>Another way of solving the problem is by using the splice method, which, unlike the slice method, alters an array in place.</p><pre>function addUpTo (arr, index) {</pre><pre>if (arr.length === 0) {</pre><pre>return 0</pre><pre>}</pre><pre>else {</pre><pre>return arr.splice(index)[0] + addUpTo(arr, arr.length - 1);</pre><pre>}</pre><pre>}</pre><p>In this solution, the first condition is the same — if the length of the array is 0, then 0 is returned. Otherwise, we splice the array at the input index position. Then, we return the value in the 0 position of the array that is returned from splicing.</p><p>For example, take an input array of [5, 6, 2, 4, 1] and an input index of 3. Calling arr.splice(3) would remove all the items from the array starting from index 3 and would return an array of what has been deleted — [4, 1]. Because we want to add the value at the index position in addition to removing it, we can call arr.splice(index)[0] to only return the 4 from [4, 1].</p><p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice">Array.prototype.splice() - JavaScript | MDN</a></p><p>Then, we can do recursion again! Since the array has been altered in place using splice, we can just enter ‘arr’ as the input array. The input index is ‘arr.length-1’. That means that when the function calls itself on the next round it will remove the last item in the array and return it, adding it to all the others. The length of the array that is returned from the splice will only be 1 after the first call (before the array is completely emptied), but returning the item at the 0 position will have the same affect as if it were a longer array.</p><h4>In Conclusion</h4><p>Recursion makes me happy. Have a nice day.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FJnbfuAcCqpY%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DJnbfuAcCqpY&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FJnbfuAcCqpY%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/2059613d7094d462267b37b71c77a576/href">https://medium.com/media/2059613d7094d462267b37b71c77a576/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FqHppZ1aXxVc%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DqHppZ1aXxVc&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FqHppZ1aXxVc%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/7be7038ac1c3062a922636aab037ccff/href">https://medium.com/media/7be7038ac1c3062a922636aab037ccff/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f577bee3379e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Recursion and Class in JavaScript]]></title>
            <link>https://medium.com/@carlie.anglemire/recursion-and-class-in-javascript-32a0a351551a?source=rss-726f287a8b5e------2</link>
            <guid isPermaLink="false">https://medium.com/p/32a0a351551a</guid>
            <category><![CDATA[object-oriented-design]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[recursion]]></category>
            <dc:creator><![CDATA[Carlie Anglemire]]></dc:creator>
            <pubDate>Thu, 13 Aug 2020 18:24:55 GMT</pubDate>
            <atom:updated>2020-08-13T21:38:14.927Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/548/1*OHotMcqJtITkedq1Rhgzcg.png" /></figure><p>This week, I have been both reviewing something old and learning something new, and I decided to bring them together in a blog post so that they can get to know each other and you can get to know them.</p><h4>Recursion</h4><p>Recursion is the new friend I am making this week, and we are still getting used to each other. Ok, it is mostly me getting used to her.</p><p>Recursion is calling on a function that you are still in the process of defining. Below is an example that takes in a number n and a string of text and logs it to the console n amount of times.</p><pre>function repeatLine (n, theLine) {</pre><pre>console.log(theLine);</pre><pre>if (n &gt; 1) {</pre><pre>repeatLine(n - 1, theLine);</pre><pre>}</pre><pre>}</pre><p>First, theLine is logged once before hitting the if condition. Then, if n is greater than one, the function as it is defined up until that point is called with n being decremented. That means that theLine is console-logged. That call will be repeated until n is no longer greater than one. If you call the function with the the number entered as 5 and the string as “whosits and whatsits galore”, you should see the following in your console.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IxnxeG0tzvLbkHq0eHlaXg.png" /></figure><p>As in:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Ft6Ol7VsZGk4%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dt6Ol7VsZGk4&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Ft6Ol7VsZGk4%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/caddaaf224f4fd59bbaf96e0ecfe7a8b/href">https://medium.com/media/caddaaf224f4fd59bbaf96e0ecfe7a8b/href</a></iframe><h4>Reminder about Function Input</h4><p>Something to keep in mind if you are just starting out with JavaScript is that it is the order of the input that matters. For example, if you had called the function with the input order reversed, like this:</p><pre>repeatLine(&quot;whosits and whatsitsgalore&quot;, 5)</pre><p>then the number 5 would have been console-logged one time. If you check the datatype in the console by adding a console.log to the function for the data type, like this</p><pre>function repeatLine (n, theLine) {</pre><pre>console.log(theLine);</pre><pre>console.log(typeof theLine);</pre><pre>if (n &gt; 1) {</pre><pre>repeatLine(n - 1, theLine);</pre><pre>}</pre><pre>}</pre><p>you will see that the data type of the 5 that is logged is ‘number’. The data type of the input is not specified by the function, so it will log theLine regardless of its type and will not coerce it into being another type. It will not decide to arbitrarily convert the number 5 into a string. But it also will not know that what you wanted was for the input that is a string to be the one that is printed out.</p><p>Why does the function log anything at all? Before the if statement, a console.log is made, so theLine, whatever it is, will print at least one time. Since the value of n is a string and you cannot compare a string to a number, the if statement is not triggered. This is what I get when trying to compare a string to a number in the console.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/456/1*SegG_Oeo6tVKCcRyUhsd0Q.png" /></figure><p>If we ask whether the variable is less than one, using the less than operator, the answer is “false”. If we ask whether the variable is greater than one, then the answer is also “false”. If you use the == and === operators to compare phrase to 1, it is also comes out false.</p><p>In my opinion, this is an example of how in code, as in life, not everything is reducible to numbers.</p><h4>Review of Class Constructors</h4><p>This is the old friend who I haven’t thought about in awhile but has been there all along.</p><p>In making applications with React a portion of the work of writing code is abstracted away, and I realized that it was a good time for me to review Class and Inheritance to be reminded of what I have been taking for granted.</p><p>While using the current version of React, it has not been necessary for me to use a constructor, so this is a basic review of what a constructor does. Below is an example of the class Frog.</p><pre>class Frog {</pre><pre>constructor (name, from, into) {</pre><pre>this.name = name;</pre><pre>this.from = from;</pre><pre>this.into = into;</pre><pre>}</pre><pre>}</pre><p>Any instance of the class Frog will have to be initialized with a name and a value for the “from” and “into” attributes. The ‘this’ as it appears in the constructor refers to the instance of a frog that is being or has been created.</p><p>For example, below is a new instance of a frog being created:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*1TLfIVfOZ9ZPjn1GRID-dQ.png" /></figure><p>The ‘new’ signals that a new object that is an instance of a class is about to be created. After that comes the name of the class, in this case ‘Frog’. Then, we need to include input between parentheses to give a value for the name, from, and into attributes. The result of the above is the following, a new frog!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/548/1*OHotMcqJtITkedq1Rhgzcg.png" /></figure><p>That means that if I type</p><pre>Kermit.from</pre><p>into the console at this point, it should return “frog”.</p><h4>Getting It Together</h4><p>Because it is sometimes nice to have a happy ending to a story, let’s bring the above concepts together.</p><p>In the console, if you call repeatLine (the second version that also gives us the data type of what is being logged) with the following input</p><pre>repeatLine(5, Kermit.from)</pre><p>you should get as the output</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/218/1*r67vWlljGIpN-SzZas2zyg.png" /></figure><p>Which kind of reminds me of a frog doing jump rope.</p><p>You’re welcome.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=32a0a351551a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>