<?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: jiiin✨</title>
    <description>The latest articles on DEV Community by jiiin✨ (@jiiincho).</description>
    <link>https://dev.to/jiiincho</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%2F1065674%2F7e0b797b-fa74-4ab9-8109-61ff432aaafe.png</url>
      <title>DEV Community: jiiin✨</title>
      <link>https://dev.to/jiiincho</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jiiincho"/>
    <language>en</language>
    <item>
      <title>JS - Map</title>
      <dc:creator>jiiin✨</dc:creator>
      <pubDate>Sun, 16 Apr 2023 11:23:05 +0000</pubDate>
      <link>https://dev.to/jiiincho/javascript-map-j19</link>
      <guid>https://dev.to/jiiincho/javascript-map-j19</guid>
      <description>&lt;p&gt;Related leetCode question: &lt;br&gt;
&lt;a href="https://leetcode.com/problems/two-sum/description/"&gt;https://leetcode.com/problems/two-sum/description/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is my original answer, which uses double for-loop. It is no ideal in terms of iteration time, because there is no breaks and it iterates two loops 😧&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var twoSum = function(nums, target) {
    let result = [];
    for(let i = 0; i &amp;lt; nums.length -1; i++ ) {
        const val1 = nums[i];
        const nextIndice = i + 1;
        const newArr = nums.slice(nextIndice);

        for(let j =0 ; j &amp;lt; newArr.length; j++) {
            const sum = val1 + newArr[j];
            if(sum === target) {
                result = [i, nextIndice + j];

            }
        } 
    }   
    return result;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can make it more performative algorithm using Map object!&lt;/p&gt;

&lt;p&gt;The return value we are interested in is getting array of 'index' that satisfies condition. Thus, first step is to create table having index as value (invert). So we can get the index using &lt;code&gt;get&lt;/code&gt; method from Map class instance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const map = new Map();
map.set(nums[i], i)  // index is value in Map object
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The value we are interested in is that the sum of two values are equal to 'target', and we will iterate through nums array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// target = nums[i] + val 
// same as val = target - nums[i]

if(map.has(target - nums[i])) {
   return [map.get(target - nums[i]), i] // this is what we are looking for! 🌟
} else {
   map.set(nums[i], i)  // update table
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again, here we use Map object to avoid double for-loops!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>JS basics - 1</title>
      <dc:creator>jiiin✨</dc:creator>
      <pubDate>Sun, 16 Apr 2023 10:38:58 +0000</pubDate>
      <link>https://dev.to/jiiincho/js-basics-1-1gj9</link>
      <guid>https://dev.to/jiiincho/js-basics-1-1gj9</guid>
      <description>&lt;p&gt;This questions if from: &lt;a href="https://dev.to/macmacky/70-javascript-interview-questions-5gfi"&gt;70 JavaScript Interview Questions&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Compre undefined vs. null
(my initial answer) Undefined and Null both are primitive value in Javascript, but I normally use Undefined when a variable is supposed to have a value in future but not sure which value it will be. Meanwhile I use null to identify the variable should not contain any reference. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;(correct answer)&lt;br&gt;
JavaScript's 7 primitive types.&lt;br&gt;
string, number, null, undefined, boolean, symbol, bigint,  &lt;del&gt;object&lt;/del&gt;&lt;/p&gt;

&lt;p&gt;Both are falsy values by converting Boolean(value) object or double Not operator (!!value).&lt;/p&gt;

&lt;p&gt;undefined: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Default value of a variable that has not been assigned a specific value yet&lt;/li&gt;
&lt;li&gt;A function that has no explicit return value&lt;/li&gt;
&lt;li&gt;Property that does not exist in an object&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;null:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A value that explicitly represents no value&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;null == undefined // abstract equality, compares value only, true&lt;br&gt;
null === undefined // strict equality, compares value and type, false&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What does the &amp;amp;&amp;amp; operator do?
Logical And operator compares conditions and returns true if all conditions are true.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const trueResult = true &amp;amp;&amp;amp; true // true
const falsyResult = true &amp;amp;&amp;amp; false // false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;del&gt;Logical operator returns true if first operand is true, &lt;br&gt;
or returns right operand value if left is false&lt;/del&gt; &lt;/p&gt;

&lt;p&gt;Logical And operator evaluates from left to right operands, and returns first &lt;code&gt;falsy&lt;/code&gt; value*. If none is false, returns last expression.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const value = " " &amp;amp;&amp;amp; true &amp;amp;&amp;amp; 'apple' // 'apple'
const apple = false &amp;amp;&amp;amp; 'apple' &amp;amp;&amp;amp; 'orange' // false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Falsy value:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;undefined&lt;/li&gt;
&lt;li&gt;null&lt;/li&gt;
&lt;li&gt;NaN&lt;/li&gt;
&lt;li&gt;empty string&lt;/li&gt;
&lt;li&gt;false&lt;/li&gt;
&lt;li&gt;0&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;What does Logical Or operator do?
Logical Or operator evaluates if any conditions are true, then return true. If one of any conditions are false, returns false&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;x || y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If x can be converted true, return x. Else, return y.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[some truthy expression] || expr
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>JS - Map &amp; Symbol object</title>
      <dc:creator>jiiin✨</dc:creator>
      <pubDate>Sun, 16 Apr 2023 09:21:34 +0000</pubDate>
      <link>https://dev.to/jiiincho/js-map-symbol-object-5b5d</link>
      <guid>https://dev.to/jiiincho/js-map-symbol-object-5b5d</guid>
      <description>&lt;p&gt;Here is my initial (failed) LeetCode answer for 2630. Memoize II 🙈🙈🙈&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function memoize(fn) {
    let previousParameters = [];
    let cachedResult;

    return function(...params) {
       // if params and previousParameters array are deep equal, use cached result
       const isEqual = isDeepEqual(params, previousParameters);

        if(isEqual) {
            return cachedResult;
        }

       // else update previousParameters, call fn(...params), assign result 
       previousParameters = params.slice();
       cachedResult = fn(...params);
       return cachedResult;     
    }
}

const isDeepEqual = (object1, object2) =&amp;gt; {

  const objKeys1 = Object.keys(object1);
  const objKeys2 = Object.keys(object2);

  if (objKeys1.length !== objKeys2.length) return false;

  for (var key of objKeys1) {
    const value1 = object1[key];
    const value2 = object2[key];

    const isObjects = isObject(value1) &amp;amp;&amp;amp; isObject(value2);

    if ((isObjects &amp;amp;&amp;amp; !isDeepEqual(value1, value2)) ||
      (!isObjects &amp;amp;&amp;amp; value1 !== value2)
    ) {
      return false;
    }
  }
  return true;
};

const isObject = (object) =&amp;gt; {
  return object != null &amp;amp;&amp;amp; typeof object === "object";
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;isDeepEqual was a method that I normally use in my practice. But this solution fails to differentiate unique parameters. Because previousParameters is a shallow copy of given param, it creates new reference ([o, o] will always differ from next [o, o])&lt;/p&gt;

&lt;p&gt;After couple of failures, I (had to) glanced a few solutions 😏 and got a hint that &lt;code&gt;Map&lt;/code&gt; object was right way to go! Unfortunately I have no idea what javascript Map object (or Hash Map data structure) is.  &lt;/p&gt;

&lt;p&gt;To begin with, &lt;strong&gt;what is data structure?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Data structure (DS) is a way of organising data so that it can be accessed, queried, updated quickly and easily&lt;br&gt;
There is a related concept called Abstract Data Types (ADT). &lt;/p&gt;

&lt;p&gt;Abstract Data Types vs. Data Structure&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Abstract Data Type (ADT) provides the &lt;em&gt;interface&lt;/em&gt; &lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Interface&lt;/em&gt; does not give any specific details or in what programming language&lt;/li&gt;
&lt;li&gt;How Data Structure behave &amp;amp; what method this data structure have is ADT&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Abstraction (ADT)&lt;/th&gt;
&lt;th&gt;Implementation (DS)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;List&lt;/td&gt;
&lt;td&gt;Dynamic Array, Linked List&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Queue&lt;/td&gt;
&lt;td&gt;Linked List based Queue, Array based Queue, Stack based Queue&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Map&lt;/td&gt;
&lt;td&gt;Tree Map, Hash Map / Hash Table (here is our map!✨)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Here I made the second mistake that I simply assigned param as key value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let localCache = new Map();

if(!localCache.has(params)) {
  localCache.set(params, fn(...params))
}

return localCache.get(params) 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This solution failed because params in localCache.has(params) method does not compare individual values in params array 😅 &lt;/p&gt;

&lt;p&gt;For example, const foo = [2, 2] and const bar =[2, 2], foo and bar has different reference therefor it is always unique.&lt;/p&gt;

&lt;p&gt;Which implies that Map object is a perfect data structure &lt;br&gt;
when ** a value paired to a single key! **&lt;/p&gt;

&lt;p&gt;In other words, to use Map object correctly, I need to create nested Map object &lt;code&gt;from { [2, 2]: 4 } to { 2: { 2: 4 }}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But this approach has one challenge that &lt;code&gt;get&lt;/code&gt; method will return a branch instead of result. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fn(1, 2) // { 1: {2: 3 }} 
cache.get(1) // will return {2: 3} which is branch not the result 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To solve it, we need &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;a unique key to mark the value as result,&lt;/li&gt;
&lt;li&gt;a local variable that stores the last child map object in nested map so we can extract the result value by get method 🌟&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here is final solution:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const RES = Symbol('result') // 1. a unique key 
return function(...params) {
   let localCache = new Map(); // 2. a local variable 
   for(let param of params) {
     if(!localCache.has(param)) {
        localCache.set(param, new Map())
      }
     localCache = localCache.get(param) 
   }

if(!localCache.has(RES)) {
   // assign function result in last child 
   localCache.set(RES, fn(...params))
}

return localCache.get(RES)

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This question was really great to understand the usage of Map object! Faster querying 💪💪💪&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
