<?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 Joshua Blankenship on Medium]]></title>
        <description><![CDATA[Stories by Joshua Blankenship on Medium]]></description>
        <link>https://medium.com/@joshuablankenshipnola?source=rss-739a6be0e9cf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*CGd6faAJFBoKspOU</url>
            <title>Stories by Joshua Blankenship on Medium</title>
            <link>https://medium.com/@joshuablankenshipnola?source=rss-739a6be0e9cf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 24 May 2026 01:48:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@joshuablankenshipnola/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[Node HTTP Module]]></title>
            <link>https://medium.com/@joshuablankenshipnola/node-http-module-79a903f1c43d?source=rss-739a6be0e9cf------2</link>
            <guid isPermaLink="false">https://medium.com/p/79a903f1c43d</guid>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Joshua Blankenship]]></dc:creator>
            <pubDate>Sun, 15 Jul 2018 22:11:42 GMT</pubDate>
            <atom:updated>2018-07-15T22:11:42.672Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*F1HsOkjF84hUIeq8" /></figure><h3>What is it?</h3><p>It’s a built in Node.js module that allows Node.js to transfer data over the Hyper Text Transfer Protocol (HTTP). If you want to use it all that is needed is this code.</p><blockquote>const http = require(‘http’); //to use HTTP require http</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/917/0*bxDKfJauHG6hjB-5" /><figcaption>Handling Requests</figcaption></figure><h3>Create a server</h3><p>The module can be use to create a server ,which can handle requests and responses. To set it up use the following code.</p><blockquote>//create a server object:</blockquote><blockquote>http.createServer((request, response) =&gt;{</blockquote><blockquote>});//the callback function is called the request handler</blockquote><p>The request handler is called once for every HTTP request that’s made against that serve. The <a href="https://nodejs.org/api/http.html#http_class_http_server">Server</a> object returned by <a href="https://nodejs.org/api/http.html#http_http_createserver_requestlistener">createServer</a> is actually an <a href="https://nodejs.org/api/events.html#events_class_eventemitter">EventEmitter</a>. The code is just shorthand for creating a server object and then adding the listener later. The same server can be created like this.</p><blockquote>//create a server object:</blockquote><blockquote>const server = http.createServer();</blockquote><blockquote>server.on(‘request’,(request, response) =&gt;{</blockquote><blockquote>});</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/0*y-gED2ZGWDkwuZeZ" /></figure><h3>Requests</h3><p>The following code can be used to handle requests from the server.</p><blockquote>http.createServer((request, response) =&gt;{<br>const { headers, method, url } = request; <br>// request.headers, request.method, request.url<br>let body = [];<br> request.on(‘error’, (err) =&gt; { // Check for errors<br> console.error(err);<br> }).on(‘data’, (chunk) =&gt; { // Adds data to the body in chunks<br> body.push(chunk);<br> }).on(‘end’, () =&gt; {<br> body = Buffer.concat(body).toString(); // toString() decodes the data<br> // we have the headers, method, url and body, and can now<br> // do whatever we need to in order to respond to this request.<br> });<br>}).listen(3000); // Activates this server, listening on port 3000.</blockquote><h3>Responses</h3><p>Now let’s set up our responses to the requests.</p><blockquote>response.on(‘error’, (err) =&gt; { // Check for errors<br> console.error(err);<br> });<br>response.writeHead(200, {‘Content-Type’: ‘application/json’});<br>//response.statusCode = 200;<br>//response.setHeader(‘Content-Type’, ‘application/json’);</blockquote><blockquote>const responseBody = { headers, method, url, body };<br>// response.headers, response.method, response.url, response.body<br>response.write(JSON.stringify(responseBody));<br>response.end();</blockquote><h3><strong>Routes</strong></h3><p>Routes for http requests can be set up now. Let’s look at a get and post.</p><p>//GET messages</p><p>if (request.url === ‘/classes/messages’ &amp;&amp; request.method === ‘GET’) {</p><p>statusCode = statusCode || 200; // success code</p><p>response.writeHead(statusCode, headers);</p><p>const obj = { results: database };</p><p>response.end(JSON.stringify(obj));</p><p>}</p><p>// POST a message to the database</p><p>if (request.url === ‘/classes/messages’ &amp;&amp; request.method === ‘POST’) {</p><p>response.writeHead(201, headers); // post code</p><p>const accumulatedData = [];</p><p>request.on(‘data’, function (chunk) {</p><p>accumulatedData.push(chunk);</p><p>}).on(‘end’, function () {</p><p>accumulatedData = Buffer.concat(accumulatedData).toString();</p><p>database.push(JSON.parse(accumulatedData));</p><p>console.log(database);</p><p>});</p><p>response.end(JSON.stringify(database));</p><p>}</p><p>When setting up routes, you will also want to send a 404 code for any routes not on the server.</p><blockquote>// send a 404 status code if the url is not correct<br> if (request.url !== ‘/classes/messages’) {<br> response.writeHead(404, headers);<br> response.end(‘wrong header’);<br> }</blockquote><p>Now you have your server set up, and you can send people to websites on your local host using this route.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=79a903f1c43d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React: Component Lifecycle Events]]></title>
            <link>https://medium.com/@joshuablankenshipnola/react-component-lifecycle-events-cb77e670a093?source=rss-739a6be0e9cf------2</link>
            <guid isPermaLink="false">https://medium.com/p/cb77e670a093</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Joshua Blankenship]]></dc:creator>
            <pubDate>Mon, 09 Jul 2018 05:23:45 GMT</pubDate>
            <atom:updated>2018-07-15T21:52:45.340Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*pqn5ljaOw4kWrUdF" /></figure><h4>What are component lifecycle events?</h4><p>React lets you define components as classes or functions. The methods that you are able to use on these are called lifecycle events. These methods can be called during the lifecycle of a component, and they allow you to update the UI and application states.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0saPKFiTUk6W3FYp" /><figcaption>React Lifecycle Events</figcaption></figure><p>Mounting, Updating, and Unmounting are the three phases of the component lifecycle.</p><h3>Mounting</h3><p>When an instance of a component is being created and inserted into the DOM it occurs during the mounting phase. Constructor, <strong>static getDerivedStateFromProps, render, componentDidMount, </strong>and <strong>UNSAFE_componentWillMount</strong> all occur in this order during mounting.</p><h3>Updating</h3><p>Anytime a component is updated or state changes then it is rerendered. These lifecycle events happen during updating in this order.</p><p><strong>static getDerivedStateFromProps, shouldComponentUpdate, render,<br>getSnapshotBeforeUpdate, componentDidUpdate, UNSAFE_componentWillUpdate, UNSAFE_componentWillReceiveProps</strong></p><h3>Unmounting</h3><p>The final phase of the lifecycle if called when a component is being removed from the DOM. <a href="https://reactjs.org/docs/react-component.html#static-getderivedstatefromprops"><strong>componentWillUnmount</strong></a><strong> </strong>is the only lifecycle event during this phase.</p><h4><a href="https://reactjs.org/docs/react-component.html#constructor">constructor()</a></h4><p>The constructor for a React component is called before it is mounted.If the component is a subclass you should call super(props), or the props will be undefined. constructors can be used to assign state using this.state or to bind event handle methods to an instance. Let’s take a look at some example code.</p><blockquote>class FishTableRow extends React.Component {</blockquote><blockquote>constructor() {</blockquote><blockquote>super(props); //gives us access to props</blockquote><blockquote>//Don’t call this.setState() here</blockquote><blockquote>this.state = { //intitialize local state</blockquote><blockquote>showDescription: false</blockquote><blockquote>}; }</blockquote><p>Avoid using <strong>this.setState()</strong> in the constructor because it can lead to side effects, and it is unnecessary. Doing this will ignore all updates to props, so it shouldn’t be done unless it is intentional.</p><h4><a href="https://reactjs.org/docs/react-component.html#static-getderivedstatefromprops">static getDerivedStateFromProps()</a></h4><p>This method exists for rare cases where the state relies on changes in props over time.</p><h4><a href="https://reactjs.org/docs/react-component.html#render">render()</a></h4><p>Render is the only required method in a class component. It will examine this.props and this.state when called. The render function should not modify the component state because it would cause a lot of bugs by changing the state every time it rerenders. I also should not directly interact with the browser. render will not be invoked if shouldComponentUpdate() returns false. Here is an example of using render.</p><blockquote>ReactDOM.render(</blockquote><blockquote>&lt;FishTable fishes= {fishData}/&gt;,//set fishes document.getElementById(‘app’)</blockquote><blockquote>);</blockquote><h4><a href="https://reactjs.org/docs/react-component.html#componentdidmount">componentDidMount()</a></h4><p>This method is invoked immediately after a component is mounted. If you need to load anything using a network request or initialize the DOM, it should go here. This method is a good place to set up any subscriptions. If you do that, don’t forget to unsubscribe in componentWillUnmount().</p><p>setState() can be called here, but it should be used sparingly, because it will cause a rerender, which can lead to perfomance issues.</p><p>Here we use componentDidMount() to connect to the YouTube API and get videos when the components is rendered.</p><blockquote>componentDidMount() {</blockquote><blockquote>console.log(‘got videos’);</blockquote><blockquote>this.getVideos(‘cats’);</blockquote><blockquote>}</blockquote><blockquote>getVideos(query) {</blockquote><blockquote>var options = {</blockquote><blockquote>key: this.props.YOUTUBE_API_KEY,</blockquote><blockquote>query: query</blockquote><blockquote>};</blockquote><h4><a href="https://reactjs.org/docs/react-component.html#shouldcomponentupdate">shouldComponentUpdate()</a></h4><p>The default behavior in react is to rerender after every state change. Setting shouldComponentUpdate() to false allows you to prevent this from happening. This is in order to optimize performance. If you want to use this method, it may be better to use PureComponent instead, which performs a shallow comparison of props and state. If you do decide to use this method, be sure to check the previous props and state with the current props and state. If shouldComponentUpdate() returns false, then UNSAFE_componentWillUpdate(), render(), and componentDidUpdate() will not be invoked.</p><h4><a href="https://reactjs.org/docs/react-component.html#getsnapshotbeforeupdate">getSnapshotBeforeUpdate()</a></h4><p>This is another rarely used method that allows you to capture a picture of the DOM to check it before actually changing anything on the DOM.</p><h4><a href="https://reactjs.org/docs/react-component.html#getsnapshotbeforeupdate">componentDidUpdate()</a></h4><p>This method is useful for performing network requests after a change has occurred.</p><blockquote>componentDidUpdate(prevProps) {<br> // Typical usage (don’t forget to compare props):<br> if (this.props.userID !== prevProps.userID) {<br> this.fetchData(this.props.userID);<br> }<br>}</blockquote><h4><a href="https://reactjs.org/docs/react-component.html#static-getderivedstatefromprops">componentWillUnmount()</a></h4><p>This method allows you to clean up the DOM and netwrok requests/ subscriptions.</p><h3>UNSAFE Lifecycle Events</h3><h4><a href="https://reactjs.org/docs/react-component.html#unsafe_componentwillmount">UNSAFE_componentWillMount()</a></h4><h4><a href="https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate">UNSAFE_componentWillUpdate()</a></h4><h4><a href="https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops">UNSAFE_componentWillReceiveProps()</a></h4><p>These events have lead to a lot of bugs and unintended side effects, so in React 17 these will no longer be able to be used without the UNSAFE tag in front of them. Instead of componentWillMount use ComponentDidMount.<br>Instead of componentWillReceiveProps use static getDerivedStateFromProps.<br>Instead of componentWillUpdate us getSnapshotBeforeUpdate.</p><h4>Here is a handy chart to help you keep track of the lifecycle events</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/622/1*4y9V5936WdJKaIeVPFEa3w.png" /><figcaption>React Lifecycle Events</figcaption></figure><h4><a href="https://reactjs.org/docs/react-component.html">https://reactjs.org/docs/react-component.html</a></h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cb77e670a093" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Checking For Linked List Cycles in JavaScript]]></title>
            <link>https://medium.com/@joshuablankenshipnola/checking-for-linked-list-cycles-in-javascript-77ec9adc6822?source=rss-739a6be0e9cf------2</link>
            <guid isPermaLink="false">https://medium.com/p/77ec9adc6822</guid>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Joshua Blankenship]]></dc:creator>
            <pubDate>Sat, 30 Jun 2018 22:24:30 GMT</pubDate>
            <atom:updated>2018-06-30T22:24:30.539Z</atom:updated>
            <content:encoded><![CDATA[<p>Using Floyd’s (Tortoise and the Hare) Algorithm</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/0*L8lNtCMF8EQkJeFr" /></figure><h4>What is a Linked List?</h4><p>A linked list is a linear data structure where each element is a separate object usually called a <strong>node</strong>. A <strong>node</strong> is comprised of the data and a reference to the next node. The last node has a reference to null. The <strong>head</strong> of the linked list is not a separate node. It is a reference to the first node. If the list is empty then the head is a null reference. This type of linked list is called a <strong>singly linked list</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/0*oQiQkrMqe4lXMbW4.png" /><figcaption>Singly Linked LIst</figcaption></figure><p>A <strong>doubly linked</strong> list has two references, one to the next node and another to previous node.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/907/0*Qp_A2SF_6n44wGG8.png" /><figcaption>Doubly Linked List</figcaption></figure><h4>What is a Linked List Cycle?</h4><p>A <strong>cycle</strong> occurs when the <strong>linked list</strong> is no longer linear with a beginning and end — instead, it <strong>cycles</strong> through a <strong>loop</strong> of nodes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/347/0*LPBOvzrYalI7_SYG.gif" /></figure><h4>How Can I Check for a Cycle?</h4><p>The problem of checking for a cycle in a linked list may seem intimidating at first, but using Floyd’s Tortoise and Hare algorithm it is easy to understand and solve. The idea is to use a slow pointer named “<strong>tortoise</strong>” and a fast pointer named “<strong>hare</strong>”. The <strong>tortoise </strong>starts at the 0 position and the <strong>hare </strong>starts at the 1 position. If the <strong>tortoise</strong> moves 1 place at a time and the <strong>hare</strong> and 2 places at a time, will they ever meet?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/425/0*diWsQuFLuPxgbZGL.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*23iVw_Un3wQq1e8V.jpg" /></figure><p>Yes. If you draw out each movement, you can see that the place they meet is a cycle.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/780/0*Gb4Howah13JFpMGQ.gif" /><figcaption>Here is a great animation showing this idea.</figcaption></figure><p>Let’s look at some JavaScript code implementing this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/853/1*XtTECjKhdJXqGGjZW8Fbpw.png" /><figcaption>First we will pseudo-code a solution.</figcaption></figure><p>We will create two variables for the slow and fast pointers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/855/1*07hcv08qHVFJXpcLIMDJjg.png" /><figcaption>We have declared our variables</figcaption></figure><p>We will create a while loop to go over each node in the list as long as the <strong>hare</strong> node exists and has a next node. If either the <strong>hare </strong>or the next node are null, then the list does not end in a cycle, so we will return false.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/844/1*AhHXg5sVoJGyQFvoJ2zBaw.png" /><figcaption>Setting up the while loop</figcaption></figure><p>Now we just need to move the <strong>tortoise</strong> and <strong>hare</strong>. If at any time the two pointers are equal, then the linked list contains a cycle.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/840/1*J3iN6l64OKJUn7fK1UhgmQ.png" /><figcaption>Move the tortoise by one node and the hare by two nodes</figcaption></figure><p>Now we have a working solution to check for linked list cycles in JavaScript. I hope that this solution was helpful. More on linked list cycles can be found here.</p><ul><li><a href="https://www.cs.cmu.edu/~adamchik/15-121/lectures/Linked%20Lists/linked%20lists.html">LinkedLists</a></li><li><a href="https://en.wikipedia.org/wiki/Cycle_detection">Cycle detection - Wikipedia</a></li><li><a href="https://cs.stackexchange.com/questions/10360/floyds-cycle-detection-algorithm-determining-the-starting-point-of-cycle">Floyd&#39;s Cycle detection algorithm | Determining the starting point of cycle</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=77ec9adc6822" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Higher Order Functions]]></title>
            <link>https://medium.com/@joshuablankenshipnola/higher-order-functions-c8f8740ae20?source=rss-739a6be0e9cf------2</link>
            <guid isPermaLink="false">https://medium.com/p/c8f8740ae20</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[higher-order-function]]></category>
            <dc:creator><![CDATA[Joshua Blankenship]]></dc:creator>
            <pubDate>Mon, 25 Jun 2018 04:27:06 GMT</pubDate>
            <atom:updated>2018-06-25T04:48:11.336Z</atom:updated>
            <content:encoded><![CDATA[<h4><strong>What is a higher order function?</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/0*NL9I4GqnfYlJ9-St.jpg" /></figure><p>A higher order function is a function that takes a function as an argument(s) and/ or returns a function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/0*394VPqcAEqQ3S93J" /></figure><p>JavaScript has a lot of useful native higher order functions that you can use: <strong>map</strong>, <strong>filter</strong>, <strong>sort</strong>, <strong>reduce</strong> and <strong>bind </strong>are a few.</p><h4><strong>When should I use higher order functions?</strong></h4><p>Higher order functions are useful when you want to customize the behavior of a function or you want to use that function to do the same thing many times.</p><h4><strong>Sort</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/706/1*MX0B2-FO9_fcNhRaemIhYw.png" /><figcaption>Using sort to sort an array of objects by age</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/296/1*mYnXkD2FlHnUEDGaipVJyQ.png" /><figcaption>The ages are returned in an array sorted by age</figcaption></figure><p>The sort functions allows us to choose the order to sort the array of objects. We input an anonymous function as an argument and return person1.age minus person2.age. This returns an array of objects that are now sorted by age. Cool right?</p><p>More information can be found on using <strong>sort </strong>here.</p><p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort">Array.prototype.sort() - JavaScript | MDN</a></p><h4><strong>Why should I use higher order functions?</strong></h4><p>They are easier to read and debug. Using higher order functions will save you a lot of time and typing in the long run.</p><p>Higher order functions are completely customizable, and they let you do cool stuff.</p><h4><strong>Map</strong></h4><p><strong>Map</strong> allows you to input a function as an argument, and it returns a new map containing the values after the function is called. It is useful for performing mathematical operations on each item in an array, or any time you want a new array of values modified by a function. More on <strong>map </strong>can me found here.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/907/1*CB6ycizn8zczi15aU73-6g.png" /><figcaption>Here is an example of using <strong>map</strong></figcaption></figure><p>Here map takes every item in the array, performs a function on it and returns a new array of the results.</p><p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map">Array.prototype.map() - JavaScript | MDN</a></p><h4><strong>Higher Order functions are completely customizable</strong></h4><p>You can create a higher order function any time to do anything. Reduce is one of the most useful higher order functions.</p><h4><strong>Reduce</strong></h4><p>Reduce can be customized to do just about anything, which makes it one of the most useful higher order functions. Here I use <strong>reduce</strong> to flatten nested arrays.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C20AGpI42HCxUBlaGICKkg.png" /><figcaption>using reduce to flatten an array</figcaption></figure><p>Here I use reduce to sum an array of numbers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*20-RgH9XctpXQ4MLBkUdlg.png" /><figcaption>Using reduce to sum an array of numbers</figcaption></figure><p>More on using reduce can be found here.</p><ul><li><a href="https://medium.freecodecamp.org/reduce-f47a7da511a9">A Guide To The Reduce Method In Javascript​</a></li><li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce">Array.prototype.reduce() - JavaScript | MDN</a></li></ul><p>There are also a lot of great libraries out there with useful higher order functions. Underscore and Lodash are pretty useful libraries that I encourage you to explore. You can always write your own high order functions, and make a library to use of your own.</p><ul><li><a href="http://underscorejs.org/">Underscore.js</a></li><li><a href="https://lodash.com/">Lodash</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c8f8740ae20" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>