<?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: CodeOnJIm</title>
    <description>The latest articles on DEV Community by CodeOnJIm (@codeonjim).</description>
    <link>https://dev.to/codeonjim</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%2F668248%2F70b5f97a-9f52-488c-b3cf-9e83bd8e1cc9.png</url>
      <title>DEV Community: CodeOnJIm</title>
      <link>https://dev.to/codeonjim</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/codeonjim"/>
    <language>en</language>
    <item>
      <title>LeetCode -[Roman to Integer] JS variations</title>
      <dc:creator>CodeOnJIm</dc:creator>
      <pubDate>Mon, 05 Sep 2022 22:40:23 +0000</pubDate>
      <link>https://dev.to/codeonjim/leetcode-roman-to-integer-js-variations-30fa</link>
      <guid>https://dev.to/codeonjim/leetcode-roman-to-integer-js-variations-30fa</guid>
      <description>&lt;p&gt;This is one of the lit LeetCode problems that is supposedly "easy". This is certainly not easy but "beginner friendly" after a few trials and errors, (This is where you will shine if you keep at it!). I won't get into the whole description of the problem, rather start discussing this right away assuming you know why you are here. In case you do not know about this problem, &lt;a href="https://leetcode.com/problems/roman-to-integer/"&gt;here is the problem&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So, since you know that roman integers are counted with few English letters. They are I, V, X, L, C, D, M and each of them has associated numeric value. the very first thing I'm gonna do is to store these values in an object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const romanD = {
          I: 1,
          V: 5,
          X: 10,
          L: 50,
          C: 100,
          D: 500,
          M: 1000,
    };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the thing is, there will be random input (strings) coming in as roman symbols. We have to sum them up. For instance, if the input is 'III' the sum will be 3, since I has a numeric value of 1. So, 'XV' would be 15 and so on. Only difference is, if ANY of the input value has less value than the next one, that would be subtraction, not addition. For instance,&lt;br&gt;
&lt;code&gt;XL === 40&lt;/code&gt; but not &lt;code&gt;60&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;The very first thing I do is pseudo-code. Thanks to CS50!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//store the roman value {}

//split the input

//have an accumulator variable

//run a loop over

//if the curInput &amp;lt; nextInput (substract) else (sum up)

//return or console.log the accumulator
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the first code is with a traditional ForLoop. Goes like this -&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Tq3XnbiK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ll82kzdzmmuutyo6s9h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Tq3XnbiK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ll82kzdzmmuutyo6s9h.png" alt="With Traditional forLoop" width="880" height="425"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ok, In case, this too abstracted for you, here is the &lt;strong&gt;simplified version&lt;/strong&gt; -&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--s_o5vJAz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9noptefpj43v4vurnpe4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--s_o5vJAz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9noptefpj43v4vurnpe4.png" alt="simplified version of the for loop" width="880" height="425"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;hope this is pretty self-explanatory for you. In case not, leave me a comment I will break down the issue for you.&lt;/p&gt;

&lt;p&gt;Now, another variation with &lt;strong&gt;ForEach&lt;/strong&gt; loop now. Goes like this -&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yNW8jAkT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3supj6molplwevmaxs3x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yNW8jAkT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3supj6molplwevmaxs3x.png" alt="With ForEach looop" width="880" height="350"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In case this is abstract again, look at the simplified version up there. It's just the if..else statement that you know. notice that we were able to run a &lt;strong&gt;forEach&lt;/strong&gt; here since we &lt;code&gt;split()&lt;/code&gt; the input, which gives us an array. Ya, I know, I haven't mentioned them. &lt;strong&gt;Here is the full blown code&lt;/strong&gt; ...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CAt5vfr4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lq0faczn20419r7suorc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CAt5vfr4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lq0faczn20419r7suorc.png" alt="FullCode" width="880" height="1103"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With a forLoop, the &lt;em&gt;execution time&lt;/em&gt; is between - &lt;code&gt;78 to 102ms&lt;/code&gt;.&lt;br&gt;
with a forEach, the &lt;em&gt;execution time&lt;/em&gt; is between - &lt;code&gt;74 to 100ms&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Although, your machine's hardware quality impacts this time as well.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
