<?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"><channel><title><![CDATA[Javascript Musings]]></title><description><![CDATA[Concepts in Javascript]]></description><link>https://strider99.github.io/blog</link><generator>RSS for Node</generator><lastBuildDate>Tue, 12 Dec 2017 14:03:12 GMT</lastBuildDate><atom:link href="https://strider99.github.io/blog/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title><![CDATA[Fibonacci using Recursion]]></title><description><![CDATA[<div class="sect2">
<h3 id="_problem">Problem:</h3>
<div class="paragraph">
<p><strong>Given a position, find the fibonacci number at that position.</strong> We will use recursion to find the solution even though it is not as efficient as memoization.</p>
</div>
</div>
<div class="sect2">
<h3 id="_solution">Solution</h3>
<div class="paragraph">
<p><strong>solution 1</strong></p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-javascript" data-lang="javascript">const fibonacci = position =&gt; {
	if(position &lt; 3){
		return 1;

	}
	else {
		return fibonacci(position - 1) + fibonacci(position - 2);
	}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p><strong>solution 2 - a terse solution using ternary operator</strong></p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code>const fibonacci = position =&gt;  position &lt; 3 ? 1 : fibonacci(position - 1) + fibonacci(position - 2);</code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_explanation">Explanation</h3>
<div class="paragraph">
<p><strong>Fibonacci Sequence</strong> - 1 1 2 3 5 8 13 21 &#8230;&#8203;</p>
</div>
<div class="paragraph">
<p>A fibonacci sequence starts with 1 and 1 and then continues on with the sum of previous two numbers.</p>
</div>
<div class="paragraph">
<p>For example if the position passed to the function is 6, the value 8 will be returned. If 8 is passed to the function, then 21 will be returned.</p>
</div>
<div class="paragraph">
<p>We have used recursion to find the result. We keep calling the fibonacci function using recursion until the position is less than 3. When the position &lt; 3 it will return 1.</p>
</div>
<div class="paragraph">
<p>Lets say the position passed to the function is 6. Since 6 is not less than 3, the else clause will be executed which will be</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code>return fibonacci(5) + fibonacci(4);</code></pre>
</div>
</div>
<div class="paragraph">
<p>Now fibonacci(5) will be executed as</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code>return fibonacci(4) + fibonacci(3);</code></pre>
</div>
</div>
<div class="paragraph">
<p>and so on until position becomes less than 3.</p>
</div>
<div class="paragraph">
<p>The following flowchart will make it clear how recursion works.</p>
</div>
<div class="imageblock">
<div class="content">
<img src="https://i.imgur.com/smCpy5j.jpg" alt="fibonacci.jpeg">
</div>
</div>
</div>]]></description><link>https://strider99.github.io/blog/2017/12/12/Fibonacci-using-Recursion.html</link><guid isPermaLink="true">https://strider99.github.io/blog/2017/12/12/Fibonacci-using-Recursion.html</guid><category><![CDATA[javascript]]></category><category><![CDATA[recursion]]></category><category><![CDATA[algorithms]]></category><dc:creator><![CDATA[Omprakash Panigrahi]]></dc:creator><pubDate>Tue, 12 Dec 2017 00:00:00 GMT</pubDate></item><item><title><![CDATA[Algorithm - Harmless Ransom Note]]></title><description><![CDATA[<div class="sect2">
<h3 id="_problem">Problem :</h3>
<div class="paragraph">
<p><strong>You have been given two strings. You have to find out whether you can make up the first string with the words present in the second string.</strong></p>
</div>
</div>
<div class="sect2">
<h3 id="_solution">Solution :</h3>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-javascript" data-lang="javascript"> function harmlessRansom(noteText, magazineText) {
	var noteArr = noteText.split(' ');
	var magazineArr = magazineText.split(' ');

	var magazineObj = {};

	magazineArr.forEach(word =&gt; {
		if(!magazineObj[word]) magazineObj[word] = 0;
		magazineObj[word]++;
	});
  var noteIsPossible = true;
	noteArr.forEach(word =&gt; {
		if(magazineObj[word]){
			magazineObj[word]--;
			if(magazineObj[word] &lt; 0)
				noteIsPossible=false;
		}
		else
			noteIsPossible = false;
	});

return noteIsPossible;
}

var result = harmlessRansom('this is a secret note for you from a secret admirer', 'puerto rico is a place of great wonder and excitement it has many secret waterfall locatoins that i am an admirer of you must hike quite a distance to find the secret places as they are far from populated areas but it is worth the effort a tip i have for you is to go early in the morning when it is not so hot out also note that you must wear hiking boots this is one of the best places i have ever visited');
console.log(result);</code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_explanation">Explanation</h3>
<div class="paragraph">
<p>We will go through the solution line by line. We have to form the string present in the variable <code>noteText</code> from the words present in the string in the variable <code>magazineText</code>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre>var noteArr = noteText.split(' ');
var magazineArr = magazineText.split(' ');</pre>
</div>
</div>
<div class="paragraph">
<p>The above splits both the strings into array of individual words.</p>
</div>
<div class="listingblock">
<div class="content">
<pre>var magazineObj = {};

magazineArr.forEach(word =&gt; {
	if(!magazineObj[word]) magazineObj[word] = 0;
	magazineObj[word]++;
});</pre>
</div>
</div>
<div class="paragraph">
<p>Then we declared an empty object called <code>magazineObj</code> which was used to store the number of times every word occured in magazineArr. We do this because it will help us later to count down the availability of a word if it is needed multiple times.</p>
</div>
<div class="listingblock">
<div class="content">
<pre>var noteIsPossible = true;
noteArr.forEach(word =&gt; {
	if(magazineObj[word]){
		magazineObj[word]--;
		if(magazineObj[word] &lt; 0)
			noteIsPossible=false;
	}
	else
		noteIsPossible = false;
});
return noteIsPossible;</pre>
</div>
</div>
<div class="paragraph">
<p>The variable <code>noteIsPossible</code> was used as a flag to check whether our forEach loop is run successfully to the end or not.
The forEach loop above checks whether the word present in noteArr is also present in magazineObj, and if its present then it reduces the count of that particular word by 1.
If the count at any point becomes less than 0, it returns a false. Otherwise it returns true.
If the word is not found at all in the magazineObj the false is returned.</p>
</div>
</div>]]></description><link>https://strider99.github.io/blog/2017/11/19/Algorithm-Harmless-Ransom-Note.html</link><guid isPermaLink="true">https://strider99.github.io/blog/2017/11/19/Algorithm-Harmless-Ransom-Note.html</guid><dc:creator><![CDATA[Omprakash Panigrahi]]></dc:creator><pubDate>Sun, 19 Nov 2017 00:00:00 GMT</pubDate></item><item><title><![CDATA[Installing ElectronJS on Windows]]></title><description><![CDATA[<div class="paragraph">
<p><em>Prerequisite</em> - NodeJs Installed. If you dont have Node JS installed, download and install it from <a href="https://nodejs.org/en/download/">here</a>.</p>
</div>
<div class="paragraph">
<p>After installing NodeJS, we start command prompt and create a folder where electron will be installed. For every project, we will do local install of Electron instead of global.</p>
</div>
<div class="paragraph">
<p>We navigate to the folder where we want electron and first initialize the folder with a package.json file. To do that, we type in the command prompt <code>npm init</code> . We have to fill in a series of information like name, version, author etc. To initialize the package.json with generic information to save time, we can do <code>npm init -y</code> instead.</p>
</div>
<div class="paragraph">
<p>Now, to install electron, we type in</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code>npm install electron -D</code></pre>
</div>
</div>
<div class="paragraph">
<p>The -D is to save electron as a dev dependency in our package.json file. Now we open the package.json file in our favorite text editor. We will also create one more folder inside where we will keep our code. Lets name it <strong>src</strong>.
Inside the <strong>src</strong> folder, lets create a file called main.js which will be the entry point to our electronJS app.</p>
</div>
<div class="paragraph">
<p>Now, in the package.json file, we change the value of "main" which is index.js to "src/main.js" as that&#8217;s our entry point.</p>
</div>
<div class="paragraph">
<p>And in the "scripts", we will remove test, and add "start" whose value will be "electron ." This just tells npm to starts electron when we type <code>npm start</code> in the command prompt.</p>
</div>
<div class="paragraph">
<p>So, the package.json look something like this now,</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code>{
  "name": "electron-blog-demo",
  "version": "1.0.0",
  "description": "",
  "main": "src/main.js",
  "scripts": {
    "start": "electron ."
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "electron": "^1.6.11"
  }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Well, we have done everything we can to install and start electron. As a final confirmation, that we have done everything correctly, lets go to main.js which is inside the src folder and write a <code>console.log</code> statement.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code>console.log(" Electron Started");</code></pre>
</div>
</div>
<div class="paragraph">
<p>Now in the command prompt, type</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code>npm start</code></pre>
</div>
</div>
<div class="paragraph">
<p>we will see the output <em>Electron Started</em> in the command prompt.</p>
</div>]]></description><link>https://strider99.github.io/blog/2017/08/01/Installing-ElectronJS-on-Windows.html</link><guid isPermaLink="true">https://strider99.github.io/blog/2017/08/01/Installing-ElectronJS-on-Windows.html</guid><dc:creator><![CDATA[Omprakash Panigrahi]]></dc:creator><pubDate>Tue, 01 Aug 2017 00:00:00 GMT</pubDate></item></channel></rss>