<?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>Forem: Nitin Sijwali</title>
    <description>The latest articles on Forem by Nitin Sijwali (@nsijwali).</description>
    <link>https://forem.com/nsijwali</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%2F861271%2F5bfb2e15-e293-480c-a077-34eab6e0869c.jpeg</url>
      <title>Forem: Nitin Sijwali</title>
      <link>https://forem.com/nsijwali</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/nsijwali"/>
    <language>en</language>
    <item>
      <title>Convert number to string in JS</title>
      <dc:creator>Nitin Sijwali</dc:creator>
      <pubDate>Fri, 03 Jun 2022 13:35:58 +0000</pubDate>
      <link>https://forem.com/nsijwali/convert-number-to-string-in-js-822</link>
      <guid>https://forem.com/nsijwali/convert-number-to-string-in-js-822</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Looking at the title, you might think it's a simple task that any noob can complete. right??&lt;/em&gt;&lt;br&gt;
&lt;em&gt;However, the actual question was, write a function that accepts an args as a number and returns the string version of the same number, given that you cannot use string methods like function toString(), or initialise a variable with an empty string, or use any other tricks.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;So we will approach this problem by keeping two points in mind&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Every number is formed from digits 0-9;&lt;/li&gt;
&lt;li&gt;Divide and conquer rule&lt;/li&gt;
&lt;li&gt;Recursion
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let stringNum;
const arr = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
let temp;
function convertToString(num) {
  if (num &amp;gt; 0) { // condition will give the string but in reverse order.
    const dividend = num % 10;
    if (temp) {
      temp += arr[dividend];
    } else {
      temp = arr[dividend];
    }
    convertToString(Math.floor(num / 10)); // recursion
  } else {
// here we will reverse the string to get the actual number in string format.
    for (let i = temp.length - 1; i &amp;gt;= 0; i--) {
      if (stringNum) {
        stringNum += temp.charAt(i);
      } else {
        stringNum = temp.charAt(i);
      }
    }

  }
  return stringNum;
}

const result = convertToString(125)
console.log(result) // "125"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Please give it a shot and let me know what you think.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Please ❤️ it and share it your with friends or colleagues. Spread the knowledge.&lt;/p&gt;




&lt;p&gt;Thats all for now. Keep learning and have faith in Javascript❤️&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>programming</category>
    </item>
    <item>
      <title>Javascript: Sum diagonal and counter diagonal elements of n * n matrix</title>
      <dc:creator>Nitin Sijwali</dc:creator>
      <pubDate>Tue, 31 May 2022 14:09:50 +0000</pubDate>
      <link>https://forem.com/nsijwali/javascript-sum-diagonal-and-counter-diagonal-elements-of-n-n-matrix-ghc</link>
      <guid>https://forem.com/nsijwali/javascript-sum-diagonal-and-counter-diagonal-elements-of-n-n-matrix-ghc</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Given a square matrix of size n, calculate the sum of diagonal and counter diagonal elements.&lt;br&gt;
where n &amp;gt; 1&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Example:
[
  [1, 8, 7],
  [2, 9, 6],
  [3, 4, 5]
]
diagonal = positions [0][0] + [1][1]+ [2][2] =&amp;gt; 1+9+5 =15,
counterDiagonal = positions [0][2] + [1][1]+ [3][0] =&amp;gt; 7+9+3 =19
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Let's try first with brute force method then will optimise it.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sumOfDiagonals(matrix) {
  const len = matrix.length;
  let diagonalSum = 0;
  let counterDiagonalSum = 0;
  for (let i = 0; i &amp;lt; len; i++) {
    for (let k = 0; k &amp;lt; len; k++) {
      if (i === k) {
        diagonalSum += matrix[i][k];
      }
      if (i + k == len - 1) {
        counterDiagonalSum += matrix[i][k];
      }

    }
  }

  console.log("sum of diagonal elements --- ", diagonalSum)
  console.log("sum of counter diagonal elements ---", counterDiagonalSum)
}
sumOfDiagonals([
  [1, 8, 7],
  [2, 9, 6],
  [3, 4, 5]
]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The solution above works well but the time and space complexity are O(n^2) time and O(1) where n is the number of elements our loop iterated and power 2 is for the two loops that we used.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Let's try the optimised solution:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sumOfDiagonals(matrix) {
  const len = matrix.length;
  let diagonalSum = 0;
  let counterDiagonalSum = 0;
  for (let i = 0; i &amp;lt; len; i++) {
    diagonalSum += matrix[i][i];
    counterDiagonalSum += matrix[i][len - i - 1];
  }

  console.log("sum of diagonal elements --- ", diagonalSum)
  console.log("sum of counter diagonal elements ---", counterDiagonalSum)
}
sumOfDiagonals([
  [1, 8, 7],
  [2, 9, 6],
  [3, 4, 5]
]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Now the time complexity has been reduced to  O(n).&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Please ❤️ it and share it your with friends or colleagues. Spread the knowledge.&lt;/p&gt;




&lt;p&gt;Thats all for now. Keep learning and have faith in Javascript❤️&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>dsa</category>
      <category>algorithms</category>
      <category>webdev</category>
    </item>
    <item>
      <title>FizzBuzz program in javascript</title>
      <dc:creator>Nitin Sijwali</dc:creator>
      <pubDate>Mon, 16 May 2022 14:13:16 +0000</pubDate>
      <link>https://forem.com/nsijwali/fizzbuzz-program-in-javascript-51bo</link>
      <guid>https://forem.com/nsijwali/fizzbuzz-program-in-javascript-51bo</guid>
      <description>&lt;p&gt;&lt;em&gt;In javascript interviews, the most frequently asked question is "FizzBuzz." The goal of asking this question is to see how you approach solving the problem rather than the solution itself.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The following is an example of a question. Print the numbers 1 to 100 that meet the following conditions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Print "Fizz" if a number is divisible by 3.&lt;/li&gt;
&lt;li&gt;Print "Buzz" if a number is divisible by 5.&lt;/li&gt;
&lt;li&gt;Print "FizzBuzz" if the number is divisible by both 3 and 5.&lt;/li&gt;
&lt;li&gt;else print the number itself.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;The solution is straightforward, but the approach is critical.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A newcomer would do it like this:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fizzBuzz(){
   for(let i = 1; i &amp;lt;= 100; i++){
     if(i % 3 === 0 &amp;amp;&amp;amp; i % 5 === 0){
        console.log('FizzBuzz');
     }else if(i % 3 === 0){
        console.log('Fizz');
     }else if(i % 5 === 0){
        console.log('Buzz');
     }else{
        console.log(i);
     }
   }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;This is how people with some experience would do it:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; function fizzBuzz(){
   for(let i = 1; i &amp;lt;= 100; i++){
// instead of checking if divisible by both 3 and 5 check with 15.
     if(i % 15 === 0){ 
        console.log('FizzBuzz');
     }else if(i % 3 === 0){
        console.log('Fizz');
     }else if(i % 5 === 0){
        console.log('Buzz');
     }else{
        console.log(i);
     }
   }
}

fizzBuzz()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;However, a pro developer🚀 would do it as follows:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fizzBuzz() {
 for(let i = 1; i &amp;lt;= 100; i++){
  console.log(((i%3  === 0 ?  "fizz": '') + (i% 5 === 0 ? "buzz" : ''))|| i)
 }
}
fizzBuzz()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I hope this helps you guys solve it like a pro. Any additional pro solutions are welcome.&lt;/p&gt;

&lt;p&gt;Please ❤️ it and share it your with friends or colleagues. Spread the knowledge.&lt;/p&gt;




&lt;p&gt;Thats all for now. Keep learning and have faith in Javascript❤️&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>dsa</category>
      <category>algorithms</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Function Composition in Javascript</title>
      <dc:creator>Nitin Sijwali</dc:creator>
      <pubDate>Fri, 13 May 2022 17:41:29 +0000</pubDate>
      <link>https://forem.com/nsijwali/function-composition-in-javascript-158e</link>
      <guid>https://forem.com/nsijwali/function-composition-in-javascript-158e</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Function composition is an approach where the result of one function is passed on to the next function, which is passed to another until the final function is executed for the final result&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

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

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const square = x =&amp;gt; x*x;

const addTen = x =&amp;gt; x + 10;

const result = compose(square, addTen)(20);
result: 410;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets write the &lt;strong&gt;compose&lt;/strong&gt; function for the above question..&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function compose(f1, f2){
  return function(value){
      return f2(f1(value));
  }
}
const result = compose(square, addTen)(20);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;if we console log the &lt;strong&gt;result&lt;/strong&gt;, the answer would be 410.&lt;/p&gt;

&lt;p&gt;However, the solution above is only valid if compose accepts no more than two functions. So, to make it work for an n-number of functions, we need to make a few changes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function compose(...args){
  return function(value){
      return args.reduce((pre, current) =&amp;gt; current(pre), value);
  }
}
const result = compose(square, addTen, addTen, addTen)(20);
answer: 430.

// here were are using rest operator ...args to accept n-number of arguments in an array.
args.reduce() will iterate over each element/function with the previously computed value, returning the final computed value.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function above can be shortened as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const compose =(...args) =&amp;gt; value =&amp;gt; args.reduce((pre, current) =&amp;gt; current(pre), value);
const result = compose(square, addTen, addTen, addTen)(20);
answer: 430.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I hope this helps you solve such a problem during your interview.&lt;/p&gt;

&lt;p&gt;Thats all for now. Keep learning and have faith in Javascript❤️&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>dsa</category>
      <category>algorithms</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Merging two arrays and sort the resultant without sort functions using recursion in Javascript</title>
      <dc:creator>Nitin Sijwali</dc:creator>
      <pubDate>Thu, 12 May 2022 05:32:50 +0000</pubDate>
      <link>https://forem.com/nsijwali/merging-two-arrays-and-sort-the-resultant-without-sort-functions-using-recursion-in-javascript-37f8</link>
      <guid>https://forem.com/nsijwali/merging-two-arrays-and-sort-the-resultant-without-sort-functions-using-recursion-in-javascript-37f8</guid>
      <description>&lt;p&gt;&lt;em&gt;Long ago, during an interview for one of the product-based companies, the interviewer asked me a DSA question, which triggered my sixth sense.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So the question was to merge two arrays and sort the resultant array in ascending order.&lt;/strong&gt; &lt;br&gt;
Example:-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr1 = [2,4,8,7, 10, 25, 17, 13, 12 ];
const arr2 = [1,4,6,9,8, 11, 13, 3, 0];
So I used spread operator to merge two arrays and  Array.sort() function to sort the values.
const resultant = [...arr1, ...arr2].sort((a, b) =&amp;gt; a-b);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will give the desired result.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The interviewer then asked me not to use the sort function and to create one on my own, explaining that it would require two loops.&lt;br&gt;
So I had to demonstrate the power of javascript to him, and I used recursion instead of two loops.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const result = [...arr1, ...arr2];
let count = 0;
function sorting(){
// to break the recursion when sorting is done.
if(count ===result.length) return true; 

//else do
for(let i = 0; i&amp;lt; result.length; i++){
  if(result[i] &amp;gt; result[i+1]){

//used array destructing to swap values
   [ result[i+1],  result[i]]= [result[i],result[i+1]]; 
  }
}
count++;
sorting();
}
sorting();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;In javascript, recursion is the most underappreciated concept. When DSA is used with javascript, this becomes quite strong. It can flatten arrays, objects, and construct tree structures like UI, among other things.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;That's all for now, folks. Continue to learn and have faith in Javascript.&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>dsa</category>
      <category>algorithms</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
