<?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 Jeremy Wood on Medium]]></title>
        <description><![CDATA[Stories by Jeremy Wood on Medium]]></description>
        <link>https://medium.com/@jwood206?source=rss-8c3df8ccd5ae------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*tPATf_xt5w5qcmL_Xnp41A.png</url>
            <title>Stories by Jeremy Wood on Medium</title>
            <link>https://medium.com/@jwood206?source=rss-8c3df8ccd5ae------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 22:35:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jwood206/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[CSS: Counters]]></title>
            <link>https://jwood206.medium.com/css-counters-dcd0df69e1dd?source=rss-8c3df8ccd5ae------2</link>
            <guid isPermaLink="false">https://medium.com/p/dcd0df69e1dd</guid>
            <category><![CDATA[lists]]></category>
            <category><![CDATA[content]]></category>
            <category><![CDATA[html]]></category>
            <category><![CDATA[counter]]></category>
            <category><![CDATA[css]]></category>
            <dc:creator><![CDATA[Jeremy Wood]]></dc:creator>
            <pubDate>Sat, 05 Feb 2022 09:55:48 GMT</pubDate>
            <atom:updated>2022-02-05T09:55:48.411Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Several clocks on a wall" src="https://cdn-images-1.medium.com/max/1024/1*8f399rq3ZHetn1tA_YHBiw.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@jontyson?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Jon Tyson</a> on <a href="https://unsplash.com/s/photos/clocks?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>I was recently watching some <a href="https://www.youtube.com/kepowob">Kevin Powell</a> videos on CSS, and I learned about an interesting feature of the CSS library. It turns out CSS provides us with counters, which are just variables that can be incremented, decremented, or reset.</p><p>These counters can be handy for a number of things. If you’re making a list, this makes it relatively easy to make custom numbers for all the items in that list. You can make lists that count down. If you were to make an index page that lists chapters, and that also lists numbered sections within each chapter, you could have separate counters running for each type of list. Let’s take a look at that last example.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528242%252C172%252C247%252C0.51%2529%26t%3Da11y-dark%26wt%3Dnone%26l%3Dhtmlmixed%26width%3D700%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D66px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D52px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%25250A%25253Col%252520class%25253D%252522index%252522%25253E%25250A%252520%252520%25253Cli%25253E%25250A%252520%252520%252520%252520First%252520Chapter%25250A%252520%252520%252520%252520%25253Col%252520class%25253D%252522sections%252522%25253E%25250A%252520%252520%252520%252520%252520%252520%25253Cli%25253EFirst%252520Section%25253C%25252Fli%25253E%25250A%252520%252520%252520%252520%252520%252520%25253Cli%25253ESecond%252520Section%25253C%25252Fli%25253E%25250A%252520%252520%252520%252520%252520%252520%25253Cli%25253EThird%252520Section%25253C%25252Fli%25253E%25250A%252520%252520%252520%252520%25253C%25252Fol%25253E%25250A%252520%252520%25253C%25252Fli%25253E%25250A%252520%252520%25253Cli%25253E%25250A%252520%252520%252520%252520Second%252520Chapter%25250A%252520%252520%252520%252520%25253Col%252520class%25253D%252522sections%252522%25253E%25250A%252520%252520%252520%252520%252520%252520%25253Cli%25253EFirst%252520Section%25253C%25252Fli%25253E%25250A%252520%252520%252520%252520%252520%252520%25253Cli%25253ESecond%252520Section%25253C%25252Fli%25253E%25250A%252520%252520%252520%252520%252520%252520%25253Cli%25253EThird%252520Section%25253C%25252Fli%25253E%25250A%252520%252520%252520%252520%252520%252520%25253Cli%25253EFourth%252520Section%25253C%25252Fli%25253E%25250A%252520%252520%252520%252520%25253C%25252Fol%25253E%25250A%252520%252520%25253C%25252Fli%25253E%25250A%252520%252520%25253Cli%25253E%25250A%252520%252520%252520%252520Third%252520Chapter%25250A%252520%252520%252520%252520%25253Col%252520class%25253D%252522sections%252522%25253E%25250A%252520%252520%252520%252520%252520%252520%25253Cli%25253EFirst%252520Section%25253C%25252Fli%25253E%25250A%252520%252520%252520%252520%252520%252520%25253Cli%25253ESecond%252520Section%25253C%25252Fli%25253E%25250A%252520%252520%252520%252520%25253C%25252Fol%25253E%25250A%252520%252520%25253C%25252Fli%25253E%25250A%25253C%25252Fol%25253E&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528242%25252C172%25252C247%25252C0.51%252529%26t%3Da11y-dark%26wt%3Dnone%26l%3Dhtmlmixed%26width%3D700%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D66px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D0px%26ph%3D52px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%2525250A%2525253Col%25252520class%2525253D%25252522index%25252522%2525253E%2525250A%25252520%25252520%2525253Cli%2525253E%2525250A%25252520%25252520%25252520%25252520First%25252520Chapter%2525250A%25252520%25252520%25252520%25252520%2525253Col%25252520class%2525253D%25252522sections%25252522%2525253E%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%2525253Cli%2525253EFirst%25252520Section%2525253C%2525252Fli%2525253E%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%2525253Cli%2525253ESecond%25252520Section%2525253C%2525252Fli%2525253E%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%2525253Cli%2525253EThird%25252520Section%2525253C%2525252Fli%2525253E%2525250A%25252520%25252520%25252520%25252520%2525253C%2525252Fol%2525253E%2525250A%25252520%25252520%2525253C%2525252Fli%2525253E%2525250A%25252520%25252520%2525253Cli%2525253E%2525250A%25252520%25252520%25252520%25252520Second%25252520Chapter%2525250A%25252520%25252520%25252520%25252520%2525253Col%25252520class%2525253D%25252522sections%25252522%2525253E%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%2525253Cli%2525253EFirst%25252520Section%2525253C%2525252Fli%2525253E%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%2525253Cli%2525253ESecond%25252520Section%2525253C%2525252Fli%2525253E%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%2525253Cli%2525253EThird%25252520Section%2525253C%2525252Fli%2525253E%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%2525253Cli%2525253EFourth%25252520Section%2525253C%2525252Fli%2525253E%2525250A%25252520%25252520%25252520%25252520%2525253C%2525252Fol%2525253E%2525250A%25252520%25252520%2525253C%2525252Fli%2525253E%2525250A%25252520%25252520%2525253Cli%2525253E%2525250A%25252520%25252520%25252520%25252520Third%25252520Chapter%2525250A%25252520%25252520%25252520%25252520%2525253Col%25252520class%2525253D%25252522sections%25252522%2525253E%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%2525253Cli%2525253EFirst%25252520Section%2525253C%2525252Fli%2525253E%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%2525253Cli%2525253ESecond%25252520Section%2525253C%2525252Fli%2525253E%2525250A%25252520%25252520%25252520%25252520%2525253C%2525252Fol%2525253E%2525250A%25252520%25252520%2525253C%2525252Fli%2525253E%2525250A%2525253C%2525252Fol%2525253E&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/20b98ad83fcf0e3544817001b7ecdf49/href">https://medium.com/media/20b98ad83fcf0e3544817001b7ecdf49/href</a></iframe><p>Here is the basic HTML. We have a list with a class of “index”, this list contains chapters, and each chapter contains a nested list of sections within that chapter. Each of our lists is going to be numbered out of the gate, but we want to use custom numbers for all of our lists. We will have to change our list-style to none on our lists.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528242%252C172%252C247%252C0.51%2529%26t%3Da11y-dark%26wt%3Dnone%26l%3Dcss%26width%3D700%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D66px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D94px%26ph%3D52px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dol%252520%25257B%25250A%252520%252520%252520%252520list-style%25253A%252520none%25253B%25250A%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528242%25252C172%25252C247%25252C0.51%252529%26t%3Da11y-dark%26wt%3Dnone%26l%3Dcss%26width%3D700%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D66px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D94px%26ph%3D52px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dol%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520list-style%2525253A%25252520none%2525253B%2525250A%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/621b64d4b459ad3952f2fb37ada04ead/href">https://medium.com/media/621b64d4b459ad3952f2fb37ada04ead/href</a></iframe><p>Now we can make our counters and add them to our lists. One nice thing about counters is that you use the same command to both instantiate them, and to reset them. Therefore, the counter should be created where you want to reset it as well if possible. This is done with the counter-reset: ; property.</p><p>In our example we want one counter to track our chapters, and one to track our sections, so we will instantiate two counters. You simply pass the counter name of your choice to the counter-reset property.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528242%252C172%252C247%252C0.51%2529%26t%3Da11y-dark%26wt%3Dnone%26l%3Dcss%26width%3D700%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D66px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D68px%26ph%3D52px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D.index%252520%25257B%25250A%252520%252520%252520%252520counter-reset%25253A%252520chapter-counter%25253B%25250A%25257D%25250A%25250A.sections%252520%25257B%25250A%252520%252520%252520%252520counter-reset%25253A%252520section-counter%25250A%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528242%25252C172%25252C247%25252C0.51%252529%26t%3Da11y-dark%26wt%3Dnone%26l%3Dcss%26width%3D700%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D66px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D68px%26ph%3D52px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D.index%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520counter-reset%2525253A%25252520chapter-counter%2525253B%2525250A%2525257D%2525250A%2525250A.sections%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520counter-reset%2525253A%25252520section-counter%2525250A%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/f6b24ca53e92396d764376df327916c4/href">https://medium.com/media/f6b24ca53e92396d764376df327916c4/href</a></iframe><p>We just instantiated two new counters. Since we created the chapter-counter on our index class, the counter will also reset every time we encounter a new element with a class of index. Likewise our section-counter will reset at any new element with the sections class. This is great, because it makes the same counter reusable across different lists of the same class.</p><p>Now that we have our counters, we just need to attach them to our list items. An easy was to do this is with the ::before pseudo element. We can just select every item in the list, and append the pseudo element to it with our current counter number inside of it. Then we just increment the counter after every element.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528242%252C172%252C247%252C0.51%2529%26t%3Da11y-dark%26wt%3Dnone%26l%3Dcss%26width%3D700%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D66px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D46px%26ph%3D52px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D.index%252520%25257B%25250A%252520%252520%252520%252520counter-reset%25253A%252520chapter-counter%25253B%25250A%25257D%25250A%25250A.index%252520%25253E%252520li%25253A%25253Abefore%252520%25257B%25250A%252520%252520%252520%252520content%25253A%252520counter%2528chapter-counter%2529%252520%252522%25253A%252520%252522%25253B%25250A%252520%252520%252520%252520counter-increment%25253A%252520chapter-counter%25253B%25250A%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528242%25252C172%25252C247%25252C0.51%252529%26t%3Da11y-dark%26wt%3Dnone%26l%3Dcss%26width%3D700%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D66px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D46px%26ph%3D52px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D.index%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520counter-reset%2525253A%25252520chapter-counter%2525253B%2525250A%2525257D%2525250A%2525250A.index%25252520%2525253E%25252520li%2525253A%2525253Abefore%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520content%2525253A%25252520counter%252528chapter-counter%252529%25252520%25252522%2525253A%25252520%25252522%2525253B%2525250A%25252520%25252520%25252520%25252520counter-increment%2525253A%25252520chapter-counter%2525253B%2525250A%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/15332318ec0129addf7e6d1336db0897/href">https://medium.com/media/15332318ec0129addf7e6d1336db0897/href</a></iframe><p>For every line-item that is a direct child of our index class, we are adding a ::before pseudo element. We can use the content property to display the current value of our counter to the element. We then use the counter-increment property to increment the counter by one.</p><p>Keep a couple things in mind with counter-increment. If we wanted to, we could change the increment amount by just passing in a number after our counter; counter-increment: chapter-counter 5; would cause our counter to increment by 5 every time, counter-increment: chapter-counter -3; would decrement our counter by 3, and so on. Also note that it doesn’t matter if we call counter-increment: before or after we display it in our content property. Either way it will display the newly incremented value.</p><p>We then do the same thing with our section lists. Before each line item we append a pseudo element containing the current value of our section-counter, then increment it.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528242%252C172%252C247%252C0.51%2529%26t%3Da11y-dark%26wt%3Dnone%26l%3Dcss%26width%3D700%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D66px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D52px%26ph%3D52px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D.sections%252520%25257B%25250A%252520%252520%252520%252520counter-reset%25253A%252520section-counter%25250A%25257D%25250A%25250A.sections%252520%25253E%252520li%25253A%25253Abefore%252520%25257B%25250A%252520%252520%252520%252520content%25253A%252520counter%2528chapter-counter%2529%252520%252522.%252522%252520counter%2528section-counter%2529%252520%252522%25253A%252520%252522%25253B%25250A%252520%252520%252520%252520counter-increment%25253A%252520section-counter%25253B%25250A%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528242%25252C172%25252C247%25252C0.51%252529%26t%3Da11y-dark%26wt%3Dnone%26l%3Dcss%26width%3D700%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D66px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D52px%26ph%3D52px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D.sections%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520counter-reset%2525253A%25252520section-counter%2525250A%2525257D%2525250A%2525250A.sections%25252520%2525253E%25252520li%2525253A%2525253Abefore%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520content%2525253A%25252520counter%252528chapter-counter%252529%25252520%25252522.%25252522%25252520counter%252528section-counter%252529%25252520%25252522%2525253A%25252520%25252522%2525253B%2525250A%25252520%25252520%25252520%25252520counter-increment%2525253A%25252520section-counter%2525253B%2525250A%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/7dbf262e7a84e5607cdf79dc79ee8e67/href">https://medium.com/media/7dbf262e7a84e5607cdf79dc79ee8e67/href</a></iframe><p>The difference here is that we’re also grabbing the current count of our chapter-counter as well. This way we can concatenate the two for our custom bullet points. Our numbers increment for each section, and we’re done with that chapter. When we move on to the next chapter, we hit a new section. Once we hit the new section, counter-reset fires on our section-counter again, and the count starts over again for the new section.</p><p>Once we run through all of our lists we end up with this:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528242%252C172%252C247%252C0.51%2529%26t%3Da11y-dark%26wt%3Dnone%26l%3Dmarkdown%26width%3D700%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D66px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D12px%26ph%3D52px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D1%25253A%252520First%252520Chapter%25250A%2525091.1%25253A%252520First%252520Section%25250A%2525091.2%25253A%252520Second%252520Section%25250A%2525091.3%25253A%252520Third%252520Section%25250A2%25253A%252520Second%252520Chapter%25250A%2525092.1%25253A%252520First%252520Section%25250A%2525092.2%25253A%252520Second%252520Section%25250A%2525092.3%25253A%252520Third%252520Section%25250A%2525092.4%25253A%252520Fourth%252520Section%25250A3%25253A%252520Third%252520Chapter%25250A%2525093.1%25253A%252520First%252520Section%25250A%2525093.2%25253A%252520Second%252520Section&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528242%25252C172%25252C247%25252C0.51%252529%26t%3Da11y-dark%26wt%3Dnone%26l%3Dmarkdown%26width%3D700%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D66px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D12px%26ph%3D52px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D1%2525253A%25252520First%25252520Chapter%2525250A%252525091.1%2525253A%25252520First%25252520Section%2525250A%252525091.2%2525253A%25252520Second%25252520Section%2525250A%252525091.3%2525253A%25252520Third%25252520Section%2525250A2%2525253A%25252520Second%25252520Chapter%2525250A%252525092.1%2525253A%25252520First%25252520Section%2525250A%252525092.2%2525253A%25252520Second%25252520Section%2525250A%252525092.3%2525253A%25252520Third%25252520Section%2525250A%252525092.4%2525253A%25252520Fourth%25252520Section%2525250A3%2525253A%25252520Third%25252520Chapter%2525250A%252525093.1%2525253A%25252520First%25252520Section%2525250A%252525093.2%2525253A%25252520Second%25252520Section&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/954ccee00fc415d4feba8b884b56552b/href">https://medium.com/media/954ccee00fc415d4feba8b884b56552b/href</a></iframe><p>What’s more, since each of these numbers is a pseudo element, we can style each one accordingly. Now instead of just a plain old number, you can give it a background, a border, space it out to where you want it, add text to it, whatever you want! Pretty cool!</p><p>Unfortunately, as with many things on the web, there are some drawbacks to this system. Since the numbers are displayed in a content property, the numbers themselves cannot be selected via highlighting. This in turn means the contents of the pseudo element cannot be accessed by accessibility tools. Which means it’s important to think about where you use a system such as this. If it’s just for design purposes, then you’ll likely be fine. But if these elements are an integral part of understanding and navigating your site, you’ll likely want to consider other options.</p><p>There are more fun things you can do with counters. For instance, there’s a counter-style property which can turn your counter into roman numerals, alpha characters, and decimal lead numbers. There’s a counters() property that allows you to run, or nest, multiple instances of the same counter at once.</p><p>One of the coolest parts about the web is all these interesting tools that you might not use very often, but may end up saving your butt someday. There seems to be no end to these strange and exciting tools, libraries, and APIs in the web development space.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dcd0df69e1dd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What are CSS Keyframes?]]></title>
            <link>https://jwood206.medium.com/what-are-css-keyframes-f6d7f7744f9f?source=rss-8c3df8ccd5ae------2</link>
            <guid isPermaLink="false">https://medium.com/p/f6d7f7744f9f</guid>
            <category><![CDATA[css]]></category>
            <category><![CDATA[keyframes]]></category>
            <category><![CDATA[frontend-development]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[keyframe-animations]]></category>
            <dc:creator><![CDATA[Jeremy Wood]]></dc:creator>
            <pubDate>Sat, 29 Jan 2022 10:51:20 GMT</pubDate>
            <atom:updated>2022-01-29T10:51:20.621Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RVcCJiPIyZIOuzp7RM__LQ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@reskp?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Jametlene Reskp</a> on <a href="https://unsplash.com/s/photos/key?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/@keyframes">Keyframes</a> in CSS give us a way to define how our animations behave. They are used along with the <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Animations">animations</a> properties to describe what our elements will do, and help us to really fine tune them. And the best part is they’re easy to use!</p><p>If you’ve used the transition property in CSS, you may have wished you could tweak the animations a little more. Keyframes allow you to easily define individual points of action for an animation. This means instead of just describing the overall action you want to take place, you can describe different parts of that action, and change behavior from one action to the next.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528130%252C247%252C247%252C0.51%2529%26t%3Da11y-dark%26wt%3Dnone%26l%3Dcss%26width%3D700%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D15px%26ph%3D32px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%252540keyframes%252520move%252520%25257B%25250A%252520%252520%252520%252520from%252520%25257B%25250A%252520%252520%252520%252520%252520%252520%252520%252520transform%25253A%252520translateX%25280px%2529%25253B%25250A%252520%252520%252520%252520%25257D%25250A%252520%252520%252520%252520to%252520%25257B%25250A%252520%252520%252520%252520%252520%252520%252520%252520transform%25253A%252520translateX%2528120px%2529%25253B%25250A%252520%252520%252520%252520%25257D%25250A%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528130%25252C247%25252C247%25252C0.51%252529%26t%3Da11y-dark%26wt%3Dnone%26l%3Dcss%26width%3D700%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D15px%26ph%3D32px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%25252540keyframes%25252520move%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520from%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520transform%2525253A%25252520translateX%2525280px%252529%2525253B%2525250A%25252520%25252520%25252520%25252520%2525257D%2525250A%25252520%25252520%25252520%25252520to%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520transform%2525253A%25252520translateX%252528120px%252529%2525253B%2525250A%25252520%25252520%25252520%25252520%2525257D%2525250A%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/7aaa90168d083c880fb9165ab6822003/href">https://medium.com/media/7aaa90168d083c880fb9165ab6822003/href</a></iframe><p>In its most basic form, you can just define a keyframe as a simple translate from one spot to another. Here, I’ve named this keyframe “move”, but you can name it whatever is appropriate for your situation. The “from” is just setting where our animation will start, “to” is telling us where it will end. Here the element starts at its original location, and moves 120px on the X axis, or to the right.</p><p>But for more control, you can specify the percentage of the animation’s lifecycle you want the action to occur at, and the different properties you want to change. For instance, if you wanted to make a ghost bat, swinging from beyond the grave, the keyframe might look something like this:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528130%252C247%252C247%252C0.51%2529%26t%3Da11y-dark%26wt%3Dnone%26l%3Dcss%26width%3D700%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D15px%26ph%3D32px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%252540keyframes%252520swing%252520%25257B%25250A%252520%252520%252520%2525200%252525%25257Bopacity%25253A%2525200%25253B%25257D%25250A%252520%252520%252520%25252012%252525%25257Btransform%25253A%252520rotateZ%2528-10deg%2529%252520rotateX%252810deg%2529%25253B%252520opacity%25253A%2525200%25257D%25250A%252520%252520%252520%25252025%252525%25257Btransform%25253A%252520rotateZ%2528-21deg%2529%252520rotateX%252821deg%2529%25253B%252520opacity%25253A%2525200%25257D%25250A%252520%252520%252520%25252036%252525%25257Btransform%25253A%252520rotateZ%2528-33deg%2529%252520rotateX%252833deg%2529%25253B%252520opacity%25253A%252520.1%25257D%25250A%252520%252520%252520%25252050%252525%25257Btransform%25253A%252520rotateZ%2528-45deg%2529%252520rotateX%252845deg%2529%25253B%252520opacity%25253A%252520.2%25257D%25250A%252520%252520%252520%25252062%252525%25257Btransform%25253A%252520rotateZ%2528-58deg%2529%252520rotateX%252858deg%2529%25253B%252520opacity%25253A%252520.4%25257D%25250A%252520%252520%252520%25252075%252525%25257Btransform%25253A%252520rotateZ%2528-71deg%2529%252520rotateX%252871deg%2529%25253B%252520opacity%25253A%252520.6%25257D%25250A%252520%252520%252520%25252087%252525%25257Btransform%25253A%252520rotateZ%2528-83deg%2529%252520rotateX%252883deg%2529%25253B%252520opacity%25253A%252520.8%25257D%25250A%252520%252520%252520%252520100%252525%25257Btransform%25253A%252520rotateZ%2528-90deg%2529%252520rotateX%252890deg%2529%25253B%252520opacity%25253A%2525201%25257D%25250A%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528130%25252C247%25252C247%25252C0.51%252529%26t%3Da11y-dark%26wt%3Dnone%26l%3Dcss%26width%3D700%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D15px%26ph%3D32px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%25252540keyframes%25252520swing%25252520%2525257B%2525250A%25252520%25252520%25252520%252525200%25252525%2525257Bopacity%2525253A%252525200%2525253B%2525257D%2525250A%25252520%25252520%25252520%2525252012%25252525%2525257Btransform%2525253A%25252520rotateZ%252528-10deg%252529%25252520rotateX%25252810deg%252529%2525253B%25252520opacity%2525253A%252525200%2525257D%2525250A%25252520%25252520%25252520%2525252025%25252525%2525257Btransform%2525253A%25252520rotateZ%252528-21deg%252529%25252520rotateX%25252821deg%252529%2525253B%25252520opacity%2525253A%252525200%2525257D%2525250A%25252520%25252520%25252520%2525252036%25252525%2525257Btransform%2525253A%25252520rotateZ%252528-33deg%252529%25252520rotateX%25252833deg%252529%2525253B%25252520opacity%2525253A%25252520.1%2525257D%2525250A%25252520%25252520%25252520%2525252050%25252525%2525257Btransform%2525253A%25252520rotateZ%252528-45deg%252529%25252520rotateX%25252845deg%252529%2525253B%25252520opacity%2525253A%25252520.2%2525257D%2525250A%25252520%25252520%25252520%2525252062%25252525%2525257Btransform%2525253A%25252520rotateZ%252528-58deg%252529%25252520rotateX%25252858deg%252529%2525253B%25252520opacity%2525253A%25252520.4%2525257D%2525250A%25252520%25252520%25252520%2525252075%25252525%2525257Btransform%2525253A%25252520rotateZ%252528-71deg%252529%25252520rotateX%25252871deg%252529%2525253B%25252520opacity%2525253A%25252520.6%2525257D%2525250A%25252520%25252520%25252520%2525252087%25252525%2525257Btransform%2525253A%25252520rotateZ%252528-83deg%252529%25252520rotateX%25252883deg%252529%2525253B%25252520opacity%2525253A%25252520.8%2525257D%2525250A%25252520%25252520%25252520%25252520100%25252525%2525257Btransform%2525253A%25252520rotateZ%252528-90deg%252529%25252520rotateX%25252890deg%252529%2525253B%25252520opacity%2525253A%252525201%2525257D%2525250A%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/b325a29e492ca506ac4a859fe80f5ac3/href">https://medium.com/media/b325a29e492ca506ac4a859fe80f5ac3/href</a></iframe><p>The element changes at the percentages we specify. The element we apply this keyframe to will gain the opacity we want, and rotate exactly how much we want, precisely when we want. Now we just have to apply the keyframes to our elements, and animate them. For our swinging bat, this could look like this</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528130%252C247%252C247%252C0.51%2529%26t%3Da11y-dark%26wt%3Dnone%26l%3Dcss%26width%3D700%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D15px%26ph%3D32px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D.bat%25257B%25250A%252520%252520%252520%252520transform-origin%25253A%25252010px%252520200px%25253B%25250A%252520%252520%252520%252520animation-name%25253A%252520swing%25253B%25250A%252520%252520%252520%252520animation-timing-function%25253A%252520linear%25253B%25250A%252520%252520%252520%252520animation-duration%25253A%2525202s%25253B%25250A%252520%252520%252520%252520animation-delay%25253A%2525201s%25253B%25250A%252520%252520%252520%252520animation-direction%25253A%252520alternate%25253B%25250A%252520%252520%252520%252520animation-iteration-count%25253A%252520infinite%25253B%25250A%252520%252520%252520%252520animation-fill-mode%25253A%252520both%25253B%25250A%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528130%25252C247%25252C247%25252C0.51%252529%26t%3Da11y-dark%26wt%3Dnone%26l%3Dcss%26width%3D700%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D15px%26ph%3D32px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D.bat%2525257B%2525250A%25252520%25252520%25252520%25252520transform-origin%2525253A%2525252010px%25252520200px%2525253B%2525250A%25252520%25252520%25252520%25252520animation-name%2525253A%25252520swing%2525253B%2525250A%25252520%25252520%25252520%25252520animation-timing-function%2525253A%25252520linear%2525253B%2525250A%25252520%25252520%25252520%25252520animation-duration%2525253A%252525202s%2525253B%2525250A%25252520%25252520%25252520%25252520animation-delay%2525253A%252525201s%2525253B%2525250A%25252520%25252520%25252520%25252520animation-direction%2525253A%25252520alternate%2525253B%2525250A%25252520%25252520%25252520%25252520animation-iteration-count%2525253A%25252520infinite%2525253B%2525250A%25252520%25252520%25252520%25252520animation-fill-mode%2525253A%25252520both%2525253B%2525250A%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/fdff111fb4d33582395d8bc1515f4613/href">https://medium.com/media/fdff111fb4d33582395d8bc1515f4613/href</a></iframe><p>In this example, transform origin is just moving the anchor point for rotation, so our bat swings from its end and not its center. Our animation properties are what move our element.</p><p>Here we attach our “swing” keyframe to our element with animation-name. With timing-function, we can specify whether the animation changes speeds across its lifecycle. We set the duration of the animation, and the delay before it starts. With direction we can specify whether the animation runs in the normal direction from 0%-100% (from-to), or in reverse, or we can even have it alternate between normal and reverse. Iteration-count declares how many times the animation will run. Fill mode will let you keep an element at its starting and/or ending position, before or after the animation has run. This last one is very important, as once an animation is done, the element would otherwise default back to its original position</p><p>We can even use shorthand to make our animation properties much cleaner. With the animation property, you can specify all these properties in one place.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528130%252C247%252C247%252C0.51%2529%26t%3Da11y-dark%26wt%3Dnone%26l%3Dcss%26width%3D700%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D15px%26ph%3D32px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D.bat%25257B%25250A%252520%252520%252520%252520transform-origin%25253A%25252010px%252520200px%25253B%25250A%252520%252520%252520%252520animation%25253A%252520swing%2525202s%2525201s%252520infinite%252520linear%252520alternate%25253B%25250A%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528130%25252C247%25252C247%25252C0.51%252529%26t%3Da11y-dark%26wt%3Dnone%26l%3Dcss%26width%3D700%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D15px%26ph%3D32px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D.bat%2525257B%2525250A%25252520%25252520%25252520%25252520transform-origin%2525253A%2525252010px%25252520200px%2525253B%2525250A%25252520%25252520%25252520%25252520animation%2525253A%25252520swing%252525202s%252525201s%25252520infinite%25252520linear%25252520alternate%2525253B%2525250A%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/d540c950e1cacab85447e6df2c08abd1/href">https://medium.com/media/d540c950e1cacab85447e6df2c08abd1/href</a></iframe><p>Much better. And our reusability is looking great as well. We can apply the keyframes to multiple elements, and then change each individual elements’ behavior with the animation properties assigned to them.</p><p>Keyframes can be a very handy tool for animation, and are a lot of fun to mess around with. I highly recommend spending some time building them and exploring all the different animation options.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f6d7f7744f9f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[CSS: Min, Max, and Clamp]]></title>
            <link>https://jwood206.medium.com/css-min-max-and-clamp-845f7a5c3af9?source=rss-8c3df8ccd5ae------2</link>
            <guid isPermaLink="false">https://medium.com/p/845f7a5c3af9</guid>
            <category><![CDATA[css3]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[html]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[css]]></category>
            <dc:creator><![CDATA[Jeremy Wood]]></dc:creator>
            <pubDate>Sat, 22 Jan 2022 08:26:29 GMT</pubDate>
            <atom:updated>2022-01-22T08:26:29.710Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZOmdvmuCZw0-raiKNK4b7A.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@mattartz?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Matt Artz</a> on <a href="https://unsplash.com/s/photos/clamp?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>When I’m writing CSS, sometimes the amount of properties I have on my classes starts to feel ridiculous. One way we can reduce the amount of code in our CSS files is by using the min(), max(), and clamp() functions.</p><p>These functions allow you to specify which value from a list of values is applied to a specific CSS property. This is helpful when you want the value to dynamically change. For example, if you want an element’s width, height, or font size to change in response to different window sizes. These functions are also valuable because they can handle the job of more than one property at once. Here is a basic example of min and max.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528130%252C247%252C247%252C0.51%2529%26t%3Da11y-dark%26wt%3Dnone%26l%3Dcss%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D15px%26ph%3D32px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D.my-class%252520%25257B%25250A%252520%252520%252520%252520width%25253A%252520min%252850%252525%25252C%252520300px%2529%25253B%25250A%252520%252520%25250A%252520%252520%252509height%25253A%252520max%2528300px%25252C%25252050vh%2529%25253B%25250A%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528130%25252C247%25252C247%25252C0.51%252529%26t%3Da11y-dark%26wt%3Dnone%26l%3Dcss%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D15px%26ph%3D32px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D.my-class%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520width%2525253A%25252520min%25252850%25252525%2525252C%25252520300px%252529%2525253B%2525250A%25252520%25252520%2525250A%25252520%25252520%25252509height%2525253A%25252520max%252528300px%2525252C%2525252050vh%252529%2525253B%2525250A%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/b50e48b84f72e70b171076579cf1a715/href">https://medium.com/media/b50e48b84f72e70b171076579cf1a715/href</a></iframe><p>Min will set the width to the smallest of the values you pass in. The width of this element would be 50% of its parents’ width, until the parent grew larger than 600px. After that it would stay at the smaller 300px value.</p><p>Max will set the value to the larger of the values passed in. Here our element would stay at 300px until our window grew taller than 600px, at which point it would stay at the larger 50vh value.</p><p>To do this without min and max, we’d have to write this:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528130%252C247%252C247%252C0.51%2529%26t%3Da11y-dark%26wt%3Dnone%26l%3Dcss%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D15px%26ph%3D32px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D.my-class%252520%25257B%25250A%252520%252520%252520%252520width%25253A%25252050%252525%25253B%25250A%252520%252520%252520%252520max-width%25253A%252520300px%25253B%25250A%25250A%252520%252520%252520%252520height%25253A%252520300px%25253B%25250A%252520%252520%252520%252520min-height%25253A%25252050vh%25253B%25250A%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528130%25252C247%25252C247%25252C0.51%252529%26t%3Da11y-dark%26wt%3Dnone%26l%3Dcss%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D15px%26ph%3D32px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D.my-class%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520width%2525253A%2525252050%25252525%2525253B%2525250A%25252520%25252520%25252520%25252520max-width%2525253A%25252520300px%2525253B%2525250A%2525250A%25252520%25252520%25252520%25252520height%2525253A%25252520300px%2525253B%2525250A%25252520%25252520%25252520%25252520min-height%2525253A%2525252050vh%2525253B%2525250A%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/163bbc7e41f8cdc486599622542ccc01/href">https://medium.com/media/163bbc7e41f8cdc486599622542ccc01/href</a></iframe><p>We’ve not only shortened our code, but also arguably made it easier to quickly understand as well.</p><p>Lastly, the clamp function is a combination of the two. Clamp takes three arguments: the min, the preferred middle value, and the max.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528130%252C247%252C247%252C0.51%2529%26t%3Da11y-dark%26wt%3Dnone%26l%3Dcss%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D15px%26ph%3D32px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D.my-class%252520%25257B%25250A%252520%252520%252520%252520width%25253A%252520clamp%2528300px%25252C%25252050%252525%25252C%252520500px%25253B%25250A%252520%252520%252520%252520%252520%252520%25250A%252520%252520%252520%252520height%25253A%252520300px%25253B%25250A%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528130%25252C247%25252C247%25252C0.51%252529%26t%3Da11y-dark%26wt%3Dnone%26l%3Dcss%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D15px%26ph%3D32px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D.my-class%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520width%2525253A%25252520clamp%252528300px%2525252C%2525252050%25252525%2525252C%25252520500px%2525253B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%2525250A%25252520%25252520%25252520%25252520height%2525253A%25252520300px%2525253B%2525250A%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/1a8446f15b3f3a30a412328863b7ba2c/href">https://medium.com/media/1a8446f15b3f3a30a412328863b7ba2c/href</a></iframe><p>Here, the smallest the element can be is 300px, the largest is 500px, and if 50% of the parents’ width is between them, that’s your width. Turning 3 separate property declarations into one is pretty nice!</p><p>You can even do calculations inside the functions, without calling a calc() function. Just add the math inline and it’s handled.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528130%252C247%252C247%252C0.51%2529%26t%3Da11y-dark%26wt%3Dnone%26l%3Dcss%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D15px%26ph%3D32px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D.my-class%252520%25257B%25250A%252520%252520%252520%252520width%25253A%252520clamp%2528500px%25252C%252520100vw%252520-%25252020%252525%25252C%252520100vh%252520%252A%2525201.2%2529%25253B%25250A%252520%252520%25250A%252520%252520%252509height%25253A%252520300px%25253B%25250A%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528130%25252C247%25252C247%25252C0.51%252529%26t%3Da11y-dark%26wt%3Dnone%26l%3Dcss%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D15px%26ph%3D32px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D.my-class%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520width%2525253A%25252520clamp%252528500px%2525252C%25252520100vw%25252520-%2525252020%25252525%2525252C%25252520100vh%25252520%25252A%252525201.2%252529%2525253B%2525250A%25252520%25252520%2525250A%25252520%25252520%25252509height%2525253A%25252520300px%2525253B%2525250A%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/b327d5cd7f5e9dc4d531cfe9be43b46f/href">https://medium.com/media/b327d5cd7f5e9dc4d531cfe9be43b46f/href</a></iframe><p>These functions may just be syntactic sugar, but since they make the code easier to write, parse, and understand while not overly obscuring what is truly going on, I’d say they’re the best kind of syntactic sugar.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=845f7a5c3af9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Multi Threaded Ruby]]></title>
            <link>https://jwood206.medium.com/multi-threaded-ruby-8f88c39ff1f1?source=rss-8c3df8ccd5ae------2</link>
            <guid isPermaLink="false">https://medium.com/p/8f88c39ff1f1</guid>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[threads]]></category>
            <category><![CDATA[virtual-machine]]></category>
            <category><![CDATA[concurrency]]></category>
            <dc:creator><![CDATA[Jeremy Wood]]></dc:creator>
            <pubDate>Sat, 15 Jan 2022 10:37:42 GMT</pubDate>
            <atom:updated>2022-01-15T10:37:42.454Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vyy7N0awIHsrIyyZ7MsjzQ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@redviking509?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">John Anvik</a> on <a href="https://unsplash.com/s/photos/thread?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Ruby has a reputation of being slower than some other programming languages. One way we can help to improve this is by using different <a href="https://ruby-doc.org/core-2.5.0/Thread.html">Threads</a> to perform I/O (Input/Output) operations.</p><p>Ruby in its typical configuration is single threaded, as all operations need to be passed through a GVL, or Global Virtual Machine. When your Ruby code is run, it is tokenized with some C code, parsed out into an <a href="https://www.twilio.com/blog/abstract-syntax-trees">Abstract Syntax Tree</a>, which is then processed and finally executed by the Ruby Virtual Machine referred to as YARV (Yet Another Ruby VM). This stack-based virtual machine is where our optimization can occur.</p><p>YARV is not internally thread safe, and can’t run multiple <em>parallel</em> threads. This means it can only handle the processing of one thread at a time. And in some operations, such as database and API calls, the processing is handled outside of the virtual machine. Because of this, if you’re querying a database, making a request to an API, or pulling from some other external data sources, the rest of your code has to wait for that process to finish before anything else can be executed. Meanwhile your virtual machine is sitting idle, when it could be working on the rest of the stack.</p><p>We can add threads to our ruby code to make it more performant. If one thread is off querying our API, our VM can start executing another thread <em>concurrently</em>. When the data comes back from our API, that thread then waits for the VM to be free to be processed again.</p><p>It’s important to keep in mind, if you don’t have many of these API or Database calls, then threads may not benefit you at all, and if used incorrectly may even slow your code down. But if a good portion of your time is spent requesting data, multiple threads can add huge benefits.</p><p>Making threads is as easy as Thread.new { puts 1 + 1 }. Here’s a simple example.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528230%252C208%252C255%252C0.75%2529%26t%3Da11y-dark%26wt%3Dnone%26l%3Druby%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D15px%26ph%3D32px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%252520%252520%252520%252520%252520%252520%252520%252520threads%252520%25253D%252520%25255B%25255D%25250A%252520%252520%252520%252520%252520%252520%252520%252520threads%252520%25253C%25253C%252520Thread.new%252520%25257B%252520%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520uri%252520%25253D%252520URI%2528%2527https%25253A%25252F%25252Fpokeapi.co%25252Fapi%25252Fv2%25252Fpokemon%25252Fgengar%2527%2529%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520res%252520%25253D%252520Net%25253A%25253AHTTP.get_response%2528uri%2529%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252540gengar%252520%25253D%252520JSON.parse%2528res.body%2529%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%25250A%252520%252520%252520%252520%252520%252520%252520%252520%25257D%25250A%25250A%252520%252520%252520%252520%252520%252520%252520%252520threads%252520%25253C%25253C%252520Thread.new%252520%25257B%252520%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520uri%252520%25253D%252520URI%2528%2527https%25253A%25252F%25252Fpokeapi.co%25252Fapi%25252Fv2%25252Fpokemon%25252Fblastoise%2527%2529%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520res%252520%25253D%252520Net%25253A%25253AHTTP.get_response%2528uri%2529%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252540blastoise%252520%25253D%252520JSON.parse%2528res.body%2529%25250A%252520%252520%252520%252520%252520%252520%252520%252520%25257D%25250A%25250A%252520%252520%252520%252520%252520%252520%252520%252520threads.each%252520%25257B%252520%25257Cthr%25257C%252520thr.join%252520%25257D%25250A%25250A%252520%252520%252520%252520%252520%252520%252520%252520puts%252520%252540gengar%25255B%252522moves%252522%25255D%25250A%252520%252520%252520%252520%252520%252520%252520%252520puts%252520%252540blastoise%25255B%252522moves%252522%25255D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528230%25252C208%25252C255%25252C0.75%252529%26t%3Da11y-dark%26wt%3Dnone%26l%3Druby%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D15px%26ph%3D32px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520threads%25252520%2525253D%25252520%2525255B%2525255D%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520threads%25252520%2525253C%2525253C%25252520Thread.new%25252520%2525257B%25252520%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520uri%25252520%2525253D%25252520URI%252528%252527https%2525253A%2525252F%2525252Fpokeapi.co%2525252Fapi%2525252Fv2%2525252Fpokemon%2525252Fgengar%252527%252529%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520res%25252520%2525253D%25252520Net%2525253A%2525253AHTTP.get_response%252528uri%252529%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252540gengar%25252520%2525253D%25252520JSON.parse%252528res.body%252529%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525257D%2525250A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520threads%25252520%2525253C%2525253C%25252520Thread.new%25252520%2525257B%25252520%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520uri%25252520%2525253D%25252520URI%252528%252527https%2525253A%2525252F%2525252Fpokeapi.co%2525252Fapi%2525252Fv2%2525252Fpokemon%2525252Fblastoise%252527%252529%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520res%25252520%2525253D%25252520Net%2525253A%2525253AHTTP.get_response%252528uri%252529%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252540blastoise%25252520%2525253D%25252520JSON.parse%252528res.body%252529%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525257D%2525250A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520threads.each%25252520%2525257B%25252520%2525257Cthr%2525257C%25252520thr.join%25252520%2525257D%2525250A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520puts%25252520%25252540gengar%2525255B%25252522moves%25252522%2525255D%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520puts%25252520%25252540blastoise%2525255B%25252522moves%25252522%2525255D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/a56a862329a980c3765748ae63ae598b/href">https://medium.com/media/a56a862329a980c3765748ae63ae598b/href</a></iframe><p>The data requesting portions of these two threads are now non-blocking, allowing any other threads to continue running while they fetch their data. Here you can see we’re also creating an array and pushing our thread objects into it. We’re then looping over those threads, and calling .join on each one. Join is just a blocking operation for when you do need to wait for your threads to finish.</p><p>This isn’t the only native tool we have for concurrency. We also have <a href="https://ruby-doc.org/core-2.5.0/Fiber.html">Fibers</a>, which are very similar to threads in that they’re blocks of code that can be paused and resumed. Fibers however are started and paused programmatically by the developer. This allows you to implement generator style functions, and could feasibly be used to manually set up concurrent calls.</p><p>Rails 3 has also brought us the <a href="https://ruby-doc.org/core-3.0.0/Ractor.html">Ractor</a>. Ractor actually provides us thread-safe parallel execution. If you’re on the latest versions of Ruby you can now truly run your operations in parallel, as opposed to just concurrently. This is huge, as now all your code can be run in a non-blocking manner. There are of course rules however about the communication these Ractors can have between each other. You wouldn’t want to end up with multiple executions modifying the same piece of state after all.</p><p>There are also plenty of Gems to help you handle concurrency. If you’d just rather not deal with it, these can be a very simple option to toss in real quick. They also have the added benefit of having some edge case scenarios handled for you.</p><p>Performance in Ruby certainly doesn’t have to be a contradictory statement. Depending on your use case, concurrency and parallelism may be well worth the investment for your app.</p><p>Here are some great resources on threads, and concurrency in general.</p><p><a href="https://www.speedshop.co/2020/05/11/the-ruby-gvl-and-scaling.html">The Practical Effects of the GVL on Scaling in Ruby - Nate Berkopec</a></p><p><a href="https://sitaramshelke.medium.com/inside-rubyvm-967b25e234db">Inside RubyVM - Sitaram Shelke</a></p><p><a href="https://www.tutorialspoint.com/ruby/ruby_multithreading.htm">Ruby - Multithreading</a></p><p><a href="https://pawelurbanek.com/ruby-concurrent-requests">Concurrency for HTTP Requests in Ruby and Rails - Pawet Urbanek</a></p><p><a href="https://thesmartnik.com/why-on-earth-do-fibers-exist.html">Why on earth do fibers exist? - Nikita Misharin</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8f88c39ff1f1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JavaScript: Surprise Octal Numbers]]></title>
            <link>https://jwood206.medium.com/javascript-surprise-octal-numbers-c255add0a685?source=rss-8c3df8ccd5ae------2</link>
            <guid isPermaLink="false">https://medium.com/p/c255add0a685</guid>
            <category><![CDATA[8base]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[base]]></category>
            <category><![CDATA[octal]]></category>
            <category><![CDATA[binary]]></category>
            <dc:creator><![CDATA[Jeremy Wood]]></dc:creator>
            <pubDate>Sun, 09 Jan 2022 01:38:29 GMT</pubDate>
            <atom:updated>2022-01-09T01:38:29.253Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZKXPaS2_Rxb98WvKBfhYhQ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@cesconfra?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Francesco Dondi</a> on <a href="https://unsplash.com/s/photos/octagon?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>In a recent problem I was working on, I was parsing the individual ones and zeroes representing the bits of a binary number, and assigning them to variables. I quickly ran into something I didn’t expect.</p><p>When I tried to assign some numbers to a variable, it would be saved as a completely different number. What I found was when JavaScript encounters a number prefixed with 0, and that number doesn’t contain an 8 or a 9, it will be interpreted as an Octal, or a Base-8 number. That is, a number system that is represented by the numbers 0–7.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528238%252C225%252C249%252C0.5%2529%26t%3Da11y-dark%26wt%3Dnone%26l%3Djsx%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D3px%26ph%3D32px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dconst%252520a%252520%25253D%252520007%252520%25252F%25252F%252520Interpreted%252520as%252520an%252520octal%25250Aconsole.log%2528a%2529%252520%25252F%25252F%2525207%25250A%25250Aconst%252520b%252520%25253D%252520008%252520%25252F%25252F%252520Contains%252520an%2525208%25252C%252520interpreted%252520as%252520base-10%25250Aconsole.log%2528b%2529%252520%25252F%25252F%2525208%25250A%25250Aconst%252520c%252520%25253D%252520009%252520%25252F%25252F%252520Contains%252520a%2525209%25252C%252520interpreted%252520as%252520base-10%25250Aconsole.log%2528c%2529%252520%25252F%25252F%2525209%25250A%25250Aconst%252520d%252520%25253D%252520010%252520%25252F%25252F%252520Back%252520to%252520an%252520octal%25252C%252520logs%2525208%25250Aconsole.log%2528d%2529%252520%25252F%25252F%2525208%25250A%25250Aconst%252520e%252520%25253D%252520017%252520%25252F%25252F%252520Octal%25250Aconsole.log%2528e%2529%252520%25252F%25252F%25252015%25250A%25250Aconst%252520f%252520%25253D%252520018%252520%25252F%25252F%252520Base-10%25250Aconsole.log%2528f%2529%252520%25252F%25252F%25252018&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528238%25252C225%25252C249%25252C0.5%252529%26t%3Da11y-dark%26wt%3Dnone%26l%3Djsx%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D3px%26ph%3D32px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dconst%25252520a%25252520%2525253D%25252520007%25252520%2525252F%2525252F%25252520Interpreted%25252520as%25252520an%25252520octal%2525250Aconsole.log%252528a%252529%25252520%2525252F%2525252F%252525207%2525250A%2525250Aconst%25252520b%25252520%2525253D%25252520008%25252520%2525252F%2525252F%25252520Contains%25252520an%252525208%2525252C%25252520interpreted%25252520as%25252520base-10%2525250Aconsole.log%252528b%252529%25252520%2525252F%2525252F%252525208%2525250A%2525250Aconst%25252520c%25252520%2525253D%25252520009%25252520%2525252F%2525252F%25252520Contains%25252520a%252525209%2525252C%25252520interpreted%25252520as%25252520base-10%2525250Aconsole.log%252528c%252529%25252520%2525252F%2525252F%252525209%2525250A%2525250Aconst%25252520d%25252520%2525253D%25252520010%25252520%2525252F%2525252F%25252520Back%25252520to%25252520an%25252520octal%2525252C%25252520logs%252525208%2525250Aconsole.log%252528d%252529%25252520%2525252F%2525252F%252525208%2525250A%2525250Aconst%25252520e%25252520%2525253D%25252520017%25252520%2525252F%2525252F%25252520Octal%2525250Aconsole.log%252528e%252529%25252520%2525252F%2525252F%2525252015%2525250A%2525250Aconst%25252520f%25252520%2525253D%25252520018%25252520%2525252F%2525252F%25252520Base-10%2525250Aconsole.log%252528f%252529%25252520%2525252F%2525252F%2525252018&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/9859562f0d212080ab33fd59376e9144/href">https://medium.com/media/9859562f0d212080ab33fd59376e9144/href</a></iframe><p>While prefixing zeros is not something you should be doing, especially when declaring a variable, this automatic assignment may cause you a quick headache as well someday. Luckily, this type of integer assignment is officially discouraged. As of ECMAScript 5, this type of integer assignment throws an error in strict mode.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528238%252C225%252C249%252C0.5%2529%26t%3Da11y-dark%26wt%3Dnone%26l%3Djsx%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D10px%26ph%3D32px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%2527use%252520strict%2527%25253B%25250A%25250Aconst%252520x%252520%25253D%252520010%25250Aconsole.log%2528x%2529%252520%25252F%25252F%252520Uncaught%252520SyntaxError%25253A%252520Octal%252520literals%252520are%252520not%252520allowed%252520in%252520strict%252520mode.%25250A%25250Aconst%252520y%252520%25253D%252520017%25250Aconsole.log%2528y%2529%252520%25252F%25252F%252520Uncaught%252520SyntaxError%25253A%252520Octal%252520literals%252520are%252520not%252520allowed%252520in%252520strict%252520mode.%25250A%25250Aconst%252520z%252520%25253D%252520018%252520%25252F%25252F%252520Since%252520number%252520contains%252520an%2525208%252520it%2527s%252520not%252520an%252520octal.%252520Fails%252520for%252520strict%252520number%252520standards%252520instead.%25250Aconsole.log%2528z%2529%252520%25252F%25252F%252520Uncaught%252520SyntaxError%25253A%252520Decimals%252520with%252520leading%252520zeros%252520are%252520not%252520allowed%252520in%252520strict%252520mode.&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528238%25252C225%25252C249%25252C0.5%252529%26t%3Da11y-dark%26wt%3Dnone%26l%3Djsx%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D10px%26ph%3D32px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%252527use%25252520strict%252527%2525253B%2525250A%2525250Aconst%25252520x%25252520%2525253D%25252520010%2525250Aconsole.log%252528x%252529%25252520%2525252F%2525252F%25252520Uncaught%25252520SyntaxError%2525253A%25252520Octal%25252520literals%25252520are%25252520not%25252520allowed%25252520in%25252520strict%25252520mode.%2525250A%2525250Aconst%25252520y%25252520%2525253D%25252520017%2525250Aconsole.log%252528y%252529%25252520%2525252F%2525252F%25252520Uncaught%25252520SyntaxError%2525253A%25252520Octal%25252520literals%25252520are%25252520not%25252520allowed%25252520in%25252520strict%25252520mode.%2525250A%2525250Aconst%25252520z%25252520%2525253D%25252520018%25252520%2525252F%2525252F%25252520Since%25252520number%25252520contains%25252520an%252525208%25252520it%252527s%25252520not%25252520an%25252520octal.%25252520Fails%25252520for%25252520strict%25252520number%25252520standards%25252520instead.%2525250Aconsole.log%252528z%252529%25252520%2525252F%2525252F%25252520Uncaught%25252520SyntaxError%2525253A%25252520Decimals%25252520with%25252520leading%25252520zeros%25252520are%25252520not%25252520allowed%25252520in%25252520strict%25252520mode.&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/360743244e28135df400867c0d561ed2/href">https://medium.com/media/360743244e28135df400867c0d561ed2/href</a></iframe><p>If you do want to declare a variable as a specific base number, ECMAScript 6 offered a smarter solution for that. Now you can prefix numbers to indicate what type of number they represent.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528238%252C225%252C249%252C0.5%2529%26t%3Da11y-dark%26wt%3Dnone%26l%3Djsx%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D44px%26ph%3D32px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dconst%252520x%252520%25253D%2525200b010%252520%25252F%25252F%2525200b%252520or%2525200B%252520for%252520binary%25250Aconsole.log%2528x%2529%252520%25252F%25252F%2525202%25250A%25250Aconst%252520y%252520%25253D%2525200o010%252520%25252F%25252F%2525200o%252520or%2525200O%252520for%252520octal%25250Aconsole.log%2528y%2529%252520%25252F%25252F%2525208%25250A%25250Aconst%252520z%252520%25253D%2525200x010%252520%25252F%25252F%2525200x%252520or%2525200X%252520for%252520hexadecimal%25250Aconsole.log%2528z%2529%252520%25252F%25252F%25252016&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528238%25252C225%25252C249%25252C0.5%252529%26t%3Da11y-dark%26wt%3Dnone%26l%3Djsx%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D44px%26ph%3D32px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dconst%25252520x%25252520%2525253D%252525200b010%25252520%2525252F%2525252F%252525200b%25252520or%252525200B%25252520for%25252520binary%2525250Aconsole.log%252528x%252529%25252520%2525252F%2525252F%252525202%2525250A%2525250Aconst%25252520y%25252520%2525253D%252525200o010%25252520%2525252F%2525252F%252525200o%25252520or%252525200O%25252520for%25252520octal%2525250Aconsole.log%252528y%252529%25252520%2525252F%2525252F%252525208%2525250A%2525250Aconst%25252520z%25252520%2525253D%252525200x010%25252520%2525252F%2525252F%252525200x%25252520or%252525200X%25252520for%25252520hexadecimal%2525250Aconsole.log%252528z%252529%25252520%2525252F%2525252F%2525252016&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/4596f3f543765b5db0be5edb5d1f3d4b/href">https://medium.com/media/4596f3f543765b5db0be5edb5d1f3d4b/href</a></iframe><p>Note however that the variable will still be the base-10 representation of that number. To display the other base versions of a number you can use the toString() method. It accepts a <em>radix, a </em>base number to convert to from 2–36, as an argument. Keep in mind however that it’s still just a string representation, and not a JavaScript number.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528238%252C225%252C249%252C0.5%2529%26t%3Da11y-dark%26wt%3Dnone%26l%3Djsx%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D44px%26ph%3D32px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dconst%252520x%252520%25253D%2525200b010%252520%25252F%25252F%2525200b%252520or%2525200B%252520for%252520binary%25250Aconsole.log%2528x.toString%25282%2529%2529%252520%25252F%25252F%252520%25252210%252522%25250A%25250Aconst%252520y%252520%25253D%2525200o010%252520%25252F%25252F%2525200o%252520or%2525200O%252520for%252520octal%25250Aconsole.log%2528y.toString%25288%2529%2529%252520%25252F%25252F%252520%25252210%252522%25250A%25250Aconst%252520z%252520%25253D%2525200x010%252520%25252F%25252F%2525200x%252520or%2525200X%252520for%252520hexadecimal%25250Aconsole.log%2528z.toString%252816%2529%2529%252520%25252F%25252F%252520%25252210%252522&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528238%25252C225%25252C249%25252C0.5%252529%26t%3Da11y-dark%26wt%3Dnone%26l%3Djsx%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D44px%26ph%3D32px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dconst%25252520x%25252520%2525253D%252525200b010%25252520%2525252F%2525252F%252525200b%25252520or%252525200B%25252520for%25252520binary%2525250Aconsole.log%252528x.toString%2525282%252529%252529%25252520%2525252F%2525252F%25252520%2525252210%25252522%2525250A%2525250Aconst%25252520y%25252520%2525253D%252525200o010%25252520%2525252F%2525252F%252525200o%25252520or%252525200O%25252520for%25252520octal%2525250Aconsole.log%252528y.toString%2525288%252529%252529%25252520%2525252F%2525252F%25252520%2525252210%25252522%2525250A%2525250Aconst%25252520z%25252520%2525253D%252525200x010%25252520%2525252F%2525252F%252525200x%25252520or%252525200X%25252520for%25252520hexadecimal%2525250Aconsole.log%252528z.toString%25252816%252529%252529%25252520%2525252F%2525252F%25252520%2525252210%25252522&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/ea0211f34f55625dde08fc14dd9bc5b0/href">https://medium.com/media/ea0211f34f55625dde08fc14dd9bc5b0/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c255add0a685" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What are JavaScript Workers?]]></title>
            <link>https://jwood206.medium.com/what-are-javascript-workers-2cef311806e?source=rss-8c3df8ccd5ae------2</link>
            <guid isPermaLink="false">https://medium.com/p/2cef311806e</guid>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-workers-in-javascript]]></category>
            <category><![CDATA[webworker]]></category>
            <category><![CDATA[service-worker]]></category>
            <dc:creator><![CDATA[Jeremy Wood]]></dc:creator>
            <pubDate>Sun, 02 Jan 2022 01:44:40 GMT</pubDate>
            <atom:updated>2022-01-02T01:44:40.953Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BG1FuzUakrq3B690ph588w.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@solimonster?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">sol</a> on <a href="https://unsplash.com/s/photos/worker?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Web responsiveness and performance is becoming increasingly important both for user experience, and SEO. One big thing that has always hindered us in these areas is lengthy JavaScript execution. Calling scripts at the bottom of your document, and using asynchronous JavaScript helps, but certainly can’t fix all your problems.</p><p>Let’s say you have to do a lot of image processing and some other expensive computations to handle when your page loads. While you can do some of this work asynchronously, at the end of the day everything still has to be eventually handled by the same single JavaScript thread. What workers do is provide other threads to run your code on. Essentially, workers are just JavaScript files that can be run on separate threads at the same time as your main JavaScript thread.</p><p>The typical types of workers you run in to are Dedicated Workers, Shared Workers, and Service Workers. You can simply instantiate a new worker as a variable in your code, and then use each worker’s respective methods and event handlers to communicate between that worker and the main thread in your project. It’s important to keep in mind when writing the code in your worker file, that workers don’t have access to your window’s global scope, and instead have their own scope, methods, and functions that they have access to. Because of this there are some things you can’t do within a worker. For instance, you can’t manipulate the DOM from a worker, or use some window properties. To communicate between these workers and your main thread, you use postMessage(“example-data”) to send data. An onMessage event handler is used to handle these postMessage calls.</p><p>Dedicated workers, new Worker(“example.js”), are JavaScript files that are only accessible by the script that called them initially. It’s a nice way to move a specific piece of standalone logic into it’s own thread, as long as you only need access to that code from a single source.</p><p>Shared workers, new SharedWorker(&#39;example.js&#39;), on the other hand can be accessed from multiple different scripts, or browsing contexts. Because of this the communication between these workers and your scripts has an added step. There is a port object attached to your worker that must be called to use your postMessage() and onMessage functionality. While this is done implicitly for you in a dedicated service worker, you must specify it in a shared worker, as the MessageEvent interface used to communicate with workers can sometimes return an array of ports. So in short there’s just an extra step when communicating with a shared worker, but you can access that worker from any scripts, windows, or iFrames you’re using, provided they’re under the same domain.</p><p>Service workers are a little different. They are workers that are set up to intercept and handle network requests. They are often used to handle offline capabilities, but also can be used to handle caching, updating of resources, or other background server and request related operations. These are a little more complex, as they require an initial registration, local installation on initial page load which handles things like setting up local caches, then activation to make the worker available for use. While this does take a bit of setup, the advantages of being able to handle unstable connections could be a huge benefit.</p><p>If your application is suffering from heavy computations, and you need a way to do some heavy lifting without blocking your main thread, workers are likely a great solution. If you’re interested in learning more about workers, here are some great resources:</p><p><a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API">Web Workers API - MDN Web Docs</a></p><p><a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers">Using Web Workers - MDN Web Docs</a></p><p><a href="https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API">Service Worker API - MDN Web Docs</a></p><p><a href="https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers">Using Service Workers - MDN Web Docs</a></p><p><a href="https://bitsofco.de/web-workers-vs-service-workers-vs-worklets/">Web Workers vs Service Workers vs Worklets - Ire Aderinokun</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2cef311806e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Basic JavaScript Memoization]]></title>
            <link>https://jwood206.medium.com/basic-javascript-memoization-5db06f06aaa?source=rss-8c3df8ccd5ae------2</link>
            <guid isPermaLink="false">https://medium.com/p/5db06f06aaa</guid>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[recursion]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[memoization]]></category>
            <dc:creator><![CDATA[Jeremy Wood]]></dc:creator>
            <pubDate>Sun, 26 Dec 2021 01:05:31 GMT</pubDate>
            <atom:updated>2021-12-26T01:05:31.644Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ILN2gnWFFHqXcce37hS0RQ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@chiabra?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Paolo Chiabrando</a> on <a href="https://unsplash.com/s/photos/memo?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>If you’re new to JavaScript, there’s a good chance you’re using a website like LeetCode or HackerRank to improve your algorithm skills. A big part of this is getting better at using both recursion and iteration to solve problems. One trick that can greatly improve your recursive solutions is a technique called memoization.</p><p>Memoization is just a way to store the results of function calls. This is incredibly helpful in functions that end up getting called with the same arguments over and over. If your function is pure, and the same input always returns the same output, then it wouldn’t make sense to call the function with the same input multiple times.</p><p>The simplest way to implement memoization is just by passing an object along with your function calls. I was able to use this method to recursively answer a <a href="https://leetcode.com/problems/climbing-stairs/">LeetCode problem</a>, that would otherwise time out. Imagine you’re climbing a set of stairs, and you’re given the amount of steps to the top. If you can take either one or two steps at a time, how many different ways are there to get you to the top? The result ends up being a fibonacci sequence from 3 steps onward. This means the return value of 3 steps will be the sum of 2 steps and 1 step, the return value of 4 steps is the sum of 3 steps and 2 steps, and so on. The JavaScript code to implement this recursively could look like this:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528238%252C225%252C249%252C1%2529%26t%3Da11y-dark%26wt%3Dnone%26l%3Djsx%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D32px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dvar%252520climbStairs%252520%25253D%252520function%2528n%2529%252520%25257B%252520%252520%252520%252520%25250A%252509if%2528n%252520%25253C%25253D%2525200%2529%252520return%2525200%25250A%252520%252520%252520%252520if%2528n%252520%25253D%25253D%25253D%2525201%2529%252520return%2525201%25250A%252520%252520%252520%252520if%2528n%252520%25253D%25253D%25253D%2525202%2529%252520return%2525202%25250A%252520%252520%252520%252520return%252520climbStairs%2528n%252520-%2525201%2529%252520%25252B%252520climbStairs%2528n%252520-%2525202%2529%25250A%25257D%25250A%25250AclimbStairs%25280%2529%252520%25252F%25252F%2525200%25250AclimbStairs%25281%2529%252520%25252F%25252F%2525201%25250AclimbStairs%25282%2529%252520%25252F%25252F%2525202%25250AclimbStairs%25283%2529%252520%25252F%25252F%2525203%25250AclimbStairs%25284%2529%252520%25252F%25252F%2525205%25250AclimbStairs%25285%2529%252520%25252F%25252F%2525208%25250AclimbStairs%252845%2529%252520%25252F%25252F%2525201836311903&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528238%25252C225%25252C249%25252C1%252529%26t%3Da11y-dark%26wt%3Dnone%26l%3Djsx%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D32px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dvar%25252520climbStairs%25252520%2525253D%25252520function%252528n%252529%25252520%2525257B%25252520%25252520%25252520%25252520%2525250A%25252509if%252528n%25252520%2525253C%2525253D%252525200%252529%25252520return%252525200%2525250A%25252520%25252520%25252520%25252520if%252528n%25252520%2525253D%2525253D%2525253D%252525201%252529%25252520return%252525201%2525250A%25252520%25252520%25252520%25252520if%252528n%25252520%2525253D%2525253D%2525253D%252525202%252529%25252520return%252525202%2525250A%25252520%25252520%25252520%25252520return%25252520climbStairs%252528n%25252520-%252525201%252529%25252520%2525252B%25252520climbStairs%252528n%25252520-%252525202%252529%2525250A%2525257D%2525250A%2525250AclimbStairs%2525280%252529%25252520%2525252F%2525252F%252525200%2525250AclimbStairs%2525281%252529%25252520%2525252F%2525252F%252525201%2525250AclimbStairs%2525282%252529%25252520%2525252F%2525252F%252525202%2525250AclimbStairs%2525283%252529%25252520%2525252F%2525252F%252525203%2525250AclimbStairs%2525284%252529%25252520%2525252F%2525252F%252525205%2525250AclimbStairs%2525285%252529%25252520%2525252F%2525252F%252525208%2525250AclimbStairs%25252845%252529%25252520%2525252F%2525252F%252525201836311903&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/723676255892cb87298c211554252319/href">https://medium.com/media/723676255892cb87298c211554252319/href</a></iframe><p>This works, but not well. The problem here is how long it takes to complete the operations. When you call climbStairs(5), in order to return it calls climbStairs(4) and climbStairs(3).</p><p>Since 4 is called first, we need to find it’s solution first. When passed 4 we end up calling climbStairs(3) and climbStairs(2). Handling 3 first, we end up calling climbStairs(2) and climbStairs(1). Finally, those function calls return us 2 and 1. We add them together, and the output of climbStairs(3) is 3. Now, back inside climbStairs(4), we add the return of climbStairs(3) // 3 and climbStairs(2) // 2, so climbStairs(4) equals 5.</p><p>Now we’re back at our original function call again, climbStairs(5). We have our first return from climbStairs(4) // 5, and now we need to calculate the second half, climbStairs(3). But we’ve already calculated that before! We know it returns 3, but since our function doesn’t, it ends up running all the calculations again. In this instance, instead of returning 3, it’s just climbStairs(1) and climbStairs(2). But as your starting number increases, the number of repeat calls grows exponentially.</p><p>When calling climbStairs(6), you end up calling climbStairs(4) twice, climbStairs(3) 3 times, climbStairs(2) 5 times, and climbStairs(1) 3 times.</p><p>Calling climbStairs(7) calls climbStairs(5) twice, climbStairs(4) 3 times, climbStairs(3) 5 times, climbStairs(2) 8 times, and climbStairs(1) 5 times. That’s a lot of wasted computations.</p><p>The solution here is memoization. If we pass a cache along that contains answers to each value we’ve already passed in, we can check it for a solution to prevent repeating lengthy calculations. The implementation could look like this:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528238%252C225%252C249%252C1%2529%26t%3Da11y-dark%26wt%3Dnone%26l%3Djsx%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D32px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dvar%252520climbStairs%252520%25253D%252520function%2528n%25252C%252520memo%252520%25253D%252520%25255B0%25252C1%25252C2%25255D%2529%252520%25257B%25250A%252520%252520%252520%252520if%2528memo%25255Bn%25255D%2529%252520return%252520memo%25255Bn%25255D%25250A%252520%252520%25250A%252520%252520%252520%252520memo%25255Bn%25255D%252520%25253D%252520climbStairs%2528n%252520-%2525201%25252C%252520memo%2529%252520%25252B%252520climbStairs%2528n%252520-%2525202%25252C%252520memo%2529%25250A%25250A%252520%252520%252520%252520return%252520memo%25255Bn%25255D%252520%252520%252520%25250A%25257D%25250A%25250AclimbStairs%25280%2529%252520%25252F%25252F%2525200%25250AclimbStairs%25281%2529%252520%25252F%25252F%2525201%25250AclimbStairs%25282%2529%252520%25252F%25252F%2525202%25250AclimbStairs%25283%2529%252520%25252F%25252F%2525203%25250AclimbStairs%25284%2529%252520%25252F%25252F%2525205%25250AclimbStairs%25285%2529%252520%25252F%25252F%2525208%25250AclimbStairs%252845%2529%252520%25252F%25252F%2525201836311903&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528238%25252C225%25252C249%25252C1%252529%26t%3Da11y-dark%26wt%3Dnone%26l%3Djsx%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D32px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dvar%25252520climbStairs%25252520%2525253D%25252520function%252528n%2525252C%25252520memo%25252520%2525253D%25252520%2525255B0%2525252C1%2525252C2%2525255D%252529%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520if%252528memo%2525255Bn%2525255D%252529%25252520return%25252520memo%2525255Bn%2525255D%2525250A%25252520%25252520%2525250A%25252520%25252520%25252520%25252520memo%2525255Bn%2525255D%25252520%2525253D%25252520climbStairs%252528n%25252520-%252525201%2525252C%25252520memo%252529%25252520%2525252B%25252520climbStairs%252528n%25252520-%252525202%2525252C%25252520memo%252529%2525250A%2525250A%25252520%25252520%25252520%25252520return%25252520memo%2525255Bn%2525255D%25252520%25252520%25252520%2525250A%2525257D%2525250A%2525250AclimbStairs%2525280%252529%25252520%2525252F%2525252F%252525200%2525250AclimbStairs%2525281%252529%25252520%2525252F%2525252F%252525201%2525250AclimbStairs%2525282%252529%25252520%2525252F%2525252F%252525202%2525250AclimbStairs%2525283%252529%25252520%2525252F%2525252F%252525203%2525250AclimbStairs%2525284%252529%25252520%2525252F%2525252F%252525205%2525250AclimbStairs%2525285%252529%25252520%2525252F%2525252F%252525208%2525250AclimbStairs%25252845%252529%25252520%2525252F%2525252F%252525201836311903&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/acb9e1fb15c2e40b03db776baca61db6/href">https://medium.com/media/acb9e1fb15c2e40b03db776baca61db6/href</a></iframe><p>Lets call climbStairs(5) once more. Here, on our initial call, we instantiate our memo object, (or array in this instance), with our smallest values that do not follow the fibonacci sequence. The first part of our code behaves very similar to our last solution. Since there is no memo[5], we set memo[5] to equal climbStairs(4) + climbStairs(3), passing the memo object along with our calls. This calls climbStairs(4) first, which again doesn’t exist in our memo, so we set memo[4] to climbStairs(3) + climbStairs(2). climbStairs(3) doesn’t find memo[3] and sets it to climbStairs(2) + climbStairs(1). Since memo[1] and memo[2] do exist, climbStairs(1) returns memo[1], or just 1. climbStairs(2) returns memo[2] or just 2.</p><p>This takes us back to climbStairs(3), where we set memo[3] to 2 + 1, or 3. We then return memo[3]. This sends us back into climbStairs(4) where we set memo[4] to the return of climbStairs(3) // 3 and climbStairs(2) // 2. memo[4] now equals 5, and we return memo[4] to our initial call of climbStairs(5).</p><p>So far, we haven’t saved ourselves any time. But now we hit the second half of climbStairs(5), and call climbStairs(3) again. Since we’ve already called climbStairs(3), we’ve already set memo[3] to equal 3. Now instead of calling climbStairs(2) + climbStairs(1), we simply return 3. That’s one array index lookup instead of two function calls and one operation. We set memo[5] to 5 + 3, and return memo[5] which holds our final answer of 8.</p><p>Starting with a small number like 5 hasn’t really improved our solution at all. In fact all it did was add extra memory to our solution. This is something you must keep in mind when memoizing. If you’re not calling functions with the same values often, it might not be worth memoizing. But when you start with a much larger number, you end up cutting out exponentially more calculations.</p><p>When using the memoized method on climbStairs(7), you still end up calling climbStairs(5) twice, but you only call climbStairs(4) twice instead of three times, climbStairs(3) is also only called twice instead of 5 times, climbStairs(2) is only called twice instead of 8 times, and climbStairs(1) is only called once.</p><p>This is a decent savings in the number of operations you’re performing, even with a relatively low starting number. And as you increase your initial value, you’ll still only be performing each calculation at most 2 times. This keeps the algorithm performant, even with larger numbers.</p><p>And the proof is in the pudding. If you try to solve the problem on LeetCode with the first solution, the test environment will time out. With the second solution it passes the tests. You can get an idea of why by wrapping each function call between a console.time() and a console.timeEnd(). This will implement a counter to give you a basic idea of how long a function takes to complete. console.time(&#39;arg&#39;) starts a timer, and the time elapsed is returned with console.timeEnd(‘arg’). It’s not extremely accurate, but can help you see larger discrepancies between functions:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528238%252C225%252C249%252C1%2529%26t%3Da11y-dark%26wt%3Dnone%26l%3Djsx%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D32px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dconsole.time%2528%2527Memoized%25253A%252520%2527%2529%25250AclimbStairsMemoized%25287%2529%25250Aconsole.timeEnd%2528%2527Memoized%25253A%252520%2527%2529%252520%25252F%25252F%252520Memoized%25253A%252520%25253A%2525200.487ms%25250A%25250A%25250Aconsole.time%2528%2527Not%252520Memoized%25253A%252520%2527%2529%25250AclimbStairs%25287%2529%25250Aconsole.timeEnd%2528%2527Not%252520Memoized%25253A%252520%2527%2529%252520%25252F%25252F%252520Not%252520Memoized%25253A%252520%25253A%2525200.154ms%25250A%25250A%25250Aconsole.time%2528%2527Memoized%25253A%252520%2527%2529%25250AclimbStairsMemoized%252845%2529%25250Aconsole.timeEnd%2528%2527Memoized%25253A%252520%2527%2529%252520%25252F%25252F%252520Memoized%25253A%252520%25253A%2525200.113ms%25250A%25250A%25250Aconsole.time%2528%2527Not%252520Memoized%25253A%252520%2527%2529%25250AclimbStairs%252845%2529%25250Aconsole.timeEnd%2528%2527Not%252520Memoized%25253A%252520%2527%2529%252520%25252F%25252F%252520Not%252520Memoized%25253A%252520%25253A%2525209.702s%25250A&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528238%25252C225%25252C249%25252C1%252529%26t%3Da11y-dark%26wt%3Dnone%26l%3Djsx%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D0px%26ph%3D32px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dconsole.time%252528%252527Memoized%2525253A%25252520%252527%252529%2525250AclimbStairsMemoized%2525287%252529%2525250Aconsole.timeEnd%252528%252527Memoized%2525253A%25252520%252527%252529%25252520%2525252F%2525252F%25252520Memoized%2525253A%25252520%2525253A%252525200.487ms%2525250A%2525250A%2525250Aconsole.time%252528%252527Not%25252520Memoized%2525253A%25252520%252527%252529%2525250AclimbStairs%2525287%252529%2525250Aconsole.timeEnd%252528%252527Not%25252520Memoized%2525253A%25252520%252527%252529%25252520%2525252F%2525252F%25252520Not%25252520Memoized%2525253A%25252520%2525253A%252525200.154ms%2525250A%2525250A%2525250Aconsole.time%252528%252527Memoized%2525253A%25252520%252527%252529%2525250AclimbStairsMemoized%25252845%252529%2525250Aconsole.timeEnd%252528%252527Memoized%2525253A%25252520%252527%252529%25252520%2525252F%2525252F%25252520Memoized%2525253A%25252520%2525253A%252525200.113ms%2525250A%2525250A%2525250Aconsole.time%252528%252527Not%25252520Memoized%2525253A%25252520%252527%252529%2525250AclimbStairs%25252845%252529%2525250Aconsole.timeEnd%252528%252527Not%25252520Memoized%2525253A%25252520%252527%252529%25252520%2525252F%2525252F%25252520Not%25252520Memoized%2525253A%25252520%2525253A%252525209.702s%2525250A&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/f083041142153638c1dea1d57ce99073/href">https://medium.com/media/f083041142153638c1dea1d57ce99073/href</a></iframe><p>Here we can see that we can call the memoized and the non-memoized versions of the function on a small number like 7, the speed is in milliseconds for both, and memoization makes little difference in completion time, and may even be slower. But when tested with a number like 45, the time goes from milliseconds for the memoized version, to over 9 seconds for the non-memoized version. The difference speaks for itself. In a situation where you had to repeatedly run a function like this, you could be wasting a ridiculous amount of time.</p><p>Of course there is a downside to memoization as well. Each value you store in your memoize object takes up memory. If you never end up calling a function with the same input more than once, you’re creating an extra step in storing that return value and never getting the benefit of using that value to prevent unnecessary operations. This would waste extra time and space, and would ultimately be less performant than the original brute force recursive solution. Having a good idea of what data is being passed into your function is crucial to memoization performance.</p><p>This is about as simple an example of memoization as you can have. To be more abstract / practical, you could have a memoization function that you pass another function into. The memoization function could then automatically add a memoization object and recursively call the function you passed in, making memoization easily accessible across your environment. You can read more about memoization here:</p><p><a href="https://dev.to/anishkumar/memoizing-fetch-api-calls-in-javascript-1d16">Memoization in JavaScript - Anish Kumar</a></p><p><a href="https://www.digitalocean.com/community/tutorials/understanding-memoization-in-javascript">Understanding Memoization in JavaScript - Philip Obosi</a></p><p><a href="https://www.section.io/engineering-education/an-introduction-to-memoization-in-javascript/">Introduction to Memorization in JavaScript - Joseph Chege</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5db06f06aaa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why GraphQL when I can REST?]]></title>
            <link>https://jwood206.medium.com/why-graphql-when-i-can-rest-fe8b57075b9f?source=rss-8c3df8ccd5ae------2</link>
            <guid isPermaLink="false">https://medium.com/p/fe8b57075b9f</guid>
            <category><![CDATA[rest-api]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[graphql-vs-rest]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[rest]]></category>
            <dc:creator><![CDATA[Jeremy Wood]]></dc:creator>
            <pubDate>Fri, 17 Dec 2021 08:24:50 GMT</pubDate>
            <atom:updated>2021-12-17T08:24:50.554Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6NmQ_hgDO7t1rgdbgdk4fg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@freestocks?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">freestocks</a> on <a href="https://unsplash.com/s/photos/rest-dog?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>If you’ve had to fetch data before, there’s a good chance you’ve used a RESTful conventions to get data from an API. You have also probably heard that GraphQL is another interface through which you can access your data. But why would you want to use GraphQL over your trusty old RESTful routes? To see why let’s first look at REST.</p><p>REST is a great way to structure the way we grab our data, and there’s a reason it’s used as widely as it is. It’s great for grabbing a data set specific to a certain model. For instance, if you just need to get information about a specific user, the REST API is easy and predictable. It’s easy to send a GET request to /users/:user_id, and you will be returned all of that user’s information.</p><p>But let’s say you want to display a list of users’ names , and you also want to display each user’s five favorite restaurants? If you’re not dynamically generating your API based on queries in your backend, you may have an issue here. In that instance, there’s a good chance your API has more information than you actually need in that moment. You can send your GET request to /users, but if you’re just listing the users’ names, you don’t need to know their phone numbers, or address, or height, or whatever other information is stored at that endpoint. This is referred to as <strong>Overfetching</strong>,<strong> </strong>where you end up with more data than you need.</p><p>And that’s not the only issue. If there’s no relational mapping at that endpoint, you may have to send a GET request to users/:user_id/favorite_restaurants for <strong>each</strong> user’s favorite restaurants. This is what’s known as an n+1 request, or <strong>Underfetching</strong>, where multiple requests are required to get all of your data. There must be a better way!</p><p>The great part about GraphQL is the ability to query for only the pieces of data you require. This is done through <em>Declarative Data Fetching</em>. Instead of having multiple and defined endpoints to request your data from, GraphQL has a single endpoint. And instead of returning model specific API endpoints, you have a Schema. A schema is a collection of Types, which you define to correspond with the data in your backend. In your schema you specify what data can be accessed, what data is required, and you can even declare who has access to specific pieces of data. To retrieve your data, you simply write queries to your graphQL endpoint that specify the data you want back.</p><p>This ends up with a little more code being written to get your data, but potentially much less data being exposed and retrieved. And with one request you can get your data custom tailored to what you need in that specific situation. You can even pass in arguments to be even more precise about the data you’re retrieving.</p><p>While there are certainly situations where REST is still the best solution, GraphQL could be an improvement in some of your projects. It’s definitely worth checking out. If you’d like to learn more about using GraphQL here are some resources.</p><p><a href="https://www.howtographql.com/">The Fullstack Tutorial for GraphQL</a></p><p><a href="https://blog.api.rakuten.net/graphql-vs-rest/">GraphQL vs. REST: A Comprehensive Comparison - Alfrick Opidi</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fe8b57075b9f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Positioning with Mouse Events, Offset, getBoundingClientRect, and getComputedStyle]]></title>
            <link>https://jwood206.medium.com/positioning-with-mouse-events-offset-getboundingclientrect-and-getcomputedstyle-afe12bfcb5f?source=rss-8c3df8ccd5ae------2</link>
            <guid isPermaLink="false">https://medium.com/p/afe12bfcb5f</guid>
            <category><![CDATA[css]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[html]]></category>
            <category><![CDATA[positioning]]></category>
            <category><![CDATA[events]]></category>
            <dc:creator><![CDATA[Jeremy Wood]]></dc:creator>
            <pubDate>Sun, 12 Dec 2021 02:54:00 GMT</pubDate>
            <atom:updated>2021-12-12T02:54:00.398Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MdiHDR06_7A9kmdS5Be8Kg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@sharonmccutcheon?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Sharon McCutcheon</a> on <a href="https://unsplash.com/s/photos/color-squares?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>On a recent project, I was using event listeners on mouse events. I was trying to determine where on some elements I had clicked, as a percentage in relation to their width. During the process I came across a couple different ways to solve the problem, using different methods provided by JavaScript. I wanted to list some basic information about the different methods and when one might be more useful than the other.</p><p>So I had an event triggered from the element I clicked on. The MouseEvent provides us with several ways of determining where we clicked. Here’s a quick rundown:</p><ul><li>MouseEvent.clientX / MouseEvent.clientY ~ Coordinates of mouse event relative to viewport. Calculate from the left side and the top of the viewport.</li><li>MouseEvent.movementX / MouseEvent.movementY ~ Coordinates of mouse event relative to last mouse event.</li><li>MouseEvent.offsetX / MouseEvent.offsetY ~ Coordinates of mouse event relative to the edge of the padding on the targeted node. Calculated from the left padding and the top padding.</li><li>MouseEvent.pageX / MouseEvent.pageY ~ Coordinates of mouse event relative to the entire document. Calculated from left and top sides of entire document, even if scrolled out of view.</li><li>MouseEvent.screenX / MouseEvent.screenY ~ Coordinates of mouse event relative to the user’s entire screen. Calculated from the left and top sides of screen. If multiple screens are being used, value is calculated from the farthest left, and uppermost screens respectively.</li></ul><p>This gives us a lot of flexibility when handling mouse events. For my use case, I was trying to determine the mouse’s horizontal position inside a specific element. I went with the MouseEvent.offsetX option, as it would give me the position relative to the element the event listener was attached to.</p><p>Now I just had to figure out the width of the element I had clicked on, and calculate the percentage based on how far into the element I clicked. Some ways we can do this, are with the <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement#properties">HTMLElements API’s</a> <strong>offset</strong> properties, with the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect">Element class’ getBoundingClientRect method</a>, or with the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle">Window interfaces’ getComputedStyle method</a>. Each gives you some slightly different information, and therefore one may fit your use case better than another.</p><p>HTMLElements have a number of offset properties associated with them. In my case, HTMLElement.offsetWidthgave me the width property I needed to determine how far into a div I had clicked, and it was easy to calculate my horizontal percentage by dividing my offsetX value by my elements width, and multiplying by 100. But maybe you need more options. Here is a quick rundown of some of the <strong>HTMLElement offset properties.</strong></p><ul><li>HTMLElement.offsetHeight ~ Returns height of element, including padding and borders. <strong>Does not</strong> include any pseudo elements, such as ::before or ::after.</li><li>HTMLElement.offsetWidth ~ Returns width of element, including padding and borders. <strong>Does not</strong> include any pseudo elements, such as ::before or ::after.</li><li>HTMLElement.offsetTop ~ Returns the distance between the outside of the current elements’ top border to the inside of its parent node’s top border.</li><li>HTMLElement.offsetLeft ~ Returns the distance between the outside of the current elements’ left border to the inside of its parent node’s left border.</li></ul><p>For determining positioning, the offsetTop and offsetLeft methods can be useful, but it’s important to know what the output is based on. <strong>The offset position number returned is the number of pixels from the edge of the selected element</strong>, <strong>to the edge of the selected elements’ parent element</strong>, and the parent element can change depending on your code.</p><ul><li>HTML.offsetParent ~ The closest parent element that has been positioned, <strong>as long as that position is not: </strong>static, initial that reverts to static, revert that points to either static or initial as previously defined, unset that defaults to initial, or none. All other positioned elements can be an offsetParent. If no parents have been positioned, it’s the closest td, th, ortable element. If none of those exist, the parent reverts to the body element.</li></ul><p>Another way for us to determine positioning is with the Element class’ Element.getBoundingClientRect() method. Calling this method on an element returns to you an object containing a number of different properties. Here however, <strong>you are returned position values relative to the viewport, instead of the document</strong>. The properties the <strong>getBoundingClientRect() </strong>object contains are:</p><ul><li>top / bottom / left / right / x / y ~Each property contains the distance between the outer edge of its border and the edge of the viewport. This distance is only calculated from the left and top sides of the viewport. So the left value plus the width of the element equals the right value, and the top value plus the height of the element equals the bottom value. x and y hold the same values as left and top.</li><li>height / width ~ Contains height / width of element, including padding and borders. <strong>Does not</strong> include any pseudo elements, such as ::before or ::after.</li></ul><p>If you need to do calculations based on the viewport instead of the element’s position on the page, or if something is preventing you from using an offset property this might be a good option for you. It’s worth noting that getBoundingClientRect() is not as performant as something like offsetX. It likely won’t make a huge difference in most situations, but it’s good to remember for when it does matter.</p><p>Another option for getting the width of an element is the Window interface’s <strong>getComputedStyle()</strong> method:</p><ul><li>getComputedStyle(element, pseudoElement)~ Returns an object containing all the css properties currently applied to that object. Pseudo element is optional.</li></ul><p>This route is great if you need to measure any pseudo elements along with your element, as the other height and width methods don’t account for this. If all you need however is just the width or height of one element, just using an offset property would likely read cleaner.</p><p>These were just some interesting ways I found to determine positioning, and there are undoubtedly many more. It’s nice to have such a range of options available for different situations. I highly recommend playing around with each method to get a good handle on the data available for each.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=afe12bfcb5f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React: props.children]]></title>
            <link>https://jwood206.medium.com/react-props-children-b59ff13dd5f?source=rss-8c3df8ccd5ae------2</link>
            <guid isPermaLink="false">https://medium.com/p/b59ff13dd5f</guid>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[props]]></category>
            <category><![CDATA[react-jsx]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[props-and-state]]></category>
            <dc:creator><![CDATA[Jeremy Wood]]></dc:creator>
            <pubDate>Sun, 05 Dec 2021 02:02:00 GMT</pubDate>
            <atom:updated>2021-12-05T02:02:00.447Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JbqESM3UslCs0Et2M2Z3hQ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@alpduran?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Alp Duran</a> on <a href="https://unsplash.com/s/photos/mannequin-small?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Code reusability is key in React. One nice tool we get for abstraction and reusability is props.children. Based on some explanations out there, it can take a second to understand exactly what’s happening in the code. However once you get it, it quickly becomes apparent how useful this feature could be. Basically, props.children is another way for us to dynamically change the JSX returned from our React components and functions.</p><p>When rendering a React component, we typically use a self closing tag, like this:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528170%252C0%252C255%252C1%2529%26t%3Da11y-dark%26wt%3Dnone%26l%3Djsx%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D114px%26ph%3D11px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%25253CReactComponent%252520someProps%25253D%25257BpropsBeingPassedIn%25257D%25252F%25253E&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528170%25252C0%25252C255%25252C1%252529%26t%3Da11y-dark%26wt%3Dnone%26l%3Djsx%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D114px%26ph%3D11px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%2525253CReactComponent%25252520someProps%2525253D%2525257BpropsBeingPassedIn%2525257D%2525252F%2525253E&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/25cbc1d279854158e27610da817af671/href">https://medium.com/media/25cbc1d279854158e27610da817af671/href</a></iframe><p>But, if we display a component with an opening and closing tag, <strong>everything between the component’s opening and closing tags is passed to the component as props.children.</strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528170%252C0%252C255%252C1%2529%26t%3Da11y-dark%26wt%3Dnone%26l%3Djsx%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D90px%26ph%3D11px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%25253CReactComponent%252520someProps%25253D%25257BpropsBeingPassedIn%25257D%25252F%25253E%25250A%252509%25253Ch2%25253ESome%252520Heading%25253C%25252Fh2%25253E%25250A%25253C%25252FReactComponent%25253E&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528170%25252C0%25252C255%25252C1%252529%26t%3Da11y-dark%26wt%3Dnone%26l%3Djsx%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D90px%26ph%3D11px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%2525253CReactComponent%25252520someProps%2525253D%2525257BpropsBeingPassedIn%2525257D%2525252F%2525253E%2525250A%25252509%2525253Ch2%2525253ESome%25252520Heading%2525253C%2525252Fh2%2525253E%2525250A%2525253C%2525252FReactComponent%2525253E&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/f8db9d89d0b57d632b0d5106e2f3a7c1/href">https://medium.com/media/f8db9d89d0b57d632b0d5106e2f3a7c1/href</a></iframe><p>In these examples we’re passing props to the component as we normally would, and we have access to props.someProps inside the ReactComponent. But you also have access to props.children. In this case, props.children contains a React element, which holds the JSX between our opening and closing Component tags. And if we drop props.children into the render portion of our ReactComponent, it will display that entire React element.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528170%252C0%252C255%252C1%2529%26t%3Da11y-dark%26wt%3Dnone%26l%3Djsx%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D17px%26ph%3D11px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dimport%252520React%252520from%252520%2527react%2527%25250A%25250Aconst%252520ReactComponent%252520%25253D%252520props%252520%25253D%25253E%252520%25257B%25250A%252520%252520%252520%252520return%252520%2528%25250A%252520%252520%252520%252520%252520%252520%252520%252520%25253Cdiv%25253E%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%25257Bprops.someProps%25257D%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%25257Bprops.children%25257D%25250A%252520%252520%252520%252520%252520%252520%252520%252520%25253C%25252Fdiv%25253E%25250A%252520%252520%252520%252520%2529%25250A%25257D%25250A%25250Aexport%252520default%252520ReactComponent&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528170%25252C0%25252C255%25252C1%252529%26t%3Da11y-dark%26wt%3Dnone%26l%3Djsx%26ds%3Dtrue%26dsyoff%3D21px%26dsblur%3D72px%26wc%3Dtrue%26wa%3Dfalse%26pv%3D17px%26ph%3D11px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dimport%25252520React%25252520from%25252520%252527react%252527%2525250A%2525250Aconst%25252520ReactComponent%25252520%2525253D%25252520props%25252520%2525253D%2525253E%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520return%25252520%252528%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525253Cdiv%2525253E%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525257Bprops.someProps%2525257D%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525257Bprops.children%2525257D%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525253C%2525252Fdiv%2525253E%2525250A%25252520%25252520%25252520%25252520%252529%2525250A%2525257D%2525250A%2525250Aexport%25252520default%25252520ReactComponent&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/a0d24f039d276fb3558a88f58fa1706a/href">https://medium.com/media/a0d24f039d276fb3558a88f58fa1706a/href</a></iframe><p>On the page, we would see the props we passed down normally inside the opening tag displayed, followed by any JSX passed down between our opening and closing tags. If we were to remove props.children from our return div, the “Some Heading” we passed in wouldn’t appear on the page.</p><p>This makes our ReactComponent very reusable, and can also come in handy if you don’t know what you’re going to be rendering inside of your component ahead of time. Maybe you have a navigation section or sidebar that needs to hold an image and a button in one tab, just some data in another, and a link in yet another. You could have a reusable sidebar item component that changes based on the JSX passed in between it’s call tags. This can help to keep logic out of the presentational sidebar item component itself, and in the containing sidebar component, which is key to reusability. You also have much less code in the way of messy props with different variable names being passed to a child component.</p><p>Hopefully this helped illustrate the very basics of props.children, and why it might be useful to you. Here are some good resources for further reading and uses.</p><p><a href="https://reactjs.org/docs/composition-vs-inheritance.html">Composition vs Inheritance - React Docs</a></p><p><a href="https://codeburst.io/a-quick-intro-to-reacts-props-children-cb3d2fce4891">A quick intro to React’s props.children - Jason Arnold</a></p><p><a href="https://stackoverflow.com/questions/49706823/what-is-this-props-children-and-when-you-should-use-it">What is {this.props.children} and when you should use it?</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b59ff13dd5f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>