<?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 Alina Arakelyan on Medium]]></title>
        <description><![CDATA[Stories by Alina Arakelyan on Medium]]></description>
        <link>https://medium.com/@alinaarakelyan?source=rss-257404157a5f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*2g_wLZK4w9kjJ2ML2kG_eA.jpeg</url>
            <title>Stories by Alina Arakelyan on Medium</title>
            <link>https://medium.com/@alinaarakelyan?source=rss-257404157a5f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 05:30:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@alinaarakelyan/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[Algorithm Interview Question: How to reverse an integer using JavaScript]]></title>
            <link>https://medium.com/@alinaarakelyan/algorithm-interview-question-how-to-reverse-an-integer-using-javascript-893eabde7113?source=rss-257404157a5f------2</link>
            <guid isPermaLink="false">https://medium.com/p/893eabde7113</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[reverse]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[engineering]]></category>
            <dc:creator><![CDATA[Alina Arakelyan]]></dc:creator>
            <pubDate>Mon, 02 Mar 2020 03:10:47 GMT</pubDate>
            <atom:updated>2020-03-02T03:10:47.673Z</atom:updated>
            <content:encoded><![CDATA[<p>One common interview question that many Software Engineering applicants receive is the reverse integer question, which states: “Given an integer, return an integer that is the reverse ordering of the numbers”. This seems simple enough, but many beginner programmers often struggle to come up with a complete solution. This blog will go into an in-depth review and breakdown of the answer to this problem.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FJo85Nij8XBKRvY5O00%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Freverse-card-uno-Jo85Nij8XBKRvY5O00&amp;image=https%3A%2F%2Fmedia2.giphy.com%2Fmedia%2FJo85Nij8XBKRvY5O00%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/8241c6b5a4c9fa64257e794f97bf8b62/href">https://medium.com/media/8241c6b5a4c9fa64257e794f97bf8b62/href</a></iframe><p>First, I’m going to present the answer, and then I will break down and explain each aspect of the answer.</p><pre>function reverseInt(n) {</pre><pre>const reverse = parseInt(n.toString().split(&#39;&#39;).reverse().join(&#39;&#39;))</pre><pre>return Math.sign(n) * reverse</pre><pre>}</pre><p>Let’s examine this further.</p><p>Step 1) First, we create a function called reverseInt which takes in an argument of a number (n). While there is no built-in way to reverse a number in JS, we know that .reverse() can be called to reverse the contents of an array. But how would turn an integer into an array? This is a two-step process. Let’s start with the number 531. Let’s create a variable called reverse.</p><p>Step 2) To start this we need to convert the integer into a string, so we start with n.toString() which achieves just that. 531 will become “531” if we call 531.toString() on it.</p><p>Step 3) One we have the integer as a string, we can call .split(‘’) on the string. If you’re not familiar with the .split function in JavaScript, check out this <a href="https://www.w3schools.com/jsref/jsref_split.asp">link</a>. Split has a separator and limit option. If an empty string (“”) is used as the separator, the string is split between each character. It returns an array with the items that were “split”. Now we have an array where each element represent every digit of the starting number. “531” is now [ 5, 3, 1]</p><p>Step 4) Now that we have the integer as an array, we can call .reverse() on the array, which will reverse it. If the array is [5, 3, 1] it will now be [1, 3, 5]. Now that we have it reversed in array form, our next step is to turn it back into a string.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*tzVypcEpsuF2KW3g4QyPCQ.jpeg" /></figure><p>Step 5) In order to turn an array into a string, we will need to call the .join() method on the array with the parameters of an empty string (“”). This will take our array, for example [1, 3, 5] and turn it into a string of “135”.</p><p>Step 6) We’re so close now! We have reversed our string, now we have to turn the string back into an integer. The way we do that is by passing it into parseInt(). Once we call parseInt on the string “135”, our result is the number 135.</p><p>Step 7) This is an essential step since integers can be positive or negative! Math.sign to the rescue! Math.sign() takes an integer, and based on it’s sign, returns either a 1 or a -1. If we pass n as the argument to Math.sign, it will give us a 1 if it’s a positive number or a -1 if it’s a negative number, so all we need to do is multiply the result of Math.sign(n) by the reverse variable integer that we received to get our answer!</p><p>There you have it, an in-depth explanation of a simple and effective method to solve for the reverse integer problem. Happy coding!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F6vj5quVNRhoQw%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fpictures-6vj5quVNRhoQw&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F6vj5quVNRhoQw%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="326" frameborder="0" scrolling="no"><a href="https://medium.com/media/bb78a7e88a55e56b5fdb115da58eab19/href">https://medium.com/media/bb78a7e88a55e56b5fdb115da58eab19/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=893eabde7113" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using Controlled Forms in React]]></title>
            <link>https://medium.com/@alinaarakelyan/using-controlled-forms-in-react-4cca98d37ac1?source=rss-257404157a5f------2</link>
            <guid isPermaLink="false">https://medium.com/p/4cca98d37ac1</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[code]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[forms]]></category>
            <dc:creator><![CDATA[Alina Arakelyan]]></dc:creator>
            <pubDate>Mon, 06 Jan 2020 19:28:52 GMT</pubDate>
            <atom:updated>2020-01-09T21:13:07.596Z</atom:updated>
            <content:encoded><![CDATA[<p>A quick guide for implementing controlled forms in React Apps</p><p>React is a powerful tool for building out fast and responsive web applications. Once you get a handle on the basics of React it can easily become a go-to for front end development, I know it has for me. When I first started out using React, once I learned the proper Parent-Child relationships as well as data flow through state and props, the next step was learning to use forms.</p><p>An important concept when learning programming is the concept of having a single source of truth. This is considered best practice across all languages in programming. When using controlled forms in React, the preferred single source of truth should be the state(as opposed to the DOM, etc.), which will be updated by using setState. This blog will be a brief walkthrough on how we can use state and react to implement a form on our web applications.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aLg1-G2UAlaKpBopRnmCRg.png" /></figure><ol><li>Figure out the parent-child relationships of your form. Find the top-most parent component which the form will be sending the collected data back up to. Inside this component we need to create a function that will hold the logic on how this form will update this parent component (usually by updating state). This can look like this:</li></ol><pre>addNewPost = (evt, postObj) =&gt; {</pre><pre>evt.preventDefault()</pre><pre>this.setState({</pre><pre>posts: [...this.state.posts, postObj]<br>})</pre><pre>}</pre><p>2. You then pass down the function to the child components until you hit Form. In our example, the parent component is App, passing it down to PostList, then into Form. This can look like this:</p><pre>render() {</pre><pre>return (</pre><pre>&lt;aside&gt;</pre><pre>&lt;ul&gt; &lt;NavLink to=&quot;/Posts&quot;&gt;Posts&lt;/NavLink&gt;&lt;/ul&gt;</pre><pre>&lt;Switch&gt;</pre><pre>&lt;Route path=&quot;/Posts&quot;&gt;</pre><pre>&lt;PostList posts={this.state.posts} handleSubmit={this.addNewPost} /&gt;} &gt;</pre><pre>&lt;/Route&gt;</pre><pre>&lt;/Switch&gt;</pre><pre>&lt;/aside&gt;</pre><pre>)</pre><pre>}</pre><p>We are passing down the addNewPost function to PostList as handleSubmit. Under the render of PostList we can add:</p><pre>&lt;Form handleSubmit={this.props.handleSubmit}/&gt;</pre><p>We are now passing handleSubmit down as a prop to Form, which will be where data will be collected and sent back up to App.js through a process called inverse data flow. We need to do this in order to add the new item to be displayed, which is handled in a different child component of App (PostList.js, which is where we map over all the posts and users in state). In React (much like in real life), child components do not “communicate” directly to each-other, but rather send information up to the parent component, which will pass it back down to the appropriate child component.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FfYMPwd8rph8LxyJpYX%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fwar-fruit-cute-food-fYMPwd8rph8LxyJpYX&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FfYMPwd8rph8LxyJpYX%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/40f4f3c3630275df7edbe865ae37c35d/href">https://medium.com/media/40f4f3c3630275df7edbe865ae37c35d/href</a></iframe><p>Before we can call on the handleSubmit prop function to send the information back up to parent, let’s make sure we have our Form component set up properly.</p><p>3. Create a State inside the form. The state of the form will hold the data input that we want to extract from the form and send back up to the parent to update the state of our App. In our example, our form will be taking in two inputs; a user and a post body. The setup will look something like this:</p><pre>state = {</pre><pre>user: &quot;&quot;,</pre><pre>post: &quot;&quot;</pre><pre>}</pre><p>4. Now we can create our form. How your form looks largely depends on what information you need to gather to send back up to your App.js. In our example, I will be collecting the name of a user as well as the contents of the post. My form ended up looking like this:</p><pre>render() {</pre><pre>return &lt;div&gt;{</pre><pre>&lt;form onSubmit={(evt) =&gt; this.props.handleSubmit(evt, this.state)}&gt;</pre><pre>&lt;label&gt; Your name: &lt;/label&gt;&lt;input type=&quot;text&quot; name=&quot;user&quot; value={this.state.user.name} onChange={this.handleInputChange}&gt;&lt;/input&gt;</pre><pre>&lt;label&gt; What&#39;s on your mind?: &lt;/label&gt;&lt;input type=&quot;text&quot; name=&quot;post&quot; value={this.state.post.post} onChange={this.handleInputChange}&gt;&lt;/input&gt;</pre><pre>&lt;button type=&quot;submit&quot; value=&quot;submit&quot;&gt;submit&lt;/button&gt;</pre><pre>&lt;/form&gt;</pre><pre>}&lt;/div&gt;</pre><pre>}</pre><p>Let’s break this code down. We are rendering a form with the following input fields: user and post. The values entered into this form by the user will be used to set the state. The onChange will take the values entered by the user and pass them into the handleInputChange function. When the “Submit” button is clicked the form will be submitted. One the submit is clicked, it will active the onSubmit function, which takes in an argument of the event (submitting of the entire form), and passes both the event and this.state(the state of the Form that we created in step 3) as an argument to be sent back into PostList, and then into App. This is where the inverse data flow starts, but before that happens we need to define a function to setState in Form.</p><p>5. As you can see from the code above, we will have to set up a function to handle the the input change on the form and update our Form’s state. The function from our example is this:</p><pre>handleInputChange = (evt) =&gt; {</pre><pre>const value = evt.target.value;</pre><pre>const name = evt.target.name;</pre><pre>this.setState({</pre><pre>[name]: value</pre><pre>})</pre><pre>}</pre><p>Let’s break this down. We create a function called handleInputChange, which will take in the user’s input and set it as the new value of our state. The user’s input is being passed into the function as (evt) in this example. I have set up the form so that the “name” field matches the fields from my state (“user” and “post”). The value represents what we will be updating the fields in our state to be. I create constants to make our lives easier here, but then we set each constant (name) to the value inputed by the user while filling out the form (value).</p><p>These are the 5 basic steps to follow when creating a controlled form in React. The way we can send information inputted by the user in a child component back up through the parent is through inverse data flow, which is achieved by passing a function down as props, and then sending the form input back up as an argument of that function into the parent component.</p><p>Happy coding!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FJIX9t2j0ZTN9S%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2FJIX9t2j0ZTN9S&amp;image=https%3A%2F%2Fmedia4.giphy.com%2Fmedia%2FJIX9t2j0ZTN9S%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="435" frameborder="0" scrolling="no"><a href="https://medium.com/media/4fdc232ae3bdfcfaf32679f72befd62a/href">https://medium.com/media/4fdc232ae3bdfcfaf32679f72befd62a/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4cca98d37ac1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Bubble Sort Algorithm — Explained]]></title>
            <link>https://medium.com/@alinaarakelyan/the-bubble-sort-algorithm-explained-1fc1895a4447?source=rss-257404157a5f------2</link>
            <guid isPermaLink="false">https://medium.com/p/1fc1895a4447</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[Alina Arakelyan]]></dc:creator>
            <pubDate>Wed, 04 Dec 2019 20:38:47 GMT</pubDate>
            <atom:updated>2019-12-04T20:46:53.719Z</atom:updated>
            <content:encoded><![CDATA[<h3>The Bubble Sort Algorithm — Explained</h3><p>If you’re an entry level programmer, especially one who is switching careers from a different field, having to pass a technical interview before getting your first role is definitely one of the most stressful components. As such you might find yourself looking up sample interview questions, which will lead you to algorithms. Yes, that scary math word reserved exclusively for nerds and geniuses. Or so popular culture has led us to believe. Let’s explore what an algorithm actually is.</p><p>A simple google search will show that algorithm is defined as “a process or set of rules to be followed in calculations or other problem-solving operations, especially by a computer”. With this definition in mind we can that it can be a very complex operation, but it can also be something fairly simple.</p><p>In fact we create algorithms in our heads all the time for computing solutions to everyday problems. For example, you’re at a restaurant and your bill is $100.00 and you want to add a 20% tip for your waiter. Oh no your phone battery is dead, rendering your calculator app unavailable to help you!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FASvQ3A2Q7blzq%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FASvQ3A2Q7blzq%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FASvQ3A2Q7blzq%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="355" frameborder="0" scrolling="no"><a href="https://medium.com/media/028ee4f1ceb0ea8864f7c9effb297258/href">https://medium.com/media/028ee4f1ceb0ea8864f7c9effb297258/href</a></iframe><p>What do you do? You create an algorithm in your head, which would probably looks something like this:<br>tip = bill(.20), total = bill + tip. In the case of our $100 tab, the tip would resolve to be $20, and your total $120.00. Go ahead and breathe a sigh of relief cause algorithms just helped you successfully avert that crisis.</p><p>Now that we know what algorithms are, let’s explore one well known and relatively easy algorithm called “The Bubble Sort”. As you might have inferred from the name it is an algorithm whose function is to sort through an unsorted list. In most cases this list will be an array of random numbers, and the Bubble Sort algorithm will be deployed in order to sort the array numerically.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3RWSwlmS5W8FMutTOdAFmg.jpeg" /><figcaption>Photo by <a href="https://www.pexels.com/@adrien-olichon-1257089?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Adrien Olichon </a>from <a href="https://www.pexels.com/photo/bubbles-floating-during-daytime-2736220/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Pexels</a></figcaption></figure><p>Bubble Sort uses an iteration technique which compares every individual element in that array to the one before it. If it finds that an element is greater than the one before it, then it will “swap” places of the element, and continue onward until the array is sorted in ascending order. Let’s use the following array as an example:</p><pre>someArray = [1,3,2,4]</pre><p>we can then implement the following algorithm to order this array:</p><pre>let bubbleSort = (someArray) =&gt; {<br>    let length = someArray.length;<br>    for (let i = 0; i &lt; length; i++) {<br>        for (let j = 0; j &lt; length; j++) {<br>            if (someArray[j] &gt; someArray[j + 1]) {<br>                let temp = someArray[j];<br>               someArray[j] = someArray[j + 1];<br>                someArray[j + 1] = temp;<br>            }<br>        }<br>    }<br>    return someArray;<br>};</pre><p>Once we run bubbleSort on the unsortedArray the output will be:</p><pre>someArray = [1,2,3,4]</pre><p>The function bubbleSort takes in an argument of someArray. It selects an element [i] from the array and runs a for-loop starting at the index of 0(let i =0), until it reaches the end of the array (represented by i &lt; length), and it will keep incrementing until it’s done (i++). Inside that loop we nest another one with the same parameters but we call the element [j]. This is because we need to iterate through the nested loop, and each time we do the upper loop decreases by one, so we know what have sorted for the index[i]. This is a necessity because every time a change is made in the inner loop, we need to go back to the upper loop and rerun the inner loop again, until we get to the end of the top loop.</p><p>Anytime when I am explaining anything nested, especially two nested things of the same type such as loops, it can get very wordy and hard to follow. Thankfully, coders before me have created a visual representation of this concept of looping through the array, pictured below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/901/1*RfHrEI8NAlfbfhduORRcdg.png" /></figure><p>There are several other methods to solving this, but this is the classic bubble method. One in which the element that will have to be moved “bubbles up” and moves forward within the array. Please note that because the bubble sort method needs to iterate through the array many times it is not recommended for larger data sets.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1fc1895a4447" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Callbacks in JavaScript]]></title>
            <link>https://medium.com/@alinaarakelyan/understanding-callbacks-in-javascript-96f8265c0ac0?source=rss-257404157a5f------2</link>
            <guid isPermaLink="false">https://medium.com/p/96f8265c0ac0</guid>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[callback-function]]></category>
            <category><![CDATA[callback-hell]]></category>
            <category><![CDATA[js]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Alina Arakelyan]]></dc:creator>
            <pubDate>Wed, 20 Nov 2019 15:34:13 GMT</pubDate>
            <atom:updated>2019-11-20T18:31:34.255Z</atom:updated>
            <content:encoded><![CDATA[<p>As someone who is just starting their Front-End journey with JavaScript I know firsthand how callback functions, despite being an essential concept, can often leave students mystified and overwhelmed. I decided to do some more research on callbacks, which led me to a ton of information on functional programming, promises, .then, and even something programmer’s refer to as “Callback Hell”. Nonetheless, JavaScript is a powerful and widely used language, and callbacks are a vital concept that will aid in executing it so it is definitely worth putting in the time to understand it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kTeOHJWyLhInAWW7Qg_9HQ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@mr_vero?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Irvan Smith</a> on <a href="https://unsplash.com/s/photos/javascript?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>First, let’s explore what a “callback function” is. JavaScript has many quirks that separate it from many other popular programming languages. One of those quirks is called “functional programming”. Fundamentally speaking, functional programming involves using functions as an argument of another function. In JavaScript, functions are treated as “first class” objects. First class objects include strings, numbers, and arrays, etc. and can be saved as variables, created inside functions, passed as arguments to functions and returned from functions. When a function is passed into another function it is referred to as a higher-order function, which is just another name for identifying a callback function. It looks something like this:</p><pre>someFunnyVariable.addEventListener(&quot;click&quot;, function(){<br>alert(&quot;Something funny!&quot;)<br>}</pre><p>In the above example a variable, aptly named someFunnyVariable is having a very common JS function, called addEventListener called on it. The addEventListener takes in a first argument of the user’s action, in this example a click. This means that when a User clicks on the variable<em> </em>someFunnyVariable it triggers the callback function, which is represented here as an anonymous function, who’s role is to alert the user with a pop-up showing the words “Something funny!”.</p><p>The reason we need a callback function in this case is because we do not want the alert “Something funny!” popping up when the User load’s the webpage, we want the user to click on someFunnyVariable to trigger that alert to come up. This is because JavaScript is an asynchronous language, and callbacks are a way for programmers to ensure that functions are executed at the proper times, giving the programmer greater control and power in curating the perfect front-end experience for the user.</p><p>In order to ensure that this alert only pop’s up when the someFunnyVariable is triggered, we must put the callback function within the scope of the addEventListener function. It is important to note that when we are passing a callback function we are only passing the function definition. The callback function is not automatically triggered or written with a pair of executing parenthesis (), instead it is simply defined and will only be called back when the triggering event is happened within the callback containing function.</p><p>This is a very powerful tool which allows for a fluid front-end user experience thanks to JavaScript’s functional programming ability. Since callback functions are closures, they have access to the both the variables defined within the scope of the containing function as well as global variables. It is good practice to place the callback functions as the last parameter in a containing function.</p><p>One thing that should be noted is that because of the nature of callbacks, and because they can be anonymous, it is important not to get trapped in a “Callback Hell” or the “Pyramid of Death”, which is a programmer, inception-type, function within a function within a function within a function nightmare. It is very difficult to read, and even harder to debug should something go wrong. It looks something like this, taken from the website Callbackhell.com;</p><pre>fs.readdir(source, <strong>function</strong> (err, files) {<br>  <strong>if</strong> (err) {<br>    console.log(&#39;Error finding files: &#39; <strong>+</strong> err)<br>  } <strong>else</strong> {<br>    files.<strong>forEach</strong>(<strong>function</strong> (filename, fileIndex) {<br>      console.log(filename)<br>      gm(source <strong>+</strong> filename).size(<strong>function</strong> (err, values) {<br>        <strong>if</strong> (err) {<br>          console.log(&#39;Error identifying file size: &#39; <strong>+</strong> err)<br>        } <strong>else</strong> {<br>          console.log(filename <strong>+</strong> &#39; : &#39; <strong>+</strong> values)<br>          aspect <strong>=</strong> (values.width / values.height)<br>          widths.<strong>forEach</strong>(<strong>function</strong> (width, widthIndex) {<br>            height <strong>=</strong> Math.round(width / aspect)<br>            console.log(&#39;resizing &#39; <strong>+</strong> filename <strong>+</strong> &#39;to &#39; <strong>+</strong> height <strong>+</strong> &#39;x&#39; <strong>+</strong> height)<br>            <strong>this</strong>.resize(width, height).write(dest <strong>+</strong> &#39;w&#39; <strong>+</strong> width <strong>+</strong> &#39;_&#39; <strong>+</strong> filename, <strong>function</strong>(err) {<br>              <strong>if</strong> (err) console.log(&#39;Error writing file: &#39; <strong>+</strong> err)<br>            })<br>          }.bind(<strong>this</strong>))<br>        }<br>      })<br>    })<br>  }<br>})</pre><p>Notice how the shape resembles a pyramid, which is why many programmer’s refer to this as the “Pyramid of Death”.</p><p>The best way to avoid doing this would be naming your functions, as well as defining them outside of the containing function, possibly in their own file that we can call upon these callback functions in more than one place. If you just follow good programming conventions and DRY out your code and follow the above two pieces of advice there is no reason to avoid Callback functions for fear of descending into Callback Hell.</p><p>This is an introduction to the concept and necessity of Callbacks, but it should be noted that Callbacks have many other uses and features that give them even more versatility and programming power including promises, which are essential for AJAX and fetch (more on this in a later article).</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F15BuyagtKucHm%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2F15BuyagtKucHm&amp;image=https%3A%2F%2Fmedia4.giphy.com%2Fmedia%2F15BuyagtKucHm%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="246" frameborder="0" scrolling="no"><a href="https://medium.com/media/decb4656bb3f3e99e2db1a1a134072c1/href">https://medium.com/media/decb4656bb3f3e99e2db1a1a134072c1/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=96f8265c0ac0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ruby — Combing two Arrays into a Hash]]></title>
            <link>https://medium.com/@alinaarakelyan/ruby-combing-two-arrays-into-a-hash-3d4f1c6bcf67?source=rss-257404157a5f------2</link>
            <guid isPermaLink="false">https://medium.com/p/3d4f1c6bcf67</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[methods]]></category>
            <category><![CDATA[codingbootcamp]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Alina Arakelyan]]></dc:creator>
            <pubDate>Tue, 29 Oct 2019 22:40:26 GMT</pubDate>
            <atom:updated>2019-10-30T14:38:20.445Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to combine two Arrays into a Hash in Ruby</h3><p>As someone who recently embarked on her coding journey I am constantly coming across shorter and easier methods for simplifying the complicated, newbie code that I have written.</p><p>Just last week while working on a group project (blog post for that coming soon!) my partners and I discovered a method that takes out all of the repetitive work of combing two arrays into a single hash! We were amazed. So amazed that I decided to write a blog post for anyone who is just starting out or an experienced coder and is looking for a way to simplify and DRY out their code.</p><p>Before I dive into the Ruby magic, I’d like to review the “classic” way of combining arrays into a hash. Let’s take these two arrays as examples:</p><pre>wine = [“Pinot Noir”, “Chianti”, “Malbec”]</pre><pre>cheese = [“Gouda”, “Asiago”, “Manchego”]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m5Qx6rtffzgFULuk6kN9uA.jpeg" /></figure><p>Now every wine connoisseur clearly knows that this was setup so that the element at index [0] from the wine array pairs perfectly with index[0] of the cheese array.</p><p>The longer you look at these separate arrays the more one thing becomes clear; we need to combine these two arrays!</p><h3><strong>Creating the combined hash by iterating over each item of the wine array</strong></h3><p>The first (and long) way to combined these two arrays is:</p><pre>wine = [“Pinot Noir”, “Chianti”, “Malbec”]</pre><pre>cheese = [“Gouda”, “Asiago”, “Manchego”]</pre><pre>combined_hash = {}</pre><pre>i = 0</pre><pre>wine.each do |wine_type|</pre><pre>combined_hash[wine_type] = cheese[i]</pre><pre>end</pre><pre>combined_hash</pre><p>Let’s go over this code. Our first step was to create a new hash variable. This is done by setting the name of your new hash (combined_hash) equal to either Hash.new or {}. I usually go with {} because I am a programmer through and through and prefer to always go the route of least typing.</p><p>Next, we set our index (represented here by i) at 0, so that when we reference the arrays we can dynamically specify which element of the array we are using.</p><p>We then call the .each method on our wine array, and select each individual element in the brackets, represented in the code as wine_type.</p><p>In our block we set each individual element, represented by wine_type here, as the KEY of our hash, called combined_hash. We set this key equal to the element at index i of the cheese array. This ensures that each element at index 0 of the wine array is paired with each element at index 0 of the cheese array (Pinot Noir pairs perfectly with Gouda!), etc.</p><p>We call on our combined_hash at the end of our block so that our terminal can display the final combined hash of elements.</p><p>It works, but we had to iterate, set an index counter, and manually create a blank hash. Seems like a lot of work. So how can we simplify this?</p><h3>Using the .zip method from Ruby!</h3><p>The .zip method does some Ruby magic. Let’s demonstrate it first, and then dive deeper into the code.</p><pre>wine = [“Pinot Noir”, “Chianti”, “Malbec”]</pre><pre>cheese = [“Gouda”, “Asiago”, “Manchego”]</pre><pre>combined_hash = Hash[wine.zip(cheese)]</pre><pre>combined_hash</pre><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FsHytgXD2vlNVC%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fwow-deal-with-it-doge-sHytgXD2vlNVC&amp;image=http%3A%2F%2Fgiphygifs.s3.amazonaws.com%2Fmedia%2FsHytgXD2vlNVC%2Fgiphy-facebook_s.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="341" frameborder="0" scrolling="no"><a href="https://medium.com/media/ec2f05d29400d0ea87b5020fb119c04a/href">https://medium.com/media/ec2f05d29400d0ea87b5020fb119c04a/href</a></iframe><p>The .zip method takes out all of the work of iterating over each item of the array and creating the hash and let’s us do everything in one simple line.</p><p>combined_hash = Hash[wine.zip(cheese)]</p><p>We still have to set the variable name of combined_hash and then we make it equal to this syntax necessary for creating a hash using the zip method, which is Hash[key.zip(value)].</p><p>We simply fill in the name of the array we wish to use as the key in the key area inside the brackets, and set the name of the array we wish to use as the value inside the parentheses.</p><p>Just like that we simplified 5 lines of Ruby code into 1 by using the .zip method!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3d4f1c6bcf67" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>