<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Natalie Lang</title>
    <description>The latest articles on DEV Community by Natalie Lang (@natalielang).</description>
    <link>https://dev.to/natalielang</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F227024%2F5e5313fb-2c68-4d02-9d30-1f566749412e.jpeg</url>
      <title>DEV Community: Natalie Lang</title>
      <link>https://dev.to/natalielang</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/natalielang"/>
    <language>en</language>
    <item>
      <title>Day 1/31: Detect Capital</title>
      <dc:creator>Natalie Lang</dc:creator>
      <pubDate>Sun, 02 Aug 2020 17:48:53 +0000</pubDate>
      <link>https://dev.to/natalielang/1-31-detect-capital-2f28</link>
      <guid>https://dev.to/natalielang/1-31-detect-capital-2f28</guid>
      <description>&lt;p&gt;In this post, I'll be going through the &lt;strong&gt;Detect Capital&lt;/strong&gt; problem, day 1 of LeetCode's August daily challenge (which can be found &lt;a href="https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/549/week-1-august-1st-august-7th/3409/"&gt;here&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;The problem is as follows:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Given a word, you need to judge whether the usage of capitals in it is right or not.&lt;/p&gt;

&lt;p&gt;We define the usage of capitals in a word to be right when one of the following cases holds:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;All letters in this word are capitals, like "USA".&lt;/li&gt;
&lt;li&gt;All letters in this word are not capitals, like "leetcode".&lt;/li&gt;
&lt;li&gt;Only the first letter in this word is capital, like "Google".&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Otherwise, we define that this word doesn't use capitals in a right way.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The input will be a non-empty word consisting of uppercase and lowercase latin letters.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The approach I decided to take to this problem was to check the word &lt;em&gt;character by character&lt;/em&gt; against each of the three "valid" patterns (as described in the problem above):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;All characters must be uppercase&lt;/li&gt;
&lt;li&gt;All characters must be lowercase&lt;/li&gt;
&lt;li&gt;The first character is uppercase, and the rest are lowercase&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If the word matched any of the three valid patterns, then return &lt;code&gt;true&lt;/code&gt; and if not, return &lt;code&gt;false&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;detectCapitalUse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;allUpperCase&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;allLowerCase&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;matchCondition1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;allUpperCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;matchCondition1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;matchCondition2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;allLowerCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;matchCondition2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;matchCondition3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;allLowerCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;matchCondition3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you can see, I created three variables: &lt;code&gt;matchCondition1&lt;/code&gt;, &lt;code&gt;matchCondition2&lt;/code&gt; and &lt;code&gt;matchCondition3&lt;/code&gt;  corresponding to the three valid patterns. Each condition is checked and we return &lt;code&gt;true&lt;/code&gt; &lt;em&gt;immediately&lt;/em&gt; if the word matches that particular pattern - for example, if a word is all capitals, I don't need to check the other two patterns, I already know that the word is valid and so I can return &lt;code&gt;true&lt;/code&gt; at that point.&lt;/p&gt;

&lt;p&gt;I also decided to separate out the methods which check if all characters are uppercase or lowercase. I felt this made the code more readable as I do this repeatedly. Let's dive in to these methods a little bit, and look at &lt;code&gt;allUpperCase&lt;/code&gt;. It takes in a string and iterates over each character using a &lt;code&gt;for...of&lt;/code&gt; loop which gives us the &lt;em&gt;value&lt;/em&gt; of each character (this is different to a &lt;code&gt;for...in&lt;/code&gt; loop which would give us the &lt;em&gt;index&lt;/em&gt;). For each character, I check whether it is lowercase: if it is, I break out of the loop and return &lt;code&gt;false&lt;/code&gt;, otherwise, return &lt;code&gt;true&lt;/code&gt; (because it means that &lt;em&gt;every&lt;/em&gt; character must be uppercase). Again, I purposely chose to use a &lt;code&gt;for...of&lt;/code&gt; loop because it allows me to abort the loop, which you can't do using a &lt;code&gt;forEach&lt;/code&gt;. By aborting the loop as soon as I encounter a lowercase letter, I know that I am not performing any unnecessary operations - if one letter is not uppercase, that is enough for me to know that they are not &lt;em&gt;all&lt;/em&gt; uppercase. &lt;/p&gt;

&lt;p&gt;As you might've guessed, &lt;code&gt;allLowerCase&lt;/code&gt; is doing the same thing, except that it checks whether a character is uppercase, and returns &lt;code&gt;false&lt;/code&gt; if it encounters any characters that are, and &lt;code&gt;true&lt;/code&gt; if it doesn't.&lt;/p&gt;

&lt;p&gt;And that's it! We have solved this problem 🎉&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Time complexity: O(n)&lt;/strong&gt; where &lt;em&gt;n&lt;/em&gt; is the word length. At worst, we need to check each character in the word 3 times.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Space complexity: O(1)&lt;/strong&gt; . We only need constant space to store our variables.&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>javascript</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Try and fail, but don't fail to try.</title>
      <dc:creator>Natalie Lang</dc:creator>
      <pubDate>Fri, 10 Jul 2020 08:40:13 +0000</pubDate>
      <link>https://dev.to/natalielang/try-and-fail-but-don-t-fail-to-try-1p9a</link>
      <guid>https://dev.to/natalielang/try-and-fail-but-don-t-fail-to-try-1p9a</guid>
      <description>&lt;p&gt;That's the quote that I saw on my &lt;a href="https://momentumdash.com/"&gt;momentum&lt;/a&gt; dashboard this morning. It seemed pretty pertinent.&lt;/p&gt;

&lt;p&gt;For months I have been telling myself that I'm going to start blogging. I kept debating with myself and making excuses.&lt;/p&gt;

&lt;p&gt;Should I start an online course and use blog posts to track my progress? Should I write about an interesting project at work? Should I write about something I'm working on in my spare time? Should I focus on a concept that I've always found difficult and use a blog to make it clearer to me and others?&lt;/p&gt;

&lt;p&gt;I couldn't decided on what the "right" thing would be to start with and convinced myself that until I found that thing, I shouldn't write anything. &lt;/p&gt;

&lt;p&gt;Today I decided to just jump in, to just &lt;em&gt;try&lt;/em&gt; - what have I got to lose? It's almost as if Chrome knew.&lt;/p&gt;

</description>
      <category>start</category>
      <category>first</category>
      <category>post</category>
      <category>newbie</category>
    </item>
  </channel>
</rss>
