<?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 Prashant Yadav on Medium]]></title>
        <description><![CDATA[Stories by Prashant Yadav on Medium]]></description>
        <link>https://medium.com/@learnersbucket?source=rss-55ea03e0926e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*akZUpR4q9pfuh-tTmGIFjA.jpeg</url>
            <title>Stories by Prashant Yadav on Medium</title>
            <link>https://medium.com/@learnersbucket?source=rss-55ea03e0926e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 22:47:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@learnersbucket/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[How to merge objects in JavaScript]]></title>
            <link>https://javascript.plainenglish.io/how-to-merge-objects-in-javascript-98f2209710e3?source=rss-55ea03e0926e------2</link>
            <guid isPermaLink="false">https://medium.com/p/98f2209710e3</guid>
            <category><![CDATA[tech]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Prashant Yadav]]></dc:creator>
            <pubDate>Thu, 06 Jun 2019 05:35:06 GMT</pubDate>
            <atom:updated>2019-06-14T09:54:52.250Z</atom:updated>
            <content:encoded><![CDATA[<h4>Learn how to merge two or more objects in JavaScript.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8ZTLtP6uVkzLgf8awDgbKA.png" /><figcaption>learnersbucket.com</figcaption></figure><blockquote>I run a blog named <a href="https://learnersbucket.com/">learnersbucket.com</a> where I write about <a href="https://learnersbucket.com/tutorials/es6/es6-intro/">ES6</a>, <a href="https://learnersbucket.com/tutorials/topics/data-structures/">Data structures</a>, and <a href="https://learnersbucket.com/examples/topics/algorithms/">Algorithms</a> to help others crack coding interviews. Follow me on <a href="https://twitter.com/LearnersBucket">Twitter</a> for regular updates.</blockquote><p>Almost everything in JavaScript is object, but still it does not have any good methods to merge two or more different objects.</p><p>After <a href="https://learnersbucket.com/tutorials/es6/es6-intro">ES6</a> there are new methods added which can be used to merge objects in JavaScript.</p><p>There are two different types of merge that can be performed on the objects.<br> 1). Shallow merge<br> 2). Deep merge</p><h3>Shallow Merge objects</h3><p>In shallow merge only the properties owned by the object will be merged, it will not merge the extended properties or methods.</p><h3>Using ... <a href="https://learnersbucket.com/tutorials/es6/spread-and-rest-operators-in-javascript">spread</a> operator</h3><p>... spread operators copies all the properties of the objects into another object.</p><pre>let obj1 = {<br>  name: &#39;prashant&#39;,<br>  age: 23,<br>}</pre><pre>let obj2 = {<br>  qualification: &#39;BSC CS&#39;,<br>  loves: &#39;Javascript&#39;<br>}</pre><pre>let merge = {...obj1, ...obj2};</pre><pre>console.log(merge);</pre><pre>/*<br>Object {<br>  age: 23,<br>  loves: &quot;Javascript&quot;,<br>  name: &quot;prashant&quot;,<br>  qualification: &quot;BSC CS&quot;<br>}<br>*/</pre><h3><strong>Using </strong><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign"><strong>Object.assign()</strong></a><strong> method</strong></h3><p>Object.assign(target, source1, soure2, ...) method copies all the enumerable own property of source object to target object and returns the target object.</p><pre>let obj1 = {<br>  name: &#39;prashant&#39;,<br>  age: 23,<br>}</pre><pre>let obj2 = {<br>  qualification: &#39;BSC CS&#39;,<br>  loves: &#39;Javascript&#39;<br>}</pre><pre>let merge = Object.assign({}, obj1, obj2);;</pre><pre>console.log(merge);</pre><pre>/*<br>Object {<br>  age: 23,<br>  loves: &quot;Javascript&quot;,<br>  name: &quot;prashant&quot;,<br>  qualification: &quot;BSC CS&quot;<br>}<br>*/</pre><p>We have used an empty object {} as a target and passed the sources objects which should be merged in target.</p><h3>Using custom function to merge objects</h3><p>We can also create custom function to merge two or more objects.</p><pre>let merge = (...arguments) =&gt; {<br>  <br>  // Create a new object<br>  let target = {};</pre><pre>  // Merge the object into the target object<br>  let merger = (obj) =&gt; {<br>     for (let prop in obj) {<br>       if (obj.hasOwnProperty(prop)) {<br>         // Push each value from `obj` into `target`<br>         target[prop] = obj[prop];<br>       }<br>     }<br>  };</pre><pre>   // Loop through each object and conduct a merge<br>  for (let i = 0; i &lt; arguments.length; i++) {<br>    merger(arguments[i]);<br>  }</pre><pre>  return target;<br>}</pre><pre>let obj1 = {<br>  name: &#39;prashant&#39;,<br>  age: 23,<br>}</pre><pre>let obj2 = {<br>  qualification: &#39;BSC CS&#39;,<br>  loves: &#39;Javascript&#39;<br>}</pre><pre>let merged = merge(obj1, obj2);;</pre><pre>console.log(merged);</pre><pre>/*<br>Object {<br>  age: 23,<br>  loves: &quot;Javascript&quot;,<br>  name: &quot;prashant&quot;,<br>  qualification: &quot;BSC CS&quot;<br>}<br>*/</pre><h3>Deep Merge Objects</h3><p>To deep merge an object we have to copy the own properties and extended properties as well, if it exits.</p><p>The best way to do so is to create a custom function.</p><pre>let merge = (...arguments) =&gt; {<br>  let target = {};</pre><pre>  // Merge the object into the target object<br>  let merger = (obj) =&gt; {<br>      for (let prop in obj) {<br>        if (obj.hasOwnProperty(prop)) {<br>          if (Object.prototype.toString.call(obj[prop]) === &#39;[object Object]&#39;){<br>            // If we&#39;re doing a deep merge <br>            // and the property is an object<br>            target[prop] = merge(target[prop], obj[prop]);<br>          } else {<br>            // Otherwise, do a regular merge<br>            target[prop] = obj[prop];<br>          }<br>         }<br>      }<br>  };</pre><pre>   //Loop through each object and conduct a merge<br>   for (let i = 0; i &lt; arguments.length; i++) {<br>      merger(arguments[i]);<br>   }</pre><pre>     return target;<br>};</pre><pre>let obj1 = {<br>  name: &#39;prashant&#39;,<br>  age: 23,<br>  nature: {<br>    &quot;helping&quot;: true,<br>    &quot;shy&quot;: false<br>  }<br>}</pre><pre>let obj2 = {<br>  qualification: &#39;BSC CS&#39;,<br>  loves: &#39;Javascript&#39;,<br>  nature: {<br>    &quot;angry&quot;: false,<br>    &quot;shy&quot;: true<br>  }<br>}</pre><pre>console.log(merge(obj1, obj2));</pre><pre>/*<br>Object {<br>  age: 23,<br>  loves: &quot;Javascript&quot;,<br>  name: &quot;prashant&quot;,<br>  nature: Object {<br>    angry: false,<br>    helping: true,<br>    shy: true<br>  },<br>  qualification: &quot;BSC CS&quot;<br>}<br>*/</pre><p>We can combine both the function for shallow copy and deep copy together to create a single function which will perform merge based on the arguments passed.</p><p>If we will pass true as first argument then it will perform deep merge else it will perform shallow merge.</p><pre>let merge = (...arguments) =&gt; {</pre><pre>   // Variables<br>   let target = {};<br>   let deep = false;<br>   let i = 0;</pre><pre>   // Check if a deep merge<br>   if (typeof (arguments[0]) === &#39;boolean&#39;) {<br>      deep = arguments[0];<br>      i++;<br>   }</pre><pre>   // Merge the object into the target object<br>   let merger = (obj) =&gt; {<br>      for (let prop in obj) {<br>         if (obj.hasOwnProperty(prop)) {<br>           if (deep &amp;&amp; Object.prototype.toString.call(obj[prop]) === &#39;[object Object]&#39;) {<br>           // If we&#39;re doing a deep merge <br>           // and the property is an object<br>              target[prop] = merge(target[prop], obj[prop]);<br>          } else {<br>           // Otherwise, do a regular merge<br>           target[prop] = obj[prop];<br>          }<br>       }<br>     }<br>   };</pre><pre>    //Loop through each object and conduct a merge<br>    for (; i &lt; arguments.length; i++) {<br>       merger(arguments[i]);<br>    }</pre><pre>    return target;<br>};</pre><pre>let obj1 = {<br>  name: &#39;prashant&#39;,<br>  age: 23,<br>  nature: {<br>    &quot;helping&quot;: true,<br>    &quot;shy&quot;: false<br>  }<br>}</pre><pre>let obj2 = {<br>  qualification: &#39;BSC CS&#39;,<br>  loves: &#39;Javascript&#39;,<br>  nature: {<br>    &quot;angry&quot;: false,<br>    &quot;shy&quot;: true<br>  }<br>}</pre><pre>//Shallow merge<br>console.log(merge(obj1, obj2));</pre><pre>/*<br>Object {<br>  age: 23,<br>  loves: &quot;Javascript&quot;,<br>  name: &quot;prashant&quot;,<br>  nature: Object {<br>    angry: false,<br>    shy: true<br>  },<br>  qualification: &quot;BSC CS&quot;<br>}<br>*/</pre><pre>//Deep merge<br>console.log(merge(true, obj1, obj2));<br>/*<br>Object {<br>  age: 23,<br>  loves: &quot;Javascript&quot;,<br>  name: &quot;prashant&quot;,<br>  nature: Object {<br>    angry: false,<br>    helping: true,<br>    shy: true<br>  },<br>  qualification: &quot;BSC CS&quot;<br>}<br>*/</pre><p>There are other libraries available which you can use to merge or two objects like</p><h3>Jquery’s <a href="https://api.jquery.com/jQuery.extend/#jQuery-extend-target-object1-objectN">$.extend()</a> method</h3><p>$.extend(deep, copyTo, copyFrom) can be used to make a complete deep copy of any array or object in javascript.</p><h3>Lodash <a href="https://www.npmjs.com/package/lodash.merge">merge()</a> method</h3><p>which will merge objects and arrays by performing deep merge recursively.</p><p><em>Originally published at </em><a href="https://learnersbucket.com/examples/javascript/how-to-merge-objects-in-javascript/"><em>https://learnersbucket.com</em></a><em> on June 6, 2019.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=98f2209710e3" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/how-to-merge-objects-in-javascript-98f2209710e3">How to merge objects in JavaScript</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Javascript Objects: Complete Reference]]></title>
            <link>https://medium.com/@learnersbucket/javascript-objects-complete-reference-def1e8f9d400?source=rss-55ea03e0926e------2</link>
            <guid isPermaLink="false">https://medium.com/p/def1e8f9d400</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[tech]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Prashant Yadav]]></dc:creator>
            <pubDate>Sun, 28 Apr 2019 15:21:10 GMT</pubDate>
            <atom:updated>2019-06-14T06:30:05.440Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Tttmk3mOJuX1p7wOXBHWGg.png" /><figcaption>learnersbucket.com</figcaption></figure><blockquote>I run a blog named <a href="https://learnersbucket.com/">learnersbucket.com</a> where I write about <a href="https://learnersbucket.com/tutorials/es6/es6-intro/">ES6</a>, <a href="https://learnersbucket.com/tutorials/topics/data-structures/">Data structures</a>, and <a href="https://learnersbucket.com/examples/topics/algorithms/">Algorithms</a> to help others crack coding interviews. Follow me on <a href="https://twitter.com/LearnersBucket">Twitter</a> for regular updates.</blockquote><h3>Lets Start</h3><p>Javascript is an object-based programming language in which except for few primitive types everything is an object.</p><p>Objects in javascript are not class based like in other programming languages. They are quite independent and they don’t have any restrictions on the names of the new properties and methods or on the values.</p><h3>What is an object in javascript?.</h3><p>Basically, Objects in javascript are a box of properties which are mutable, where properties are represented by key-value pair. Key can be any string including the empty string and value can be of any type available in javascript except it cannot be undefined that means it is mandatory to assign a value to the key.</p><p>They are very useful in storing and organizing the data and as objects can have another object, they are very handy in representing the tree or graph structures.</p><p>Except for numbers, strings, booleans, null and undefined all other things are objects in javascript. While numbers, strings, booleans are object-like as they have methods, but they are immutable.</p><p>With the introduction of <a href="https://learnersbucket.com/tutorials/es6/es6-intro">ES6</a>, there are various new capabilities added to the Objects.</p><h4>list of objects in javascript</h4><ul><li>Dates</li><li>Regular Expressions</li><li>Maths</li><li>Functions</li><li>Objects</li><li>Booleans (When called as constructor with new keyword.)</li><li><a href="https://learnersbucket.com/tutorials/data-structures/javascript-string">Strings</a> (When called as constructor with new keyword.)</li><li>Numbers (When called as constructor with new keyword.)</li></ul><h3>Declaring Objects in Javascript</h3><p>There are different ways through which we can create objects in javascript.</p><h3>Using object literals</h3><p>The most simplest and prominent method to declare objects in javascript is by using { } braces.</p><pre>let obj = {}; <br>obj.name = &#39;Prashant&#39;; <br>obj[&quot;age&quot;] = 24; <br>let obj2 = { &quot;name&quot;: &#39;Prashant&#39;, age: 24 };</pre><p>As the keys of the objects can be any string including the empty string we can omit the quotes around the property names while declaring in object literals if the string does not contain any spaces.</p><p>You can also nest the objects within the objects.</p><pre>let obj = { name: &quot;Prashant&quot;,<br> age: 24, <br> details: { <br>     hobbies: [&quot;Cycling&quot;, &quot;Writing&quot;, &quot;Teaching&quot;], <br>     Gender: &quot;Male&quot;<br>  }<br>};</pre><p>We can also declare functions which are called methods inside the objects.</p><pre>let obj = { <br>   name: &#39;Prashant&#39;,<br>   getName: function() { return this.name; } <br>} </pre><pre>console.log(obj.getName()); <br>//&quot;Prashant&quot;</pre><p>You can also check out the new syntax of <a href="https://learnersbucket.com/tutorials/es6/object-literals">declaring the methods inside the object</a>.</p><h3>By creating instance of the object using new keyword.</h3><pre>let obj = new Object(); <br>obj.name = &quot;Prashant&quot;; <br>obj.age = 24; <br>obj.getDetails = function(){ <br>   return `${this.name} is ${this.age} years old`; <br>}; </pre><pre>console.log(obj.getDetails()); <br>//&quot;Prashant is 24 years old&quot;</pre><h3>By creating a constructor</h3><p>In javascript, we can create a function which can be used as a constructor to create new objects.</p><pre>function Obj(name, age){ <br>  //Properties <br>  this.name = name; <br>  this.age = age; </pre><pre>  //Methods <br>  this.getDetails = function(){ <br>    return `${this.name} is ${this.age} years old`; <br>  }; <br>} </pre><pre>let person = new Obj(&quot;Prashant&quot;, 24); console.log(person.getDetails()); <br>//&quot;Prashant is 24 years old&quot; </pre><pre>let person2 = new Obj(&quot;Sachin&quot;, 23); console.log(person2.getDetails()); <br>//&quot;Sachin is 23 years old&quot;</pre><p>This is very useful as it allows you to mimic the class-based objects but the properties and methods cannot be made private.</p><h3>Retrieving data from objects</h3><p>Values from objects can be retrieved using two different techniques.</p><h3>Using dot . notation</h3><p>We can use the period or . to retrieve the value of properties or call the methods if the name of the properties names does not contains any space.</p><pre>let obj = {<br>   name: &quot;Prashant&quot;,<br>   &quot;what is this&quot;: &quot;Example&quot;,<br>   &quot;my age&quot;: 25<br>};</pre><pre>obj.name;<br>//&quot;Prashant&quot;</pre><pre>obj.what is this<br>//Uncaught SyntaxError: Unexpected identifier</pre><p>We get an error when trying to access the property names which has spaces.</p><h3>Using [] to access the values</h3><p>In cases where you are not sure about the properties name whether it contains white space or not you can use [] notations.</p><pre>let obj = {<br>   name: &quot;Prashant&quot;,<br>   &quot;what is this&quot;: &quot;Example&quot;,<br>   &quot;my age&quot;: 25<br>};</pre><pre>obj[&quot;name&quot;];<br>//&quot;Prashant&quot;</pre><pre>obj[&quot;what is this&quot;];<br>//&quot;Example&quot;</pre><pre>obj[&quot;my age&quot;];<br>//25</pre><p>Both methods may be giving the required result but there is a little difference in the way they work.</p><p>The word after the period or . is interpreted as object literal while the world inside the [] are evaluated and then the value assigned to evaluated word is returned. Let us see the below example for better understanding.</p><pre>let obj = {<br>   1: &quot;one&quot;,<br>   2: &quot;two&quot;,<br>   3: &quot;three&quot;<br>};</pre><pre>for(let i = 1; i &lt;= 3; i++){<br>     console.log(obj.i);<br>}<br>//undefined<br>//undefined<br>//undefined</pre><pre>for(let i = 1; i &lt;= 3; i++){<br>     console.log(obj[i]);<br>}<br>//&quot;one&quot;<br>//&quot;two&quot;<br>//&quot;three&quot;</pre><p>In the first loop when we are using period or . to retrieve the value it is returning undefined because it is treating i as a property name and there is no key with i name inside the object.</p><p>On the other hand in the second loop, the value of i is evaluated inside the obj[i] and it becomes obj[1] so it returns one.</p><h3>Handling the non existent property</h3><p>Retrieving the property which does not exist inside the obj will return undefined.</p><pre>let obj = {<br>   first_name: &quot;Prashant&quot;<br>};</pre><pre>obj.first_name;<br>//&quot;Prashant&quot;</pre><pre>obj.last_name;<br>//undefined</pre><p>We can use the || operator to fill in the default value whenever property does not exists.</p><pre>let lastName = obj.last_name || &quot;Yadav&quot;;<br>//&quot;Yadav&quot;</pre><p>Accessing the property of nonexistence properties will result in an error, it is better to use &amp;&amp; operator to restrict the error.</p><pre>obj.last_name;<br>//undefined</pre><pre>obj.last_name.intial<br>//Uncaught TypeError: Cannot read property &#39;intial&#39; of undefined</pre><pre>obj.last_name &amp;&amp; obj.last_name.intial<br>//undefined</pre><p>&amp;&amp; will evaluate the right hand side expression only if the left hand side expression is true.</p><h3>Updating the object values</h3><p>We can update the values of the object by assigning the new values to it. If the property does not already exists then it will add a new one.</p><pre>let obj = {<br>   name: &quot;Prashant&quot;<br>}</pre><pre>obj.name = &quot;Sachin&quot;;<br>obj.name;<br>//&quot;Sachin&quot;</pre><pre>obj.age = 24;<br>obj.age;<br>//24</pre><pre>obj[&quot;gender&quot;] = &quot;Male&quot;;<br>obj[&quot;gender&quot;];<br>//&quot;Male&quot;</pre><p>While copying the value from another object always remember objects are always passed around as a reference they are never actually copied.</p><pre>let obj = {}; let person = obj; person.name = &quot;Prashant&quot;; let person2 = obj.name; person2; //&quot;Prashant&quot;</pre><p>In the above example if you see we have assigned value to the person.name but when we are accessing obj.name we are getting same value because we are referencing to the same object.</p><pre>//Referencing to the same object<br>let a = b = c = {};</pre><pre>//Referencing to the different objects<br>let a = {}, b = {}, c = {};</pre><h3><a href="https://learnersbucket.com/examples/array/how-to-copy-array-in-javascript">Copying an object in javascript</a></h3><h4>Shallow copy</h4><p>If we want to do a shallow copy of an object then we can use Object.assign(To, From) method which copies one object to other.</p><pre>let obj = {<br>   name: &quot;Prashant&quot;,<br>   age: 24,<br>   details: {<br>     gender: &quot;Male&quot;<br>   }<br>};</pre><pre>let copy = Object.assign({}, obj);<br>copy.name;<br>//&quot;Prashant&quot;</pre><pre>copy.age<br>//24</pre><pre>copy.details.gender;<br>//&quot;Male&quot;</pre><pre>//Update the gender<br>obj.details.gender = &quot;Female&quot;;</pre><pre>copy.details.gender;<br>//&quot;Female&quot;</pre><p>As you can see we have updated the value of obj.details.gender = &quot;Female&quot;; and it is reflected to the copied object copy.details.gender;.</p><p>In shallow copy, the nested objects are still passed as a reference to the new object.</p><h4>Deep copy</h4><p>We can use JSON.parse() and JSON.stringify() together to create a deep copy of an object.</p><pre>let obj = {<br>   name: &quot;Prashant&quot;,<br>   age: 24<br>};</pre><pre>let copy = JSON.parse(JSON.stringify(obj));<br>copy.name;<br>//&quot;Prashant&quot;</pre><pre>copy.age<br>//24</pre><h3>Extending objects with prototype</h3><p>Every object in javascript is linked to a prototype object from which it can inherit properties and methods. Prototype object comes standard with javascript and is available with every object that is created with object literals.</p><pre>let obj = {};</pre><pre>obj.name = &quot;Prashant&quot;;<br>obj.age = 24;<br>obj.prototype.getDetails = function(){<br>   return `${this.name} is ${this.age} years old`<br>};</pre><pre>console.log(obj.getDetails());<br>//&quot;Prashant is 24 years old&quot;</pre><p>While accessing the value from the object if the property is not present then it will look into its prototype object and if it is not present here then it will look into its prototype until it bottoms out.</p><p>If the property is not present in prototype linkage then it will return undefined. This process of looking into the prototype chain is called <strong>Delegation</strong>.</p><h3>Deleting object properties</h3><p>We can use the delete operator to remove the property from the object. If the property is not present then it will simply ignore it.</p><pre>let obj = {};</pre><pre>obj.name = &quot;Prashant&quot;;<br>obj.age = 24;</pre><pre>//Add properties and Methods inside the prototype.<br>obj.prototype.age = 23;<br>obj.prototype.getDetails = function(){return `${this.name} is ${this.age} years old`};</pre><pre>console.log(obj.getDetails());<br>//&quot;Prashant is 24 years old&quot;</pre><pre>//Remove age<br>delete obj.age;</pre><pre>console.log(obj.getDetails());<br>//&quot;Prashant is 23 years old&quot;</pre><p>It will only delete the property from the given object, It will not remove it from its prototype link. So if the property is present in the prototype then it will access that one.</p><h3>Checking if property or method exist in object</h3><h3>Using hasOwnProperty()</h3><p>We can use hasOwnProperty() to check if object has a given property or method. It will not check for the properties present in the prototype chain.</p><pre>let obj = {<br>    name: &quot;Prashant&quot;,<br>    age: 24,<br>    getDetails: function(){<br>       return `${this.name} is ${this.age} years old`;<br>    }<br>};</pre><pre>//Adding property to prototype<br>obj.prototype.gender = &quot;Male&quot;;</pre><pre>console.log(obj.hasOwnProperty(&quot;name&quot;));<br>//true</pre><pre>console.log(obj.hasOwnProperty(&quot;age&quot;));<br>//true</pre><pre>console.log(obj.hasOwnProperty(&quot;getDetails&quot;));<br>//true</pre><pre>console.log(obj.hasOwnProperty(&quot;gender&quot;));<br>//false</pre><h3>Using in operator</h3><p>We can use in operator to check if a given property exist or not. It will look for the property in the prototype chain also.</p><pre>let obj = {<br>    name: &quot;Prashant&quot;,<br>    age: 24,<br>    getDetails: function(){<br>       return `${this.name} is ${this.age} years old`;<br>    }<br>};</pre><pre>//Adding property to prototype<br>obj.prototype.gender = &quot;Male&quot;;</pre><pre>console.log(&quot;name&quot; in obj);<br>//true</pre><pre>console.log(&quot;age&quot; in obj);<br>//true</pre><pre>console.log(&quot;getDetails&quot; in obj);<br>//true</pre><pre>console.log(&quot;gender&quot; in obj);<br>//true</pre><h3>Enumerating the objects in javascript.</h3><h3>Using for...in loop</h3><p>We can use the for...in loop to iterate through each property of the given object.</p><pre>let obj = {<br>    name: &quot;Prashant&quot;,<br>    age: 24,<br>    getDetails: function(){<br>       return `${this.name} is ${this.age} years old`;<br>    }<br>};</pre><pre>//Adding property to prototype<br>obj.prototype.gender = &quot;Male&quot;;</pre><pre>for(let key in obj){<br>   console.log(obj[key]);<br>}</pre><pre>//&quot;Prashant&quot;<br>//24<br>/*<br>function () {<br>  return `${this.name} is ${this.age} years old`;<br>}<br>*/<br>//&quot;Male&quot;</pre><p>It iterates through all the properties, even the one in prototye chain. We can restrict to enumerate through the properties in the current scope only using hasOwnProperty().</p><pre>for(let key in obj){<br>   if(obj.hasOwnProperty(key)){<br>     console.log(obj[key]);<br>   }<br>}</pre><pre>//&quot;Prashant&quot;<br>//24<br>/*<br>function () {<br>  return `${this.name} is ${this.age} years old`;<br>}<br>*/</pre><p>As we can see the source code for the methods are getting printed because we are accessing them not evaluating them with (). So we can restrict them as well by checking their type.</p><pre>for(let key in obj){<br>   if(obj.hasOwnProperty(key) &amp;&amp; typeof obj[key] !== &#39;function&#39;){<br>     console.log(obj[key]);<br>   }<br>}</pre><pre>//&quot;Prashant&quot;<br>//24</pre><p>Object.Keys(obj) takes an object as an input and returns the array of keys as a output. It does not return keys from the prototype chain so we can use this safely.</p><pre>console.log(Object.keys(obj));</pre><pre>//[&quot;name&quot;, &quot;age&quot;, &quot;getDetails&quot;]</pre><p>We can use this array of keys to iterate through each property of the object.</p><h3>for...of loop</h3><pre>//For of loop<br>for(let key of Object.keys(obj)){<br>  if(typeof obj[key] !== &#39;function&#39;){<br>     console.log(obj[key]);<br>   }<br>}</pre><pre>//&quot;Prashant&quot;<br>//24</pre><h3>forEach loop</h3><pre>//For each loop<br>Object.keys(obj).forEach((key) =&gt; {<br>  if(typeof obj[key] !== &#39;function&#39;){<br>     console.log(obj[key]);<br>   }<br>});</pre><pre>//&quot;Prashant&quot;<br>//24</pre><p>We can clearly see that the object is the heart of the javascript. That is why it is continuously evolving. You can check out the new <a href="https://learnersbucket.com/tutorials/es6/object-literals">features</a> that are introduced in <a href="https://learnersbucket.com/tutorials/es6/es6-intro">ES6</a>.</p><p><em>Originally published at </em><a href="https://learnersbucket.com/tutorials/data-structures/javascript-objects-complete-reference/"><em>https://learnersbucket.com</em></a><em> on April 28, 2019.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=def1e8f9d400" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to create a responsive sliding menu]]></title>
            <link>https://medium.com/free-code-camp/how-to-create-a-responsive-sliding-menu-97b90852a455?source=rss-55ea03e0926e------2</link>
            <guid isPermaLink="false">https://medium.com/p/97b90852a455</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[design]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Prashant Yadav]]></dc:creator>
            <pubDate>Sun, 14 Apr 2019 00:00:00 GMT</pubDate>
            <atom:updated>2019-04-23T16:12:36.309Z</atom:updated>
            <content:encoded><![CDATA[<p>I run a blog named <a href="https://learnersbucket.com/">learnersbucket.com</a> where I write about <a href="https://learnersbucket.com/tutorials/es6/es6-intro/">ES6</a>, <a href="https://learnersbucket.com/tutorials/topics/data-structures/">Data structures</a>, and <a href="https://learnersbucket.com/examples/topics/algorithms/">Algorithms</a> to help others crack coding interviews. Follow me on <a href="https://twitter.com/LearnersBucket">Twitter</a> for regular updates.</p><p>When I was designing my blog with a mobile-first approach, I decided to keep my sidebar navigation menu separate at the bottom right. There is no need for a sticky header and the user can read everything in full height.</p><p>This is the simple version of how my mobile menu looks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*_qfdprsLd9bgo4VB1gTWyA.gif" /><figcaption>Sliding Sidebar Navigation Menu</figcaption></figure><p>Here is how you can create your own responsive sidebar navigation menu.</p><h3>Overview</h3><p>Before we move on to designing the menu, let us imagine what components we need.</p><ul><li>A hamburger 🍔 button which will <strong>show</strong>/<strong>hide</strong> the sliding menu.</li><li>Animation on the hamburger button to represent the current state of the menu.</li><li>A side navigation menu.</li></ul><p>As the side navigation menu will toggle on the click of the hamburger menu, we can keep them together in a single container.</p><h3>Dependencies</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/50c6fc2d0ac473e04a00b6956990c4fb/href">https://medium.com/media/50c6fc2d0ac473e04a00b6956990c4fb/href</a></iframe><p>I like to use jQuery for <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction">DOM</a> manipulation as it reduces the amount of code I need to write.</p><h3>Hamburger button</h3><h4>HTML structure</h4><p>There is a simple trick to creating a hamburger menu.</p><p>We are going to use a &lt;div&gt; with a .hamburger class to create the hamburger button wrapper. Then we will place three &lt;span&gt;s to create the layers of the hamburger.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3ea7646d84a6b61af05dffbaa688655f/href">https://medium.com/media/3ea7646d84a6b61af05dffbaa688655f/href</a></iframe><h3>Designing the hamburger button</h3><p>Now that the HTML structure for our button is ready, we need to design it to make it look like a hamburger. While designing, we need to keep in mind that we have to provide the animation for open &amp; close when the user clicks on it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0931aee247109499289609ce608f2413/href">https://medium.com/media/0931aee247109499289609ce608f2413/href</a></iframe><p>As we are creating a fixed dimension hamburger button, we are going to provide fixed dimensions to the wrapper.</p><ul><li>We have created a fixed parent .hamburger{position:fixed} to place it wherever we want on the screen.</li><li>Then we have designed all the &lt;span&gt;s as small rectangular boxes with position:absolute.</li><li>As we need to show three different strips we have changed the top position of the 2nd span .hamburger &gt; span:nth-child(2){ top: 16px; } &amp; 3rd span .hamburger &gt; span:nth-child(3){ top: 27px; }.</li><li>We have also provided transition: all .25s ease-in-out; to all the spans so that the change of their properties should be smooth.</li></ul><h3>Opening &amp; Closing hamburger button with jQuery</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ac08e8ce96e553a775a215edc73330ac/href">https://medium.com/media/ac08e8ce96e553a775a215edc73330ac/href</a></iframe><p>Whenever the hamburger button is clicked it will toggle an open class to it. We can now use this class to add the opening &amp; closing effect.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6e7befe49ba5cfde9046fcbdaf52b3a1/href">https://medium.com/media/6e7befe49ba5cfde9046fcbdaf52b3a1/href</a></iframe><p>.hamburger.open &gt; span:nth-child(2){ transform: translateX(-100%); opacity: 0;} will slide the middle strip of the hamburger to the left and make it transparent.</p><p>.hamburger.open &gt; span:nth-child(1){ transform: rotateZ(45deg); top:16px; } &amp; .hamburger.open &gt; span:nth-child(2){ transform: rotateZ(-45deg); top:16px; } will bring the first and last span to the same top position and rotate them to make an X.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*wV23krL_L1ewHBrk.gif" /><figcaption>Hamburger Button</figcaption></figure><p>Kudos 🙌 we have our hamburger 🍔 button ready, so let us create the side navigation now.</p><h3>Responsive side navigation menu</h3><h4>HTML structure</h4><p>We will create a simple navigation menu.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9c5f2ccf5979efa90208d64456dc57d4/href">https://medium.com/media/9c5f2ccf5979efa90208d64456dc57d4/href</a></iframe><p>We used a nav element for creating the navigation menu and placed the links in ul.</p><h3>Designing the navigation menu</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f6f8e3722172cd37080a479833d67f5e/href">https://medium.com/media/f6f8e3722172cd37080a479833d67f5e/href</a></iframe><p>I have created a full-screen side menu, you can change the dimensions according to your need. We are using &gt; selector to avoid overwriting the style of other elements.</p><p>Now we have our navigation menu and hamburger button ready, so we can wrap them inside a wrapper to make them functional.</p><h3>Sliding navigation menu</h3><h4>HTML structure</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/72af2364f9597506fc2b67a6a4e0787d/href">https://medium.com/media/72af2364f9597506fc2b67a6a4e0787d/href</a></iframe><p>We have placed the hamburger button and navigation menu inside the .mobile-menu wrapper.</p><h3>Designing the sliding navigation menu</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2b4a4e2ec68ebf2f6388d9b05de0afee/href">https://medium.com/media/2b4a4e2ec68ebf2f6388d9b05de0afee/href</a></iframe><p>We have updated the design a little by providing some property of the .hamburger to .mobile-menu making it fixed and made .hamburger relative to keep the &lt;span&gt; design the same.</p><p>As there can be multiple navs, we have updated all the selectors .mobile-menu &gt; nav as well to make sure we are pointing to the required elements only.</p><h3>Making sidebar menu functional with jQuery</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f9af54d643aeb0210e226bb82e163acf/href">https://medium.com/media/f9af54d643aeb0210e226bb82e163acf/href</a></iframe><p>We now add our .open class to the .mobile-menu so that we can handle both the hamburger button and the sliding menu with a single change.</p><p>Our CSS for the animation is also updated accordingly.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e8ab247765bc14396c092788447fb5fc/href">https://medium.com/media/e8ab247765bc14396c092788447fb5fc/href</a></iframe><p>Well done 🎉🎉👍 we covered everything.</p><p>Check out the working demo here</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2FLearnersbucket%2Fembed%2Fpreview%2FbJvEmB%3Fheight%3D600%26slug-hash%3DbJvEmB%26default-tabs%3Dcss%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io&amp;url=https%3A%2F%2Fcodepen.io%2FLearnersbucket%2Fpen%2FbJvEmB&amp;image=https%3A%2F%2Fscreenshot.codepen.io%2F2982602.bJvEmB.small.d481bb55-a928-43cd-a231-c3a953d8f718.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/6fbedb0ab91cce2a2321a292f12a00e5/href">https://medium.com/media/6fbedb0ab91cce2a2321a292f12a00e5/href</a></iframe><h3>Conclusion</h3><p>This post was about a simple sliding menu. I tried to break it into different components so that you can use them independently.</p><p>Thank you for having patience while reading this. If you learned something new today then give some 👏. Also, share it with your friends so that they can learn something new too.</p><p>That’s it, follow me on <a href="https://twitter.com/LearnersBucket">Twitter</a> for knowledge sharing. I write about <a href="https://learnersbucket.com/tutorials/es6/es6-intro/">ES6</a>, Nodejs, <a href="https://learnersbucket.com/tutorials/topics/data-structures/">Data structures</a> &amp; <a href="https://learnersbucket.com/examples/topics/algorithms/">Algorithms</a> and full stack web development with JavaScript.</p><p><em>Originally published at </em><a href="https://learnersbucket.com/examples/html/how-to-create-responsive-sidebar-menu/"><em>learnersbucket.com</em></a><em> on April 14, 2019.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=97b90852a455" width="1" height="1" alt=""><hr><p><a href="https://medium.com/free-code-camp/how-to-create-a-responsive-sliding-menu-97b90852a455">How to create a responsive sliding menu</a> was originally published in <a href="https://medium.com/free-code-camp">We’ve moved to freeCodeCamp.org/news</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to build a custom file uploader with HTML5, JavaScript, & Bootstrap]]></title>
            <link>https://medium.com/free-code-camp/custom-file-uploader-with-html5-javascript-bootstrap-85a56a0437c5?source=rss-55ea03e0926e------2</link>
            <guid isPermaLink="false">https://medium.com/p/85a56a0437c5</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Prashant Yadav]]></dc:creator>
            <pubDate>Thu, 07 Mar 2019 00:00:00 GMT</pubDate>
            <atom:updated>2019-04-05T05:35:11.588Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*PRXzv8NRN4Clwp1XLatBeg.gif" /><figcaption>Custom File Picker</figcaption></figure><p>In this short article, we’ll learn how to create custom file uploader with JQuery, <a href="https://learnersbucket.com/tutorials/es6/es6-intro">ES6</a>, and Bootstrap4.</p><p>We will create a file uploader with a custom design and an option to preview selected files and remove them.</p><p><em>Support me by reading this article </em><a href="https://learnersbucket.com/examples/bootstrap4/custom-fileuploader-in-javascript/"><em>here</em></a><em>.</em></p><h3>Demo</h3><p>Check out the live demo <a href="https://codepen.io/Learnersbucket/pen/drWENz">here</a>.</p><h3>Implementation</h3><ul><li>We will use the html5 file uploader to upload the files.</li><li>Then, with the help of Bootstrap popover, we will preview the selected files.</li><li>While previewing the files, we will provide an option to remove the selected file.</li><li>As JQuery is one of the dependencies for Bootstrap popover, we will use it to ease our work.</li></ul><h3>Dependencies</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/66bdc9c93040c8c7613c35ad9dc40214/href">https://medium.com/media/66bdc9c93040c8c7613c35ad9dc40214/href</a></iframe><h3>HTML layout for file uploader</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b166c3fd78089da9aef9822463b97dc6/href">https://medium.com/media/b166c3fd78089da9aef9822463b97dc6/href</a></iframe><p>Explanation</p><ul><li>We have created a container named custom-file-picker.</li><li>In this, we have our custom file upload picture-container and our popover previewer popover-container.</li><li>Every file picker has a unique id a8755cf0-f4d1-6376-ee21-a6defd1e7c08 and its corresponding popover refers to that id data-target=&quot;a8755cf0-f4d1-6376-ee21-a6defd1e7c08&quot; to preview the files.</li></ul><h3>Styling our components</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c499800b920fc33fb1c086149934bdc6/href">https://medium.com/media/c499800b920fc33fb1c086149934bdc6/href</a></iframe><h3>Handling the functionality</h3><p>Now that we have styled our components, it is time to handle the functionality. We will use Jquery with <a href="https://learnersbucket.com/tutorials/es6/es6-intro">ES6</a> to make things easy.</p><h3>Storing the files</h3><p>We will create a global variable to store the files.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7ed47d068e3d7ddcadbaf4ea5f722520/href">https://medium.com/media/7ed47d068e3d7ddcadbaf4ea5f722520/href</a></iframe><p>We will use this variable to store all the files of the corresponding file picker with the help of its id.</p><p>Now we will create a function which will manage the storing of the file and displaying the count of the files. This function will take id and array of files as input.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/abcc8c27568f0d33b8fe0beca631d4db/href">https://medium.com/media/abcc8c27568f0d33b8fe0beca631d4db/href</a></iframe><p>$(`[data-id=&quot;${id}&quot;] &gt; .file-total-viewer`).text(files.length); will update the file count in popover previewer.</p><h3>Handling the file picking</h3><p>We have our function ready to update the count and store the files. We will just pass data to this function once the files are selected or changed.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/20be7bf9266c3d8be9963788152b86bf/href">https://medium.com/media/20be7bf9266c3d8be9963788152b86bf/href</a></iframe><p>Once the files are selected we will show the complete animation with SVG to notify users that files are changed.</p><p>Right now we have our file stored and the count visible. Let&#39;s create the file previewer with a bootstrap popover.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e2c2db0a53f82acf29ac6923bcbec78b/href">https://medium.com/media/e2c2db0a53f82acf29ac6923bcbec78b/href</a></iframe><p>Bootstrap provides us a method to dynamically generate the content of the popover. So we attach the popover to [data-toggle=&quot;popover&quot;]. Learn more about it <a href="https://getbootstrap.com/docs/4.1/components/popovers/">here</a>.</p><h4>How it works</h4><ul><li>Every time a popup is about to render it will use its [data-target] id and pull all the files from the fileStorage.</li><li>If there are files, then render those files along with the delete button.</li><li>If there is no file, then show some message.</li></ul><p>Now in case you have multiple file uploaders and you want only one popover to be open at a time, add the following code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ccc68101b66e0949cccae3b31a24c098/href">https://medium.com/media/ccc68101b66e0949cccae3b31a24c098/href</a></iframe><p>If you select some file and click on view you should be able to view it. Now the last thing we will do is handle the deletion of files.</p><h3>Deleting the file</h3><p>We have provided the id of the file picker to the delete button through data-target and the name of the file through data-name. Every time the delete icon is clicked we will use these values to remove the files.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f0428bc97e2bca5716c832a875ce217d/href">https://medium.com/media/f0428bc97e2bca5716c832a875ce217d/href</a></iframe><p>As we are dynamically generating the content of the popover and it does not already exist in the DOM, we cannot assign an event to it. So we have to use a workaround of assigning an event on the DOM and checking if the delete icon is clicked with $(document).on(&#39;click&#39;, &#39;.popover-content-remove&#39;, function (e) {});.</p><h4>How it works</h4><ul><li>Once the delete icon is clicked we will ask for confirmation from the user.</li><li>If the user wants to proceed, then we fetch the id and the name assigned to delete button through data-target and data-name.</li><li>We remove that particular file using filter() method.</li><li>Once the file is removed from the array, then we update its count by passing the value to our helper function storeFile(id, newArr);.</li><li>Also, we remove the element from the popover. If the array is empty then show some message.</li></ul><p>Note: You should provide a unique id to each file picker and its popover previewer.</p><h3>Complete code</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9234aaced035d0634d5b13c0e4506c37/href">https://medium.com/media/9234aaced035d0634d5b13c0e4506c37/href</a></iframe><p>If you liked this article, please give it 50+ 👏and share it! If you have any questions related to this feel free to ask me.</p><p><em>For more like this and algorithmic solutions in Javascript, follow me on </em><a href="https://twitter.com/LearnersBucket"><strong>Twitter</strong></a><em>. </em>I write about <a href="https://learnersbucket.com/tutorials/es6/es6-intro/">ES6</a>, React, Nodejs, <a href="https://learnersbucket.com/tutorials/topics/data-structures/">Data structures</a>, and <a href="https://learnersbucket.com/examples/topics/algorithms/">Algorithms</a> on <a href="https://learnersbucket.com/"><em>learnersbucket.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=85a56a0437c5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/free-code-camp/custom-file-uploader-with-html5-javascript-bootstrap-85a56a0437c5">How to build a custom file uploader with HTML5, JavaScript, &amp; Bootstrap</a> was originally published in <a href="https://medium.com/free-code-camp">We’ve moved to freeCodeCamp.org/news</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[List of 50 algorithms to study for your first javascript interview.]]></title>
            <link>https://medium.com/hackernoon/list-of-50-algorithms-to-study-for-your-first-javascript-interview-7fb6cb76229?source=rss-55ea03e0926e------2</link>
            <guid isPermaLink="false">https://medium.com/p/7fb6cb76229</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Prashant Yadav]]></dc:creator>
            <pubDate>Sun, 10 Feb 2019 08:57:53 GMT</pubDate>
            <atom:updated>2019-02-27T05:43:57.220Z</atom:updated>
            <content:encoded><![CDATA[<p>Many students are using Javascript as there first programming language and why they should not. It is the only language in the elite league that can make a newbie a fullstack developer and also it is easy to learn.</p><p>But when i was trying to switch to the fullstack engineer role, it was difficult for me to solve the algorithms in Javascript as there was not much resources available. Everything was in C++, Java or Python. So i decided to create my own <a href="https://learnersbucket.com/">blog</a> where i can share the solved solutions in javascript along with time and space complexities with the community. I tried to solve the problems in <a href="https://learnersbucket.com/tutorials/es6/es6-intro/">ES6</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w8VbcKFYPiVLfax_2ieIMw.jpeg" /><figcaption>Source: Pixabay</figcaption></figure><h4>Today I have finished writing 50 problems on my <a href="https://learnersbucket.com/">blog</a>.</h4><h3>Here are the 50 algorithmic questions solved in javascript, <a href="https://learnersbucket.com/tutorials/es6/es6-intro/">ES6</a> which you may find helpful for interviews.</h3><ul><li><a href="https://learnersbucket.com/examples/algorithms/given-an-unsorted-array-of-integers-find-a-pair-with-given-sum-in-it/">Given an unsorted array of integers find a pair with given sum in it | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/right-circular-rotation-on-an-array-of-integers/">Right circular rotation on an array of integers | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/left-circular-rotation-on-array-of-integers/">Left circular rotation on array of integers | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/the-base-converter-algorithm/">The base converter algorithm | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/count-number-of-sub-string-occurrence-in-a-string/">Count number of sub-string occurrence in a string | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/swap-two-numbers-without-temp-variables/">Swap two numbers without temp variables | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/palindrome-string/">Palindrome string | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/balanced-parentheses/">Balanced parentheses | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/find-the-biggest-perfect-square-in-an-array/">Find the biggest perfect square in an array | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/subarrays-with-a-given-sum-k-in-an-array/">Subarrays with a given sum k in an array | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/find-digital-root-of-a-number/">Find digital root of a given number | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/buggy-calculator/">Buggy Calculator | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/form-the-smallest-possible-number-from-the-given-number/">Form the smallest possible number from the given number | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/find-missing-alphabets-to-make-a-string-panagram/">Find missing alphabets to make a string panagram | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/check-if-string-has-duplicate-letters/">Check if string has duplicate letters | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/difference-between-square-of-sum-of-numbers-and-sum-of-square-of-numbers/">Difference between square of sum of numbers and sum of square of numbers. | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/converting-string-to-jadencase/">Converting string to jadencase | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/check-if-given-number-is-armstrong-in-javascript/">Check if given number is armstrong in javascript | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/find-all-the-armstrong-number-between-two-numbers/">Find all the armstrong number between two numbers | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/print-matrix-in-zigzag-format/">Print matrix in zigzag format | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/print-matrix-in-l-pattern/">Print matrix in L pattern | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/algorithm-to-check-the-prime-number/">Algorithm to check the prime number | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/find-the-largest-prime-factor/">Find the largest prime factor | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/find-factorial-of-a-given-number-in-javascript/">Find factorial of a given number in javascript | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/convert-roman-numeral-to-an-integer/">Convert Roman numeral to an integer | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/print-all-the-unique-2-digit-combinations-of-given-numbers/">Print all the unique 2 digit combinations of given numbers | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/program-to-find-the-nth-fibonacci-number/">Program to find the nth Fibonacci number | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/program-to-print-the-collatz-sequence-in-javascript/">Program to print the Collatz sequence in javascript. | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/maximum-collatz-sequence-under-1000000/">Maximum Collatz sequence under 1000000 | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/check-if-an-array-is-palindrome/">Check if an array is palindrome | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/sort-a-stack-using-another-stack/">Sort a stack using another stack | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/linear-search/">Linear Search in javascript | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/search-substring-in-a-given-string/">Search substring in a given string | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/program-to-check-if-a-subarray-with-0-sum-exits-or-not/">Program to check if a subarray with 0 sum exits or not | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/find-the-correct-position-to-insert-an-element-in-the-array/">Find the correct position to insert an element in the array | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/check-if-two-string-are-anagram-of-each-other/">Check if two string are anagram of each other | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/minimum-characters-to-delete-to-make-strings-anagram/">Minimum characters to delete to make string anagram | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/find-the-maximum-sum-of-products-of-two-arrays/">Find the maximum sum of products of two arrays. | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/program-to-add-two-binary-numbers/">Program to add two binary numbers | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/find-distinct-ways-to-climb-the-stairs-in-javascript/">Find distinct ways to climb the stairs in javascript. | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/program-to-reverse-a-queue/">Program to reverse a queue | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/selection-sort-in-javascript/">Selection sort in javascript | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/reverse-a-stack-using-recursion/">Reverse a stack using recursion. | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/caesar-cipher-in-javascript/">Caesar Cipher in javascript | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/program-to-find-the-gcd-of-two-numbers-in-javascript/">Program to find the GCD of two numbers in javascript. | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/find-the-lcm-of-two-numbers-in-javascript/">Find the LCM of two numbers in javascript | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/fizzbuzz-program-in-javascript/">FizzBuzz program in javascript | LearnersBucket</a></li><li><a href="https://learnersbucket.com/examples/algorithms/program-to-print-all-the-prime-numbers-from-1-to-n/">Program to print all the prime numbers from 1 to N. | LearnersBucket</a></li><li><a href="https://learnersbucket.com/tutorials/algorithms/queue-implementation-in-javascript/">Queue implementation in javascript | LearnersBucket</a></li><li><a href="https://learnersbucket.com/tutorials/algorithms/stack/">Stack | LearnersBucket</a></li></ul><p>If you liked this article, please give it some 50+ 👏and share it! If you have any questions related to this feel free to ask me.</p><p><em>For more like this and algorithmic solutions in Javascript, follow me on </em><a href="https://twitter.com/LearnersBucket"><strong>Twitter</strong></a><em>. </em>I write about <a href="https://learnersbucket.com/tutorials/es6/es6-intro/">ES6</a>, React, Nodejs, <a href="https://learnersbucket.com/tutorials/topics/data-structures/">Data structures</a>, and <a href="https://learnersbucket.com/examples/topics/algorithms/">Algorithms</a> on <a href="https://learnersbucket.com/"><em>learnersbucket.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7fb6cb76229" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/list-of-50-algorithms-to-study-for-your-first-javascript-interview-7fb6cb76229">List of 50 algorithms to study for your first javascript interview.</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to form the smallest possible number from a given number in JavaScript]]></title>
            <link>https://medium.com/free-code-camp/forming-the-smallest-possible-number-from-the-given-number-in-javascript-bda790655c8e?source=rss-55ea03e0926e------2</link>
            <guid isPermaLink="false">https://medium.com/p/bda790655c8e</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Prashant Yadav]]></dc:creator>
            <pubDate>Sat, 29 Dec 2018 00:00:00 GMT</pubDate>
            <atom:updated>2019-02-13T05:42:24.292Z</atom:updated>
            <content:encoded><![CDATA[<p>In this tutorial, we will implement an algorithm to form the smallest possible number with <a href="https://learnersbucket.com/tutorials/es6/es6-intro/">ES6</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XoEDgZmZ-IWBZivc4DJakg.jpeg" /><figcaption>Source: Pixabay</figcaption></figure><pre>Input: <br>55010 <br>7652634</pre><pre>Output: <br>10055 <br>2345667</pre><p><strong>Note</strong>: The transformed number should not start with 0 if it has at least one non-zero character.</p><p>We are going to use two different approaches to solve this problem. Everything will be written in <a href="https://learnersbucket.com/tutorials/es6/es6-intro">ES6</a>.</p><ul><li>In the first approach, we will assume that the provided number is in string format and solve it using sorting which will take O(nlogn).</li><li>In the Second approach, we will solve with numeric value with O(d) time, where d is the number of digits.</li></ul><h3>Using sorting O(nlogn).</h3><h4>Implementation</h4><ul><li>We will convert the number to the character array and then sort that array.</li><li>After sorting we will check if the first character in the array is 0.</li><li>If it is not 0 then we will join the array and return it.</li><li>If it is 0 then we will find the first non-zero number and swap it with 0 and return it.</li></ul><pre>function smallestPossibleNumber(num){</pre><pre>//Create a character array and sort it in ascending order<br>let sorted = num.split(&#39;&#39;).sort();</pre><pre>//Check if first character is not 0 then join and return it if(sorted[0] != &#39;0&#39;){ <br>   return sorted.join(&#39;&#39;);<br>}</pre><pre>//find the index of the first non - zero character let index = 0; for(let i = 0; i &lt; sorted.length; i++){ <br> if(sorted[i] &gt; &quot;0&quot;){ <br>   index = i; <br>   break; <br> } <br>}</pre><pre>//Swap the indexes <br> let temp = sorted[0]; <br> sorted[0] = sorted[index]; <br> sorted[index] = temp;</pre><pre>//return the string after joining the characters of array return<br> sorted.join(&#39;&#39;); <br>}</pre><p>Running the Program</p><pre>Input:<br>console.log(smallestPossibleNumber(&#39;55010&#39;));<br>console.log(smallestPossibleNumber(&#39;7652634&#39;));<br>console.log(smallestPossibleNumber(&#39;000001&#39;));<br>console.log(smallestPossibleNumber(&#39;000000&#39;));</pre><pre>Output:<br>10055<br>2345667<br>100000<br>000000</pre><pre>/*How it works<br>  let sorted = num.split(&#39;&#39;).sort(); <br>  = [&#39;5&#39;,&#39;5&#39;,&#39;0&#39;,&#39;1&#39;,&#39;0&#39;].sort() = [&#39;0&#39;,&#39;0&#39;,&#39;1&#39;,&#39;5&#39;,&#39;5&#39;]<br>  if(sorted[0] != &#39;0&#39;){   // &#39;0&#39; != &#39;0&#39; condition fails<br>     return sorted.join(&#39;&#39;);<br>  }<br>  <br>  //Find the index of the first non - zero character<br>  let index = 0;<br>  for(let i = 0; i &lt; sorted.length; i++){<br>     if(sorted[i] &gt; &#39;0&#39;){  // &#39;1&#39; &gt; &#39;0&#39;<br>       index = i;      // index = 2;<br>       break;          // break;<br>     }<br>  }<br>  <br>  //swap the index<br>  var temp = sorted[0];      <br>  sorted[0] = sorted[index];<br>  sorted[index] = temp;<br>  <br>  //return the string<br>  return sorted.join(&#39;&#39;);<br>*/</pre><h4>How it works</h4><p>We first created the array of characters like [&#39;5&#39;, &#39;5&#39;, &#39;0&#39;, &#39;1&#39;, 0] . Then we sort this to[&#39;0&#39;, &#39;0&#39;, &#39;1&#39;, &#39;5&#39;, &#39;5&#39;] After this, we find the first non-zero element and swap it with first zero elements like [&#39;1&#39;, &#39;0&#39;, &#39;0&#39;, &#39;5&#39;, &#39;5&#39;] . Now we have our smallest number ready we just need to concatenate them together and return it.</p><p>Learn more about the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split">split()</a>, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort">sort()</a>, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join">join()</a>.</p><p>Time Complexity: O(nlogn).<br>Space Complexity: O(n).</p><h4>Time and Space complexity explanation</h4><p>We are creating a character array which will take O(n) time. Then sorting the array will take O(nlogn).</p><p>After that, we are finding the index of smallest non zero number which can take O(n) in the worst case and joining the array to create the string will take O(n). As these all operations are running one after other. So time complexity is O(n + nlogn + n + n) = O(nlogn).</p><p>We are creating an array of characters from the string, so space complexity is O(n).</p><h3>Using numeric value O(logn).</h3><p>There is a drawback in this approach: if the number only contains zeros then it will return a single zero.</p><h4>Implementation</h4><ul><li>We will create an array of numbers from 0 to 9.</li><li>Then we will keep track of the digits present in the number by increasing their count in the array.</li><li>After that, we will find the smallest non-zero digit and decrease its count by 1.</li><li>In the end, we will recreate the number by arranging them in ascending order and return the result.</li><li>This solution is based on the counting sort.</li></ul><pre>function smallestPossibleNumber(num) { <br>   // initialize frequency of each digit to Zero<br>   let freq = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; <br>      <br>   // count frequency of each digit in the number<br>   while (num &gt; 0){ <br>     let d = parseInt(num % 10); // extract last digit<br>     freq[d]++; // increment counting<br>     num = parseInt(num / 10); //remove last digit<br>   }</pre><pre>// Set the LEFTMOST digit to minimum expect 0<br>   let result = 0; <br>   for (let i = 1 ; i &lt;= 9 ; i++) { <br>      if (freq[i] != 0) { <br>         result = i; <br>         freq[i]--; <br>         break;<br>      } <br>   } <br>       <br>   // arrange all remaining digits<br>   // in ascending order<br>   for (let i = 0 ; i &lt;= 9 ; i++) {<br>      while (freq[i]-- != 0){ <br>         result = result * 10 + i; <br>      }<br>   }<br>      <br>  return result; <br>}</pre><p>Running the program</p><pre>Input:<br>console.log(smallestPossibleNumber(&#39;55010&#39;));<br>console.log(smallestPossibleNumber(&#39;7652634&#39;));<br>console.log(smallestPossibleNumber(&#39;000001&#39;));<br>console.log(smallestPossibleNumber(&#39;000000&#39;));</pre><pre>Output:<br>10055<br>2345667<br>1<br>0</pre><pre>/* How it works<br>   // initialize frequency of each digit to Zero<br>   let freq = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];<br>   <br>   // count frequency of each digit in the number<br>   while (num &gt; 0){ <br>     let d = parseInt(num % 10); // extract last digit<br>     freq[d]++; // increment counting        <br>     num = parseInt(num / 10); //remove last digit<br>   } <br>   //After incrementing count<br>   //freq = [2, 1, 0, 0, 0, 2, 0, 0, 0, 0]<br>   <br>   // Set the LEFTMOST digit to minimum expect 0<br>   let result = 0; <br>   for (let i = 1 ; i &lt;= 9 ; i++) { <br>      if (freq[i] != 0) { <br>         result = i; <br>         freq[i]--; <br>         break;<br>      } <br>   } <br>   // result = 1<br>  <br>   // arrange all remaining digits<br>   // in ascending order<br>   for (let i = 0 ; i &lt;= 9 ; i++) {<br>      while (freq[i]-- != 0){ <br>         result = result * 10 + i; <br>      }<br>   }</pre><pre>   //10<br>   //100<br>   //1005<br>   //10055<br>   //10055<br>   <br>   return result<br>*/</pre><p>Time Complexity: O(nlogn).<br>Space Complexity: O(1).</p><h4>Time and Space complexity explanation</h4><p>We are removing each digit from the number and incrementing its respective count in an array that will take O(logn). Then we find the smallest non-zero number from the array in O(10).</p><p>After that we are rearranging the digits to create the smallest number in O(10 * logn). Time complexity is O(logn + 10+ 10logn) = O(logn) or O(d), where d is the no of digits</p><p>We are using constant space (an array of 10 numbers), so space complexity is O(1).</p><p>If you liked this article, please give it some 👏and share it! If you have any questions related to this feel free to ask me.</p><p><em>For more like this and algorithmic solutions in Javascript, follow me on </em><a href="https://twitter.com/LearnersBucket"><strong>Twitter</strong></a><em>. </em>I write about <a href="https://learnersbucket.com/tutorials/es6/es6-intro/">ES6</a>, React, Nodejs, <a href="https://learnersbucket.com/tutorials/topics/data-structures/">Data structures</a>, and <a href="https://learnersbucket.com/examples/topics/algorithms/">Algorithms</a> on <a href="https://learnersbucket.com/"><em>learnersbucket.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bda790655c8e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/free-code-camp/forming-the-smallest-possible-number-from-the-given-number-in-javascript-bda790655c8e">How to form the smallest possible number from a given number in JavaScript</a> was originally published in <a href="https://medium.com/free-code-camp">We’ve moved to freeCodeCamp.org/news</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to create a custom confirm box with Bootstrap4 and ES6]]></title>
            <link>https://medium.com/free-code-camp/custom-confirm-box-with-bootstrap-4-377aa67723c2?source=rss-55ea03e0926e------2</link>
            <guid isPermaLink="false">https://medium.com/p/377aa67723c2</guid>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[tech]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Prashant Yadav]]></dc:creator>
            <pubDate>Sun, 23 Dec 2018 15:55:43 GMT</pubDate>
            <atom:updated>2019-04-04T16:39:57.062Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*HlcCDg_OPlooOVUx5BayXg.gif" /><figcaption>Custom confirm box</figcaption></figure><p>We put lots of sweat into achieving good design but imagine a scenario where we have to use something which is styled default to browsers. It ruins our hard work as it changes browser to browser.</p><p>Same happened with me: I had put lots of effort into creating a SPA but the client wanted a confirm box with business logic. Unfortunately, using the inbuilt confirm box was the only odd factor in the design. So I created a custom confirm box.</p><p>Let us see how you can create your own custom confirm box with Bootstrap4 and <a href="https://learnersbucket.com/tutorials/es6/es6-intro/">ES6</a>.</p><p>A <strong>modal </strong>is a popup dialog that is available with Bootstrap. We can use its functionality to handle the popup and design it according to our needs. It has different methods which we can use to achieve our goal.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/511/1*jrEUFkPpPS4MUkcTpS9Chw.png" /><figcaption>Custom Confirm Box</figcaption></figure><p>Check out the live demo <a href="https://learnersbucket.com/examples/bootstrap4/custom-confirm-box-with-bootstrap/">here</a>.</p><ul><li>We are going to use Bootstrap4 modal for creating the popup dialog.</li><li>We will use the Javascript <em>callback</em> function to handle the response.</li><li>As Bootstrap has a JQuery dependency, we will use it for events.</li></ul><h3>#Bootstrap Modal Methods</h3><p>.modal(options): Activates the content as a modal with the below options.</p><p>.modal(“toggle”): Shows or Hides the modal based on its current state.</p><p>.modal(“show”): Opens the modal.</p><p>.modal(“hide”): Hides the modal.</p><h3>#Bootstrap Modal Options</h3><p>{backdrop: true or false, default: true}: Should modal hide when clicked outside the modal.</p><p>{ keyboard: true or false, default: true}: Should modal hide when a keyboard button is pressed.</p><p>{ show: true or false, default: true}: To show the modal when initialized.</p><h3>#Bootstrap Modal Events</h3><p>.on(‘show.bs.modal’): When modal is about to be shown but it is not yet shown.</p><p>.on(‘ shown.bs.modal’): When modal is shown.</p><p>.on(‘ hide.bs.modal’): When modal is about to be hidden but it is not yet hidden.</p><p>.on(‘ hidden.bs.modal’): When modal is hidden.</p><h4>Dependencies</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5d401c5836c0193767169ce914bf2c44/href">https://medium.com/media/5d401c5836c0193767169ce914bf2c44/href</a></iframe><p>Import all the dependencies using CDN.</p><p>We will now create a function that will generate our confirm box with our custom message and return the output selected by the user.</p><p>We don’t need the header and footer of the modal. We will just use the modal body and display our message in a &lt;h4&gt; tag. Then we will create two buttons below it with different classes btn-yes and btn-no which will be used to handle the response.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c2a572fa05742f6355d6da4b2600acf5/href">https://medium.com/media/c2a572fa05742f6355d6da4b2600acf5/href</a></iframe><h4>Explanation</h4><ul><li>We have created a function which accepts a message and a callback handler.</li><li>In this, we are appending the modal at the end of the body tag with JQuery’s appendto method.</li><li>After appending the modal, we are triggering or showing the modal with Bootstrap’s modal method. We are also passing two parameters {backdrop: &#39;static&#39;, keyboard:false} which prevent the modal from hiding with keyboard events, like pressing the <strong>ESC </strong>button or clicking in the backdrop area.</li><li>We are checking if the modal is hidden with bootstrap’s hidden.bs.modal and then we are removing the modal with JQuery’s remove method. This will prevent it from adding a modal every time.</li></ul><p>We have created a function that will render and open our custom confirm box, so now we have to handle the response provided by the user.</p><p>We will use a callback function to handle the response. In JavaScript, we can pass the function as an argument to another function and call it. The functions passed as arguments are called the callback functions.</p><h3>Handling the response</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d1868488091df20a383528768f3a23a6/href">https://medium.com/media/d1868488091df20a383528768f3a23a6/href</a></iframe><h4>Explanation</h4><p>If <strong>yes </strong>or <strong>no</strong> button is pressed then we pass true or false to the callback function and call it. After that, we hide the modal.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8923fc82fd6a3ff580b6e1f18678c0df/href">https://medium.com/media/8923fc82fd6a3ff580b6e1f18678c0df/href</a></iframe><p>We can call our confirm box like this wherever we want.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/24b2341a45dcaa0d2bcda364d5030229/href">https://medium.com/media/24b2341a45dcaa0d2bcda364d5030229/href</a></iframe><p>Check out the live demo <a href="https://learnersbucket.com/examples/bootstrap4/custom-confirm-box-with-bootstrap/">here</a>.</p><p>With a custom confirm box, we have complete control over the design and some control over the functionality.</p><p>If you liked this article, please give it some 👏and share it! If you have any questions related to this feel free to ask me.</p><p><em>For more like this and algorithmic solutions in Javascript, follow me on </em><a href="https://twitter.com/LearnersBucket"><strong>Twitter</strong></a><em>. </em>I write about <a href="https://learnersbucket.com/tutorials/es6/es6-intro/">ES6</a>, React, Nodejs, <a href="https://learnersbucket.com/tutorials/topics/data-structures/">Data structures</a>, and <a href="https://learnersbucket.com/examples/topics/algorithms/">Algorithms</a> on <a href="https://learnersbucket.com/"><em>learnersbucket.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=377aa67723c2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/free-code-camp/custom-confirm-box-with-bootstrap-4-377aa67723c2">How to create a custom confirm box with Bootstrap4 and ES6</a> was originally published in <a href="https://medium.com/free-code-camp">We’ve moved to freeCodeCamp.org/news</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to implement a stack in vanilla JavaScript and ES6]]></title>
            <link>https://medium.com/free-code-camp/stack-5404d9735f88?source=rss-55ea03e0926e------2</link>
            <guid isPermaLink="false">https://medium.com/p/5404d9735f88</guid>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[tech]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Prashant Yadav]]></dc:creator>
            <pubDate>Sun, 02 Dec 2018 00:00:00 GMT</pubDate>
            <atom:updated>2019-02-18T16:44:23.484Z</atom:updated>
            <content:encoded><![CDATA[<p>A <strong>stack</strong> is an ordered collection of items that follow the <strong>Last In First Out</strong> (LIFO) principle. The addition and removal of items take place at the same end, i.e. at the top. The newest elements are at the top, and the oldest elements are at the bottom.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*438AQhqI5h98h8JFNSA8Bw.jpeg" /><figcaption>A stack of books: Pixabay</figcaption></figure><p>We have many examples of <strong>stacks</strong> around us like a pile of books, a stack of trays or dishes, etc.</p><p>A <strong>Stack</strong> is used by compilers in programming languages, by computer memory to store variables and function calls, and in text editors to perform undo &amp; redo operations.</p><h4>List of operations performed on Stack</h4><ul><li><strong>push()</strong>: Adds a single or multiple items to the top of the Stack.</li><li><strong>pop()</strong>: Removes and Returns the top item of the Stack.</li><li><strong>peek()</strong>: Returns the top item of the Stack.</li><li><strong>isEmpty()</strong>: Returns True if Stack is empty, False otherwise.</li><li><strong>clear()</strong>: Removes all the items from the Stack.</li><li><strong>size()</strong>: Returns the length of the stack.</li></ul><h3>Creating a Stack</h3><h4>A classical approach</h4><p>We are going to implement a <strong>stack </strong>like how it is implemented in other languages apart from JavaScript.</p><p>We will use an <strong>array </strong>and an <strong>extra variable</strong> to keep track of the items.</p><pre>function Stack(){ <br>  var items = []; <br>  var top = 0; <br>  //other methods go here <br>}</pre><h4>Push an item in the Stack</h4><pre>//Push an item in the Stack<br>this.push = function(element){<br>  items[top++] = element <br>} //top++, first performs the operation then increment&#39;s the value</pre><h4>Pop an item from the Stack</h4><pre>//Pop an item from the Stack<br>this.pop = function(){<br>  return items[--top]; <br>} //--top, first decrements the value then performs the operation</pre><h4>Peek top item from the Stack</h4><pre>//peek an item in the Stack<br>this.peek = function(){ <br>  return items[top - 1]; <br>}</pre><h4>Check if Stack is empty</h4><pre>//Is stack empty<br>this.isEmpty = function(){ <br>  return top === 0; <br>}</pre><h4>Clear the Stack</h4><pre>//clear the stack<br>this.clear= function(){ <br>  top = 0; <br>}</pre><h4>Size of the Stack</h4><pre>//Size of the Stack<br>this.size = function(){ <br>  return top; <br>}</pre><h4>Complete implementation of the Stack</h4><pre>function Stack(){<br> <br>  var items = []; <br>  var top = 0; <br>  //other methods go here </pre><pre>  //Push an item in the Stack <br>  this.push = function(element){ <br>     items[top++] = element <br>  } //top++, first performs the operation then increment&#39;s the value </pre><pre>  //Pop an item from the Stack <br>  this.pop = function(){ <br>     return items[--top]; <br>  } //--top, first decrements the value then performs the operation </pre><pre>  //Peek top item of the Stack <br>  this.peek = function(){ <br>     return items[top - 1]; <br>  } </pre><pre>  //Is Stack empty <br>  this.isEmpty = function(){ <br>    return top === 0; <br>  } </pre><pre>  //Clear the Stack <br>  this.clear = function(){ <br>    top = 0; <br>  } </pre><pre>  //Size of the Stack <br>  this.size = function(){ <br>     return top; <br>  }</pre><pre>}</pre><h4>Example</h4><p>We will now create a new instance of what we have implemented and check if it is working correctly.</p><pre>var stack = new Stack(); //creating new instance of Stack stack.push(1); <br>stack.push(2); <br>stack.push(3); <br>console.log(stack.peek()); <br>console.log(stack.isEmpty()); <br>console.log(stack.size()); <br>console.log(stack.pop()); <br>console.log(stack.size()); <br>stack.clear(); <br>console.log(stack.isEmpty()); </pre><pre>Output: <br>3 <br>false <br>3 <br>3 <br>2 <br>true</pre><h3>Stack implementation with JavaScript</h3><p>We are going to implement a stack with a JavaScript <strong>array</strong> which has inbuilt methods like <strong><em>push</em></strong> and <strong><em>pop.</em></strong></p><pre>function Stack(){ <br>  var items = []; <br>  //other methods go here <br>}</pre><h4>Push an item in the Stack</h4><pre>//push an item in the Stack<br>this.push = function(element){ <br>  items.push(element); <br>}</pre><h4>Pop an item from the Stack</h4><pre>//Pop an item from the Stack<br>this.pop = function(){ <br>  return items.pop(); <br>}</pre><h4>Peek top item from the Stack</h4><pre>//Peek top item of the Stack<br>this.peek = function(){ <br>  return items[items.length - 1]; <br>}</pre><h4>Check if Stack is empty</h4><pre>//Is Stack empty<br>this.isEmpty = function(){ <br>   return items.length === 0; <br>}</pre><h4>Clear the Stack</h4><pre>//Clear the Stack<br>this.clear = function(){ <br>  items.length = 0; <br>}</pre><h4>Size of the Stack</h4><pre>//Size of the Stack<br>this.size = function(){ <br>  return items.length; <br>}</pre><h4>Complete implementation of Stack</h4><pre>function Stack(){ <br>  var items = []; <br>  //other methods go here </pre><pre>  //Push a item in the Stack <br>  this.push = function(element){ <br>     items.push(element); <br>  } </pre><pre>  //Pop a item from the Stack <br>  this.pop = function(){ <br>     return items.pop(); <br>  } </pre><pre>  //Peek top item of the Stack <br>  this.peek = function(){ <br>     return items[items.length - 1]; <br>  }</pre><pre>  //Is Stack empty <br>  this.isEmpty = function(){ <br>     return items.length === 0; <br>  } </pre><pre>  //Clear the Stack <br>  this.clear = function(){ <br>     items.length = 0; <br>  } </pre><pre>  //Size of the Stack <br>  this.size = function(){ <br>     return items.length; <br>  } </pre><pre>}</pre><h4>Making the properties and methods private with closure and <strong>IIFE</strong> (Immediately Invoked Function Expression).</h4><pre>var Stack = (function () { <br>  return function Stack(){ <br>    var items = [];<br>    //other methods go here </pre><pre>    //Push an item in the Stack <br>    this.push = function(element){ <br>       items.push(element); <br>    } </pre><pre>    //Pop an item from the Stack <br>    this.pop = function(){ <br>       return items.pop(); <br>    } </pre><pre>    //Peek top item from the Stack <br>    this.peek = function(){ <br>       return items[items.length - 1]; <br>    } </pre><pre>    //Is Stack empty <br>    this.isEmpty = function(){ <br>        return items.length === 0; <br>    } </pre><pre>    //Clear the Stack <br>    this.clear = function(){ <br>       items.length = 0; <br>    } <br>    <br>    //Size of the Stack this.size = function(){ <br>       return items.length; <br>    } <br>  }<br>})();</pre><h4>Stack using <a href="https://learnersbucket.com/tutorials/es6/es6-intro">ES6</a>.</h4><pre>class Stack{<br>  constructor(){<br>    this.items = [];<br>  }<br>  <br>   //other methods go here<br>   //Push an item in the Stack<br>   push = function(element){<br>     this.items.push(element);<br>   }</pre><pre>//Pop an item from the Stack<br>   pop = function(){<br>     return this.items.pop();<br>   }<br>     <br>   //Peek top item from the Stack<br>   peek = function(){<br>     return this.items[this.items.length - 1];<br>   }</pre><pre>//Is Stack empty<br>   isEmpty = function(){<br>     return this.items.length === 0;<br>   }</pre><pre>//Clear the Stack<br>   clear = function(){<br>      this.items.length = 0;<br>   }<br>     <br>   //Size of the Stack<br>   size = function(){<br>     return this.items.length;<br>   }<br>}</pre><h4>Stack using <a href="https://learnersbucket.com/tutorials/es6/es6-intro">ES6</a> <a href="https://learnersbucket.com/tutorials/es6/maps">WeakMap</a>.</h4><pre>const items = new WeakMap();<br>class Stack{<br>  constructor(){<br>    items.set(this, []);<br>  }<br>  <br>   //other methods go here<br>   //Push an item in the Stack<br>   push = function(element){<br>     let temp = items.get(this);<br>     temp.push(element);<br>   }</pre><pre>//Pop an item from the Stack<br>   pop = function(){<br>     let temp = items.get(this);<br>     return temp.pop();<br>   }<br>     <br>   //Peek top item from the Stack<br>   peek = function(){<br>     let temp = items.get(this);<br>     return temp[temp.length - 1];<br>   }</pre><pre>//Is Stack empty<br>   isEmpty = function(){<br>     let temp = items.get(this);<br>     return temp.length === 0;<br>   }</pre><pre>//Clear the Stack<br>   clear = function(){<br>      let temp = items.get(this);<br>      temp.length = 0;<br>   }<br>     <br>   //Size of the Stack<br>   size = function(){<br>     let temp = items.get(this);<br>     return temp.length;<br>   }<br>}</pre><h4>Making the properties and methods private with closure and IIFE (Immediately Invoked Function Expression) for Stack using <a href="https://learnersbucket.com/tutorials/es6/es6-intro">ES6</a> <a href="https://learnersbucket.com/tutorials/es6/maps">WeakMap</a>.</h4><pre>let Stack = (() =&gt; {<br>  const items = new WeakMap();<br>  return class Stack{<br>    constructor(){<br>      items.set(this, []);<br>    }</pre><pre>//other methods go here<br>     //Push an item in the Stack<br>     push = function(element){<br>       let temp = items.get(this);<br>       temp.push(element);<br>     }</pre><pre>//Pop an item from the Stack<br>     pop = function(){<br>       let temp = items.get(this);<br>       return temp.pop();<br>     }</pre><pre>//Peek top item from the Stack<br>     peek = function(){<br>       let temp = items.get(this);<br>       return temp[temp.length - 1];<br>     }</pre><pre>//Is Stack empty<br>     isEmpty = function(){<br>       let temp = items.get(this);<br>       return temp.length === 0;<br>     }</pre><pre>//Clear the Stack<br>     clear = function(){<br>        let temp = items.get(this);<br>        temp.length = 0;<br>     }</pre><pre>//Size of the Stack<br>     size = function(){<br>       let temp = items.get(this);<br>       return temp.length;<br>     }<br>  }<br>})();</pre><h4>Time Complexity</h4><p><strong><em># Average</em></strong></p><p>Access: Θ(N)</p><p>Search: Θ(N)</p><p>Insert: Θ(1)</p><p>Delete: Θ(1)</p><p><strong><em># Worst</em></strong></p><p>Access: O(N)</p><p>Search: O(N)</p><p>Insert: O(1)</p><p>Delete: O(1)</p><h4>Space Complexity</h4><p><strong><em># Worst</em></strong>: O(N)</p><p>There are lots of problems where Stack can be the perfect data structure needed for the solution.</p><ul><li><a href="https://learnersbucket.com/examples/algorithms/the-base-converter-algorithm">The base converter algorithm</a></li><li><a href="https://learnersbucket.com/examples/algorithms/balanced-parentheses">Balanced Parentheses</a></li></ul><p>If you liked this article, please give it some 👏and share it! If you have any questions related to this feel free to ask me.</p><p><em>For more like this and algorithmic solutions in Javascript, follow me on </em><a href="https://twitter.com/LearnersBucket"><strong>Twitter</strong></a><em>. </em>I write about <a href="https://learnersbucket.com/tutorials/es6/es6-intro/">ES6</a>, React, Nodejs, <a href="https://learnersbucket.com/tutorials/topics/data-structures/">Data structures</a>, and <a href="https://learnersbucket.com/examples/topics/algorithms/">Algorithms</a> on <a href="https://learnersbucket.com/"><em>learnersbucket.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5404d9735f88" width="1" height="1" alt=""><hr><p><a href="https://medium.com/free-code-camp/stack-5404d9735f88">How to implement a stack in vanilla JavaScript and ES6</a> was originally published in <a href="https://medium.com/free-code-camp">We’ve moved to freeCodeCamp.org/news</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>