<?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 Bridget Mailley on Medium]]></title>
        <description><![CDATA[Stories by Bridget Mailley on Medium]]></description>
        <link>https://medium.com/@beemailley?source=rss-4f3b14625ed2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*vFY8A6L6Z2Cfamb31l2HTg.jpeg</url>
            <title>Stories by Bridget Mailley on Medium</title>
            <link>https://medium.com/@beemailley?source=rss-4f3b14625ed2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 08:38:08 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@beemailley/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[Happy (belated?) Birthday!]]></title>
            <link>https://medium.com/@beemailley/happy-belated-birthday-a05c5fbbc94b?source=rss-4f3b14625ed2------2</link>
            <guid isPermaLink="false">https://medium.com/p/a05c5fbbc94b</guid>
            <category><![CDATA[conditional-statements]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[ternary-operator]]></category>
            <dc:creator><![CDATA[Bridget Mailley]]></dc:creator>
            <pubDate>Fri, 24 Mar 2023 16:24:20 GMT</pubDate>
            <atom:updated>2023-03-24T16:24:20.897Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="confetti on a teal background" src="https://cdn-images-1.medium.com/max/1024/0*C3dRs0ASnBxE2l32" /><figcaption>Photo by <a href="https://unsplash.com/@ninjason?utm_source=medium&amp;utm_medium=referral">Jason Leung</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>How to write a conditional message in Javascript</h3><p>I try to send out birthday cards to friends and family every year. I started this habit a few years ago as a part of a New Year’s resolution. I have really enjoyed both shopping for new stationery each year and picking out cards that I think each person will enjoy. I love the idea of sending funny or poignant messages but often struggle with what to write.</p><p>Furthermore, I don’t live in the same country or on the same continent as most of the people I send cards to. So, I never quite know how long it will take each card to wind its way through various postal systems and just how late or early the card will arrive. I often find the first half of my message waffling on trying to address whether the card will be late or early, and setting up wishes for each circumstance.</p><p>Little did I know that learning coding would help offer a solution to my (very minor) predicament! Now, I can offer arrival-appropriate messages, and show off that I am indeed a nerd at the same time. Hallelujah!</p><p>If you should care to, now you can as well!</p><h4>First, we set up our HTML:</h4><p>We’ll assume that the card is received on March 24th for a birthday on March 27th. A surprisingly timely arrival. (Thanks, PostNord &amp; USPS.)</p><pre> &lt;div id=”birthday-message”&gt;Birthday Message Here&lt;/div&gt;</pre><p>This is just a placeholder message, of course. The good bits come from the JavaScript.</p><h4>Then we write the JavaScript:</h4><p>We start by connecting the id from the HTML to a variable in the JS. That way, we can update the HTML later on. Because, of course, we cannot just console log the birthday message. Birthday cards don’t have consoles!</p><pre>const birthdayMessage = document.getElementById(‘birthday-message’)</pre><p>Now, we need to declare some variables. First, we’ll declare dayReceived.</p><pre>const dayReceived = new Date();</pre><p>Set your dayReceived variable to be a Date. If this were real code and not a greeting card, this would return the full date as a UNIX timestamp. E.g. If the card arrived on March 24 2023 at 3:36 pm CEST, we now have “2023–03–24T14:36:37.188Z” to work with.</p><pre>console.log(dayReceived)</pre><p><em>Double check everything is working. (Even though birthday cards don’t have consoles.)</em></p><p>Then, we’ll declare dateReceived.</p><pre>const dateReceived = dayReceived.getDate()</pre><p>Because we had the date as a UNIX timestamp, we need to actually extract the date for the day from it. We don’t want the day of the week, but the date for the day of arrival. E.g. 24, for the 24th of March.</p><pre>console.log(dateReceived)</pre><p><em>Keep checking that things aren’t broke yet.</em></p><p>If you’re pretty sure that the card will arrive in the correct month, we can move ahead with creating a function that just uses the date for the day.</p><pre>const message = () =&gt; {<br>  if (dateReceived &lt;= 27 ) {<br>   birthdayMessage.innerHTML =`I hope you have a great birthday!`<br>  } else if (dateReceived &gt; 27){<br>   birthdayMessage.innerHTML = `I’m so sorry this is late! <br>   I hope you had a great birthday!`<br>  }<br>}</pre><p>Be sure to check a couple of syntax and punctuation things:</p><ol><li>Using the “=” after .innerHTML will ensure that the correct message replaces any placeholder verbiage you’ve used in your HTML. Because birthday cards definitely have HTML.</li><li>Also, be sure to use backticks and NOT single quotes.</li><li>Double-check that you have spelled “receive” correctly everywhere. Or, if it’s spelled incorrectly, make sure that your incorrect spellings all match.</li></ol><p>Finally, even though this is written on paper and mailed around the world, be sure to call your function!</p><pre>message()</pre><p>Now, if you want to further complicate, or uncomplicate things. You could add a variable for the month or try writing the whole thing as a ternary operator.</p><h4>Adding a monthReceived variable:</h4><pre>const birthdayMessage = document.getElementById(‘birthday-message’)<br><br>const dayReceived = new Date();<br>const dateReceived = dayReceived.getDate()<br>const monthReceived = dayReceived.getMonth()<br><br>const message = () =&gt; {<br> if (dateReceived &lt;= 27 &amp;&amp; monthReceived &lt;= 2) {<br> birthdayMessage.innerHTML =`I hope you have a great birthday!`<br> } else if (dateReceived &gt; 27 || monthReceieved &gt; 2){<br> birthdayMessage.innerHTML = `I’m so sorry this is late! I hope you had a great birthday!`<br> }<br>}<br><br>message()</pre><p><em>Keep in mind that when using .getMonth(), January is 0. So, March = 2.</em></p><h4>Using a Ternary Operator instead:</h4><pre>const birthdayMessage = document.getElementById(&#39;birthday-message&#39;)<br><br>const dayReceived = new Date();<br>const dateReceived = dayReceived.getDate()<br><br>const message = () =&gt; {dateReceived &lt;= 27 ? birthdayMessage.innerHTML =`I hope you have a great birthday!` : birthdayMessage.innerHTML = `I&#39;m so sorry this is late! I hope you had a great birthday!`}<br><br>message()</pre><p>Now you can also wow your non-coding friends and family with an incomprehensible birthday message! However, hopefully, the coders in your life will feel a tiny sparkle of delight.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a05c5fbbc94b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>