<?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 Shingo K on Medium]]></title>
        <description><![CDATA[Stories by Shingo K on Medium]]></description>
        <link>https://medium.com/@k1ckap0l03?source=rss-9270b22b8c37------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*kgyIHjGTf7qB5GmUIE8SOw.png</url>
            <title>Stories by Shingo K on Medium</title>
            <link>https://medium.com/@k1ckap0l03?source=rss-9270b22b8c37------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 06:51:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@k1ckap0l03/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[Puzzle Pt.3 Convert Arabic Numerals To Roman Numerals]]></title>
            <link>https://medium.com/@k1ckap0l03/puzzle-pt-3-convert-arabic-numerals-to-roman-numerals-9735816206ed?source=rss-9270b22b8c37------2</link>
            <guid isPermaLink="false">https://medium.com/p/9735816206ed</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[coding-interviews]]></category>
            <dc:creator><![CDATA[Shingo K]]></dc:creator>
            <pubDate>Sat, 09 May 2020 20:49:52 GMT</pubDate>
            <atom:updated>2020-05-09T20:49:52.071Z</atom:updated>
            <content:encoded><![CDATA[<p>The puzzle part 3 is a typical interview question: “Convert Arabic numerals To Roman numerals”.</p><h3>Problem</h3><p>Given Arabic numerals, write a function that returns Roman numerals corresponding to the given Arabic numerals. In this problem, the Arabic numerals are greater than or equal to “1” and less than or equal to “3999”.</p><p>For example,</p><ul><li>“123” is converted to “CXXIII”.</li><li>“499” is converted to “CDXCIX”.</li></ul><h3>Answer</h3><p>I have the following two solutions. The solution 2 is shorter than the solution 1. In both solutions, when Arabic numerals are like 4, 9, 40, 90, 400, 900, etc., these are special cases. For example, “4” is converted to “IV”. 499 is converted to “CDXCIX”.</p><h4>Solution 1</h4><pre>const ARABIC_TO_ROMAN = {<br>  1: &#39;I&#39;,<br>  4: &#39;IV&#39;,<br>  5: &#39;V&#39;,<br>  9: &#39;IX&#39;,<br>  10: &#39;X&#39;,<br>  40: &#39;XL&#39;,<br>  50: &#39;L&#39;,<br>  90: &#39;XC&#39;,<br>  100: &#39;C&#39;,<br>  400: &#39;CD&#39;,<br>  500: &#39;D&#39;,<br>  900: &#39;CM&#39;,<br>  1000: &#39;M&#39;<br>};</pre><pre>const convert = (number, digit) =&gt; {<br>  let result = &#39;&#39;;<br>  for (let i = 0; i &lt; Math.floor(number / digit); i += 1) {<br>    result += ARABIC_TO_ROMAN[digit];<br>  }<br>  return result;<br>};</pre><pre>const convertArabicToRoman_1 = number =&gt; {<br>  let result = &#39;&#39;;<br>  result += convert(number, 1000);<br>  number = number % 1000;<br>  if (number &gt;= 900 &amp;&amp; number &lt;= 999) {<br>    result += ARABIC_TO_ROMAN[900];<br>  } else if (number &gt;= 400 &amp;&amp; number &lt;= 499) {<br>    result += ARABIC_TO_ROMAN[400];<br>  } else {<br>    result += convert(number, 500);<br>    number = number % 500;<br>    result += convert(number, 100);<br>  }<br>  <br>  number = number % 100;<br>  if (number &gt;= 90 &amp;&amp; number &lt;= 99) {<br>    result += ARABIC_TO_ROMAN[90];<br>  } else if (number &gt;= 40 &amp;&amp; number &lt;= 49) {<br>    result += ARABIC_TO_ROMAN[40];<br>  } else {<br>    result += convert(number, 50);<br>    number = number % 50;<br>    result += convert(number, 10);<br>  }<br>  <br>  number = number % 10;<br>  if (number === 9) {<br>    result += ARABIC_TO_ROMAN[9];<br>  } else if (number === 4) {<br>    result += ARABIC_TO_ROMAN[4];<br>  } else {<br>    result += convert(number, 5);<br>    number = number % 5;<br>    result += convert(number, 1);<br>  }</pre><pre>  return result;<br>};</pre><h4>Solution 2</h4><pre>const convert = (number, i, v, x) =&gt; {<br>  let result = &#39;&#39;;<br>  if (number === 9) {<br>    result += i + x;<br>  } else if (number === 4) {<br>    result += i + v;<br>  } else {<br>    for (let j = 0; j &lt; Math.floor(number / 5); j += 1){<br>      result += v;<br>    }<br>    number = number % 5;<br>    for (let j = 0; j &lt; number; j += 1){<br>      result +=  i;<br>    }<br>  }<br>  return result;<br>};</pre><pre>const convertArabicToRoman_2 = number =&gt; {<br>  let result = &#39;&#39;;<br>  for (let i = 0; i &lt; Math.floor(number / 1000); i += 1){<br>    result += &#39;M&#39;;<br>  }<br>  <br>  number = number % 1000;<br>  result += convert(Math.floor(number / 100), &#39;C&#39;, &#39;D&#39;, &#39;M&#39;);</pre><pre>  number = number % 100;<br>  result += convert(Math.floor(number / 10), &#39;X&#39;, &#39;L&#39;, &#39;C&#39;);<br>  <br>  number = number % 10;<br>  result += convert(number, &#39;I&#39;, &#39;V&#39;, &#39;X&#39;);</pre><pre>  return result;<br>};</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9735816206ed" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[It’s Fun. Let’s use recursion. Pt.2]]></title>
            <link>https://medium.com/@k1ckap0l03/its-fun-let-s-use-recursion-pt-2-a6b7ce98b8ee?source=rss-9270b22b8c37------2</link>
            <guid isPermaLink="false">https://medium.com/p/a6b7ce98b8ee</guid>
            <category><![CDATA[multidimensional-arrays]]></category>
            <category><![CDATA[recursion]]></category>
            <category><![CDATA[coding-question]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Shingo K]]></dc:creator>
            <pubDate>Sat, 09 May 2020 20:47:27 GMT</pubDate>
            <atom:updated>2020-05-09T20:47:27.113Z</atom:updated>
            <content:encoded><![CDATA[<h4>Multidimensional Array</h4><p>In this article, I’d like to talk about multidimensional array with recursion.</p><h4>Before we start</h4><p>Multidimensional array is an array containing one or more sub arrays, some of which are deeply nested.</p><pre>[1, [1, [1, 2, [1, 2, 3, [1, 2, 3, 4]]]]</pre><h4>Problem</h4><p>Let’s say you’re now thinking of adding a new array: [1, 2, 3, 4] into the original array: [1, [1, 2, [1, 2, 3]]]. The thing is, the position where you want to push the new array is originalArray[1][2]: [1, 2, 3]. So what it should look like is [1, [1, [1, 2, [1, 2, 3, [1, 2, 3, 4]]]]. To be honest, it’s a bit complicated to make it because the original array consists of arrays, which are deeply nested. How can we get to the deepest nested array?</p><p>A good way to track such an array with a complex structure is <strong>recursion</strong>. How should you break down this problem using recursion? First of all, you need to grasp the structure of this original array carefully.</p><p>Let’s get started with a simple case. In this case, the original array is: const originalArray = [1, [1, 2]]. In the first index (index = 0), the element is a number, originalArray[0] = 1. In the last index (index = 1), the nested sub array, originalArray[1] = [1, 2], is there. Next, let’s think of a slightly more complicated case: [1, [1, 2, [1, 2, 3]]]. The deepest nested sub array is [1, 2, 3]. It is located in the last index of the second nested sub array, [1, 2, [1, 2, 3]]. Let’s move onto [1, 2, 3]. This sub array is the most deeply nested. The one thing you will notice is that all elements are numbers. This is exactly where we want to push a new array, [1, 2, 3, 4], into the end of this array, in other words, next to 3 (the third element).</p><h4>Summary of what we want to do</h4><p>I’m going to summarize what I mentioned above. In this example, the input array is [1, [1, 2, [1, 2, 3]]].</p><p>Round 1: [1, sub_array_1 (= [1, 2, [1, 2, 3]]) ]<br>If the type of the end element in this array is array, we need to go on a dive into the sub_array_1.</p><p>Round 2: sub_array_1 = [1, 2, sub_array_2 (= [1, 2, 3])]<br>If the type of the last element in this array is array, we need to move to the sub_array_2.</p><p>Round 3: sub_array_2 = [1, 2, 3]<br>The last element is number, not array so we have to stop tracking. Here, we need to generate a new array and push it into the end of the sub_array_2. Pushing [1, 2, 3, 4] into sub_array_2, the result is [1, [1, [1, 2, [1, 2, 3, [1, 2, 3, 4]]]].</p><h4>Skeleton</h4><p>Now we should start coming up with the skeleton code.</p><pre>const recur = array =&gt; {<br>  if the last element of the array is array<br>    recursive case with sub array<br>  else<br>    do something when you get to the deepest sub array<br>  <br>  return array<br>}</pre><h4>Solution</h4><p>Here is a full code:</p><pre>const recur = array =&gt; {<br>  //Check if the last element of the array is array or not<br>  if (Array.isArray(array[array.length - 1])) {<br>    //Call recur with the sub array, which is in the last element <br>    recur(array[array.length - 1]);        <br>  } else {<br>    //Generate a new array<br>    let child = [];<br>    let max = array.length + 1;<br>    for (let i = 1; i &lt;= max; i += 1) {<br>      child.push(i);<br>    }<br>    //Push the new array <br>    array.push(child);<br>  }<br>  return array;<br>}</pre><pre>recur([1, [1, 2, [1, 2, 3]]])<br>The output is: <br>[1, [1, [1, 2, [1, 2, 3, [1, 2, 3, 4]]]]</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a6b7ce98b8ee" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Puzzle Pt.2 Pokemon Stamp Rally]]></title>
            <link>https://medium.com/@k1ckap0l03/puzzle-pt2-pokemon-stamp-rally-12d015cf62e9?source=rss-9270b22b8c37------2</link>
            <guid isPermaLink="false">https://medium.com/p/12d015cf62e9</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[puzzle]]></category>
            <dc:creator><![CDATA[Shingo K]]></dc:creator>
            <pubDate>Fri, 31 Aug 2018 18:02:22 GMT</pubDate>
            <atom:updated>2018-08-31T19:33:21.573Z</atom:updated>
            <content:encoded><![CDATA[<p>In 1996, the Pokemon stamp rally started on the Yamanote train line in Tokyo, Japan. What is “stamp rally”? For the stamp rally, people collect a series of rubber stamps in a passport type booklet at railway stops on the Yamanote line. In Japan, this style of entertainment has become really popular. Anyway, today’s puzzle is related to the stamp rally.</p><h3>Problem</h3><p>Yamanote line, which is <strong>a loop line</strong>, has 29 stations. (In 2020, a new station will be completed and opened between Shinagawa and Osaki stations.)</p><p>How many different stop variations are there for collecting Pokemon stamps when participants must enter at station<strong> No.1</strong> and exit at station<strong> No.14</strong>?</p><p><strong>Note: The participants in this stamp rally don’t necessarily have to collect stamps at each station</strong>.</p><pre>[1]: the station No.1. Participants must start at this station.<br>[14]: the station No.14. Participants must exit at this station.</pre><pre>  [27][28][29][<strong><em>01</em></strong>][02][03][04]<br>[26]                        [05]<br>[25]                        [06]<br>[24]                        [07]<br>[23]                        [08]<br>[22]                        [09]<br>[21]                        [10]<br>[20]                        [11]<br>[19][18][17][16][15][<strong><em>14</em></strong>][13][12]</pre><h3>How to think of this puzzle</h3><p>When participants get to a train stop, they have 2 options: 0 or 1.</p><ul><li>0: not getting off the stop and going to the next stop.</li><li>1: getting off the stop, getting the Pokemon stamp and going to the next stop.</li></ul><p>You need to think of the inner track (counter clockwise) and the outer track (clockwise) in the loop train.</p><p>Participants must get on the start station and get off the end one. Then, they think of wether or not they get off the stop and get the Pokemon stamp “<em>the number of stations between the start and the end</em>” times.</p><p>This is a loop line. Even if they use the inner track and the outer track, when they only stamp Pokemons at the start and end, they will get the same 2 kinds of stamps. That’s why we have to subtract one from the sum in order to prevent overlap.</p><p>In order to make the problem more simple, let’s think about the loop line which consists of 5 stations.</p><pre> [1]-[2]-[3]<br>  |      /<br> [5]-[4]</pre><p>In this simple case, the answer is bellow:</p><pre>The inner track (1-&gt;5): counter clockwise<br>1-5</pre><pre>The outer track (1-&gt;2-&gt;…-&gt;5): clockwise<br>1-2-3-4-5<br>1-2-3-5<br>1-2-4-5<br>1-3-4-5<br>1-2-5<br>1-3-5<br>1-4-5<br>1-5</pre><pre>2³ + 2⁰ - 1 = 8 ways</pre><ul><li>2³: 3 is the number of stations between the start and the end in the outer track (1-&gt;2-&gt;…-&gt;5). At each train stop (No. 2, No. 3 and No. 4), we have 2 options about whether or not we will get off the stop and get the Pokemon stamp of the station. That’s why there are 2*2*2 ways.</li><li>2⁰: 0 is the number of stations between the start and the end in the inner track (1-&gt;5). Like the above, there is only 1 way (1-&gt;5).</li><li>-1: the inner track and the outer track have the same way (1-&gt;5) so we subtract one way to avoid overlap.</li></ul><h3>Answer</h3><pre>const stampRally = (numberOfStaions, start, end) =&gt; {<br>  return 2 ** (Math.abs(start - end) - 1) + 2 ** (numberOfstaions - Math.abs(start - end) - 1) - 1;<br>}</pre><p>The answer is 36863 ways.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=12d015cf62e9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Puzzle Pt1. Majority Decision]]></title>
            <link>https://medium.com/@k1ckap0l03/puzzle-pt1-majority-decision-dd22ed28d8be?source=rss-9270b22b8c37------2</link>
            <guid isPermaLink="false">https://medium.com/p/dd22ed28d8be</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[coding-interviews]]></category>
            <dc:creator><![CDATA[Shingo K]]></dc:creator>
            <pubDate>Thu, 30 Aug 2018 04:33:00 GMT</pubDate>
            <atom:updated>2018-08-30T04:33:00.212Z</atom:updated>
            <content:encoded><![CDATA[<p>Let’s start doing coding gymnastics! Today’s topic is “<em>Majority Decision</em>”.</p><h3>Problem</h3><p>There are 3 plans: plan A, plan B, plan C. How many patterns are there when a plan (plan A, plan B or plan C) is chosen by majority vote?</p><p>Given the number of voters and initial value of count (= 0), write a function that returns the total value of count.</p><p>For example,</p><ul><li>The number of voters is 3. The output is 9. (There are 9 patterns.)</li><li>The number of voters is 4. The output is 12. (There are 12patterns.)</li></ul><h3>How to think of this problem</h3><p>Applying combination with repetition in math, try to solve this problem. The famous way is using partitions to separate three groups (The group which consists of voters to support plan A, plan B or plan C).</p><pre>o: voter, |: partition</pre><pre>||oooo // =&gt; 4 people support plan C.<br>|o|ooo // =&gt; 1 person supports plan B. 3 people support plan C.<br>|oo|oo // =&gt; We need to remove this kind of pattern.<br>|ooo|o<br>|oooo|<br>o||ooo<br>o|o|oo<br>o|oo|o<br>o|ooo|<br>oo||oo // =&gt; We need to remove this kind of pattern.<br>oo|o|o<br>oo|oo| // =&gt; We need to remove this kind of pattern.<br>ooo||o<br>ooo|o|<br>oooo||</pre><h3>Answer</h3><pre>const majorityDecision = (numberOfVoters, count) =&gt; {<br>  for (let left = 0; left &lt;= numberOfVoters; left += 1) {<br>    for (let right = left; right &lt;= numberOfVoters; right += 1) {<br>      if (left &gt; right - left) {<br>        if (left !== numberOfVoters - right) {<br>          count += 1;<br>        }<br>      } else if (left &lt; right - left) {<br>        if (right - left !== numberOfVoters - right) {<br>          count += 1;<br>        }<br>      } else {<br>        if (left &lt; numberOfVoters - right) {<br>          count += 1;<br>        }<br>      }<br>    }<br>  }</pre><pre>  return count;<br>}</pre><pre>majorityDecision(4, 0)</pre><p>The output is:</p><pre>12 // The output is 12 patterns.</pre><pre>A B C (A = left, B = right - left, C = numberOfVoters - right)<br>0 0 4 // C is chosen by majority vote.<br>0 1 3 // C is chosen by majority vote.<br>0 3 1 // B is chosen by majority vote.<br>0 4 0 // B is chosen by majority vote.<br>1 0 3 // C is chosen by majority vote.<br>1 1 2 // C is chosen by majority vote.<br>1 2 1 // B is chosen by majority vote.<br>1 3 0 // B is chosen by majority vote.<br>2 1 1 // A is chosen by majority vote.<br>3 0 1 // A is chosen by majority vote.<br>3 1 0 // A is chosen by majority vote.<br>4 0 0 // A is chosen by majority vote.</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dd22ed28d8be" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Nested div elements with click event]]></title>
            <link>https://medium.com/@k1ckap0l03/nested-div-elements-with-click-event-c5750c83f8?source=rss-9270b22b8c37------2</link>
            <guid isPermaLink="false">https://medium.com/p/c5750c83f8</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[html]]></category>
            <dc:creator><![CDATA[Shingo K]]></dc:creator>
            <pubDate>Mon, 20 Aug 2018 08:41:46 GMT</pubDate>
            <atom:updated>2018-08-20T08:41:46.238Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article, I’d like to talk about nested div elements with click event.</p><h3>Normal div elements</h3><p>First, given that normalDivs.html and clickEvent.js.</p><pre>normalDivs.html</pre><pre>&lt;html lang=&quot;en&quot;&gt;<br>  &lt;body&gt;<br>    &lt;div id=&#39;1&#39;&gt;<br>      Div1<br>    &lt;/div&gt;<br>    &lt;div id=&#39;2&#39;&gt;<br>      Div2<br>    &lt;/div&gt;<br>    &lt;div id=&#39;3&#39;&gt;<br>      Div3<br>    &lt;/div&gt;<br>    &lt;script type=&quot;text/javascript&quot; src=&quot;clickEvent.js&quot;&gt;&lt;/script&gt;<br>  &lt;/body&gt;<br>&lt;/html&gt;</pre><pre>clickEvent.js</pre><pre>const divs = document.getElementsByTagName(&#39;div&#39;);</pre><pre>for (var i = 0; i &lt; divs.length; i += 1) {<br>  divs[i].addEventListener(&#39;click&#39;, function () {<br>    console.log(&#39;this is&#39;, this.id);<br>  }, false);<br>}</pre><p>The above normalDivs.html and clickEnvent.js generate the following:</p><pre>Div1  // -&gt; Clicking “Div1”, the output is &quot;this is 1&quot;.<br>Div2  // -&gt; Clicking “Div2”, the output is &quot;this is 2&quot;.<br>Div3  // -&gt; Clicking “Div3”, the output is &quot;this is 3&quot;.</pre><p>These results are natural and apparent.</p><h3>Nested div elements</h3><p>Next, given that normalDivs.html and clickEvent.js.</p><pre>nestedDivs.html</pre><pre>&lt;html lang=&quot;en&quot;&gt;<br>  &lt;body&gt;<br>    &lt;div id=&#39;1&#39;&gt;<br>      Div1<br>      &lt;div id=&#39;2&#39;&gt;<br>        Div2<br>        &lt;div id=&#39;3&#39;&gt;<br>          Div3<br>        &lt;/div&gt;<br>      &lt;/div&gt;<br>    &lt;/div&gt;<br>    &lt;script type=&quot;text/javascript&quot; src=&quot;clickEvent.js&quot;&gt;&lt;/script&gt;<br>  &lt;/body&gt;<br>&lt;/html&gt;</pre><p>The above nestedDivs.html and clickEvent.js generates the following. This looks like the same as normalDivs.html.</p><pre>Div1<br>Div2<br>Div3</pre><p>What happens if you click Div1, Div2 or Div3?</p><p>Clicking “Div1”, the result is</p><pre>this is 1</pre><p>Clicking “Div2”, the result is</p><pre>this is 2<br>this is 1</pre><p>Clicking “Div3”, the result is</p><pre>this is 3<br>this is 2<br>this is 1</pre><p>From these results, we can see the difference between normal and nested div elements with click event. If you click one of div elements in nested div elements, you also click its parent elements after clicking the target element. (e.g. Div1 is the parent element of Div2 and Div3 (child elements).Div2 is the parent element of Div1.)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c5750c83f8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Rabbit and Turtle Algorithm]]></title>
            <link>https://medium.com/@k1ckap0l03/rabbit-and-turtle-algorithm-2c68dde598a2?source=rss-9270b22b8c37------2</link>
            <guid isPermaLink="false">https://medium.com/p/2c68dde598a2</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Shingo K]]></dc:creator>
            <pubDate>Mon, 20 Aug 2018 03:05:32 GMT</pubDate>
            <atom:updated>2018-08-23T17:28:26.711Z</atom:updated>
            <content:encoded><![CDATA[<p>“<em>Once upon a time, there was a rabbit and a turtle. They were good friends. The rabbit boasted that he could run faster than the turtle. So they decided to have a race…</em>”</p><p>This is a famous and fantastic story. But, in this article, it’s not like I want to talk about this story. I’m going to write the topic on how to detect a loop in a simple linked list. This is also a famous and interesting problem in algorithms problems. The topic of this article is this one! The concept of rabbit and turtle story is useful to solve this algorithm problem.</p><h3>How to detect a loop in a singly-linked list</h3><p>This problem is not as difficult as it seemed at the first sight. Let’s think about the problem with rabbit and turtle.</p><h4>0. Basic concept</h4><p>We prepare two pointers. One is a turtle and the other is a rabbit. A turtle is moved by one. At the same time, a rabbit is moved by two.</p><p>After starting at the same start point, then if the turtle meets up the rabbit again, it means this liked list has a loop. However, the rabbit reaches a goal (the last node) ahead of the turtle. Which means this liked list is straight.</p><h4>1. Linked list without a loop</h4><p>The rabbit reaches a goal (the last node) ahead of the turtle when the turtle arrives at the middle node (e.g. if the numbers of linked list is <em>n</em>, the rabbit reaches the last node when the turtle is still at the n/2 node.).</p><pre>a: turtle, b: rabbit, node: [1], ... ,[7]</pre><pre>&gt;Step0 (START)<br>ab<br>[1]-[2]-[3]-[4]-[5]-[6]-[7]</pre><pre>&gt;Step1<br>     a   b<br>[1]-[2]-[3]-[4]-[5]-[6]-[7]</pre><pre>&gt;Step2<br>         a       b<br>[1]-[2]-[3]-[4]-[5]-[6]-[7]</pre><pre>&gt;Step3 (GOAL)<br>             a           b<br>[1]-[2]-[3]-[4]-[5]-[6]-[7]</pre><p>This also means <strong>the way to find the middle node of a given linked list</strong>.</p><h4>2. Linked list with a loop</h4><p>If the linked list has a loop, <strong>the turtle will meets up the rabbit at the last node again</strong>.</p><pre>a: turtle, b: rabbit, node: [1], ... ,[7]</pre><pre>&gt;Step0 (START)<br>ab<br>[1]-[2]-[3]-[4]<br>     |       |<br>    [7]-[6]-[5]</pre><pre>&gt;Step1<br>     a   b<br>[1]-[2]-[3]-[4]<br>     |       |<br>    [7]-[6]-[5]</pre><pre>&gt;Step2<br>         a<br>[1]-[2]-[3]-[4]<br>     |       |<br>    [7]-[6]-[5]<br>             b<br>&gt;Step3<br>             a<br>[1]-[2]-[3]-[4]<br>     |       |<br>    [7]-[6]-[5]<br>     b<br>&gt;Step4<br>         b<br>[1]-[2]-[3]-[4]<br>     |       |<br>    [7]-[6]-[5]<br>             a<br>&gt;Step5<br><br>[1]-[2]-[3]-[4]<br>     |       |<br>    [7]-[6]-[5]<br>         a   b<br>&gt;Step6 (GOAL)<br><br>[1]-[2]-[3]-[4]<br>     |       |<br>    [7]-[6]-[5]<br>     ab</pre><h4>3. Time complexity</h4><p>Time complexity is <em>O</em>(n).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2c68dde598a2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[apply(), call() and bind() in JavaScript]]></title>
            <link>https://medium.com/@k1ckap0l03/apply-call-and-bind-in-javascript-df9beefbcc71?source=rss-9270b22b8c37------2</link>
            <guid isPermaLink="false">https://medium.com/p/df9beefbcc71</guid>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Shingo K]]></dc:creator>
            <pubDate>Sun, 19 Aug 2018 22:17:41 GMT</pubDate>
            <atom:updated>2018-11-30T04:58:39.171Z</atom:updated>
            <content:encoded><![CDATA[<p>In JavaScript, Function object has apply(), call() and bind() methods. These are the methods which can assign the value of this and arguments for functions and then execute the functions with the assigned this and arguments. These features are one of the basics in JavaScript as well.</p><h3>apply() and call()</h3><ul><li>apply(): Assign the value of this and arguments and then return a function. Arguments are assigned using <strong>array</strong>.</li><li>call(): It is the almost same as apply(). The only difference is the way to assign arguments. Arguments are given using <strong>arguments list</strong>.</li></ul><pre>const say = function(greetings, title) {<br>  console.log(greetings, title, this.name);<br>}</pre><pre>const john = { name: &quot;John Smith&quot; };<br>const jane = { name: &quot;Jane Smith&quot; };</pre><pre>//apply()<br>say.apply(john, [&quot;Hello!&quot;, &quot;Mr.&quot;]); // -&gt; &quot;Hello! Mr. John Smith&quot;<br>say.apply(jane, [&quot;Hi!&quot;, &quot;Ms.&quot;]);    // -&gt; &quot;Hi! Ms. Jane Smith&quot;</pre><pre>//call()<br>say.call(john, &quot;Hello&quot;, &quot;Mr.&quot;);    // -&gt; &quot;Hello! Mr. John Smith&quot;<br>say.call(jane, &quot;Hi&quot;, &quot;Ms.&quot;);       // -&gt; &quot;Hi! Ms. Jane Smith&quot;</pre><p>Notice: you can’t use arrow functions in ES6 when you want to use apply() and call(). Read <a href="https://medium.com/@k1ckap0l03/this-defined-by-arrow-functions-59be78f5c00b">this article</a> if you want to know more detail.</p><h3>bind()</h3><p>bind() binds functions with objects.</p><ul><li>bind(): Assign the value of this and arguments and then <strong>generate a new function</strong>.</li></ul><pre>const say = function(greetings, title) {<br>  console.log(greetings, title, this.name);<br>}</pre><pre>const john = { name: &quot;John Smith&quot; };</pre><pre>const greetJohn = say.bind(john);<br>greetJohn(&quot;Hello&quot;, &quot;Mr.&quot;);  // -&gt; Hello Mr. John Smith</pre><p>Calling greetJohn, john is always the value of this. say.bind(john) returns a new function which binds the object john with this of function say.</p><h3>Practice</h3><p>Using a typical interview question, let’s check how to use call() and apply().</p><pre>fullname = &#39;John Smith&#39;;<br>const obj = {<br>   fullname: &#39;Emma Johnson&#39;,<br>   prop: {<br>      fullname: &#39;Olivia Davis&#39;,<br>      getFullname: function() {<br>         return this.fullname;<br>      }<br>   }<br>};<br><br>console.log(obj.prop.getFullname());<br><br>const quiz = obj.prop.getFullname;<br><br>console.log(quiz());</pre><p>The result of this code is</p><pre>Olivia Davis<br>John Smith</pre><p><strong>How should you fix the code in order for </strong>quiz()<strong> to show </strong>“Olivia Davis” <strong>and </strong>“Emma Johnson”<strong>?</strong></p><p>Now quiz see fullname defined in global. That’s why the output is John Smith. How to fix it using apply() or call()?</p><p>The answer is bellow:</p><pre>quiz.call(obj.prop) // -&gt; Olivia Davis<br>quiz.call(obj) // -&gt; Emma Johnson</pre><p>Of course, even if you use apply(), you will get the same result.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=df9beefbcc71" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hoisting in JavaScript]]></title>
            <link>https://medium.com/@k1ckap0l03/hoisting-in-javascript-e7a17f5ff271?source=rss-9270b22b8c37------2</link>
            <guid isPermaLink="false">https://medium.com/p/e7a17f5ff271</guid>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Shingo K]]></dc:creator>
            <pubDate>Sun, 19 Aug 2018 19:55:48 GMT</pubDate>
            <atom:updated>2018-08-19T19:55:48.871Z</atom:updated>
            <content:encoded><![CDATA[<p>Hoisting is one of the most important features in Javascript. This is one of the significant basics. However, some people would say, “<em>It sounds familiar but I’ve never cared about this when I code. What’s this?</em>” As for me, I’d kind of understood hoisting for a long time but I was able to write code without any issues. After studying for technical interviews to get a job, I studied and understood it again. In this article, I write “Hoisting” in JavaScript with a typical interview question.</p><h3>What the hell is Hoisting?</h3><p>Look at this code and what is the output?</p><pre>function test() {<br>  console.log(a);<br>  console.log(hello());<br>   <br>  var a = 1;<br>  function hello() {<br>    return &#39;Hello&#39;;<br>  }<br>}<br>test();</pre><p>The above code is wired. What are outputted before the variable and function are declared in the function? But you would think that everybody usually declares variables and functions at the top of the code so you feel the code above is strange. Some of them doubt whether or not this code will work correctly.</p><p>The result of the code is</p><pre>undefined<br>Hello </pre><p>The reason why it works is because variables and functions are hoisted in JavaScript. Hoisting means moving at the top of the function. Variables don’t retain any assigned value. At this time, variable a is normally outputted but it doesn’t have any value at the moment. That’s why the output is undefined. As for function, thanks to hoisting, we can declare functions anywhere in the code. As follows, if a function is declared after you used it, it works correctly.</p><pre>console.log(square(5)); // -&gt; 25<br>function square(x) { return x*x; }</pre><p>But you should care about the one thing. If you use an anonymous function and insert it into a specific variable like below:</p><pre>console.log(square(5)); // -&gt; square is not a function<br>var square = function(x) { return x*x; }</pre><p>In this case, <strong>function hoisting is not conducted</strong>.</p><p>Moving back to the example about hoisting, the code is equal to the following code:</p><pre>function test() {<br>  var a;<br>  function foo() {<br>    return 2;<br>  }<br><br>  console.log(a);<br>  console.log(foo());<br>   <br>  a = 1;<br>}<br><br>test();</pre><p>This feature is convenient but a bit tricky.</p><h3>Changes related hoisting in ES6</h3><p>Moving on to changes related to hoisting in ES6, there is a big change about variable hoisting. As you know, let and const declarations are added as one of the biggest changes in ES6. <strong>Unlike var, using let and const declarations, hoisting is not conducted. </strong>See an example below:</p><pre>console.log(x);<br>var x = 2;<br>=&gt; undefined</pre><pre>console.log(x);<br>let x = 2;<br>=&gt; ReferenceError: x is not defined</pre><pre>console.log(x);<br>const x = 2;<br>=&gt; ReferenceError: x is not defined</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e7a17f5ff271" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to ensure quality in the code]]></title>
            <link>https://medium.com/@k1ckap0l03/how-to-ensure-quality-in-the-code-c1b71d1987d8?source=rss-9270b22b8c37------2</link>
            <guid isPermaLink="false">https://medium.com/p/c1b71d1987d8</guid>
            <category><![CDATA[code]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[tdd]]></category>
            <category><![CDATA[testing]]></category>
            <dc:creator><![CDATA[Shingo K]]></dc:creator>
            <pubDate>Sun, 19 Aug 2018 06:22:16 GMT</pubDate>
            <atom:updated>2018-08-19T06:22:16.629Z</atom:updated>
            <content:encoded><![CDATA[<p>In order to ensure quality in the code, when I write code, I care about these 3 things below:</p><h3>Test Driven Development (TDD)</h3><p>Firstly, regarding the software development process, TDD (test-driven development) is a good process to ensure quality in code.</p><p>As I see it, in TDD, tests tell me what I should write. Compared to other development methods, the tests become more important meaning that the quality of the code we write will directly connect to them.</p><p>For example, when I code, I write the tests first and then write code which would at least pass these tests. I repeat this cycle and gradually make the code more sophisticated.</p><p>In order for TDD to progress smoothly, I make full use of testing frameworks and TDD assertion libraries such as Mocha, Jasmine, Chai, Sinon and Jest.</p><h3>Test case scenarios</h3><p>Secondly, test cases should be accurate enough so that everybody can understand them.</p><p>In the past, I wrote test cases that were vague, for a certain sorting logic and after the app was launched, an error happened because of a glitch regarding this logic.</p><p>As a result, I learned that if the test case is too vague, the results that require confirmation would also be vague. It’s also highly likely that we could overlook potential glitches. So since then, I always bear in mind that I have to write test cases that are highly accurate.</p><h3>Concise code</h3><p>Thirdly, about the code itself, in order to reduce the number of mistakes and potential glitches in the code I write, I always make sure to write simple code, which means the code should not be too complex.</p><p>I always try to use simple logic that everybody can understand. When I have other people review my code, If they don’t fully understand the logic the quality of reviews would be affected. I also use ESLint, which can check my code automatically.</p><h3>Summary</h3><ol><li>Use test-driven development (TDD) for developing software.</li><li>Make highly accurate test case scenarios that everybody can understand.</li><li>Write simple logic that everybody can understand.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c1b71d1987d8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[“this” defined by Arrow Functions]]></title>
            <link>https://medium.com/@k1ckap0l03/this-defined-by-arrow-functions-59be78f5c00b?source=rss-9270b22b8c37------2</link>
            <guid isPermaLink="false">https://medium.com/p/59be78f5c00b</guid>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Shingo K]]></dc:creator>
            <pubDate>Sun, 19 Aug 2018 04:41:18 GMT</pubDate>
            <atom:updated>2018-08-19T05:45:19.135Z</atom:updated>
            <content:encoded><![CDATA[<p>I used to write the article about “<em>What is ‘this’ in JavaScript?</em>”. In the article, I categorized six kinds of this in JavaScript. I looked into this further and found the other information about it.</p><h3>1. What is Arrow Function?</h3><p>There was the only way to write functions using a usual way in JavaScript as bellow:</p><pre>const f = function(a) { return a*a };</pre><p>After ES6, a new way, “Arrow Function”, is introduced. Simply saying, arrow function is a shorthand way for defining anonymous functions.</p><pre>const f = a =&gt; { return a*a };</pre><p>However, it’s not the completely same as a usual function expression. You should notice how it works. One of the biggest differences is the way to handle this inside of arrow functions.</p><h3>2. What is “this” inside of arrow functions?</h3><p>Before starting to explain about this inside of arrow functions, let’s review what this is in JavaScript. The value of “this” is a reference to the object where the function belongs when the function is called.</p><p>One of the biggest differences is that the value of this is decided when the arrow function is defined. The of this inside the arrow function is that of this outside the arrow function. See the below example.</p><pre>const tom = {<br>  name: &quot;Tom&quot;,<br>  sayHello: function() {<br>    console.log(“Hello! &quot; + this.name);<br>    const nestedFunc1 = function() { console.log(this); };<br>    nestedFunc1(); <br>    const nestedFunc2 = () =&gt; { console.log(this); };<br>    nestedFunc2();<br>  }<br>}<br>tom.sayHello();</pre><p>The result is below:</p><pre>Hello! Tom<br>[object Window]// Window {frames: <em>Window</em>, …} -&gt; the usual way<br>[object Object]// {name: &quot;<em>Tom</em>&quot;, sayHello: <em>f()</em>} -&gt; arrow function</pre><p>The value of this in nestedFunc2 defined by arrow function is a reference to the object tom. In contrast, that of this in nestedFunc1 defined by a usual way is a reference to the global object.</p><p>In addition, the value of this in arrow functions is unchangeable even if you use call() and apply() in order to assign this.</p><pre>const f = function() { console.log(&quot;Hello&quot;, this.name) };<br>const g = () =&gt; console.log(&quot;Hello&quot;, this.name);<br>const tom = { name: &quot;Tom&quot; };<br>f();<br>f.call(tom);<br>g();<br>g.call(tom);</pre><p>The bellow results showed that unlike the function f, the value of this in the function g is not the value assigned by call() but the value of this in defining the function.</p><pre>Hello    //f()<br>Hello Tom//f.call(this)<br>Hello    //g()<br>Hello    //g.call(this)</pre><h3>3. Tricky example</h3><p>This was a tricky example for me.</p><pre>name = &quot;John&quot;;<br>const tom = {<br>  name: &quot;Tom&quot;,<br>  sayHello: () =&gt; console.log(&quot;Hello!&quot;, this.name),<br>  sayGoodBye: function() { console.log(&quot;Good Bye!&quot;, this.name) }<br>}<br>tom.sayHello();<br>tom.sayGoodBye();</pre><p>The result is bellow:</p><pre>Hello! John   -&gt; arrow function<br>Good Bye! Tom -&gt; a usual way</pre><p>But why? The above example is the same as the following example. sayHello property is defined outside the object tom. That’s why the value of this inside tom.sayHello() (= f()) is a reference to the global object.</p><pre>const f = () =&gt; console.log(&quot;Hello!&quot;, this.name);<br>const tom = {<br>  name: &quot;Tom&quot;,<br>  sayHello: f<br>}<br>tom.sayHello();</pre><pre>Tom</pre><p>This is a bit tricky so be careful to use arrow functions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=59be78f5c00b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>