<?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 Charlie Axelrod on Medium]]></title>
        <description><![CDATA[Stories by Charlie Axelrod on Medium]]></description>
        <link>https://medium.com/@charlieaxelrod?source=rss-d754512aa692------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*VtnTjCcEUsqEj8mvnv62ZQ.jpeg</url>
            <title>Stories by Charlie Axelrod on Medium</title>
            <link>https://medium.com/@charlieaxelrod?source=rss-d754512aa692------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 25 Nov 2026 00:34:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@charlieaxelrod/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[Reciprocally assigning object properties in JS]]></title>
            <link>https://medium.com/jsunderthescope/reciprocally-assigning-object-properties-in-js-35524aa78756?source=rss-d754512aa692------2</link>
            <guid isPermaLink="false">https://medium.com/p/35524aa78756</guid>
            <category><![CDATA[object-property]]></category>
            <category><![CDATA[oddities]]></category>
            <category><![CDATA[object-assign]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Charlie Axelrod]]></dc:creator>
            <pubDate>Fri, 17 Sep 2021 07:08:06 GMT</pubDate>
            <atom:updated>2021-09-17T07:15:20.502Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*BRdaQnDWlDBnBNsOPDv2cg.jpeg" /></figure><p>I was browsing a site’s code in DevTools today and came across something I’d never seen before. The code was along these lines:</p><p>status[status.LOADING = 4] = &#39;LOADING&#39;;</p><p>Assuming status had been an empty object beforehand, it comes out as the following:</p><p>{4: &#39;LOADING&#39;, LOADING: 4}</p><p>Though a bit difficult to parse, this type of reciprocal assignment is pretty neat; we’re left with a bi-directional map achieved with only one line of code. Since the middle expression, status.LOADING = 4 , evaluates to 4, we can perform two assignments in one.</p><p>I then started playing around with this type of assignment syntax and got some funky results. It turns out that you can just keep on chaining these assignments endlessly:</p><p>status[status[status.ONE = &quot;TWO&quot;] = &quot;THREE&quot;] = &quot;FOUR&quot;;</p><p>Which leaves us with:</p><p>{ONE: &quot;TWO&quot;, TWO: &quot;THREE&quot;, THREE: &quot;FOUR&quot;}</p><p>I can’t see any use for this aside from making whoever is reading your code suffer. It is funny though.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=35524aa78756" width="1" height="1" alt=""><hr><p><a href="https://medium.com/jsunderthescope/reciprocally-assigning-object-properties-in-js-35524aa78756">Reciprocally assigning object properties in JS</a> was originally published in <a href="https://medium.com/jsunderthescope">JSUnderTheScope</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Table of JavaScript String and Array Methods]]></title>
            <link>https://medium.com/jsunderthescope/table-of-javascript-string-and-array-methods-fdba7c5289f1?source=rss-d754512aa692------2</link>
            <guid isPermaLink="false">https://medium.com/p/fdba7c5289f1</guid>
            <category><![CDATA[array-methods]]></category>
            <category><![CDATA[string-methods]]></category>
            <category><![CDATA[toy-problems]]></category>
            <category><![CDATA[javascript-types]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Charlie Axelrod]]></dc:creator>
            <pubDate>Sun, 14 Oct 2018 22:50:49 GMT</pubDate>
            <atom:updated>2018-10-14T22:50:49.610Z</atom:updated>
            <content:encoded><![CDATA[<p>Below is a table showing different ways to use built-in methods for strings and arrays to achieve certain goals in JavaScript. A few notes about this table:</p><ol><li>Though some of these methods have the same names across different types, the way that they are implemented differs, so it’s important to read up on MDN. For example, string.concat() creates a new string, whereas array.concat() does not.</li><li>Along these same lines, keep in mind that strings are immutable in JavaScript.</li><li>The blanks in the String column don’t mean that the intended effect isn’t possible with strings, it just means that there isn’t a one-step solution.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1006/1*HJY_2vNKkNkqRQhLZcYlyg.png" /></figure><p>Note: This list is incomplete for both strings and arrays. I intend to update it later on (and hopefully add a column for objects). Any comments or suggestions are welcome!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fdba7c5289f1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/jsunderthescope/table-of-javascript-string-and-array-methods-fdba7c5289f1">Table of JavaScript String and Array Methods</a> was originally published in <a href="https://medium.com/jsunderthescope">JSUnderTheScope</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[String Manipulation: replace()]]></title>
            <link>https://medium.com/jsunderthescope/string-manipulation-replace-a02426cc600c?source=rss-d754512aa692------2</link>
            <guid isPermaLink="false">https://medium.com/p/a02426cc600c</guid>
            <category><![CDATA[toy-problems]]></category>
            <category><![CDATA[string-manipulation]]></category>
            <category><![CDATA[recursion]]></category>
            <category><![CDATA[replace]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Charlie Axelrod]]></dc:creator>
            <pubDate>Mon, 08 Oct 2018 03:42:54 GMT</pubDate>
            <atom:updated>2018-10-08T03:42:54.998Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DjZt_wrFeAYZOfAmFQgp9Q.jpeg" /></figure><p>One of the main differences between strings and arrays in Javascript is that <strong>strings are immutable. </strong>Though we can swap out a single element of an array for a different element, we cannot change a single letter of a string. Take the following example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/207/1*hW0IbjkCJO8AZL1_56W15A.png" /></figure><p>We tried to swap out the first letter of “test” to create the word “best”, but the original string remains unchanged.</p><p><strong>The only way to manipulate a string in Javascript is to create a new string. </strong>Fortunately, Javascript simplifies string manipulation by offering us various string methods, such as <em>split(), slice(), substring(), </em>and <em>trim()</em>. Let’s take a closer look at one of these methods: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace"><em>replace()</em></a><em>.</em></p><p>The replace method does almost exactly what we attempted above, except that it returns a new string.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/244/1*HEp1SohTdn6T_5Yy6zDW0A.png" /></figure><p><em>replace() </em>takes two parameters: the first represents the part of the string that we want to replace (in the form of a string or regular expression), and the second represents the text that we want to substitute in (or a function used to create this text).</p><p>Look over the most recent example one more time. Notice anything strange? There are two “t”s in the word “test”, but <em>replace()</em> only swapped out the first one!<strong> </strong>When we use a string for the first parameter, only the first occurrence is replaced. <strong>To replace all occurrences, we must use a regular expression.</strong></p><h3>A modular string replacer function</h3><p>Imagine that we had to make a series of substitutions across a wide range of files, each of them with different words or letters that needed to be swapped. It would be quite cumbersome to write the code into each file! Below is a modular string replacer that uses object keys and properties to specify the necessary substitutions.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjsfiddle.net%2Fcaxelrod%2Ffugthnzq%2Fembedded%2F&amp;display_name=jsFiddle&amp;url=https%3A%2F%2Fjsfiddle.net%2Fcaxelrod%2Ffugthnzq%2F&amp;image=https%3A%2F%2Fwww.gravatar.com%2Favatar%2Fe1da261ba37e6e5d5717d2c7c2749735%3Fs%3D80&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=jsfiddle" width="600" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/263cff7f96dd2412454195bc7c059102/href">https://medium.com/media/263cff7f96dd2412454195bc7c059102/href</a></iframe><p><em>stringReplacer</em> takes two parameters: the string that we need to manipulate and a <em>snippets</em> object, which contains key/value pairs in which the key represents the text we want to replace, and value represents the text to be substituted in. The <em>helper</em> function creates a global regular expression (<em>regex</em>) from one of the keys in our <em>snippets</em> object, and replaces every occurrence of the expression in our string. Our new, modified string is written to <em>output</em>. The key/value pair is then deleted from <em>snippets,</em> and <em>helper</em> is called again (recursively) on our new string. <strong>When all of the key/value pairs in <em>snippets </em>have been deleted, the recursion will stop</strong>, and our final string will be returned.</p><h3>An interesting application</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y56VXadV_E0xu5BO8qu8bQ.jpeg" /></figure><p>This post was inspired by <a href="https://leetcode.com/problems/roman-to-integer/description/">a LeetCode problem</a>, which challenges us to convert a string in Roman numerals to the integer of the same value. Though the premise is simple, this problem has a major “gotcha”: numbers like “IX” and “IV” can’t be read converted sequentially or they’ll become 11 and 6. One way around this problem is to use a series of if/else statements to check for numbers like “IX”, but this solution is not particularly elegant. Instead, we can use our <em>stringReplacer</em> function to replace “IX” with “VIIII”. Once all of these replacements have been performed, the Roman numeral can be iterated over normally. <strong>Test your understanding and try to solve the problem using <em>stringReplacer</em>!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a02426cc600c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/jsunderthescope/string-manipulation-replace-a02426cc600c">String Manipulation: replace()</a> was originally published in <a href="https://medium.com/jsunderthescope">JSUnderTheScope</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Generating a list of valid words in JavaScript]]></title>
            <link>https://medium.com/jsunderthescope/generating-a-list-of-valid-words-in-javascript-efd3955c8912?source=rss-d754512aa692------2</link>
            <guid isPermaLink="false">https://medium.com/p/efd3955c8912</guid>
            <category><![CDATA[wordlist]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[recursion]]></category>
            <category><![CDATA[inner-recursive-function]]></category>
            <category><![CDATA[dictionary]]></category>
            <dc:creator><![CDATA[Charlie Axelrod]]></dc:creator>
            <pubDate>Mon, 01 Oct 2018 04:56:12 GMT</pubDate>
            <atom:updated>2018-10-01T04:56:12.871Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ab_S-KPFo-h_hLlb8NX2zw.jpeg" /></figure><p>Continuing with the theme from last week, we’ll be taking a look at another useful application of recursion: generating all the valid words of a given length that can be formed from a group of letters. Just don’t use it to cheat at Scrabble!</p><h3>Approach</h3><p>First, we need to create a loop that will generate all the possible strings that can be formed using a given set of letters. For example, if we want all of the three-letter words that can be formed using the letters “a”, “b”, and “c”, our list would be the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MBOVz8Hxr1V0OOiJbhxy2g.png" /></figure><p>Then, we want to filter out the words that aren’t valid English words. We can do this after we generate our list or as we generate it.</p><h3>Recursive loop</h3><iframe src="https://cdn.embedly.com/widgets/media.html?url=https%3A%2F%2Fjsfiddle.net%2Fuvzc3e62%2F6%2F&amp;src=https%3A%2F%2Fjsfiddle.net%2Fuvzc3e62%2F6%2Fembedded%2F&amp;type=text%2Fhtml&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;schema=jsfiddle" width="600" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/e90af3ea2b2c3e74fe8ba915a3a91948/href">https://medium.com/media/e90af3ea2b2c3e74fe8ba915a3a91948/href</a></iframe><p>Our outer function, <em>genWordList</em>, takes two parameters: <em>n</em> is the length of the words that we want to create, and <em>letters</em> are the letters that we can use to make the words. This function creates a <em>results</em> array that contains the words we’ve generated.</p><p>The inner function, <em>helper</em>, provides our recursive loop. Its sole parameter, <em>cache</em>, is a <strong>constantly-changing sequence of letters</strong>. It starts out as an empty string, but on the first loop it grows, receiving the letter “a”. Since <em>cache</em> only has one letter, not the three letters that we need, the JavaScript interpreter will bypass the if-statement and run the code inside the else-statement, recursively calling <em>helper </em>again. On the second go-round, <em>cache</em> becomes “aa”, and finally “aaa” on the third call of <em>helper</em>.</p><p>Now, our <em>helper</em> function finally runs the code inside our if-statement. It adds our word to the <em>results</em> array, then removes the last letter that was added to <em>cache</em> using the <em>.slice()</em> method. The interpreter returns to the top of our for-loop, now on the letter “b”. The word “aab” is added to our <em>results</em> array. The same process is repeated with “aac”.</p><p><strong>Here comes the confusing part.</strong> When the for-loop finishes, the interpreter returns to the place where the <em>helper</em> function was most recently called: in the else-statement, back when our <em>cache </em>only had the letters “aa”. The interpreter continues from there: another letter is lopped off <em>cache</em>, and it goes back to the top of the for-loop. Our <em>cache</em> becomes “ab”, and so on. In this way, our recursive function is able to generate words of any length, <strong>without the need for nested for-loops.</strong></p><h3>Word Validation</h3><p>Now, let’s add in a step to check if the words that we’ve generated actually exist in the English language. To do this, we’re going to use the <a href="https://www.npmjs.com/package/check-word">npm package check-word</a>. Our new variable <em>words</em> establishes that we’ll be checking against an English dictionary.</p><iframe src="https://cdn.embedly.com/widgets/media.html?url=https%3A%2F%2Fjsfiddle.net%2Fuvzc3e62%2F7%2F&amp;src=https%3A%2F%2Fjsfiddle.net%2Fuvzc3e62%2F7%2Fembedded%2F&amp;type=text%2Fhtml&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;schema=jsfiddle" width="600" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/51f417bf0496bca66072d4c0f3b00c5d/href">https://medium.com/media/51f417bf0496bca66072d4c0f3b00c5d/href</a></iframe><p>In our <em>helper</em> function, we add an extra condition before including a <em>cache</em> string in our <em>results </em>array: <strong>the string must appear in check-word’s dictionary.</strong></p><p>Now that we’ve added this new condition, we can see our final list of valid words, some of which are quite unusual!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/326/1*RjWtImtJWLiZMCX6TE3Avw.png" /></figure><p>If you’re interested in playing around with this function, go ahead and fork it from <a href="https://github.com/CharlieAxelrod/wordlist-generator">my GitHub page</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=efd3955c8912" width="1" height="1" alt=""><hr><p><a href="https://medium.com/jsunderthescope/generating-a-list-of-valid-words-in-javascript-efd3955c8912">Generating a list of valid words in JavaScript</a> was originally published in <a href="https://medium.com/jsunderthescope">JSUnderTheScope</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A recursive binary search in JavaScript]]></title>
            <link>https://medium.com/jsunderthescope/a-recursive-binary-search-in-javascript-b29efaff64d6?source=rss-d754512aa692------2</link>
            <guid isPermaLink="false">https://medium.com/p/b29efaff64d6</guid>
            <category><![CDATA[recursion]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[sorted-array]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[binary-search]]></category>
            <dc:creator><![CDATA[Charlie Axelrod]]></dc:creator>
            <pubDate>Mon, 24 Sep 2018 00:33:55 GMT</pubDate>
            <atom:updated>2021-09-17T06:32:48.415Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/482/0*deIQJcXJ1IROSqq6.jpg" /></figure><p>This post seeks to clarify the idea of recursion using an algorithm that almost begs to be implemented recursively: the binary search. Before we jump into the JavaScript implementation, let’s review how a binary search works.</p><p>Imagine we have a list of names that are sorted alphabetically.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ldGixBDxU3nDAiqKPKIoug.png" /></figure><p>We want to find out two things:</p><ol><li>Is the name “Brian” in the list?</li><li>If so, where is the name located in the list?</li></ol><p>One way to do this programmatically is to check every name in our list until we either find the name or reach the end of the list. However, because our list of names is sorted, a binary search will achieve our goals much faster.</p><p>A binary search works by comparing the name that we want to find (“Brian”) to the name in the middle of the list (“Darcy”). Since “Darcy” comes after “Brian” in the alphabet, then we know that “Brian” cannot be anywhere after “Darcy” in our list.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_ZxEufeFRGsGXRe4ZwokdA.png" /></figure><p>Next, we repeat the process <strong>(psst: recursion)</strong> using only the first half of our list. Our list now has four names, so depending on our code, either “Betsy” or “Brian” could be the middle of our list. Let’s assume that our code guesses “Betsy” as the middle name.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZQCIVX3vxfPG6ZBYcerQHg.png" /></figure><p>“Betsy” comes before “Brian”, and we eliminate both “Betsy” and “Alan”. On our third guess, our binary search will guess “Brian” and end the search.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xbzJXlpY2CxbzOzZcmvFIA.png" /></figure><p>If the name had not been in the list, our binary search would have reduced the list to one name before determining that the name could not be found.</p><h3>Javascript Implementation</h3><p>Let’s take a look at this algorithm in the form of a JavaScript function:</p><pre>var binarySearch = function(array, target, min, max) {<br>	if (min === undefined) min = 0;<br>	if (max === undefined) max = array.length - 1;<br><br>	var guess = Math.floor(((max - min) / 2) + min);<br>	if (max &lt;= min &amp;&amp; array[guess] !== target) return null;<br>	else if (array[guess] === target) return guess;<br>	else if (array[guess] &lt; target) return binarySearch(array, target, guess + 1, max);<br>	else return binarySearch(array, target, min, guess - 1);<br>};</pre><p>This is a <strong>recursive</strong> function, which means that <strong>it calls itself. </strong>But before we get into the recursive aspects of the function, let’s start at the top.</p><p>The parameter <em>array</em> is our list of names and <em>target</em> is the name we are looking for. The parameters <em>min</em> and <em>max</em> determine the section of the list that we are examining. <strong>These sections get smaller as we eliminate parts of our list.</strong></p><p>In the first line of the function, we copy the array so as not to modify the original. Next, we check if any arguments were passed for <em>min</em> and <em>max. </em>If no arguments have been passed, we set <em>min</em> to 0 (the beginning of the array) and <em>max</em> to <em>array.length-1 </em>(the end of the array).</p><p>Next, the program assigns <em>guess</em> to be the median of the valid section of the array. In other words, <strong><em>guess </em>is an index.</strong> The first time that <em>binarySearch</em> is called, <em>guess </em>will be the median of the whole array. If the length of the array is 9, then <em>guess</em> will be (9–0)/2 + 0, which is then rounded down using <em>Math.floor()</em>, giving a value of 4. If the length is 8, then <em>guess </em>will be (8–0)/2 + 0, again 4. So, unlike the example above, this code will round down if the length is even.</p><p>The function then compares the name at the index <em>guess</em> to the target. If the array has been narrowed down to one name, and that name is not the target, <em>binarySearch </em>will return <em>null</em>. If the name at <em>guess</em> is the same as the target, <em>binarySearch </em>returns <em>guess</em>, the index where the name is located in the array.</p><p>Otherwise, if the name located at <em>guess </em>comes before the target in the alphabet, we call <em>binarySearch</em> again on a reduced section of the array. <strong>This is the central idea of recursion; instead of creating a loop for the second half of the function, we can just call the function again with a different set of arguments.</strong> In the example here, our new function call will set the <em>min</em> to one place after <em>guess</em>. This works in the other direction as well; if the name at <em>guess</em> is smaller than the target, then the new <em>max</em> becomes one less than <em>guess.</em></p><h3>Conclusion</h3><p>Binary searches are a great example of an algorithm looks elegant when implemented recursively. However, keep in mind that recursion is more costly than looping in terms of performance.</p><p>This post was inspired by the following GitHub repository, which I highly recommend if you’re looking to practice recursion: <a href="https://github.com/JS-Challenges/recursion-prompts">https://github.com/JS-Challenges/recursion-prompts</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b29efaff64d6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/jsunderthescope/a-recursive-binary-search-in-javascript-b29efaff64d6">A recursive binary search in JavaScript</a> was originally published in <a href="https://medium.com/jsunderthescope">JSUnderTheScope</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>