<?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: Ajay Marathe</title>
    <description>The latest articles on DEV Community by Ajay Marathe (@ajaymarathe).</description>
    <link>https://dev.to/ajaymarathe</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%2F295040%2F68fe6e84-1324-4891-87bd-89ca54348d89.png</url>
      <title>DEV Community: Ajay Marathe</title>
      <link>https://dev.to/ajaymarathe</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ajaymarathe"/>
    <language>en</language>
    <item>
      <title>Write a function that removes duplicate characters from a given string. ( Try to write core JS)</title>
      <dc:creator>Ajay Marathe</dc:creator>
      <pubDate>Thu, 15 Aug 2024 07:18:51 +0000</pubDate>
      <link>https://dev.to/ajaymarathe/write-a-function-that-removes-duplicate-characters-from-a-given-string-try-to-write-core-js-2clm</link>
      <guid>https://dev.to/ajaymarathe/write-a-function-that-removes-duplicate-characters-from-a-given-string-try-to-write-core-js-2clm</guid>
      <description>&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const removeDuplicateChar = (str1) =&amp;gt; {
    let newArr = [];
    let stringArr = str1.split("");
    for (let i = 0; i &amp;lt; stringArr.length; i++) {
      if (!newArr.includes(stringArr[i])) {
        newArr.push(stringArr[i]);
      }
    }
    return newArr.join("");
  };

console.log("removeDuplicate:",removeDuplicateChar("helloooo"));
 // removeDuplicate: helo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Problem Explanation
&lt;/h2&gt;

&lt;p&gt;Imagine you have a word, and some of the letters in that word are repeated. What if you wanted to create a version of that word where each letter appears only once? That’s what the &lt;code&gt;removeDuplicateChar&lt;/code&gt;function is for.&lt;/p&gt;

&lt;h3&gt;
  
  
  How It Works:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;&lt;strong&gt;Breaking Down the Word&lt;/strong&gt;&lt;/em&gt;: The function starts by splitting your word into individual letters. For example, if you have the word &lt;code&gt;"helloooo"&lt;/code&gt;, it breaks it down into &lt;code&gt;['h', 'e', 'l', 'l', 'o', 'o', 'o', 'o']&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;&lt;strong&gt;Checking for Repeats&lt;/strong&gt;&lt;/em&gt;: The function then goes through each letter one by one. It checks if that letter has already been added to a new list of letters. If the letter is new (meaning it hasn't been added yet), it adds it to this new list. If the letter has already been added, it skips it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;&lt;strong&gt;Building the Result&lt;/strong&gt;&lt;/em&gt;: After going through all the letters, the function ends up with a list where each letter appears only once. For &lt;code&gt;"helloooo"&lt;/code&gt;, it would be &lt;code&gt;['h', 'e', 'l', 'o']&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;&lt;strong&gt;Final Output&lt;/strong&gt;&lt;/em&gt;: The function gives you this new list, which now contains only unique characters from the original word.&lt;br&gt;
Example:&lt;br&gt;
If you use the function with &lt;code&gt;"helloooo"&lt;/code&gt;, it will return &lt;code&gt;['h', 'e', 'l', 'o']&lt;/code&gt;, removing all the extra &lt;code&gt;'o'&lt;/code&gt;s.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>string</category>
      <category>array</category>
      <category>duplicate</category>
    </item>
    <item>
      <title>Create JS function to remove spaces from giving string. ( Using core js and not in-built trim function.)</title>
      <dc:creator>Ajay Marathe</dc:creator>
      <pubDate>Thu, 15 Aug 2024 07:01:43 +0000</pubDate>
      <link>https://dev.to/ajaymarathe/create-js-function-to-remove-spaces-from-giving-string-26le</link>
      <guid>https://dev.to/ajaymarathe/create-js-function-to-remove-spaces-from-giving-string-26le</guid>
      <description>&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const trim = (string) =&amp;gt; {
    let strArr = string.split("");
    let trimedStr = [];
    strArr.forEach((item) =&amp;gt; {
      if (item !== " ") {
        trimedStr.push(item);
      }
    });
    return trimedStr.join("");
  };

  console.log("trim", trim("Hello world nice world"));
 // output =&amp;gt; trim: Helloworldniceworld
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Problem Explanation
&lt;/h3&gt;

&lt;p&gt;Let's break down the problem in simple terms:&lt;/p&gt;

&lt;p&gt;You have a piece of code that defines a function called trim. The purpose of this function is to remove all the spaces from a given string. In other words, if you pass a sentence with spaces into this function, it will return the same sentence but with all the spaces removed.&lt;/p&gt;

&lt;h4&gt;
  
  
  How the Function Works:
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Splitting the String&lt;/em&gt;&lt;/strong&gt;: The function starts by taking the input string &lt;code&gt;(e.g., "Hello world nice world")&lt;/code&gt; and splits it into an array of individual characters. For example, "Hello world" becomes &lt;code&gt;['H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']..&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;&lt;strong&gt;Filtering Out Spaces&lt;/strong&gt;&lt;/em&gt;: The function then goes through each character in the array. If the character is not a space &lt;code&gt;(' ')&lt;/code&gt;, it adds it to a new array called &lt;code&gt;trimedStr&lt;/code&gt;. If it is a space, it simply skips it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;&lt;strong&gt;Rejoining the Characters&lt;/strong&gt;&lt;/em&gt;: After filtering out the spaces, the function takes the remaining characters and joins them back together into a single string without any spaces.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;&lt;strong&gt;Returning the Result&lt;/strong&gt;&lt;/em&gt;: Finally, the function returns the new string that has no spaces.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>array</category>
      <category>string</category>
      <category>trim</category>
    </item>
    <item>
      <title>Learn Lodash _.drop - Creates a slice of array with n elements dropped from the beginning.</title>
      <dc:creator>Ajay Marathe</dc:creator>
      <pubDate>Sat, 10 Aug 2024 23:06:27 +0000</pubDate>
      <link>https://dev.to/ajaymarathe/learn-lodash-drop-creates-a-slice-of-array-with-n-elements-dropped-from-the-beginning-1ln7</link>
      <guid>https://dev.to/ajaymarathe/learn-lodash-drop-creates-a-slice-of-array-with-n-elements-dropped-from-the-beginning-1ln7</guid>
      <description>&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// first Example
  const drop = (arr, n) =&amp;gt; {
    for(let i = 0; i &amp;lt; n; i++) {
      arr.shift(arr[i])
    }
    return arr;
  }

  console.log('drop', drop([1, 2, 3], 1))

// second example 
  const drop = (arr, n) =&amp;gt; {
    return arr.slice(n)
  }

  console.log('drop', drop([1, 2, 3], 1))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Function Signature&lt;/strong&gt;:

&lt;code&gt;function drop(arr, n = 1)&lt;/code&gt;

: This function takes two arguments:&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;arr&lt;/code&gt;: The input array from which elements will be dropped.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;n&lt;/code&gt;: The number of elements to drop from the beginning of the array. It defaults to &lt;code&gt;1&lt;/code&gt; if not provided.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Slice Method&lt;/strong&gt;: The &lt;code&gt;slice&lt;/code&gt; method is used to return a shallow copy of a portion of an array into a new array. The method takes two arguments:&lt;/li&gt;
&lt;li&gt;The start index (&lt;code&gt;n&lt;/code&gt; in this case).&lt;/li&gt;
&lt;li&gt;The end index (not provided here, so it slices to the end of the array).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;drop([1, 2, 3], 1)&lt;/code&gt; starts the slice at index &lt;code&gt;1&lt;/code&gt;, so it returns &lt;code&gt;[2, 3]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>array</category>
      <category>lodash</category>
      <category>drop</category>
    </item>
    <item>
      <title>Write a function that filters out all the falsy values from a given array. (Core JS)</title>
      <dc:creator>Ajay Marathe</dc:creator>
      <pubDate>Sat, 10 Aug 2024 22:27:12 +0000</pubDate>
      <link>https://dev.to/ajaymarathe/create-a-function-which-will-remove-all-the-falsy-values-from-given-array-1ilf</link>
      <guid>https://dev.to/ajaymarathe/create-a-function-which-will-remove-all-the-falsy-values-from-given-array-1ilf</guid>
      <description>&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const removeFalsyValues = (arr) =&amp;gt; {
    let truthy = []

    for(let i = 0; i &amp;lt; arr.length; i++){
      if(arr[i]) {
        truthy.push(arr[i])
      }
    }

    return truthy;

  }

  console.log('removeFalsyValues:', removeFalsyValues([0, 1, false, 2, "", 3, undefined, NaN, null]))

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

&lt;/div&gt;



&lt;p&gt;Falsy values in JavaScript are values that are considered false when evaluated in a Boolean context. These include&lt;code&gt;0, false, "" (an empty string), undefined, NaN, and null&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Here's how the function works:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;Initialize an empty array&lt;/strong&gt;&lt;/em&gt;: The function starts by creating an empty array called truthy. This will be used to store the values from the original array that are not falsy.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;Loop through the array&lt;/strong&gt;&lt;/em&gt;: The function uses a for loop to go through each element in the input array arr.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;Check if the element is truthy&lt;/strong&gt;&lt;/em&gt;: Inside the loop, there's an if statement that checks if the current element (arr[i]) is truthy. If the element is truthy (meaning it's not one of the falsy values), it gets added to the truthy array.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;Return the truthy array&lt;/strong&gt;&lt;/em&gt;: After the loop has gone through all the elements, the function returns the truthy array, which now contains only the truthy values.&lt;/li&gt;
&lt;li&gt;The input array is &lt;code&gt;[0, 1, false, 2, "", 3, undefined, NaN, null]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The function will loop through each element and remove the falsy ones &lt;code&gt;(0, false, "", undefined, NaN, null)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The remaining truthy values &lt;code&gt;(1, 2, 3)&lt;/code&gt; are returned in a new array:&lt;code&gt;[1, 2, 3]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, the output of this code will be:&lt;code&gt;removeFalsyValue [1, 2, 3]&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>array</category>
      <category>push</category>
      <category>arraymethods</category>
    </item>
  </channel>
</rss>
