<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: roadpilot</title>
    <description>The latest articles on DEV Community by roadpilot (@roadpilot).</description>
    <link>https://dev.to/roadpilot</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F651453%2Fa39bddeb-dde8-4662-ab3d-d15c6f0aff4a.png</url>
      <title>DEV Community: roadpilot</title>
      <link>https://dev.to/roadpilot</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/roadpilot"/>
    <language>en</language>
    <item>
      <title>Recursion: Two ways to convert from a JavaScript for-loop</title>
      <dc:creator>roadpilot</dc:creator>
      <pubDate>Mon, 11 Oct 2021 02:56:19 +0000</pubDate>
      <link>https://dev.to/roadpilot/recursion-two-ways-to-convert-from-a-javascript-for-loop-5eil</link>
      <guid>https://dev.to/roadpilot/recursion-two-ways-to-convert-from-a-javascript-for-loop-5eil</guid>
      <description>&lt;p&gt;One of the cases you can use for a JavaScript example of a for-loop is an iteration through an array.  For example, we will use an array of strings:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = ['how','to','iterate','an','array']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To iterate through the array, we can use a for-loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arrayLoop = arr =&amp;gt; {
  for (let i=0; i&amp;lt;arr.length; i++){
      console.log(arr[i])
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will loop through the array and perform whatever operation is in the block, in this case send the array element at the index of i to the console.  The "i" index is what is actually controlling the loop.  The loop is incrementing i, starting at 0 and adding 1 each cycle until i reaches the length of the array.  Since the index is 0 based, the index of the first element of the array is 0 and the index of the last element is a number that is one less than the total number of elements (length) in the array.  For example, if there are 4 elements in the array, the last element would have an index of 3 (one less than 4).  The loop continues as long as the increment (index) is less than the length of the array (4).&lt;/p&gt;

&lt;p&gt;One way to convert that for-loop to a recursion is to move the index into the argument for the function and then recursively call the function incrementing "i" in the call:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arrayLoop = (arr, i=0) =&amp;gt; {
  if (i&amp;gt;arr.length-1){return}
  console.log(arr[i])
  return (arrayLoop(arr, i+1))
}

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

&lt;/div&gt;



&lt;p&gt;Setting "i" to 0 as a default value allows you to call the function with just the "arr" argument.  Just like in the for-loop, the index starts at 0 and increments in each recursive call until the loop breaks when "i" is greater than the length of the array.&lt;/p&gt;

&lt;p&gt;Another option would be to have actually change or "mutate" the array passed into the argument.  It the following example, we'll change each element to an upper case replacement of the element contents and create a new array while we change the contents of the source array.  The source array passed in as an argument will no longer exist once the loop is completed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let result=[]
const capitalizeWords = arr =&amp;gt; {
  if (arr.length&amp;lt;1){return result}
  result.push(arr[0].toUpperCase())
  arr.shift()
  return (capitalizeWords(arr))
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each cycle through the loop takes the first element of the array, changes the element contents to upper case letters, adds it to the "result" array and then removes it from the source array (shift).  This moves the next element into the "0" index position and shortens the array by element that was just removed.  Then the function is recursively called until the length of the array is 0 - when all of the array elements have been "shifted" out of the array.&lt;/p&gt;

&lt;p&gt;Recursion can be a little overwhelming.  Little exercises like this help you learn a little better understanding of recursion and different ways to achieve it.  If you need to find a recursive solution to a problem, you might be able to start from a for-loop and then convert it from there.  Hope this was helpful.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>SQL: Find the sum using two tables (Join and Subquery)</title>
      <dc:creator>roadpilot</dc:creator>
      <pubDate>Mon, 04 Oct 2021 02:22:36 +0000</pubDate>
      <link>https://dev.to/roadpilot/sql-find-the-sum-using-two-tables-join-and-subquery-3c64</link>
      <guid>https://dev.to/roadpilot/sql-find-the-sum-using-two-tables-join-and-subquery-3c64</guid>
      <description>&lt;p&gt;The problem statement:&lt;br&gt;
"Given the CITY and COUNTRY tables, query the sum of the populations of all cities where the CONTINENT is 'Asia'.&lt;/p&gt;

&lt;p&gt;Note: CITY.CountryCode and COUNTRY.Code are matching key columns."&lt;/p&gt;

&lt;p&gt;The CITY table is a table of cities with a "POPULATION" field.  The field is numeric so we can use native "sum" to add up the populations of the cities that meet the criteria.  The CITY table uses "CountryCode" as it's foreign key to create the relationship to the COUNTRY table.&lt;/p&gt;

&lt;p&gt;The COUNTRY table is a table of countries and with a "Code" primary key and a "CONTINENT" which has a value for each continent in which that the country is found.  The "Code" field is the object of the CITY table foreign key.  The COUNTRY table also has "POPULATION" field.&lt;/p&gt;

&lt;p&gt;For our purposes, we want to first find the the countries from the COUNTRY table where 'Asia' is the continent:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select code from country where continent='Asia'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This gives us the "Code" of all countries that are in Asia.  We have fulfilled our criteria.  But this is in the "Country" table, and we need to filter the "City" table to get the sum of the populations.&lt;/p&gt;

&lt;p&gt;Now that we know the Country "Code" recordset, we can filter the CITY table by the cities that have the "CountryCode" from our previous query.  We will use our previous query as a subquery for this part of the filtering:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select population from city 
where countrycode in 
(select code from country where continent='Asia')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will now filter our City table and return the populations for only the cities that are in Asia.&lt;/p&gt;

&lt;p&gt;The final step is to add the "sum" function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select sum(population) from city 
where countrycode in 
(select code from country where continent='Asia')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The sum function takes the result of the filtering criteria and returns a number that is the sum of all of the field values, in this all (POPULATION) values that meet the criteria (continent='Asia')&lt;/p&gt;

&lt;p&gt;This was the subquery method of solving this problem.  We can also create the relation between the two tables by using a JOIN statement.  The JOIN statement's main purpose is to combine rows from one or more tables based on a match condition.  The combined row set is then available by the select statement for use to display, filter, or group by the columns.  There was a little hint in the problem statement:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Note: CITY.CountryCode and COUNTRY.Code are matching key columns.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We used this same information to form the relationship with our subquery but now we're going to use it in a JOIN statement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select sum(CITY.population) from city 
inner join country 
on CITY.CountryCode=COUNTRY.Code 
where continent='Asia'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice we had to specify CITY.population because when you join two or more tables, there may be duplication of field names and if you don't specify, the query engine will not know which "population" field you are referring to (it will give you an error on execution).  &lt;/p&gt;

&lt;p&gt;We use our select statement to query the sum of the population field from the CITY table when joined to the COUNTRY table.  We match the CITY.CountryCode value to the COUNTRY.Code value to create the relationship (JOIN the tables).  Then we use the 'continent' field to filter to the 'Asia' continents.  Notice we don't have to specify 'COUNTRY.contentent' - that's because there is no "continent" field in the CITY table.  Only when there are duplicated field names do you have to specify the "table.fieldname" structure.  There may be times when you want to, to prevent confusion, but you don't have to.&lt;/p&gt;

&lt;p&gt;In this particular scenario, using a subquery probably doesn't make much difference from from using a JOIN statement, but in a larger scale database or a more complex query where every subquery has to be evaluated, it may make a difference in processing time.  Some things to consider when choosing one over the other.  Subqueries are nice because they can stand alone and you can test as you go, but you might end up taking more of a processing hit than if you'd used a JOIN.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>SQL "BETWEEN" Operator</title>
      <dc:creator>roadpilot</dc:creator>
      <pubDate>Mon, 27 Sep 2021 04:17:48 +0000</pubDate>
      <link>https://dev.to/roadpilot/sql-between-operator-3oo3</link>
      <guid>https://dev.to/roadpilot/sql-between-operator-3oo3</guid>
      <description>&lt;p&gt;Take the following query directive:&lt;br&gt;
"Query the sum of Northern Latitudes (LAT_N) from STATION having values greater than 38.7880 and less than 137.2356."&lt;/p&gt;

&lt;p&gt;Imagine you have a table that has multiple records of stations with northern latitudes of various values - some above, some below, and some within the criteria range.  For testing purposes, all edge cases will be represented.  There may even be some values that are exactly at the critera range values.  Remember the criteria is "greater than" and "less than".&lt;/p&gt;

&lt;p&gt;You have two choices:&lt;/p&gt;

&lt;h1&gt;
  
  
  1. To write your query to include a combination condition (where statement), such as:
&lt;/h1&gt;

&lt;p&gt;"select sum(lat_n) from station &lt;br&gt;
WHERE lat_n &amp;gt; 38.7880 &lt;br&gt;
AND lat_n &amp;lt; 137.2356"&lt;/p&gt;

&lt;h1&gt;
  
  
  2. Or you could use "BETWEEN"
&lt;/h1&gt;

&lt;p&gt;"select sum(lat_n) from station &lt;br&gt;
WHERE lat_n BETWEEN 38.7880 AND 137.2356"&lt;br&gt;
*** BETWEEN can be a comparison operator for number, text or date values.  You can even find the inverse of the range with NOT BETWEEN&lt;/p&gt;

&lt;p&gt;Syntactically, #2 is cleaner and a little bit more readable.  But are they exactly identical?&lt;/p&gt;

&lt;p&gt;BETWEEN is "inclusive" - the result will include the criteria, if it exists.  It is essentially "greater than or equal to (&amp;gt;=)" and "less than or equal to (&amp;lt;=)".  They are not identical solutions.&lt;/p&gt;

&lt;p&gt;You could "manually" alter the range beginning and ending but you would need to know the exact level of specificity of the decimal in the data field.  You could just add "1" and make the  beginning of the range 39.7880 but that would still leave everything between 38.7881 and 39.7879 values that &lt;em&gt;should&lt;/em&gt; be included in the result but won't be.  You could add 1 to the last decimal and make the beginning of the range 38.7881 but then you would miss 38.78805, if it were to exist.  Unless you know the specificity of the data, this kind of manual alteration really could produce inaccurate results.&lt;/p&gt;

&lt;p&gt;I'm thinking, unless you want the results to be inclusive of the range, my rule of thumb will be to use the combination where statement using "greater than (&amp;gt;)" and "less than (&amp;lt;)".  If the result needs to return values greater than 38.7880, then that could be anything from 38.7881 or 39 or even 38.7880000000000001.  I can easily make this approach "inclusive" if I want to.  Just knowing that "BETWEEN" is inclusive will make me be more careful about choosing to use it.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>ES6 - What changes did it bring?</title>
      <dc:creator>roadpilot</dc:creator>
      <pubDate>Mon, 20 Sep 2021 04:30:38 +0000</pubDate>
      <link>https://dev.to/roadpilot/es6-what-changes-did-it-bring-1p1m</link>
      <guid>https://dev.to/roadpilot/es6-what-changes-did-it-bring-1p1m</guid>
      <description>&lt;p&gt;In 2015, ECMA International, the organization that prescribes the standards for client-side scripting for web browsers introduced ES6 or ECMAScript 6. The changes that ES6 brought were related to Arrow Functions, defining variables, Object Manipulation, "For" loops and the introduction of a new primitive data type 'symbol'.&lt;/p&gt;

&lt;p&gt;Arrow Functions:&lt;br&gt;
Before ES6, a function was declared using the "function" keyword.  A function would use the "return" keyword to produce the output of the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function timesThree(num){
  return (num * 3)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With arrow functions, you are not required to use the "function" keyword and return can be implicit (unstated)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const timesThree = (num) =&amp;gt; num * 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Defining Variables:&lt;br&gt;
Did you see that "const" above there?  That's also something new.  Two new ways of defining variables was added.  "const" is a constant value and can not be changed once declared and "let" is the other way that allows a variable to be defined but can allow changes to the variables value after being declared.  Previous to "const" and "let", "var" was the method to define a variable but would allow both mutable and immutable variables to be defined.&lt;/p&gt;

&lt;p&gt;Object Manipulation:&lt;br&gt;
ES6 introduced destructuring of arrays.  This could be a blog of it's own (maybe it will ...) in the meantime, you can learn more about destructuring (and the spread operator) here from Mozilla: &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;"For" loops:&lt;br&gt;
ES6 introduced "for ... of" and "for ... in" loops.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let primes = [2, 3, 5, 7];

for(value of primes) {
  console.log(value);
}
//iterates through the array and assigns the VALUE of each element to the "value" variable
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let primes = [2, 3, 5, 7];

for(index in primes) {
  console.log(index);
}
//iterates through the array and assigns the INDEX of each element to the index variable (this is returned as a string, and not an integer)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Symbol Data Type:&lt;br&gt;
A symbol is a unique and immutable data type and may be used as an identifier for object properties. The symbol object is an implicit object wrapper for the symbol primitive data type.  This primitive type is useful for so-called "private" and/or "unique" keys.  It's value is kept private and only for internal use.  No user can overwrite it's value. This prevents name clashing between object properties because no symbol is equal to another.  You create a symbol calling the "Symbol()" reserved function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const userID = Symbol()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All of these changes result in an increase in performance but there is no browser that fully supports the ES6 features.  Community support is growing but not as prolific as the pre-ES6 community.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Building a dynamic datalist from a database, Part 1</title>
      <dc:creator>roadpilot</dc:creator>
      <pubDate>Mon, 13 Sep 2021 01:04:18 +0000</pubDate>
      <link>https://dev.to/roadpilot/building-a-dynamic-datalist-from-a-database-part-1-2594</link>
      <guid>https://dev.to/roadpilot/building-a-dynamic-datalist-from-a-database-part-1-2594</guid>
      <description>&lt;p&gt;If you don't know what a datalist is, it is essentially a way to tie a list of options (like you would have in an HTML "select" element) to a single-line text input.  With the "select" element, you are restricted to only the options contained in the select options list.  With a "datalist", you can free-form type into the field and if a match is found in your datalist then that option will be chosen.  This is a great way to allow new entries to be created in an existing database field without creating duplicates.  With the traditional select element, you would not be able to create new entries.  Here is how Mozilla demonstrates the structure for a datalist:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;label for="ice-cream-choice"&amp;gt;Choose a flavor:&amp;lt;/label&amp;gt;
&amp;lt;input list="ice-cream-flavors" id="ice-cream-choice" name="ice-cream-choice" /&amp;gt;

&amp;lt;datalist id="ice-cream-flavors"&amp;gt;
    &amp;lt;option value="Chocolate"&amp;gt;
    &amp;lt;option value="Coconut"&amp;gt;
    &amp;lt;option value="Mint"&amp;gt;
    &amp;lt;option value="Strawberry"&amp;gt;
    &amp;lt;option value="Vanilla"&amp;gt;
&amp;lt;/datalist&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your input field has a "list" parameter which matches to the "datalist" id parameter.  As long as those two match, you have a functioning datalist.&lt;/p&gt;

&lt;p&gt;But what if you had a LARGE list of options from a database, like the list of every possible city in every state.  You wouldn't want to necessarily build that datalist from database every time you load the page.  You would want it to be something dynamic.  Something that would limit the number of results and make your data use lean.  You could create an experience where every time your user types a key, a new limited list of choices would be provided based on the input provided, reducing the round trip data from your database.  You would progressively reduce the number of options with every keystroke.  You could add an event listener to your input field, probably something like "oninput" or "onkeyup" - I've used both and haven't found really a difference when to choose one over the other.  You can add the event listener with a script block or you can just make it inline in the element definition itself.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;before:
&amp;lt;input list="ice-cream-flavors" id="ice-cream-choice" name="ice-cream-choice" /&amp;gt;

after:
&amp;lt;input list="ice-cream-flavors" id="ice-cream-choice" name="ice-cream-choice" oninput="myFunction(this)"/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we've added the "oninput" event listener, and we've pointed it to a "myFunction" function, and we are passing the "this" value to the function as an argument.  The myFunction function will be updating the datalist and the function will have to know what element to update - "this" will tell the function, by way of the "oninput" event, which datalist to update.  Now we need to build the myFunction function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function myFunction(el){
   console.log(el)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For now, we're just going to get the oninput event listener to fire and give us a console output to show us what happens when we create input into the text input field.  In Part 2, we will flesh out the "myFunction" function to do some cool background connecting to a database to pull the rest of the ice cream flavors.  That coconut flavor looks pretty enticing.  I'm going to have to find me some of that!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Autoresizing Textarea element</title>
      <dc:creator>roadpilot</dc:creator>
      <pubDate>Mon, 06 Sep 2021 02:45:17 +0000</pubDate>
      <link>https://dev.to/roadpilot/autoresizing-textarea-element-547n</link>
      <guid>https://dev.to/roadpilot/autoresizing-textarea-element-547n</guid>
      <description>&lt;p&gt;Have you ever been filling out a web form that used single-line text input fields for input that far exceeds the length of the field?  You have to scroll from end to end to verify what's actually been typed.  A textarea field would be far more suitable but even that is clunky with it's fixed size - still requiring scrolling.  I've found a few solutions around the web to make a textarea resize itself based on the contents and I've distilled them all down to the following very easy steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add the CSS that makes the "autoresize" class (you can name it anything you want):
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;style&amp;gt;
.autoresize{
    resize: none;
    overflow-y: hidden;
    overflow-x: hidden;
}
&amp;lt;/style&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;resize:none: hides the resizing handle from the textarea&lt;br&gt;
overflow-y: hidden: prevents the textarea from showing scrollbars when the content overflows or reaches the horizontal boundary of the input&lt;br&gt;
overflow-x: hidden: fixes a little feature in Firefox that adds an additional line at the end of the textarea content when there is only one line.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add the function to make the element size change relative to its content
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script&amp;gt;
function autoResize(elem) {
    elem.style.height = 'auto';
    elem.style.height = (elem.scrollHeight-4) + 'px';
}
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;elem.style.height = 'auto': this resets the size of the textarea element (passed in as 'elem') just before setting it to the size content based size, without this the textarea will increase the size automatically, but it will not decrease the size once it has been increased.&lt;br&gt;
elem.style.height = (elem.scrollHeight-4) + 'px': this sets the textarea to the size of the 'scrollHeight' of the element - this is the size the textarea &lt;em&gt;would be&lt;/em&gt; based on the size of the content and also adds the padding for the element (4px).  The 4px is subtracted back out or the end result would be cumulative and would increase by 4 additional pixes with every resize.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create the textarea element and add the CSS class created above:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;textarea class="autoresize" oninput="autoResize(this)" rows="1"&amp;gt;&amp;lt;/textarea&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;Add the "oninput" event listener and point it to the "autoResize" function passing in "this" for the function argument (in this case "this" refers to the textarea element itself).  This fires the function for resizing on every keystroke of text input into the textarea element. When the scrollHeight of the element changes because the line &lt;em&gt;would&lt;/em&gt; wrap, the autoResize function increases the height of the textarea element.  The same happens in reverse when removing characters, making the textarea element resize down when a line break is no longer part of the current line.&lt;/p&gt;

&lt;p&gt;If you add an event listener that captures the "Enter" key and map that to submit the form your textarea is contained by, the autoresize textarea can function just like a text input form element, without scrolling off the edge of the input field.&lt;/p&gt;

&lt;p&gt;That's it.  I've tried to make it as streamlined as possible.  Let me know if you know of other alternative solutions.  This one works for me but there may be other ways.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Optimizing JavaScript</title>
      <dc:creator>roadpilot</dc:creator>
      <pubDate>Sun, 29 Aug 2021 16:29:59 +0000</pubDate>
      <link>https://dev.to/roadpilot/optimizing-javascript-5g56</link>
      <guid>https://dev.to/roadpilot/optimizing-javascript-5g56</guid>
      <description>&lt;p&gt;One of my daily code challenges this week was to return the number of occurrences of the max value of an array.  At first I just tried the "brute force" iteration approach with a dictionary to keep track of the count:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    const ar = [3,2,1,3] // there are 2 3's so 2 should be my result
    const dict = {}
    for (let i=0; i&amp;lt;ar.length; i++){
        if (dict[ar[i]]){
            dict[ar[i]]++
        } else {
            dict[ar[i]]=1
        }
    }
    // console.log(Object.keys(dict).length)
    return (Object.values(dict).sort()[Object.keys(dict).length-1])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;... which did work but I always like to shoot for a "one-line" solution.&lt;br&gt;
So, I took my iteration and started thinking through the built in prototype.Array functions in JavaScript.  I decided to use "filter()" because it will return a sub-set of the original array.  Then I can use "length" to get the count of the elements that match the filter.  Seems easy enough:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ar.filter(el =&amp;gt; el == 3).length // returns 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;My "3" comparison value is just psuedocode for now, only because I already know the max value.  Now I have to find the max value so I can make this a dynamic function.  I can use "Math.max()" to determine the max value of the array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Math.max(...ar) // returns 3
// using the spread operator "..." is shorthand for using "apply".  The "apply" method could also be used:
Math.max.apply(null, ar)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So now I can just replace my psuedocode value with the function to find the max of the array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ar.filter(el =&amp;gt; el == Math.max(...ar)).length // returns 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;...which does work but since this is a code challenge, the solution times out and fails the test cases (if you do code challenges, you know what this means).&lt;br&gt;
I examined what was happening and noticed that even if a function is nested within another function - on one line or otherwise - it still is a nested function.  For every element of the ar filter step, it was checking to find the max value of the array.  I'm pretty sure this would be O(n^2).  But if I create a variable (maxVal) and assign the max value (Math.max(...ar)) OUTSIDE of the filter function, then the timeout errors go away and all test cases pass:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    const maxVal = Math.max(...ar)
    return (ar.filter(el =&amp;gt; el == maxVal).length)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, not exactly a one line solution but much leaner and cleaner than the "brute force" method where I started.  And a valuable lesson, for sure!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>dcOffice - Added functionality: Providing scheduling details from Notes screen</title>
      <dc:creator>roadpilot</dc:creator>
      <pubDate>Mon, 23 Aug 2021 01:29:53 +0000</pubDate>
      <link>https://dev.to/roadpilot/dcoffice-added-functionality-providing-scheduling-details-from-notes-screen-16lj</link>
      <guid>https://dev.to/roadpilot/dcoffice-added-functionality-providing-scheduling-details-from-notes-screen-16lj</guid>
      <description>&lt;p&gt;This week, I decided to add some functionality to the web application that I use to run my office.  The ability already existed to pass a date or frequency for the next visit to the scheduling staff but the option to include what &lt;em&gt;type&lt;/em&gt; of appointment was lacking.  The system would just gather the default appointment type for the particular client and apply it.  But there were times when it was necessary to schedule for a different type of appointment or to change the type of appointment if a next appointment already existed.  I thought this would be pretty simple because the &lt;em&gt;root&lt;/em&gt; functionality already existed. I was wrong.&lt;/p&gt;

&lt;p&gt;First, the variable that was being passed was a simple string and because I wanted to piggy-back on that I decided that I would just take that string and convert it into a comma-separated string that could be split, when needed, into the individual components of the scheduling detail - the date or frequency and the type.  &lt;/p&gt;

&lt;p&gt;A little background on the path that this variable takes would add a little more context here:&lt;br&gt;
At the notes screen, there existed the way to indicate when the next appointment should happen.  From here, it would pass through:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the notes form handler, to&lt;/li&gt;
&lt;li&gt;the checkout verification handler, to&lt;/li&gt;
&lt;li&gt;the posting screen (where charges are entered), to&lt;/li&gt;
&lt;li&gt;the scheduling screen where the scheduling was performed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first, most quickest way to see if the changes I wanted to make would break the system was to just add the new variable and send it through the path and see what would happen.&lt;br&gt;
At the first stop the "notes form handler", there had to be significant modification because the form handler didn't really know what to do with the new field because it's job is to combine specific form elements into fields.  Since this variable had no previous assignment, it followed the default logic to be combined into the notes, which was not where I needed it to land.  Logic had to be written to take the new variable (appt type) and combine it with the existing variable (appt date) and separate them with comma's (as 'appt_date, appt_type'), now to be referred to as "CSV" and pass them along as a single field.  This portion of the alteration is complete.  The form handler performs as it needs to.&lt;/p&gt;

&lt;p&gt;The next step, the "checkout verification handler" which reviews a TON of information ... not necessary to the discussion here ... before being sent to the scheduling staff, didn't really need any alteration because it already was passing a single field to the next step - the "posting screen".&lt;/p&gt;

&lt;p&gt;This is where charges are entered for the date's services as well as other financial configurations, client data management - really the nerve center of the checkout and scheduling process.  Breaking it is catastrophic and will result in a barrage of phone calls about what the staff is not able to do.  Extensive testing is required for even the smallest change.  The posting screen already displays the information differently based on the existence of other future appointments or the absence.  Now it had to also display an additional difference if an existing appointment type was different than the provider just directed and sent from the notes.  The updated display needed to be able show the type, if the type needed to change and the CSV was split into it's individual values, the date and type.  I decided that a "red" accent to the information would be enough to call attention that something needed to change.  &lt;/p&gt;

&lt;p&gt;From the posting screen, the scheduling staff chooses a "button" and the "scheduling screen" appears, prepopulated with the information provided, as it passed from notes, to handler, to checkout, to posting and now to scheduling (we'll call this the "pipeline").  The existing functionality would automatically choose the appointment type from the client default (which is set by polling the previous appointment type or if a specific type was manually set - but I'm not going into that logic here) and show the available times for the date that was passed - through the pipeline.  But now the appointment type was being passed in externally.  This meant that the CSV had to be split into its components and the display had to be updated to show the "pipelined" appointment type.  If it was the same as the default, then the change is unnoticeable.  If it does not match, the display would update as needed and the available times would be populated based on the date and appt type.  If there was an existing appointment, then the information from that appointment was loaded (date and current time) so that when the appointment change was made - if necessary (different appointment types have different appointment durations and arrival times) then the appointment could be rescheduled at that point and the existing appointment information would no longer exist.  This is critical because if an existing appointment is accidentally duplicated, there WILL be confusion about which one is the accurate one, if any, and which one has the information that the client been given.  All of this confusion requires unnecessary (and embarrassing) communication with the client in order to clarify.  The idea of the system is to enhance efficiency, not hinder it.&lt;/p&gt;

&lt;p&gt;Ultimately, it was successful and a welcome function addition.  Now what would have required flow interruption and manual instruction can now be automated and, since part of the existing functionality was already there (and the process was completely there) this was added seamlessly.  Of course I watched by remote control for the day to monitor and correct any bugs that occurred.  There were only a few and mainly involved the visual display.  No underlying function interference occurred.  It was a good day. :)&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Python + Flask Pt 2 ... adding React</title>
      <dc:creator>roadpilot</dc:creator>
      <pubDate>Mon, 16 Aug 2021 02:02:43 +0000</pubDate>
      <link>https://dev.to/roadpilot/python-flask-pt-2-adding-react-4j8d</link>
      <guid>https://dev.to/roadpilot/python-flask-pt-2-adding-react-4j8d</guid>
      <description>&lt;p&gt;The last post was about creating a Python + Flask application and deploying on Heroku.  This post we are going to take that same Python + Flask application and add a React frontend to it.  First some unfinished touches on the Python + Flask backend.  To make it run locally, we'll need to make some changes:&lt;/p&gt;

&lt;p&gt;In our app.py, we are going to add CORS functionality and we are going to change our output from a simple string to a JSON object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from flask import Flask
from flask_cors import CORS #comment this on deployment

app = Flask(__name__)
CORS(app) #comment this on deployment

@app.route("/")
def hello_world():
    return {
      'resultStatus': 'SUCCESS',
      'message': "Hello, World!"
      }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can now test the Flask server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// ♥  flask run
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You may run into a lot of errors at this point.  You may need to adjust your Python installation or other modules.  When you get things finalized, what you see above will confirm that your Flask service is running.&lt;/p&gt;

&lt;p&gt;When we view the output from the browser, we will see The JSON object output.  This is how we are going to display output from our backend through the React frontend.  It will look like this in the browser:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "message": "Hello, World!",
  "resultStatus": "SUCCESS"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now it's time to create the React front end.  You can create a separate repository or just a new subdirectory in the existing one.  That's all up to personal preference.  Once you decide what your "root" folder will be, you will use the "create react app" command from that directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a new React app in that root directory.  This could take some time and troubleshooting.  See the end message below.&lt;/p&gt;

&lt;p&gt;Once the installation is successful, find 'app.js' in the 'src' subdirectory and make the following changes:&lt;br&gt;
(some of this is referenced from the &lt;a href="https://towardsdatascience.com/build-deploy-a-react-flask-app-47a89a5d17d9"&gt;https://towardsdatascience.com/build-deploy-a-react-flask-app-47a89a5d17d9&lt;/a&gt; tutorial)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import logo from './logo.svg';
import './App.css';
import React, { useEffect, useState } from 'react';
import axios from 'axios'

function App() {
  const [getMessage, setGetMessage] = useState({})
  useEffect(()=&amp;gt;{
    axios.get('http://localhost:5000').then(response =&amp;gt; {
      console.log("SUCCESS", response)
      setGetMessage(response)
    }).catch(error =&amp;gt; {
      console.log(error)
    })

  }, [])

  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;header className="App-header"&amp;gt;
        &amp;lt;img src={logo} className="App-logo" alt="logo" /&amp;gt;
        &amp;lt;p&amp;gt;React + Flask Tutorial&amp;lt;/p&amp;gt;
        &amp;lt;div&amp;gt;{getMessage.status === 200 ? 
          &amp;lt;h3&amp;gt;{getMessage.data.message}&amp;lt;/h3&amp;gt;
          :
          &amp;lt;h3&amp;gt;LOADING&amp;lt;/h3&amp;gt;}&amp;lt;/div&amp;gt;
      &amp;lt;/header&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The "stock" app.js file will already have some of this.  What we are going to add is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useEffect, useState } from 'react';
import axios from 'axios'

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

&lt;/div&gt;



&lt;p&gt;"useEffect and useState" are called "React Hooks" and you can Google to read about them.  They essentially add functionality that allow you to use "state" and other React features without writing a class. "axios" is a component for React that allows "in render" async requests.  It can request information from and send information to other electronic sources and turn their output into React usable objects.  In this case we will get our "Hello, World" output from our backend server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const [getMessage, setGetMessage] = useState({})
  useEffect(()=&amp;gt;{
    axios.get('http://localhost:5000').then(response =&amp;gt; {
      console.log("SUCCESS", response)
      setGetMessage(response)
    }).catch(error =&amp;gt; {
      console.log(error)
    })

  }, [])

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

&lt;/div&gt;



&lt;p&gt;"useState" is going to create for us a pair of "setter / getter" functions.  "getMessage" will call to and collect the response from the axios request (axios.get) and "setGetMessage" will return the response.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;header className="App-header"&amp;gt;
        &amp;lt;img src={logo} className="App-logo" alt="logo" /&amp;gt;
        &amp;lt;p&amp;gt;React + Flask Tutorial&amp;lt;/p&amp;gt;
        &amp;lt;div&amp;gt;{getMessage.status === 200 ? 
          &amp;lt;h3&amp;gt;{getMessage.data.message}&amp;lt;/h3&amp;gt;
          :
          &amp;lt;h3&amp;gt;LOADING&amp;lt;/h3&amp;gt;}&amp;lt;/div&amp;gt;
      &amp;lt;/header&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next you will start the React server (yes, even though React is a "frontend framework" it runs in a separate server.)&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;The React app "return" renders the output based on the response from the backend server.  There is a conditional (ternary) view:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        &amp;lt;div&amp;gt;{
          getMessage.status === 200 
          ? 
          &amp;lt;h3&amp;gt;{getMessage.data.message}&amp;lt;/h3&amp;gt;
          :
          &amp;lt;h3&amp;gt;LOADING&amp;lt;/h3&amp;gt;
          }
       &amp;lt;/div&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;where until the response returns a status code of 200, the React component will only show the word "LOADING".&lt;br&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SZ6kGn5u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pjpee9chjakz6g9yl4ui.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SZ6kGn5u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pjpee9chjakz6g9yl4ui.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
Once the response returns the 200 status code, then the React component will re-render the "getMessage.data.message" that is returned from the "getMessage" function.  Since the response object has a "key" of "message":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "message": "Hello, World!",
  "resultStatus": "SUCCESS"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the "value" of "message" is what is displayed, in this case "Hello, World!".&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ejEvLcap--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0gnxra3l3m29jr1oa4ki.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ejEvLcap--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0gnxra3l3m29jr1oa4ki.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;"npm" is a Node package manager and installs are rarely without bumps so be ready to troubleshoot any "missing component" errors or other deprecation warnings.  Google is your friend and Node is very complicated.  You will end up with a "node_modules" folder that has anywhere from 20,000 to 30,000 resources that Node uses to make React work.  When you get installation errors, sometimes just repeating the command you just sent - that gave you those errors - is all you need to do.  Other times, it's more complicated.  Read the errors closely and try to find competent resources for steps to take for corrections.  Good luck!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Python + Flask (from Windows WSL) on Heroku.</title>
      <dc:creator>roadpilot</dc:creator>
      <pubDate>Mon, 09 Aug 2021 04:52:56 +0000</pubDate>
      <link>https://dev.to/roadpilot/python-flask-from-windows-wsl-on-heroku-572o</link>
      <guid>https://dev.to/roadpilot/python-flask-from-windows-wsl-on-heroku-572o</guid>
      <description>&lt;p&gt;This week I was working on a "hackathon" project that involved an application written in Python and served through Flask.  I decided for testing that I would host it on Heroku.  Here's what I had to do in order to get it up and runnning.&lt;/p&gt;

&lt;p&gt;Following the steps in &lt;a href="https://devcenter.heroku.com/articles/getting-started-with-python"&gt;https://devcenter.heroku.com/articles/getting-started-with-python&lt;/a&gt;, revealed to me that there were no specific steps for using Flask.  So going through the steps, I'll talk about where you diverge - specifically as it pertains to making this happen on Windows (WSL).  ***These steps will not make the app able to run locally, this will only configure the Heroku deployment to run on Heroku.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Installing the Heroku CLI: In Windows WSL, if you are using Ubuntu for your Linux OS, you would follow the Ubuntu instructions:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl https://cli-assets.heroku.com/install.sh | sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once installed, you can use the heroku command from your command shell (you might need to close and reopen your terminal)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;heroku login
heroku: Press any key to open up the browser to login or q to exit
 ›   Warning: If browser does not open, visit
 ›   https://cli-auth.heroku.com/auth/browser/***
heroku: Waiting for login...
Logging in... done
Logged in as me@example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(all of these commands were taken from the original Heroku devcenter article)&lt;br&gt;
You have now installed the Heroku CLI&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Clone the Heroku sample Python app (it is a Django app, but we'll change that later)
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git clone https://github.com/heroku/python-getting-started.git
cd python-getting-started
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Deploy the sample app
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;heroku create
Creating app... done, ⬢ serene-caverns-82714
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git push heroku main
...lots of stuff here, ultimately...
remote: Verifying deploy... done.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Make one instance of the app to run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;heroku ps:scale web=1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And view it in your web browser&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;(If that doesn't work, just copy the URL that is generated during the 'git push heroku main' step)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; You have deployed the sample (Django) app.  Now let's make some changes to make a Flask app.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When you cloned the original git sample repo, four of the files cloned are where we are going to make our changes:&lt;br&gt;
procfile&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    web: gunicorn app:app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;requirements.txt&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;runtime.txt&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;app.py&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "&amp;lt;p&amp;gt;Hello, World!&amp;lt;/p&amp;gt;"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Now one more round of
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git push heroku main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and Heroku will take it from there.  Heroku reads the 'requirements.txt' file and knows to change the existing (remote) installation from Django to Flask.  You can now write whatever Python you want and it will serve through Heroku as a Flask app.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Leetcode: Squares of a sorted array</title>
      <dc:creator>roadpilot</dc:creator>
      <pubDate>Mon, 02 Aug 2021 03:39:19 +0000</pubDate>
      <link>https://dev.to/roadpilot/leetcode-squares-of-a-sorted-array-2i0g</link>
      <guid>https://dev.to/roadpilot/leetcode-squares-of-a-sorted-array-2i0g</guid>
      <description>&lt;p&gt;The Leetcode problem: "Given an integer array 'nums' sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order."&lt;/p&gt;

&lt;p&gt;First of all, why "non-decreasing"?  Would it not be just as accurate - and less confusing - to just call it "ascending"?  Is there something about "ascending" that I don't know?&lt;/p&gt;

&lt;p&gt;So, my thinking - not anywhere near what Big-O would dictate - would be to do the obvious ... "return an array of squares of each number (element) sorted ... ascending" (there, I said it!)&lt;/p&gt;

&lt;p&gt;So let's do that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var sortedSquares = function(nums) {
    return nums.map(x =&amp;gt; x*x).sort((a,b) =&amp;gt; a-b)
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, I am mapping each element of the 'nums' array and returning an array of it's squares.  Then I am sorting the output array numerically.  Note: if you don't sort numerically, using the ((a,b) =&amp;gt; a-b) sort parameters, you will sort "ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values".  Ah... there it is.  The difference between "ascending" and "non-decreasing".&lt;/p&gt;

&lt;p&gt;I thought this one-line solution was pretty slick and daisy-chaining prototype functions in the higher level of caliber.  And ... it did give the proper result.  But Leetcode is a little more picky than that.  And it's great to aspire to the Leetcode level of achievement.  It will get your further than making excuses like "who cares if the space and time complexities are above linear?"  So, when Leetcode gives you feedback like ...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Squaring each element and sorting the new array is very trivial,
could you find an O(n) solution using a different approach?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;...you feel about 10 feet less tall.  But there are clues in their subversive backhanded compliment.  "Can you find an O(n) solution".  They want you to travel through the array no more than once.  So let's see what we can come up with.&lt;/p&gt;

&lt;p&gt;First, let's set up a loop to go through the array.  But let's not loop through the whole ... wait a minute ...&lt;/p&gt;

&lt;p&gt;I checked and re-submitted it later and got this result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Runtime: 104 ms, faster than 96.77% of JavaScript online
submissions for Squares of a Sorted Array.
Memory Usage: 45.4 MB, less than 79.37% of JavaScript online
submissions for Squares of a Sorted Array.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So I guess my solution was good enough after all.  &lt;/p&gt;

&lt;p&gt;The lessons this week are &lt;br&gt;
(1) to check your Leetcode submissions at all different times of the day.  Check back later and you may get a better score than you did at first.&lt;br&gt;
(2) there really is a difference between "ascending" and "non-decreasing" ... although "ascending" really could be corrected with just adding "numerically" ... which would still read easier. :)&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Something you may not know about the Javascript "Switch" statement...</title>
      <dc:creator>roadpilot</dc:creator>
      <pubDate>Mon, 26 Jul 2021 04:25:24 +0000</pubDate>
      <link>https://dev.to/roadpilot/something-you-may-not-know-about-the-javascript-switch-statement-390b</link>
      <guid>https://dev.to/roadpilot/something-you-may-not-know-about-the-javascript-switch-statement-390b</guid>
      <description>&lt;p&gt;The switch statement is used to perform different actions based on different conditions.  You use the switch statement to select one of many code blocks to be executed.  The following "if then" statement, where x could be any value but will only give a result with 0 or 1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (x===0) {
    text = "Zero";
} else if (x===1) {
    text = "One";
} else {
    text = "No value found";
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;...can be re-written as this "switch" statement:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;switch (x) {
  case 0:
    text = "Zero";
    break;
  case 1:
    text = "One";
    break;
  default:
    text = "No value found";
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;TIP: You always need to include the "break" command if you want the switch to stop when it finds a match.&lt;/p&gt;

&lt;p&gt;One thing to keep in mind, Switch cases use strict comparison (===).  The values must be of the same type to match.  A strict comparison can only be true if the operands are of the same type.&lt;br&gt;
In the above examples, "0" would not return a match but 0 would.&lt;/p&gt;

&lt;p&gt;But what if you wanted to switch on something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (time &amp;lt; 10) {
  greeting = "Good morning";
} else if (time &amp;lt; 20) {
  greeting = "Good day";
} else {
  greeting = "Good evening";
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You might think you could do it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;switch(time){
    case (&amp;lt;10):
        greeting = "Good morning";
        break;
    case (&amp;lt;20):
        greeting = "Good day";
        break;
    default:
        greeting = "Good evening";
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But switch comparisons do not work that way.  Switch follows these rules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The switch expression is evaluated once.&lt;/li&gt;
&lt;li&gt;The value of the expression is compared with the values of each case. (Since the values can not be compared, they can not match)&lt;/li&gt;
&lt;li&gt;If there is a match, the associated block of code is executed.&lt;/li&gt;
&lt;li&gt;If there is no match, the default code block is executed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But here's one thing you can do.  Since the value of the expression is compared to the values of each case, if you set the value of the expression to 'true', then you can make any case value an expression that would evaluate to true (or false) to test for a match.  For example, we already know the above switch expression will not work.  But if it were rewritten as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;switch(true){
    case (time &amp;lt; 10):
        greeting = "Good morning";
        break;
    case (time &amp;lt; 20):
        greeting = "Good day";
        break;
    default:
        greeting = "Good evening";
}

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

&lt;/div&gt;



&lt;p&gt;...then you could use "switch" as alternative to the "if then" JavaScript statement.&lt;/p&gt;

&lt;p&gt;The more you know!  &lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
