<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: reyes2981</title>
    <description>The latest articles on DEV Community by reyes2981 (@reyes2981).</description>
    <link>https://dev.to/reyes2981</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F540241%2Fb78e6c34-e270-4b1a-b533-04ebea62104a.jpg</url>
      <title>DEV Community: reyes2981</title>
      <link>https://dev.to/reyes2981</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/reyes2981"/>
    <language>en</language>
    <item>
      <title>what i learned last week: intro to test driven development</title>
      <dc:creator>reyes2981</dc:creator>
      <pubDate>Mon, 14 Mar 2022 16:29:42 +0000</pubDate>
      <link>https://dev.to/reyes2981/what-i-learned-last-week-intro-to-test-driven-development-jm4</link>
      <guid>https://dev.to/reyes2981/what-i-learned-last-week-intro-to-test-driven-development-jm4</guid>
      <description>&lt;p&gt;&lt;em&gt;Prerequisite: Angular.js&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Throughout the past several years, as I've launched my developer career, the one practice that has seemed daunting to me is Test Driven Development. It mostly stems from the fact that up until my time at General Assembly I never implemented it into my codebase. That has all changed as I prep for an upcoming interview that will go over TDD. So to practice I've decided to dedicate this weeks entry to this feared and misunderstood practice. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/IzRWsOco0Zp7woExht/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/IzRWsOco0Zp7woExht/giphy.gif" width="480" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;WHAT?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So what is Test Driven Development?&lt;/p&gt;

&lt;p&gt;Per &lt;a href="https://en.wikipedia.org/wiki/Test-driven_development"&gt;Wikipedia&lt;/a&gt;, Test-driven development (TDD) is a software development process relying on software requirements being converted to test cases before software is fully developed, and tracking all software development by repeatedly testing the software against all test cases. This is as opposed to software being developed first and test cases created later.&lt;/p&gt;

&lt;p&gt;Okay, but what does that mean?&lt;/p&gt;

&lt;p&gt;In other &lt;a href="https://www.guru99.com/test-driven-development.html"&gt;words&lt;/a&gt;, test cases for each functionality, of a program, are created and tested first and if the test fails then the new code is written in order to pass the test and making code simple and bug-free.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lUcOkvd4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2h6afzaf3lb9t6pcbi4j.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lUcOkvd4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2h6afzaf3lb9t6pcbi4j.jpg" alt="tdd flow graph" width="380" height="732"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;HOW?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--o2J-Ls0P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t42n3tws3xe1dx7iazzt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--o2J-Ls0P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t42n3tws3xe1dx7iazzt.png" alt="tdd steps" width="276" height="183"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So how exactly does test driven development &lt;a href="https://developer.ibm.com/articles/5-steps-of-test-driven-development/"&gt;work&lt;/a&gt;? &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Read, understand, and process the feature or bug request.&lt;/li&gt;
&lt;li&gt;Translate the requirement by writing a unit test. If you have hot reloading set up, the unit test will run and fail as no code is implemented yet.&lt;/li&gt;
&lt;li&gt;Write and implement the code that fulfills the requirement. Run all tests and they should pass, if not repeat this step.&lt;/li&gt;
&lt;li&gt;Clean up your code by refactoring.&lt;/li&gt;
&lt;li&gt;Rinse, lather and repeat.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;Note: In Angular, there are two main types of tests.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;A &lt;a href="https://smartbear.com/learn/automated-testing/what-is-unit-testing/#:~:text=A%20unit%20test%20is%20a,of%20the%20definition%20is%20important."&gt;unit test&lt;/a&gt; is a way of testing a unit - the smallest piece of code that can be logically isolated in a system. In most programming languages, that is a function, a subroutine, a method or property.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;a href="https://medium.com/swlh/angular-end-to-end-testing-with-protractor-55897de591be"&gt;End to end&lt;/a&gt; testing is the process of validating an application’s workflow from the beginning to the end. This testing methodology is aimed at replicating real user scenarios, so that the system can be validated for overall functionality.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;TDD EXAMPLE: &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: I'm going to being using Jasmine to test out my code. &lt;a href="https://www.digitalocean.com/community/tutorials/testing-angular-with-jasmine-and-karma-part-1"&gt;Jasmine&lt;/a&gt; is a behavior-driven development framework for testing JavaScript code that plays very well with Karma. Similar to Karma, it’s also the recommended testing framework within the Angular documentation as it’s setup for you with the Angular CLI. Jasmine is also dependency free and doesn’t require a DOM.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Lets get to it, in the terminal I'm going to create a new Angular project by running the following command. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;ng new zoo&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next, I'm going to create a new component named "zoo".&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ng generate component zoo&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---PdVJASa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/71zdevt169683b4np3d0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---PdVJASa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/71zdevt169683b4np3d0.png" alt="test4" width="503" height="155"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that my new Angular project has been created I'm going to head over to my terminal again and run the following command.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ng test&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The terminal output will look like the below screenshot. &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kIRbjGbD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kl2q2tzf2nws8hhtkpu6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kIRbjGbD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kl2q2tzf2nws8hhtkpu6.png" alt="test2" width="880" height="821"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also, a browser window will open up letting you know all of the initial tests are passing. Neat, huh?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9ThN6WPn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1vvm5ku6cpksjtin9vbi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9ThN6WPn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1vvm5ku6cpksjtin9vbi.png" alt="test5" width="826" height="1020"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Per the &lt;strong&gt;first step&lt;/strong&gt;, I need to figure out what new feature I want to build out. In this case, I want my zoo component to have a name.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Second&lt;/strong&gt;, I need to translate the requirement by writing a unit test.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;zoo.component.spec.ts&lt;/code&gt; write the following test.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; it('should have a name', () =&amp;gt; {
    expect(component.zooName).toBeTruthy();
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You'll notice that the test fails because no code has been implemented.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FtsFKQjw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ooqwa8ha1yych1akhrq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FtsFKQjw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ooqwa8ha1yych1akhrq.png" alt="test6" width="807" height="394"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step three&lt;/strong&gt; states that I need to now implement code that makes the test pass. How would I do that in this example? I simply need to create a new property in the zoo component called "named" and add a value to it. &lt;/p&gt;

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

&lt;p&gt;Sweet! All of our tests are now passing!&lt;/p&gt;

&lt;p&gt;This was a simple example and don't need to refactor but next we would usually want to move onto &lt;strong&gt;step four&lt;/strong&gt; and refactor. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Voila! You've implemented your first application feature utilizing TDD!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;WHY?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Test Driven Development gives developers the confidence that our code will work 100% of the time in the expected conditions. &lt;/p&gt;

&lt;p&gt;TDD provides several &lt;a href="https://www.ibm.com/garage/method/practices/code/practice_test_driven_development/"&gt;benefits&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;It can enable faster innovation and continuous delivery because the code is robust.&lt;/p&gt;

&lt;p&gt;It makes your code flexible and extensible. The code can be refactored or moved with minimal risk of breaking code.&lt;/p&gt;

&lt;p&gt;The tests themselves were tested. A key characteristic of a test is that it can fail, and the development team verifies that each new test fails.&lt;/p&gt;

&lt;p&gt;The code that is produced is, by design, easy to test.&lt;/p&gt;

&lt;p&gt;The requirements are implemented with little to no wasted effort because only the function that is needed is written.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>tdd</category>
    </item>
    <item>
      <title>what I learned last week: merge sort algorithm</title>
      <dc:creator>reyes2981</dc:creator>
      <pubDate>Mon, 07 Mar 2022 21:38:42 +0000</pubDate>
      <link>https://dev.to/reyes2981/what-i-learned-last-week-merge-sort-algorithm-1jb9</link>
      <guid>https://dev.to/reyes2981/what-i-learned-last-week-merge-sort-algorithm-1jb9</guid>
      <description>&lt;p&gt;I know it's been several weeks since I last posted an entry but I'm happy to be back after successfully completing General Assembly's Java Immersive Program! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/l0amJzVHIAfl7jMDos/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/l0amJzVHIAfl7jMDos/giphy.gif" width="480" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Prerequisite: &lt;a href="https://www.geeksforgeeks.org/recursion/"&gt;Recursion&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lets get right to it. In this entry I'm going to go over Merge Sort which is a &lt;a href="https://www.geeksforgeeks.org/divide-and-conquer-algorithm-introduction/"&gt;Divide and Conquer&lt;/a&gt; algorithm.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IFS-E_Ku--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g39i4e9bwgv1hdqckmig.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IFS-E_Ku--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g39i4e9bwgv1hdqckmig.png" alt="merge sort example" width="592" height="327"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Remember, the divide and conquer technique can be divided into three parts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Divide: This involves dividing the problem into smaller sub-problems.&lt;/li&gt;
&lt;li&gt;Conquer: Solve sub-problems by calling recursively until solved.&lt;/li&gt;
&lt;li&gt;Combine: Combine the sub-problems to get the final solution of the whole problem.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So, let's say you are asked to sort the unordered array below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tkwYFb1p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1gtlmupq7scpfhtrinxj.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tkwYFb1p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1gtlmupq7scpfhtrinxj.jpg" alt="unordered array" width="391" height="56"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How would you achieve this and return this ordered array? One way, would be utilizing Merge Sort.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--48YL1-1D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qexjwx321p60w510go4d.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--48YL1-1D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qexjwx321p60w510go4d.jpg" alt="ordered array" width="391" height="56"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first thing, we want to do is divide the unordered array into halves.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Gxu1QM19--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ijq0uoak57q12g3wbqlr.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Gxu1QM19--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ijq0uoak57q12g3wbqlr.jpg" alt="step 1" width="415" height="55"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, I divide these two arrays into halves.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--z-Agbatz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uds9zupazg3ajbi5bzp0.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--z-Agbatz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uds9zupazg3ajbi5bzp0.jpg" alt="step 2" width="463" height="56"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We further divide these arrays until we reach &lt;a href="https://www.ibm.com/docs/en/db2/11.5?topic=model-atomic-values"&gt;atomic value&lt;/a&gt; and the array can no longer be divided . &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SS24MBe6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3o79v2h6ebnx70uv34c4.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SS24MBe6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3o79v2h6ebnx70uv34c4.jpg" alt="step 3" width="566" height="55"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, I will combine them in a sorted manner. &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hWh3ZXF7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eyx2izr716knu5f0tkhs.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hWh3ZXF7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eyx2izr716knu5f0tkhs.jpg" alt="step 4" width="461" height="56"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, I continue combining the arrays in a sorted fashion. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KOmJIoxH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yu74kw0zwnd7rfriwl5p.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KOmJIoxH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yu74kw0zwnd7rfriwl5p.jpg" alt="step 5" width="417" height="56"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The final merge will look like this.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WHwv6kPg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/znpmnju4iti5i9jrgjxy.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WHwv6kPg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/znpmnju4iti5i9jrgjxy.jpg" alt="step 6" width="391" height="56"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You're probably asking yourself "how do I code the above algorithm?" First step, we need to write some pseudocode. &lt;/p&gt;

&lt;p&gt;In computer science, &lt;a href="https://en.wikipedia.org/wiki/Pseudocode"&gt;pseudocode&lt;/a&gt; is a plain language description of the steps in an algorithm or another system. Pseudocode often uses structural conventions of a normal programming language, but is intended for human reading rather than machine reading.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Example Pseudocode

//array of elements
let arr = []

mergeSort(arr) {

//variable that stores number of elements in the array
n &amp;lt;-- array.length 

//cut the array into two halves
if(n &amp;lt; 2) {
    return n
  }

mid &amp;lt;-- n/2
left &amp;lt;-- array of size(mid)
right &amp;lt;-- array of size(n-mid)

for(i &amp;lt;- 0 to mid - 1) {
    left[i] &amp;lt;- arr[i]
  }

for(i &amp;lt;- mid to n - 1) {
    right[i - mid] &amp;lt;-- arr[i]
  }

//recursion
mergeSort(left)
mergeSort(right)
Merge(left, right, arr)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=x_Z9FcAPmbk"&gt;Justin Kim&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Code Implementation in JavaScript

//helper method
const merge = (leftArr, rightArr) =&amp;gt; {
    const output = [];
    let leftIndex = 0;
    let rightIndex = 0;

    while (leftIndex &amp;lt; leftArr.length &amp;amp;&amp;amp; rightIndex &amp;lt; rightArr.length) {
        const leftEl = leftArr[leftIndex];
        const rightEl = rightArr[rightIndex];

        if (leftEl &amp;lt; rightEl) {
            output.push(leftEl);
            leftIndex++;
        } else {
            output.push(rightEl);
            rightIndex++;
        }
    }
    return [...output, ...leftArr.slice(leftIndex), ...rightArr.slice(rightIndex)];
};

//console.log(merge([3, 6], [2, 4]));

//recursive function
const mergeSort = arr =&amp;gt; {
    if (arr.length &amp;lt;= 1) {
        return arr;
    }

    const middleIndex = Math.floor(arr.length / 2);
    const leftArr = arr.slice(0, middleIndex);
    const rightArr = arr.slice(middleIndex);

    return merge(
        mergeSort(leftArr),
        mergeSort(rightArr)
    );
};


console.log(mergeSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if you run the program, you will see a newly sorted array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ node app.js
[
    1,    1,   2,   2,   4,
    8,   32,  43,  43,  55,
   63,   92, 123, 123, 234,
  345, 5643
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In conclusion, here is a group of folk dancers explaining Merge Sort. Yes, you read that right. &lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/XaqR3G_NVoo"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;resources&lt;br&gt;
&lt;a href="https://www.tutorialspoint.com/data_structures_algorithms/merge_sort_algorithm.html"&gt;https://www.tutorialspoint.com/data_structures_algorithms/merge_sort_algorithm.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=TzeBrDU-JaY&amp;amp;t=69s"&gt;https://www.youtube.com/watch?v=TzeBrDU-JaY&amp;amp;t=69s&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=x_Z9FcAPmbk"&gt;https://www.youtube.com/watch?v=x_Z9FcAPmbk&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>what i learned last week: how to build tic tac toe using html, css and javascript</title>
      <dc:creator>reyes2981</dc:creator>
      <pubDate>Mon, 10 Jan 2022 14:14:09 +0000</pubDate>
      <link>https://dev.to/reyes2981/what-i-learned-last-week-how-to-build-tic-tac-toe-using-html-css-and-javascript-33gg</link>
      <guid>https://dev.to/reyes2981/what-i-learned-last-week-how-to-build-tic-tac-toe-using-html-css-and-javascript-33gg</guid>
      <description>&lt;p&gt;Good Morning! It's a snowy Sunday here in Chicago and today I'm going to build the game tic tac toe. In the process of building this program I will provide a high level overview of HTML, CSS and JavaScript. &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg2i1ii5zqtbncp9v4mwv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg2i1ii5zqtbncp9v4mwv.png" alt="tic tac toe"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  HTML
&lt;/h1&gt;

&lt;p&gt;The first thing I'm going to do is create an &lt;code&gt;index.html&lt;/code&gt; file. Remember, &lt;a href="https://www.w3schools.com/html/html_intro.asp" rel="noopener noreferrer"&gt;HTML&lt;/a&gt; is the standard markup language for creating Web pages. I like to think of it as the "skeleton" of a website. &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0idanl4x8gvpojfk3efb.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0idanl4x8gvpojfk3efb.jpg" alt="html skeleton"&gt;&lt;/a&gt; Next, I want to start writing code in the newly created &lt;code&gt;index.html&lt;/code&gt; file. The &lt;a href="https://en.wikipedia.org/wiki/Boilerplate_code" rel="noopener noreferrer"&gt;boilerplate code&lt;/a&gt; for an &lt;code&gt;index.html&lt;/code&gt; file should look something like this...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;
    &amp;lt;title&amp;gt;Document&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;

&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML" rel="noopener noreferrer"&gt;HTML (HyperText Markup Language)&lt;/a&gt; is the most basic building block of the Web. It defines the meaning and structure of web content.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8o4q3jqixkpj1iexvtps.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8o4q3jqixkpj1iexvtps.gif" alt="element tree"&gt;&lt;/a&gt; Next, utilizing &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;'s, I'm going to build the tic tac toe board. The &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; &lt;a href="https://www.w3schools.com/tags/tag_div.ASP" rel="noopener noreferrer"&gt;tag&lt;/a&gt; defines a division or a section in an HTML document. It's also used as a container for HTML elements - which is then styled with CSS or manipulated with JavaScript.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;
    &amp;lt;title&amp;gt;Document&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;!-- Tic Tac Toe Board --&amp;gt;
    &amp;lt;div id="board-container"&amp;gt;
      &amp;lt;div data-index="1" class="box"&amp;gt;&amp;lt;/div&amp;gt;
      &amp;lt;div data-index="2" class="box"&amp;gt;&amp;lt;/div&amp;gt;
      &amp;lt;div data-index="3" class="box"&amp;gt;&amp;lt;/div&amp;gt;
      &amp;lt;div data-index="4" class="box"&amp;gt;&amp;lt;/div&amp;gt;
      &amp;lt;div data-index="5" class="box"&amp;gt;&amp;lt;/div&amp;gt;
      &amp;lt;div data-index="6" class="box"&amp;gt;&amp;lt;/div&amp;gt;
      &amp;lt;div data-index="7" class="box"&amp;gt;&amp;lt;/div&amp;gt;
      &amp;lt;div data-index="8" class="box"&amp;gt;&amp;lt;/div&amp;gt;
      &amp;lt;div data-index="9" class="box"&amp;gt;&amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I'm making good progress! So far, I've created an &lt;code&gt;index.html&lt;/code&gt; file and added &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;'s that represent a tic tac toe board. Notice that I've created a div with an &lt;a href="https://www.w3schools.com/html/html_id.asp" rel="noopener noreferrer"&gt;id&lt;/a&gt; of &lt;code&gt;board-container&lt;/code&gt;. The HTML id attribute is used to specify a unique id for an HTML element. Nested inside of the &lt;code&gt;#board-container&lt;/code&gt; div there are 9 separate, child, &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;'s that represent a cell in the tic tac toe board. Remember, an HTML document &lt;a href="https://marksheet.io/html-hierarchy.html" rel="noopener noreferrer"&gt;is&lt;/a&gt; like a big family tree, with parents, siblings, children, ancestors, and descendants. I'm about to dive into CSS and will need to somehow reference these newly created children elements so I'm adding a class of &lt;code&gt;box&lt;/code&gt; to each individual &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  CSS
&lt;/h1&gt;

&lt;p&gt;Next, I want to see this newly created grid so let's build and display it to the client side using CSS. If HTML is the skeleton of a website than CSS is the clothes that dress up a person. &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxm3d4jvyc4x3xk2x9cbv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxm3d4jvyc4x3xk2x9cbv.png" alt="CSS"&gt;&lt;/a&gt;&lt;br&gt;
Below, is a screenshot of my completed tic tac toe game - designed with CSS.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fju3mm0mfpkp6v36tvyax.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fju3mm0mfpkp6v36tvyax.png" alt="tictactoe1"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#board-container {
  width: 300px;
  height: 300px;
  margin: 0 auto;
  background-color: #e9c46a;
  color: #fff;
  border: 6px solid #e9c46a;
  border-radius: 10px;
  display: grid;
  grid-template: repeat(3, 1fr) / repeat(3, 1fr);
  position: relative;
  top: 30%;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note, I'm able to style a div by referencing it's id attribute. The same thing can also be done with the class attribute.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3ek34ccfnjomwb1ac5pe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3ek34ccfnjomwb1ac5pe.png" alt="css syntax"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  JavaScript
&lt;/h1&gt;

&lt;p&gt;Below is part of the code used to build and store the logic of my program.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.addEventListener("DOMContentLoaded", () =&amp;gt; {
    console.log("DOM Content Loaded");
    //RETREIVE HTML ELEMENTS
    const boxes = document.querySelectorAll(".box");
    const strikethrough = document.getElementById("strikethrough");
    const gameOverArea = document.getElementById("game-over-area");
    const gameOverText = document.getElementById("game-over-text");
    const playAgain = document.getElementById("play-again");
    const playerxDiv = document.getElementById("player-X");
    const playeroDiv = document.getElementById("player-O");
    //CREATE PLAYERS, ARRAY THAT REPRESENTS BOARD AND WINNING COMBOS
    const playerX = "X";
    const playerO = "O";
    const boardArray = Array(boxes.length); //TRACKS STATE OF PROGRAM - CREATED AN ARRAY WHICH HAS 9 ITEMS(BOXES)
    const winningCombos = [
        //ROWS
        {  // KEY   VALUE
            combo: [0, 1, 2],
            strikeClass: "strike-row-1"
        },
        {
            combo: [3, 4, 5],
            strikeClass: "strike-row-2"
        },
        {
            combo: [6, 7, 8],
            strikeClass: "strike-row-3"
        },
        //COLUMNS
        {
            combo: [0, 3, 6],
            strikeClass: "strike-column-1"
        },
        {
            combo: [1, 4, 7],
            strikeClass: "strike-column-2"
        },
        {
            combo: [2, 5, 8],
            strikeClass: "strike-column-3"
        },
        //DIAGONALS
        {
            combo: [0, 4, 8],
            strikeClass: "strike-diagonal-1"
        },
        {
            combo: [2, 4, 6],
            strikeClass: "strike-diagonal-2"
        },
    ];
    let turn = playerX; //TRACKS WHO'S TURN IT IS

    playAgain.addEventListener("click", restartGame);

    //THE FILL() METHOD CHANGES ALL ELEMENTS IN AN ARRAY TO A STATIC VALUE, FROM A START INDEX (DEFAULT 0) TO 
    //AN END INDEX (DEFAULT ARRAY.LENGTH). IT RETURNS THE MODIFIED ARRAY
    boardArray.fill(null);

    playerxDiv.append(playerX);
    playeroDiv.append(playerO);


    boxState();
    //FUNCTION THAT ITERATATES OVER LIST OF BOXES AND ADDS AN EVENT LISTENER TO EACH INDIVIDUAL BOX
    function boxState() {
        boxes.forEach((box) =&amp;gt; {
            box.addEventListener("click", boxClick); //FUNCTION WILL BE CALLED WHENEVER THE SPECIFIED EVENT IS DELIVERED TO THE TARGET
        })
    }

    function boxClick(e) {
        const box = e.target; //REFERENCE TO THE HTML ELEMENT CLICKED
        const boxNumber = box.dataset.index;//ACCESS DATA-INDEX ATTRIBUTE
        //IF GAMEOVER AREA CLASSLIST CONTAINNS "VISIBLE" THE FUNCTION WILL STOP EXECUTING
        if (gameOverArea.classList.contains("visible")) {
            return;
        }
        //CHECK IF SPECIFIC BOX IS EMPTY OR HAS AN "X" OR "O" IN IT
        if (box.innerText != "") {
            return;
        }
        if (turn === playerX) {
            box.innerText = playerX;
            boardArray[boxNumber - 1] = playerX; //ARRAYS ARE ZERO INDEXED SO I NEED TO SUBTRACT 1 FROM PLAYERS CHOICE FOR GAME TO BE ACCURATE
            turn = playerO;
            console.log(`It's ${playeroDiv.innerText}'s turn`);
        } else {
            box.innerText = playerO;
            boardArray[boxNumber - 1] = playerO;
            turn = playerX;
            console.log(`It's ${playerxDiv.innerText}'s turn`);
        }

        //FUNCTION IS CALLED ON EVERY SINGLE BOX CLICK - CHECKS IF THERE IS A WINNER
        checkWinner()
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Deployed Program: &lt;a href="https://clever-noether-f61a54.netlify.app/" rel="noopener noreferrer"&gt;https://clever-noether-f61a54.netlify.app/&lt;/a&gt;&lt;br&gt;
Github: &lt;a href="https://github.com/reyes2981/project1" rel="noopener noreferrer"&gt;https://github.com/reyes2981/project1&lt;/a&gt;&lt;/p&gt;

</description>
      <category>html</category>
      <category>css</category>
      <category>javascript</category>
    </item>
    <item>
      <title>what i learned last week: if/else statements </title>
      <dc:creator>reyes2981</dc:creator>
      <pubDate>Tue, 14 Dec 2021 18:56:47 +0000</pubDate>
      <link>https://dev.to/reyes2981/what-i-learned-last-week-ifelse-statements-5596</link>
      <guid>https://dev.to/reyes2981/what-i-learned-last-week-ifelse-statements-5596</guid>
      <description>&lt;p&gt;I'm happy to share that I've officially completed week one of General Assembly's Java Immersive program. We dove into the command line, HTML/CSS and JavaScript. I haven't written in about two months so lets get right to it! I'll be going over &lt;code&gt;if/else&lt;/code&gt; statements to solidify my knowledge in JavaScript.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;if&lt;/code&gt; statement &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else" rel="noopener noreferrer"&gt;executes&lt;/a&gt; a statement if a specified condition is truthy. If the condition is falsy, another statement can be executed.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;if () {}&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Let's dive into the above piece of code. Within the parentheses you'll need to write a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#expressions" rel="noopener noreferrer"&gt;conditional expression&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;if (condition) {&lt;br&gt;
statement1&lt;br&gt;
}&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;First, I've declared a &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Variable" rel="noopener noreferrer"&gt;variable&lt;/a&gt; and stored the number &lt;code&gt;400&lt;/code&gt; inside of it. Next, I created an &lt;code&gt;if&lt;/code&gt; statement and added a condition inside of the parentheses.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let x = 400&lt;br&gt;
if (x &amp;gt; 50) {&lt;br&gt;
console.log("I'm larger than the number 50!");&lt;br&gt;
}&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Within the curly brackets I've gone ahead and created a statement. In this case I've written code that will show in the console &lt;strong&gt;ONLY&lt;/strong&gt; if the condition returns a &lt;code&gt;truthy&lt;/code&gt; value. &lt;/p&gt;

&lt;p&gt;Remember, a &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Truthy" rel="noopener noreferrer"&gt;truthy&lt;/a&gt; value is a value that is considered true when encountered in a Boolean context. See below for the group of values that are considered &lt;code&gt;falsy&lt;/code&gt; in JavaScript. Note, all values outside of the group below are considered &lt;code&gt;truthy&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9zj8ylt6dr2bug6qfp56.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9zj8ylt6dr2bug6qfp56.png" alt="falsy"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What do you think will be the output when I run the above block of code. To confirm let's head over to the browser console. &lt;/p&gt;

&lt;p&gt;INPUT&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let x = 400&lt;br&gt;
if (x &amp;gt; 50) {&lt;br&gt;
console.log("I'm larger than the number 50!");&lt;br&gt;
}&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;OUTPUT&lt;br&gt;
&lt;code&gt;I'm larger than the number 50!&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Since &lt;code&gt;400&lt;/code&gt; is larger than &lt;code&gt;50&lt;/code&gt; the statement is displayed in the console. What if I want to display a different output if the number is less than 50? How would I set that up? In this case we can add an &lt;code&gt;else&lt;/code&gt; statement to the above code.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let x = 41&lt;br&gt;
if (x &amp;gt; 50) {&lt;br&gt;
console.log("I'm larger than the number 50!");&lt;br&gt;
} else {&lt;br&gt;
console.log("I'm smaller than the number 50!");&lt;br&gt;
}&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Output&lt;/p&gt;

&lt;p&gt;&lt;code&gt;I'm smaller than the number 50!&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The reason why we get the above output is because when the first condition was executed it resolved as &lt;code&gt;falsy&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Let's check out a real world example.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;function startAdventure() {&lt;br&gt;
    let local = prompt('Captian Picard has a mission for you in the Delta Quadrant. Do you take along riker or data?')&lt;br&gt;
    if (local === "riker") {&lt;br&gt;
        riker();&lt;br&gt;
    }  else if (local === "data") {&lt;br&gt;
        data();&lt;br&gt;
    } &lt;br&gt;
}&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Above I have a function that allows the client to navigate prompts depending on their response. &lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>intro to react pt. 1</title>
      <dc:creator>reyes2981</dc:creator>
      <pubDate>Wed, 06 Oct 2021 15:41:59 +0000</pubDate>
      <link>https://dev.to/reyes2981/intro-to-react-pt-1-2ag1</link>
      <guid>https://dev.to/reyes2981/intro-to-react-pt-1-2ag1</guid>
      <description>&lt;p&gt;Welcome to this weeks blog entry where I'll provide you a high level introduction to React by creating a program. Lets get to it! &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;WHAT IS REACT?&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---6L9jU55--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gxyyu56rb1xbnl8y4w5v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---6L9jU55--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gxyyu56rb1xbnl8y4w5v.png" alt="react infographic 1" width="400" height="320"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qr7hdkc3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q0d6fzdj90rutcveg6o8.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qr7hdkc3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q0d6fzdj90rutcveg6o8.jpg" alt="react infographic 2" width="624" height="344"&gt;&lt;/a&gt;&lt;br&gt;
Below is an example of a React program. As you can see the UI is seamless and organized. Imagine the possibilities!&lt;br&gt;&lt;br&gt;
&lt;a href="https://i.giphy.com/media/c5hT4dktaaAr1wxUx2/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/c5hT4dktaaAr1wxUx2/giphy.gif" width="480" height="258"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;CREATE REACT APP&lt;/strong&gt;&lt;br&gt;
The first thing I want to do is create a new React app. How would I do this? There are various ways to solve this problem and for the purpose of this entry I'm going to run the following code in my terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app react-example
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://reactjs.org/docs/create-a-new-react-app.html"&gt;Create React App&lt;/a&gt; is a comfortable environment for learning React, and is the best way to start building a new single-page application in React.&lt;br&gt;
Once the new React app is finished installing you will see something like the image below.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RX8zQhXT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dzwlblzx3bkhjaa9fu9l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RX8zQhXT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dzwlblzx3bkhjaa9fu9l.png" alt="react 2" width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;NOTE: I'll be using &lt;a href="https://classic.yarnpkg.com/en/docs/getting-started"&gt;Yarn&lt;/a&gt;, a package manager for your code. It allows you to use and share (e.g. JavaScript) code with other developers from around the world.&lt;br&gt;
Now that I've created a new React program I'm going to open it up in my text editor and you'll be able to see all of the newly created files. &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ElK-KJaj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3d2sety64r0qua7g6rmj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ElK-KJaj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3d2sety64r0qua7g6rmj.png" alt="react 3" width="338" height="900"&gt;&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Next, to view the program in a browser I'm going to run the following code in the command line.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qyu6Xsfq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4njuhcfsmagqoqwey76q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qyu6Xsfq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4njuhcfsmagqoqwey76q.png" alt="react 4" width="880" height="617"&gt;&lt;/a&gt;&lt;br&gt;
Awesome, lets go back to the text editor and check out &lt;code&gt;index.js&lt;/code&gt;.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VTPYO2VL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u2mxu1sh10u5r8iavguk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VTPYO2VL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u2mxu1sh10u5r8iavguk.png" alt="react 5" width="880" height="530"&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;
The first couple of lines seem like simple code but what do they mean? In &lt;a href="https://www.geeksforgeeks.org/reactjs-importing-exporting/"&gt;React&lt;/a&gt; we use the keywords &lt;code&gt;import&lt;/code&gt; and &lt;code&gt;from&lt;/code&gt; to import a particular module or a named parameter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import ReactDOM from 'react-dom';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import"&gt;import&lt;/a&gt; statement is used to import read only live bindings which are exported by another module.&lt;br&gt;
To summarize, I'm using both the &lt;code&gt;import&lt;/code&gt; and &lt;code&gt;from&lt;/code&gt; keywords to import React and ReactDOM into my program.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tN59JhyH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6eobrynqx1qpmnqb52jg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tN59JhyH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6eobrynqx1qpmnqb52jg.png" alt="react 6" width="795" height="341"&gt;&lt;/a&gt; The &lt;a href="https://reactjs.org/docs/react-dom.html#gatsby-focus-wrapper"&gt;react-dom&lt;/a&gt; package provides DOM-specific methods that can be used at the top level of your app and as an escape hatch to get outside of the React model if you need to. &lt;br&gt;
&lt;em&gt;methods provided by the react-dom api&lt;br&gt;
render()&lt;br&gt;
hydrate()&lt;br&gt;
unmountComponentAtNode()&lt;br&gt;
findDOMNode()&lt;br&gt;
createPortal()&lt;/em&gt;&lt;br&gt;
Next, you'll see code similar to the example below. Remember, we have access to &lt;code&gt;render()&lt;/code&gt; because we &lt;code&gt;import&lt;/code&gt;ed the &lt;code&gt;react-dom&lt;/code&gt; API into our program.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ReactDOM.render(

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://reactjs.org/docs/react-dom.html#render"&gt;ReactDOM.render()&lt;/a&gt; controls the contents of the container node you pass in. Any existing DOM elements inside are replaced when first called.&lt;br&gt;
Awesome, let's start going over what is inside &lt;code&gt;render()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import ReactDOM from 'react-dom';

ReactDOM.render(
    &amp;lt;App /&amp;gt;
  document.getElementById('root') // We call this a “root” DOM node because everything inside it will be managed by React DOM.
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First, there is an &lt;code&gt;App&lt;/code&gt; &lt;a href="https://reactjs.org/docs/rendering-elements.html"&gt;element&lt;/a&gt; which is a plain object describing a &lt;a href="https://reactjs.org/docs/components-and-props.html"&gt;component&lt;/a&gt; instance or DOM node and its desired properties. Second, &lt;a href="https://reactjs.org/docs/rendering-elements.html"&gt;Applications&lt;/a&gt; built with just React usually have a single root DOM node. If you are integrating React into an existing app, you may have as many isolated root DOM nodes as you like.&lt;br&gt;
Sweet, lets go ahead and check out the &lt;code&gt;App&lt;/code&gt; component being referenced! To do that head over to the directory in your text editor and go to the &lt;code&gt;App.js&lt;/code&gt; file. &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---Pw7FHUT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/arobq83jbnk5royf1e9o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---Pw7FHUT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/arobq83jbnk5royf1e9o.png" alt="react 6" width="880" height="514"&gt;&lt;/a&gt;&lt;br&gt;
This is the file that is holding the UI logic displayed in the browser and where I'm going to end this entry. Next week, I'll dive deeper into Components, Elements and JSX! &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mMVqBH8l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ydp2w13vlte22e4cnhg7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mMVqBH8l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ydp2w13vlte22e4cnhg7.png" alt="Alt 4" width="880" height="617"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;RESOURCES&lt;br&gt;
&lt;a href="https://reactjs.org/docs/hello-world.html"&gt;https://reactjs.org/docs/hello-world.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.c-sharpcorner.com/article/what-and-why-reactjs/"&gt;https://www.c-sharpcorner.com/article/what-and-why-reactjs/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://reactjs.org/docs/getting-started.html"&gt;https://reactjs.org/docs/getting-started.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://classic.yarnpkg.com/en/docs/getting-started"&gt;https://classic.yarnpkg.com/en/docs/getting-started&lt;/a&gt;&lt;br&gt;
&lt;a href="https://medium.com/@gabrielrasdale/react-react-dom-eli5-db2101e614e5"&gt;https://medium.com/@gabrielrasdale/react-react-dom-eli5-db2101e614e5&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.taniarascia.com/getting-started-with-react/"&gt;https://www.taniarascia.com/getting-started-with-react/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>what i learned last week: creating an animated 2d background with canvas and javascript</title>
      <dc:creator>reyes2981</dc:creator>
      <pubDate>Thu, 30 Sep 2021 13:41:42 +0000</pubDate>
      <link>https://dev.to/reyes2981/what-i-learned-last-week-creating-an-animated-2d-background-with-canvas-and-javascript-kfp</link>
      <guid>https://dev.to/reyes2981/what-i-learned-last-week-creating-an-animated-2d-background-with-canvas-and-javascript-kfp</guid>
      <description>&lt;p&gt;Hello and welcome to this weeks blog entry where I'll &lt;a href="https://dev.to/reyes2981/what-i-learned-last-week-how-to-make-a-bouncing-object-using-vanillajs-part-1-drawing-the-object-5eea"&gt;continue&lt;/a&gt; working on &lt;a href="https://github.com/reyes2981/angry_borgs_frontend" rel="noopener noreferrer"&gt;Angry Borgs&lt;/a&gt;, a 2D game I'm building using vanilla JavaScript for the frontend and Ruby on Rails on the backend. To review, I've successfully modeled physics to create a dynamic &lt;code&gt;player&lt;/code&gt; object that can be controlled with the &lt;code&gt;spacebar&lt;/code&gt;. &lt;br&gt;
&lt;a href="https://i.giphy.com/media/kv1WNKj4J4Nn70ZALP/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/kv1WNKj4J4Nn70ZALP/giphy.gif"&gt;&lt;/a&gt;&lt;br&gt;
In this entry, I want to add more depth to Angry Borgs so I'm going to build an animated background. The final product for this blog will be a variation of this...&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FU7huVmA3TKuqvznInJ%2Fgiphy-downsized-large.gif%3Fcid%3Decf05e477566r7r89ykcw4f7sbiq9daydg3rni1jwuuumnln%26rid%3Dgiphy-downsized-large.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FU7huVmA3TKuqvznInJ%2Fgiphy-downsized-large.gif%3Fcid%3Decf05e477566r7r89ykcw4f7sbiq9daydg3rni1jwuuumnln%26rid%3Dgiphy-downsized-large.gif%26ct%3Dg"&gt;&lt;/a&gt;&lt;br&gt;
First, I'll retrieve the &lt;code&gt;canvas&lt;/code&gt; reference and store it inside a similarly named variable. Next, I need to initialize the context of the canvas element and store that in a variable named &lt;code&gt;ctx&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const canvas = document.querySelector('canvas');// reference to the CANVAS element
const ctx = canvas.getContext('2d');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, I create a new &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/Image" rel="noopener noreferrer"&gt;instantiation&lt;/a&gt; of the &lt;code&gt;Image()&lt;/code&gt; constructor and store it inside of a variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const backgroundImage = new Image();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note, the above code has the same functionality as the code below. There is no "correct way" to use either form rather up to the situation and your best judgement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.create('img');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Second, I want to reference the image I'm going to use as the animated background. I can do this by calling &lt;code&gt;src&lt;/code&gt; on &lt;code&gt;backgroundImage&lt;/code&gt; and then storing the file path as a string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;backgroundImage.src = 'assets/bg5(1).jpg'; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So far the code should look something like this...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
const backgroundImage = new Image();
backgroundImage.src = 'assets/bg5(1).jpg'; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we have our variables all set up, what is the next step? We need to not only build out the logic of the program but also organize it. In this case, I'm going to create a &lt;code&gt;draw()&lt;/code&gt; &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions" rel="noopener noreferrer"&gt;Function&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function draw() {
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we are going to reference the canvas using &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/clearRect" rel="noopener noreferrer"&gt;&lt;code&gt;clearRect()&lt;/code&gt;&lt;/a&gt; which is a method of the Canvas 2D API [that] erases the pixels in a rectangular area by setting them to transparent black. For this example I want to clear out the entire browser screen so I passed in the following arguments:.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;x-coordinate = 0,
y-coordinate = 0,
width = canvas.width
height = canvas.height

clearRect(0, 0, canvas.width, canvas.height)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F3gPKkEhk8IYwNV3sJI%2Fgiphy.gif%3Fcid%3Decf05e47nrisixjintxpde9jmaw9fy38vohqj7anz470mf24%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F3gPKkEhk8IYwNV3sJI%2Fgiphy.gif%3Fcid%3Decf05e47nrisixjintxpde9jmaw9fy38vohqj7anz470mf24%26rid%3Dgiphy.gif%26ct%3Dg"&gt;&lt;/a&gt;&lt;br&gt;
Let's review all of the code I have so far and debug, debug, debug!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
const backgroundImage = new Image();
backgroundImage.src = 'assets/bg5(1).jpg'; 

function draw() {
clearRect(0, 0, canvas.width, canvas.height)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we run the above code what do you think will happen on the browser side? &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F554gk2ez5a0rr3ogl0w3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F554gk2ez5a0rr3ogl0w3.png" alt="parllx1"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;NADA!&lt;/em&gt;&lt;br&gt;
Let's call &lt;code&gt;draw()&lt;/code&gt; and see what happens&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
const backgroundImage = new Image();
backgroundImage.src = 'assets/bg5(1).jpg'; 

function draw() {
clearRect(0, 0, canvas.width, canvas.height)
}
draw()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkq0r7afbhrwc7fx2izib.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkq0r7afbhrwc7fx2izib.png" alt="parllx2"&gt;&lt;/a&gt;&lt;br&gt;
Sweet, we got an error! &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fewrroxebgx1g6d5pwq4h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fewrroxebgx1g6d5pwq4h.png" alt="parallx3"&gt;&lt;/a&gt;&lt;br&gt;
The error says &lt;code&gt;clearRect&lt;/code&gt; is not defined but if you look at the code it's clearly there. Why is there an error? The answer - &lt;code&gt;clearRect&lt;/code&gt; is a method of the Canvas 2D API. When I run the below code the program has no idea what that function is. How is this fixed?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
const backgroundImage = new Image();
backgroundImage.src = 'assets/bg5(1).jpg'; 

function draw() {
clearRect(0, 0, canvas.width, canvas.height)
}
draw()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You simply need to call &lt;code&gt;clearRect()&lt;/code&gt; on your &lt;code&gt;ctx&lt;/code&gt; variable. Remember, &lt;code&gt;ctx&lt;/code&gt; initializes the context of the canvas element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Success! &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjk5he1m0mfvepd2lulfl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjk5he1m0mfvepd2lulfl.png" alt="parllx4"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
const backgroundImage = new Image();
backgroundImage.src = 'assets/bg5(1).jpg'; 

function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height)
}
draw()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, within &lt;code&gt;draw()&lt;/code&gt; I'm going to be using the &lt;code&gt;requestAnimationFrame()&lt;/code&gt;method which tells the browser that you wish to perform an animation and requests that the browser calls a specified function to update an animation before the next repaint. The method takes a callback as an argument to be invoked before the repaint.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function draw() {
console.log('hello from the draw function')
ctx.clearRect(0, 0, canvas.width, canvas.height)
requestAnimationFrame(draw);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sweet, quick review of the code so far!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
const backgroundImage = new Image();
backgroundImage.src = 'assets/bg5(1).jpg'; 

function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height)
requestAnimationFrame(draw);
}
draw()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, I'm going to use &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/drawImage" rel="noopener noreferrer"&gt;&lt;code&gt;drawImage()&lt;/code&gt;&lt;/a&gt; which is a method of the Canvas 2D API [that] provides different ways to draw an image onto the canvas. I want to fill out the entire browser screen so I passed in the following arguments:.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;image = backgroundImage,
x-coordinate = 0,
y-coordinate = 0,
width = canvas.width,
height = canvas.height

ctx.drawImage(backgroundImage, backgroundImage, 0, 0, canvas.width, canvas.height);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Awesome, let's head over to the browser and check if we see the image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height)
ctx.drawImage(backgroundImage, 0, 0, canvas.width, canvas.height);

requestAnimationFrame(draw);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw5svyi6qnoh5x2e0t33r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw5svyi6qnoh5x2e0t33r.png" alt="parllx6"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F3ohzdIuqJoo8QdKlnW%2Fgiphy.gif%3Fcid%3Decf05e47fz7oxpm1pwvgzdvc957w1r5okv7ycpdxgabm47wo%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F3ohzdIuqJoo8QdKlnW%2Fgiphy.gif%3Fcid%3Decf05e47fz7oxpm1pwvgzdvc957w1r5okv7ycpdxgabm47wo%26rid%3Dgiphy.gif%26ct%3Dg"&gt;&lt;/a&gt;&lt;br&gt;
Finally, I don't need a static image I need a dynamic one and I can do that with the following code...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;backgroundImage.x -= 0.1;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code tells the program to move the &lt;code&gt;backgroundImage&lt;/code&gt;s x-axis an x-amount of distance. Let's see the final code for better, no pun intended, context.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const canvas = document.querySelector('canvas');// reference to the CANVAS element
const ctx = canvas.getContext('2d');
const backgroundImage = new Image();
backgroundImage.src = 'assets/bg5(1).jpg';

function draw() {
console.log('hello from the draw function')
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(backgroundImage, 0, 0, canvas.width, canvas.height);
backgroundImage.x -= 0.1;
requestAnimationFrame(draw);
}
draw();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see above, when the &lt;code&gt;draw()&lt;/code&gt; function is called it initializes a canvas element, then draws out an image, next the image's x-axis is moved an x amount of distance and finally it "hits" the &lt;code&gt;requestAnimationFrame()&lt;/code&gt; method where a loop is created. &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F9KERatxMbLWcbf2siJ%2Fgiphy.gif%3Fcid%3D790b7611ef4bbccfe75187b3747ecb97562e4670029c9c6c%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F9KERatxMbLWcbf2siJ%2Fgiphy.gif%3Fcid%3D790b7611ef4bbccfe75187b3747ecb97562e4670029c9c6c%26rid%3Dgiphy.gif%26ct%3Dg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;FIN!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Thank you for reading.&lt;/p&gt;

&lt;p&gt;Say Hi on &lt;a href="https://www.linkedin.com/in/noe-camarena-b0741467/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;! I'm always open to connecting, networking and new opportunities. &lt;/p&gt;

&lt;p&gt;and/or&lt;/p&gt;

&lt;p&gt;Checkout my &lt;a href="https://github.com/reyes2981" rel="noopener noreferrer"&gt;Github&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;RESOURCES&lt;br&gt;
&lt;a href="https://dev.to/ibrahima92/make-a-parallax-effect-with-10-lines-of-javascript-3hia"&gt;https://dev.to/ibrahima92/make-a-parallax-effect-with-10-lines-of-javascript-3hia&lt;/a&gt;&lt;br&gt;
&lt;a href="https://luka712.github.io/2018/12/02/Parallax-scrolling/" rel="noopener noreferrer"&gt;https://luka712.github.io/2018/12/02/Parallax-scrolling/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=Mg7ibYWhjPI" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=Mg7ibYWhjPI&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>what i learned last week: overview of async and await</title>
      <dc:creator>reyes2981</dc:creator>
      <pubDate>Wed, 22 Sep 2021 16:14:37 +0000</pubDate>
      <link>https://dev.to/reyes2981/what-i-learned-last-week-overview-of-async-and-await-40co</link>
      <guid>https://dev.to/reyes2981/what-i-learned-last-week-overview-of-async-and-await-40co</guid>
      <description>&lt;p&gt;Welcome! This week I'll be reviewing &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;await&lt;/code&gt;. I haven't written a blog entry in a couple of weeks so let's get to it! &lt;/p&gt;

&lt;p&gt;What is going to display first, second and third in the console when I run the below program?&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_7cjyToc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vtqcocj6zcwp9ojihf0s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_7cjyToc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vtqcocj6zcwp9ojihf0s.png" alt="async1"&gt;&lt;/a&gt;&lt;br&gt;
If you said...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello.
Goodbye!
Hello again!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You are on the right track but wrong. &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vGLwN6c6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m51vc3b23vil0t4js1v7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vGLwN6c6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m51vc3b23vil0t4js1v7.png" alt="async3"&gt;&lt;/a&gt;&lt;br&gt;
So why did the program not run the code &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Synchronous"&gt;synchronously&lt;/a&gt;, line by line? Well, because &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/setTimeout"&gt;setTimeout()&lt;/a&gt; is an asynchronous function, meaning that the timer function will not pause execution of other functions in the functions stack.&lt;/p&gt;

&lt;p&gt;For a more detailed example of asynchronous functions let's check out some code from the &lt;a href="https://dev.to/reyes2981/what-i-learned-last-week-overview-of-fetch-3pke"&gt;what i learned last week: overview of fetch()&lt;/a&gt; entry...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fetch("http://startrek/planets", configObj)
  .then(function(response) {
    return response.json();
  })
  .then(function(object) {
    console.log(object);
  }); 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First, fetch() makes an HTTP request to a resource via a URL.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fetch("http://startrek/planets")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we utilize the &lt;code&gt;then()&lt;/code&gt; method to handle the returned promise. and the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Response/json"&gt;json()&lt;/a&gt; method, It returns a promise which resolves with the result of parsing the body text as JSON.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  .then(function(response) {
    return response.json();
  })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, I can do work with the JSON data. In this case I just want to display the returned data in the console.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  .then(function(object) {
    console.log(object);
  }); 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While this code is perfectly acceptable we can make it more leaner with async/wait!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async getPlanets() {
   let response = await fetch("http://startrek/planets");
   let object = await response.json()
   console.log(object);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First, I declared a function with the &lt;code&gt;async&lt;/code&gt; keyword which means &lt;a href="https://stackoverflow.com/questions/67955033/async-await-with-fetch-js"&gt;means&lt;/a&gt; that the function will return a promise in response.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async getPlanets() {

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Second I created the &lt;code&gt;response&lt;/code&gt; variable and saved a fetch request to it. Do you notice anything specific about the below code. Take your time I'll &lt;code&gt;await&lt;/code&gt;...&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;await&lt;/code&gt; &lt;a href="https://www.w3schools.com/js/js_async.asp"&gt;keyword&lt;/a&gt; makes a function wait for a Promise.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async getPlanets() {
   let response = await fetch("http://startrek/planets");

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, I created the &lt;code&gt;data&lt;/code&gt; variable and saved the result of calling &lt;code&gt;json()&lt;/code&gt; on &lt;code&gt;response&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async getPlanets() {
   let response = await fetch("http://startrek/planets");
   let data = await request.json();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, I can do work with my newly returned JSON object, again, in this case I just want to display the data in the console.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async getPlanets() {
   let response = await fetch("http://startrek/planets");
   let object = await response.json()
   console.log(object);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see utilizing the &lt;code&gt;async&lt;/code&gt;/&lt;code&gt;await&lt;/code&gt; keywords is a fantastic supplement to promises. With that being said, it's important to note that &lt;code&gt;async&lt;/code&gt;/&lt;code&gt;await&lt;/code&gt; aren't necessarily substitutes for &lt;code&gt;promises&lt;/code&gt; but what programmers like to call &lt;a href="https://en.wikipedia.org/wiki/Syntactic_sugar"&gt;syntactic sugar&lt;/a&gt;.  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--I7RxTChs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fcvexx8h2nfw5rof609p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--I7RxTChs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fcvexx8h2nfw5rof609p.png" alt="async4"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I'm going to keep this blog entry cute and simple and end it here. I appreciate all comments and feedback. Thank you for reading!&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>what i learned last week: how to build an api utilizing ruby on rails and connecting it to a javascript frontend pt 1 </title>
      <dc:creator>reyes2981</dc:creator>
      <pubDate>Mon, 30 Aug 2021 16:38:17 +0000</pubDate>
      <link>https://dev.to/reyes2981/what-i-learned-last-week-how-to-build-an-api-utilizing-ruby-on-rails-and-connecting-it-to-a-javascript-frontend-pt-1-1c4l</link>
      <guid>https://dev.to/reyes2981/what-i-learned-last-week-how-to-build-an-api-utilizing-ruby-on-rails-and-connecting-it-to-a-javascript-frontend-pt-1-1c4l</guid>
      <description>&lt;p&gt;&lt;strong&gt;INTRO&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Hi! In this series I'm going to go over how to build an API utilizing Ruby on Rails and connecting it to a JavaScript frontend. Remember, an &lt;a href="https://en.wikipedia.org/wiki/API"&gt;application programming interface&lt;/a&gt; (API) is a connection between computers or between computer programs. It is a type of software interface, offering a service to other pieces of software. &lt;/p&gt;

&lt;p&gt;Note, I'm learning that I code most efficiently when I work on features vertically and not horizontally. What do I mean by that? It means for the purpose of this series I'm going to through the process of creating a model, controller and view for a Player. Hmm...sound familiar? &lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;API&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First, to build out and specify properties of the API head over to the terminal. As you can see I'm adding commands that tell the system that the new project being created is an API and for this example has a postgresql database. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails new example_api --api --database=postgresql&lt;/code&gt;&lt;/p&gt;

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

&lt;p&gt;Success! Once the command has been completed you should see something like the above image in your terminal.&lt;/p&gt;

&lt;p&gt;Next, to work with the newly created API head over to your preferred text editor and open up the newly created directory. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jZMUil5S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y7d9ioy6mkbg8mkokdkc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jZMUil5S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y7d9ioy6mkbg8mkokdkc.png" alt="files in text editor" width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Easy, right? Now that our API has been created we can start coding! As previously mentioned, I'll be using the MVC &lt;a href="https://www.tomdalling.com/blog/software-design/model-view-controller-explained/"&gt;architecture&lt;/a&gt; to build out my program. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;MODEL&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The central component of the pattern. It is the application's dynamic data structure, independent of the user interface. It directly manages the data, logic and rules of the application.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Lets get to it and head over to the terminal to build out our &lt;code&gt;Player&lt;/code&gt; model and run the following command: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails generate model Player username:string email:string&lt;/code&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6haJ6XSY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/95ez8ygme9t8mnq0psyw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6haJ6XSY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/95ez8ygme9t8mnq0psyw.png" alt="player model" width="880" height="680"&gt;&lt;/a&gt;&lt;br&gt;
As you can see above, multiple things occurred when the above command was ran. Most importantly, a &lt;code&gt;Player&lt;/code&gt; class and migration file were both simultaneously created. &lt;/p&gt;

&lt;p&gt;Speaking of migration file...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9dtgM6Y5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fu75virwtddljhw2r2ag.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9dtgM6Y5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fu75virwtddljhw2r2ag.png" alt="migration file" width="880" height="730"&gt;&lt;/a&gt;&lt;br&gt;
Do you notice anything about the newly created migration file? Our Player already has a username and email attribute. &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6BLxwL4r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://media.giphy.com/media/Ri7d8I18cto2jufOKc/giphy.gif%3Fcid%3Decf05e47zjnxrq0d3hj9bsaoobo0x0vhm8hs7bnierawz4zl%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6BLxwL4r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://media.giphy.com/media/Ri7d8I18cto2jufOKc/giphy.gif%3Fcid%3Decf05e47zjnxrq0d3hj9bsaoobo0x0vhm8hs7bnierawz4zl%26rid%3Dgiphy.gif%26ct%3Dg" width="480" height="270"&gt;&lt;/a&gt;&lt;br&gt;
Sweet, I have my player model set up let me go create an instance of it! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Di6tLWXV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s08a1agyvrjk2yva5pw9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Di6tLWXV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s08a1agyvrjk2yva5pw9.png" alt="new player error" width="880" height="325"&gt;&lt;/a&gt;&lt;br&gt;
WELP. An error?&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--X_urUAaL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://media.giphy.com/media/3FbGfzApiOQC7jQeyb/giphy.gif%3Fcid%3Decf05e47fvjx84ukhhwrrze385dpq3wvcfq3e6c8b67is097%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--X_urUAaL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://media.giphy.com/media/3FbGfzApiOQC7jQeyb/giphy.gif%3Fcid%3Decf05e47fvjx84ukhhwrrze385dpq3wvcfq3e6c8b67is097%26rid%3Dgiphy.gif%26ct%3Dg" width="480" height="480"&gt;&lt;/a&gt;&lt;br&gt;
I forgot to actually create the database! In the terminal run the following command:&lt;br&gt;
&lt;code&gt;rails db:create&lt;/code&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3Sbh5X3l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xmvrygt596yz771fil08.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3Sbh5X3l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xmvrygt596yz771fil08.png" alt="database" width="880" height="237"&gt;&lt;/a&gt;&lt;br&gt;
Ok, now if I head over to the console lets see if I can create an instance of a &lt;code&gt;Player&lt;/code&gt;.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NHb-AmHy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hh9m2nfig45ibegt8fn1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NHb-AmHy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hh9m2nfig45ibegt8fn1.png" alt="error" width="880" height="253"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8Iz0gqHZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://media.giphy.com/media/l3q2K5jinAlChoCLS/giphy.gif%3Fcid%3Decf05e473xt7tmvogapjsld6soq3b44kplgkabm9t5wpskk2%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8Iz0gqHZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://media.giphy.com/media/l3q2K5jinAlChoCLS/giphy.gif%3Fcid%3Decf05e473xt7tmvogapjsld6soq3b44kplgkabm9t5wpskk2%26rid%3Dgiphy.gif%26ct%3Dg" width="195" height="229"&gt;&lt;/a&gt;&lt;br&gt;
Do you know what step I'm missing? &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7A_KM5q0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hvus9foe4p3w4f1xvpny.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7A_KM5q0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hvus9foe4p3w4f1xvpny.png" alt="migration completed" width="880" height="280"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://guides.rubyonrails.org/v3.2/migrations.html"&gt;If&lt;/a&gt; you guessed run &lt;code&gt;rails db:migrate&lt;/code&gt; in the terminal you are correct! In its most basic form it just runs the up or change method for all the migrations that have not yet been run. If there are no such migrations, it exits. It will run these migrations in order based on the date of the migration. Note that running the &lt;code&gt;db:migrate&lt;/code&gt; also invokes the &lt;code&gt;db:schema:dump&lt;/code&gt; task, which will update your &lt;code&gt;db/schema.rb&lt;/code&gt; file to match the structure of your database.&lt;/p&gt;

&lt;p&gt;lets try creating a &lt;code&gt;Player&lt;/code&gt; instance.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sgDXRXKz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xtobz5nozyasefw3fxr0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sgDXRXKz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xtobz5nozyasefw3fxr0.png" alt="Success" width="880" height="298"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6N7Frnaz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://media.giphy.com/media/3oKGzgXIbxo5KoSty0/giphy.gif%3Fcid%3Decf05e47tjubu2azfy0amzuwiovcb0sw5ip0cjn1wtl9f6uz%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6N7Frnaz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://media.giphy.com/media/3oKGzgXIbxo5KoSty0/giphy.gif%3Fcid%3Decf05e47tjubu2azfy0amzuwiovcb0sw5ip0cjn1wtl9f6uz%26rid%3Dgiphy.gif%26ct%3Dg" width="480" height="264"&gt;&lt;/a&gt;&lt;br&gt;
Success!&lt;/p&gt;

&lt;p&gt;To review, in the first part of this series I've built out an API, created a &lt;code&gt;Player&lt;/code&gt; model, set up a database and finally confirmed that the created database is up and running! In the next entry I'll be going over the controller of my &lt;code&gt;Player&lt;/code&gt; model. Thank you for reading! &lt;/p&gt;

</description>
      <category>rails</category>
    </item>
    <item>
      <title>what i learned last week: how to make a bouncing object using vanillajs part 2 animation</title>
      <dc:creator>reyes2981</dc:creator>
      <pubDate>Tue, 24 Aug 2021 18:34:59 +0000</pubDate>
      <link>https://dev.to/reyes2981/what-i-learned-last-week-how-to-make-a-bouncing-object-using-vanillajs-part-2-animation-5g75</link>
      <guid>https://dev.to/reyes2981/what-i-learned-last-week-how-to-make-a-bouncing-object-using-vanillajs-part-2-animation-5g75</guid>
      <description>&lt;p&gt;Welcome to part 2 of &lt;a href="https://dev.to/reyes2981/what-i-learned-last-week-how-to-make-a-bouncing-object-using-vanillajs-part-1-drawing-the-object-5eea"&gt;how to make a bouncing object using vanillajs&lt;/a&gt;! As you may recall, I'm creating a clone of Flappy Bird and I want the player to be controlled with the &lt;code&gt;spacebar&lt;/code&gt;. This means that within my program I need to model physics! &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--o4riTHcR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://media.giphy.com/media/12n5MNdTPHGMoM/giphy.gif%3Fcid%3Decf05e47ipbgycsbbgec7obfegq4ighoor1a0gitio4q0fwq%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--o4riTHcR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://media.giphy.com/media/12n5MNdTPHGMoM/giphy.gif%3Fcid%3Decf05e47ipbgycsbbgec7obfegq4ighoor1a0gitio4q0fwq%26rid%3Dgiphy.gif%26ct%3Dg" width="480" height="271"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;CODE SO FAR&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
const x = canvas.width / 2;
const y = canvas.height / 2;

Class Player {
  constructor(x, y, radius, color) { 
        this.x = x
        this.y = y
        this.radius = radius 
        this.color = color 
  }

  draw() {
        ctx.beginPath()
        ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false)
        ctx.fillStyle = this.color
        ctx.fill()
  }

}

const player = new Player(100, 100, 30, 'blue');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;PLAYER IN BROWSER&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YLUWC8fm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c550aabt2zyk2uqvv23v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YLUWC8fm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c550aabt2zyk2uqvv23v.png" alt="player object" width="880" height="455"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first thing I did is add a gravity, velocity and lift property to the constructor of my &lt;code&gt;Player&lt;/code&gt; class. The reason why I'm adding these properties is because I want my &lt;code&gt;player&lt;/code&gt; &lt;a href="https://en.wikipedia.org/wiki/Object_(computer_science)"&gt;object&lt;/a&gt; to be affected by physics like they would in the real world.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Class Player {
  constructor(x, y, radius, color, gravity, velocity, lift) { 
        this.x = x;
        this.y = y;
        this.radius = radius; 
        this.color = color; 
        this.gravity = gravity;
        this.velocity = velocity;
        this.lift - lift;
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;PHYSICS REFRESHER&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.britannica.com/science/gravity-physics"&gt;Gravity&lt;/a&gt;, also called gravitation, in mechanics [is] the universal force of attraction acting between all matter.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tPys3QGi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lts7luno67kgm2p9xa11.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tPys3QGi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lts7luno67kgm2p9xa11.jpg" alt="gravity" width="300" height="168"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.vocabulary.com/dictionary/velocity"&gt;Velocity&lt;/a&gt; [is the] distance traveled per unit time in one direction.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9HBuRXUF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jfjm3b7zus0dsgbjtuff.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9HBuRXUF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jfjm3b7zus0dsgbjtuff.png" alt="velocity" width="750" height="393"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.grc.nasa.gov/www/k-12/airplane/lift1.html"&gt;Lift&lt;/a&gt; occurs when a moving flow of gas is turned by a solid object. The flow is turned in one direction, and the lift is generated in the opposite direction, according to Newton's Third Law of action and reaction. Because air is a gas and the molecules are free to move about, any solid surface can deflect a flow. For an aircraft wing, both the upper and lower surfaces contribute to the flow turning. Neglecting the upper surface's part in turning the flow leads to an incorrect theory of lift.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7497kCHi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/drw9x8p5750u0ffwwqna.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7497kCHi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/drw9x8p5750u0ffwwqna.gif" alt="lift" width="619" height="466"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, I created the &lt;code&gt;up&lt;/code&gt; class method which runs every time the user presses the &lt;code&gt;spacebar&lt;/code&gt;. To create a "bouncing" effect I added the &lt;code&gt;player&lt;/code&gt;s velocity plus lift and saved it to &lt;code&gt;this.lift&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  up() {
    this.velocity = this.velocity + this.lift;
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alrighty, next I created the class method -- &lt;code&gt;update&lt;/code&gt;. This is where I compare &lt;code&gt;Player&lt;/code&gt; properties versus where I'm actually manipulating the property on the screen&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;update() {
   // Update the state of the world for the elapsed time since last render
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For each frame within our animation loop I'm going to set the &lt;code&gt;velocity&lt;/code&gt; of &lt;code&gt;player&lt;/code&gt; to &lt;code&gt;this.gravity&lt;/code&gt; and the y coordinate to &lt;code&gt;this.velocity&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
const x = canvas.width / 2;
const y = canvas.height / 2;

Class Player {
  constructor(x, y, radius, color, gravity, velocity, lift) { 
        this.x = x;
        this.y = y;
        this.radius = radius; 
        this.color = color; 
        this.gravity = gravity;
        this.velocity = velocity;
        this.lift = lift;
  }

  draw() {
        ctx.beginPath()
        ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false)
        ctx.fillStyle = this.color
        ctx.fill()
  }

  update() {
        this.velocity += this.gravity;
        this.y += this.velocity; 
  }

}

const player = new Player(100, 100, 30, 'blue', 0.6, 0, -15);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, I created the function &lt;code&gt;animate&lt;/code&gt; which is responsible for the looping of the &lt;code&gt;player&lt;/code&gt; object. How do I do this?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function animate() {

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside of &lt;code&gt;animate&lt;/code&gt; I'm using the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame"&gt;requestAnimationFrame&lt;/a&gt; method which tells the browser that you wish to perform an animation and requests that the browser calls a specified function to update an animation before the next repaint. &lt;strong&gt;The method takes a callback as an argument to be invoked before the repaint&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function animate() {  
  requestAnimationFrame();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, what do I need to pass into &lt;code&gt;requestAnimationFrame&lt;/code&gt; if I want to create a moving object?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function animate() {  
  requestAnimationFrame(animate);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now when I instantiate the function &lt;code&gt;animate&lt;/code&gt; a looping effect will be created.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function animate() {  
  requestAnimationFrame(animate);
}

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

&lt;/div&gt;



&lt;p&gt;Lastly, I need to add an &lt;code&gt;EventListener&lt;/code&gt; so that the user can control the &lt;code&gt;player&lt;/code&gt; by pressing on the &lt;code&gt;spacebar&lt;/code&gt;. The first argument I pass in is &lt;code&gt;keyup&lt;/code&gt;. The second argument is a callback function that runs the class method &lt;code&gt;up&lt;/code&gt; everytime the user presses &lt;code&gt;spacebar&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;addEventListener('keyup', event =&amp;gt; {
    if (event.code === 'Space') {
      player.up();
    }

})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;VOILA!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
const x = canvas.width / 2;
const y = canvas.height / 2;

Class Player {
  constructor(x, y, radius, color, gravity, velocity, lift) { 
        this.x = x;
        this.y = y;
        this.radius = radius; 
        this.color = color; 
        this.gravity = gravity;
        this.velocity = velocity;
        this.lift = lift;

  }
  draw() {
        ctx.beginPath()
        ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false)
        ctx.fillStyle = this.color
        ctx.fill()
  }
  up() {
    this.velocity += this.lift;
    console.log(this.velocity); 
  }
  update() {
        this.velocity += this.gravity;
        this.y += this.velocity; 
  }
}

const player = new Player(100, 100, 30, 'blue', 0.6, 0, -15);

function animate() {  
  requestAnimationFrame(animate);
  ctx.fillRect(0, 0, canvas.width, canvas.height);
  player.draw();
  player.update();
}

animate();

addEventListener('keyup', event =&amp;gt; {
    if (event.code === 'Space') {
      console.log('Space pressed');
      player.up();
    }

})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you've been following along you should see something like the &lt;strong&gt;blue&lt;/strong&gt; object below with similar animation.&lt;br&gt;
&lt;a href="https://i.giphy.com/media/kv1WNKj4J4Nn70ZALP/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/kv1WNKj4J4Nn70ZALP/giphy.gif" width="480" height="476"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eQur4SL5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://media.giphy.com/media/8qqUcJZr7Ju2SWqPpk/giphy.gif%3Fcid%3Decf05e47flntfwleg2f7qnmlanh34ifbbuewd1oqyiud6oes%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eQur4SL5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://media.giphy.com/media/8qqUcJZr7Ju2SWqPpk/giphy.gif%3Fcid%3Decf05e47flntfwleg2f7qnmlanh34ifbbuewd1oqyiud6oes%26rid%3Dgiphy.gif%26ct%3Dg" width="412" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Fin&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>what i learned last week:  how to make a bouncing object using vanillajs part 1 drawing the object</title>
      <dc:creator>reyes2981</dc:creator>
      <pubDate>Mon, 16 Aug 2021 18:01:20 +0000</pubDate>
      <link>https://dev.to/reyes2981/what-i-learned-last-week-how-to-make-a-bouncing-object-using-vanillajs-part-1-drawing-the-object-5eea</link>
      <guid>https://dev.to/reyes2981/what-i-learned-last-week-how-to-make-a-bouncing-object-using-vanillajs-part-1-drawing-the-object-5eea</guid>
      <description>&lt;p&gt;Welcome to this weeks blog entry! Phase four of Flatiron School is coming to a close which means it's project time. I've decided to create a 2D game using vanillajs for the frontend and Ruby on Rails on the backend. The game is a clone of &lt;a href="https://en.wikipedia.org/wiki/Flappy_Bird"&gt;Flappy Bird&lt;/a&gt; and the player will use the &lt;code&gt;spacebar&lt;/code&gt; to "fly" and avoid the enemy. Finally, this blog will be focused on the &lt;code&gt;player&lt;/code&gt; object and how draw an object. &lt;/p&gt;

&lt;p&gt;The first thing I'll need to do is create an index.html. Within this file we need to create a &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Client-side_web_APIs/Drawing_graphics#active_learning_getting_started_with_a_%3Ccanvas%3E"&gt;canvas&lt;/a&gt; element which is used to define the area on the page into which the image will be drawn.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;link rel="stylesheet" href="style.css"&amp;gt;&amp;lt;/link&amp;gt;
    &amp;lt;title&amp;gt;Angry Borg&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;canvas&amp;gt;&amp;lt;/canvas&amp;gt;
&amp;lt;script src="index.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: I made sure my index.js file was being properly read by adding a script element to index.html with a source of &lt;code&gt;index.js&lt;/code&gt;. Once the index.html file is set up I'll want to create the index.js file that will house our code.  The first thing I'll do is select the &lt;code&gt;canvas&lt;/code&gt; element. Selecting an element....that sounds familiar....&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const canvas = document.querySelector('canvas');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above, I've created &lt;code&gt;const canvas&lt;/code&gt; and selected the &lt;code&gt;canvas&lt;/code&gt; HTML element using the querySelector method. Remember, the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/querySelector"&gt;querySelector()&lt;/a&gt; method of the Element interface returns the first element that is a descendant of the element on which it is invoked that matches the specified group of selectors.&lt;/p&gt;

&lt;p&gt;Next, to draw on the selected canvas, I create the variable &lt;code&gt;const ctx&lt;/code&gt; and save the canvas context to it. The canvas context aka The Canvas API provides a means for drawing graphics via JavaScript and the HTML  element. Neat, right? Before this project I had no idea what &lt;code&gt;Canvas&lt;/code&gt; was let alone the power behind it!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What is the first thing I'll need to do if I want to create an object that represents a player? If you said "create a class" you are correct. Remember, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes"&gt;classes&lt;/a&gt; are a template for creating objects. They encapsulate data with code to work on that data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');

Class Player {

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, I need to create a constructor which is called each time you instantiate a new version of the &lt;code&gt;Player&lt;/code&gt; class. Remember, the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/constructor"&gt;constructor&lt;/a&gt; method is a special method of a class for creating and initializing an object of that class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');

Class Player {
  constructor(x, y, radius, color) { 

  }

// constructor allows for class properties to be unique for each object that is created

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above, I've defined a constructor and passed in multiple arguments that each represent a property of our &lt;code&gt;Player&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;x&lt;/code&gt; represents the &lt;em&gt;x&lt;/em&gt; coordinate of the &lt;code&gt;Player&lt;/code&gt; object inside of the browser&lt;br&gt;
&lt;code&gt;y&lt;/code&gt; represents the &lt;em&gt;y&lt;/em&gt; coordinate of the &lt;code&gt;Player&lt;/code&gt; object inside of the browser&lt;br&gt;
&lt;code&gt;radius&lt;/code&gt; represents the radius of the &lt;code&gt;Player&lt;/code&gt; object&lt;br&gt;
&lt;code&gt;color&lt;/code&gt; represents the color of the &lt;code&gt;Player&lt;/code&gt; object&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');

Class Player {
  constructor(x, y, radius, color) { 
        this.x = x // x coordinate
        this.y = y // y coordinate
        this.radius = radius // radius of player object
        this.color = color // color of player object

  // note we set the players properties as the arguments 
     we passed in the constructor method. 

  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://blog.kevinchisholm.com/javascript/the-javascript-this-keyword-deep-dive-constructor-functions/"&gt;When&lt;/a&gt; you instantiate a JavaScript constructor function, an object is returned. The JavaScript “this” keyword has a special meaning inside of that object: it refers to itself. In other words, when you create your constructor function, you can use the “this” keyword to reference the object that &lt;strong&gt;WILL&lt;/strong&gt; be created when the constructor is instantiated. After passing in our arguments I define the properties &lt;strong&gt;inside&lt;/strong&gt; of the constructor method. Each time a new &lt;code&gt;Player&lt;/code&gt; object is created we add new properties to that new &lt;strong&gt;instance&lt;/strong&gt; of the &lt;code&gt;Player&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');

Class Player {
  constructor(x, y, radius, color) { 
        this.x = x
        this.y = y
        this.radius = radius 
        this.color = color 
  }
}

const player = new Player()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that I have our class set up we can create an instance of our &lt;code&gt;Player&lt;/code&gt;. If you look at the above code you'll see that I've created &lt;code&gt;const player&lt;/code&gt; and saved a new instance of the &lt;code&gt;Player&lt;/code&gt; class to it. Next, I need to pass in properties to this &lt;code&gt;Player&lt;/code&gt; &lt;em&gt;instance&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');

Class Player {
  constructor(x, y, radius, color) { 
        this.x = x
        this.y = y
        this.radius = radius 
        this.color = color 
  }
}

const player = new Player(100, 100, 30, 'blue');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above, I've passed in an x coordinate of 100, y coordinate of 100, radius of 30 and finally blue as the color to our &lt;code&gt;player&lt;/code&gt;. Lets confirm we've created a new object by using &lt;code&gt;console.log&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');

Class Player {
  constructor(x, y, radius, color) { 
        this.x = x
        this.y = y
        this.radius = radius 
        this.color = color 
  }
}

const player = new Player(100, 100, 30, 'blue');
console.log(player);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To view our object in the console we pass in &lt;code&gt;player&lt;/code&gt; to the console.log method. If you head over to your browsers console you should see something similar to the below image.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dlbrIllJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ozm2vb2xw7htqq9ru9cg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dlbrIllJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ozm2vb2xw7htqq9ru9cg.png" alt="console.log(player)"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Awesome, so far I've created a &lt;code&gt;Player&lt;/code&gt; class and the ability to create instances of the class with the constructor method. I've confirmed that new instances are being created by using &lt;code&gt;console.log()&lt;/code&gt; and passing &lt;code&gt;const player&lt;/code&gt; to the method. Now that we've done the above we can actually draw out the object in our browser. We do this by using the properties inside of the contructor method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');

Class Player {
  constructor(x, y, radius, color) { 
        this.x = x
        this.y = y
        this.radius = radius 
        this.color = color 
  }

  draw() {

  }

}

const player = new Player(100, 100, 30, 'blue');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First, inside of the &lt;code&gt;Player&lt;/code&gt; class I've created a new function named &lt;code&gt;draw&lt;/code&gt; and it will be responsible for drawing out an object using passed in properties.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');

Class Player {
  constructor(x, y, radius, color) { 
        this.x = x
        this.y = y
        this.radius = radius 
        this.color = color 
  }

  draw() {
        ctx.beginPath()
  }

}

const player = new Player(100, 100, 30, 'blue');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Second, I referenced the canvas context using the &lt;code&gt;ctx&lt;/code&gt; variable. Then I add &lt;code&gt;beginPath()&lt;/code&gt; to &lt;code&gt;ctx&lt;/code&gt; which lets our browser know we are going to start drawing..&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');

Class Player {
  constructor(x, y, radius, color) { 
        this.x = x
        this.y = y
        this.radius = radius 
        this.color = color 
  }

  draw() {
        ctx.beginPath()
        ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false)
      //ctx.arc(x, y, radius, startAngle, endAngle [, counterclockwise])
  }

}

const player = new Player(100, 100, 30, 'blue');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Third, I referenced the canvas context using the &lt;code&gt;ctx&lt;/code&gt; variable. Then I add &lt;code&gt;arc()&lt;/code&gt; to &lt;code&gt;ctx&lt;/code&gt; &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/arc"&gt;which&lt;/a&gt; creates a circular arc centered at (x, y) with a radius of radius. As you can see above, I'm able to pass in the properties from the constructor to inside of the &lt;code&gt;draw&lt;/code&gt; function using the &lt;code&gt;This&lt;/code&gt; keyword&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');

Class Player {
  constructor(x, y, radius, color) { 
        this.x = x
        this.y = y
        this.radius = radius 
        this.color = color 
  }

  draw() {
        ctx.beginPath()
        ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false)
        ctx.fillStyle = this.color

  }

}

const player = new Player(100, 100, 30, 'blue');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fourth, I referenced the canvas context using the &lt;code&gt;ctx&lt;/code&gt; variable. Then I specify that the fillstyle will equal to &lt;code&gt;This.color&lt;/code&gt;. Doing this tells JavaScript that the color of our &lt;code&gt;player&lt;/code&gt; object's color will be whatever is passed in when the constructor is instantiated.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');

Class Player {
  constructor(x, y, radius, color) { 
        this.x = x
        this.y = y
        this.radius = radius 
        this.color = color 
  }

  draw() {
        ctx.beginPath()
        ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false)
        ctx.fillStyle = this.color
        ctx.fill()
  }

}

const player = new Player(100, 100, 30, 'blue');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, I referenced the canvas context using the &lt;code&gt;ctx&lt;/code&gt; variable. Then I add&lt;code&gt;fill()&lt;/code&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OAosWRh8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7y0mekp74y9a1g2sy8cu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OAosWRh8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7y0mekp74y9a1g2sy8cu.png" alt="Player Object"&gt;&lt;/a&gt;&lt;br&gt;
Awesome, I've successfully created a &lt;code&gt;player&lt;/code&gt; object and drew it on my browser using the HTML element canvas and JavaScript. Now we need to position the object in our browser. For this game I want my player to be on the left side AND  middle of the browser and the enemy on the right side. Our final object position should look like something in the above image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
const x = canvas.width / 2;
const y = canvas.height / 2;

Class Player {
  constructor(x, y, radius, color) { 
        this.x = x
        this.y = y
        this.radius = radius 
        this.color = color 
  }

  draw() {
        ctx.beginPath()
        ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false)
        ctx.fillStyle = this.color
        ctx.fill()
  }

}

const player = new Player(100, 100, 30, 'blue');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To ensure that my image is always in that position, regardless of window size, we need to set up some new global variables that reference the &lt;code&gt;canvas&lt;/code&gt; height and width. &lt;/p&gt;

&lt;p&gt;Like a Stephen King novel I'm going to end with a little suspense. In the next entry I will model physics inside of my game and make an interactive object. &lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>what i learned last week: the basics of execution context</title>
      <dc:creator>reyes2981</dc:creator>
      <pubDate>Mon, 09 Aug 2021 22:23:05 +0000</pubDate>
      <link>https://dev.to/reyes2981/what-i-learned-last-week-the-basics-of-execution-context-3f4f</link>
      <guid>https://dev.to/reyes2981/what-i-learned-last-week-the-basics-of-execution-context-3f4f</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;Welcome to this weeks entry of my WILLW series. Today, I'm going over the basics of execution context.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is the execution context
&lt;/h3&gt;

&lt;p&gt;In JavaScript, &lt;a href="https://codeburst.io/js-demystified-04-execution-context-97dea52c8ac6"&gt;execution context&lt;/a&gt; is an abstract concept that holds information about the environment within which the current code is being executed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--z1dLI3v9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ds3rr95cu7f3t9uma55s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--z1dLI3v9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ds3rr95cu7f3t9uma55s.png" alt="execution context"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Types of execution context
&lt;/h3&gt;

&lt;p&gt;There are three types of execution context and we are going to focus on two. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Global Execution Context&lt;/li&gt;
&lt;li&gt;Function Execution Context&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;First, &lt;a href="https://medium.com/@happymishra66/execution-context-in-javascript-319dd72e8e2c"&gt;Global Execution Context&lt;/a&gt; is the default execution context in which JavaScript code start its execution when the file first loads in the browser.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iJsF0P1y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lbro7m6dm8scisvxu55o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iJsF0P1y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lbro7m6dm8scisvxu55o.png" alt="global execution context"&gt;&lt;/a&gt; All of the global code i.e. code which is not inside any function or object is executed inside the global execution context. GEC cannot be more than one because only one global environment is possible for JavaScript code execution as the JavaScript engine is single threaded.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0wfxhzaW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4r2k2s8zshxq7xp2hgy6.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0wfxhzaW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4r2k2s8zshxq7xp2hgy6.jpg" alt="call stack"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Remember, a &lt;a href="https://dev.to/reyes2981/what-i-learned-last-week-under-the-hood-of-javascript-5e37"&gt;call stack&lt;/a&gt; is a mechanism for an interpreter (like the JavaScript interpreter in a web browser) to keep track of its place in a script that calls multiple functions — what function is currently being run and what functions are called from within that function, etc. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iTSvH_0v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lw611h05eefh87o3y1qi.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iTSvH_0v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lw611h05eefh87o3y1qi.gif" alt="javascript"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next up, we have the &lt;a href="https://medium.com/@happymishra66/execution-context-in-javascript-319dd72e8e2c#:~:text=Functional%20execution%20context%20(FEC)%3A,has%20its%20own%20execution%20context"&gt;Function Execution Context&lt;/a&gt; which is defined as the context created by the JavaScript engine whenever it finds any function call. &lt;a href="https://medium.com/@alexandrawilll/javascript-execution-context-call-stack-and-event-queue-d58b672d76f7#:~:text=When%20a%20function%20executes%2C%20an,off%20of%20the%20call%20stack."&gt;When&lt;/a&gt; a function executes, an execution context is created and virtually placed on top of the global execution context, the stacking of execution context is, again, called the call stack. Once a function has finished executing, it gets popped off of the call stack.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GCY8o7wE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uvuq0nb9yzliy825i5ab.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GCY8o7wE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uvuq0nb9yzliy825i5ab.png" alt="Function Execution Context"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8pw0jtkM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eiyseg7ck9mf9uohwb7w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8pw0jtkM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eiyseg7ck9mf9uohwb7w.png" alt="execution context diagam"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The two phases of execution context creation
&lt;/h3&gt;

&lt;p&gt;The creation of a execution context occurs in two phases&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Creation&lt;/li&gt;
&lt;li&gt;Execution&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://www.javascripttutorial.net/javascript-execution-context/"&gt;The creation phase&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;When a script executes for the first time, the JavaScript engine creates a Global Execution Context. During this creation phase, it performs the following tasks:&lt;/p&gt;

&lt;p&gt;Create a global object i.e., window in the web browser or global in Node.js.&lt;br&gt;
Create a this object binding which points to the global object above.&lt;br&gt;
Setup a memory heap for storing variables and function references.&lt;br&gt;
Store the function declarations in the memory heap and variables within the global execution context with the initial values as undefined.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--B75Vy3zs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4z16lpxrcbwymqv6tl10.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--B75Vy3zs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4z16lpxrcbwymqv6tl10.png" alt="global execution context"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.javascripttutorial.net/javascript-execution-context/"&gt;The Execution Phase&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;During the execution phase, the JavaScript engine executes the code line by line. In this phase, it assigns values to variables and executes the function calls.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--r-SxsIpl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ajxdmq5l150260p14oqe.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--r-SxsIpl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ajxdmq5l150260p14oqe.jpg" alt="execution phase"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That is it for this entry! &lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>what i learned last week: javascript conditionals</title>
      <dc:creator>reyes2981</dc:creator>
      <pubDate>Sun, 01 Aug 2021 12:33:36 +0000</pubDate>
      <link>https://dev.to/reyes2981/what-i-learned-last-week-data-types-in-javascript-5goh</link>
      <guid>https://dev.to/reyes2981/what-i-learned-last-week-data-types-in-javascript-5goh</guid>
      <description>&lt;p&gt;Right out of the gate, what actions do you take when you're thirsty? hungry? sleepy? Maybe you drink a Gatorade, eat a burger or go to sleep. Keep the below &lt;a href="https://en.wikipedia.org/wiki/Pseudocode"&gt;pseudocode&lt;/a&gt; in the back of your mind because we are going to build on it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if thirsty =&amp;gt; drink water.
else =&amp;gt; don't drink water.

if hungry =&amp;gt; eat tamal.
else =&amp;gt; don't eat tamal.

if sleepy =&amp;gt; sleep
else =&amp;gt; write code.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://www.w3schools.com/js/js_if_else.asp"&gt;Conditional statements&lt;/a&gt; are used to perform different actions based on different conditions. We can also refer to this as the &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Control_flow"&gt;control flow&lt;/a&gt; which is the order in which the computer executes statements in a script. In JavaScript there are a couple of ways to implement a conditional based control flow structure.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else"&gt;&lt;code&gt;if...else&lt;/code&gt; statement&lt;/a&gt;, &lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch"&gt;&lt;code&gt;switch&lt;/code&gt; statement&lt;/a&gt;,&lt;/p&gt;

&lt;p&gt;First, &lt;code&gt;if...else&lt;/code&gt; executes a statement &lt;strong&gt;if&lt;/strong&gt; a specified condition is truthy. If the condition is falsy, another statement can be executed. We can make this happen with the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality"&gt;Strict equality (===)&lt;/a&gt; operator which checks whether its two operands are equal, returning a Boolean result.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let thirsty = true; // The let statement declares a block-scoped local variable, optionally initializing it to a value.
if (thirsty === true) { // You should prefer Strict Equality Operator for comparisons.
console.log('I need H20'); // displays message on console.
} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since the above condition returns a &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Truthy"&gt;truthy&lt;/a&gt; value the code runs. Neat, right! &lt;/p&gt;

&lt;p&gt;Note: In JavaScript a truthy value is a value that is considered true when encountered in a Boolean context. All values are truthy unless they are defined as falsy (i.e., except for false, 0, -0, 0n, "", null, undefined, and NaN). A &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Falsy"&gt;falsy&lt;/a&gt; (sometimes written falsey) value is a value that is considered false when encountered in a Boolean context.&lt;/p&gt;

&lt;p&gt;Next, I've added &lt;code&gt;else&lt;/code&gt; to our statement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let thirsty = false;

if (thirsty === true) {
    console.log('I need H20');
} 
else {
    console.log('I/'m not thirsty');
}

// What do you think is going to be displayed in the console? Copy and paste the above code into your browsers console to see the answer.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above I've created the &lt;code&gt;thirsty&lt;/code&gt; variable and inside of it have stored &lt;code&gt;false&lt;/code&gt;. Remember, &lt;code&gt;false&lt;/code&gt; is a &lt;a href="https://www.w3schools.com/js/js_booleans.asp"&gt;boolean&lt;/a&gt; and it's value is falsy. Next, we want to create some sort of logic that logs 'I need H20' or I'm not thirsty' to our console. In this example we want to display a statement on our console depending on the value of a condition. Were you able to figure out what will be displayed in the console? &lt;/p&gt;

&lt;p&gt;&lt;code&gt;// =&amp;gt; I'm not thirsty&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;^^SOLUTION^^&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (thirsty === true) {
    console.log('I need H20');
} 
else {
    console.log("I'm not thirsty");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again, The &lt;code&gt;if...else&lt;/code&gt; statement executes a statement if a specified condition is truthy. If the condition is falsy, another statement can be executed. The reason why the console displays &lt;code&gt;I'm not thirsty&lt;/code&gt; is because the two operands in the condition are &lt;strong&gt;NOT&lt;/strong&gt; equal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let thirsty = false;

if (thirsty === true) {
    console.log('I need H20');
} 
else {
    console.log("I'm not thirsty");
}

// =&amp;gt; I'm not thirsty
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we've briefly gone over a basic &lt;code&gt;if...else&lt;/code&gt; statement let's move on. &lt;/p&gt;

&lt;p&gt;Next, the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch"&gt;switch statement&lt;/a&gt; evaluates an expression, matching the expression's value to a case clause, and executes statements associated with that case, as well as statements in cases that follow the matching case.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;switch (expression) {
    case value1:
        // Statements
        break;
    case value2:
        // Statements
        break;
    default:
        // Statements
        break;
} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sMf5phud--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sey8dfn400k56eof9ghn.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sMf5phud--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sey8dfn400k56eof9ghn.gif" alt="Cardi B"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ok, but what does that mean? Lets break it &lt;a href="https://javascript.info/switch"&gt;down&lt;/a&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The value of &lt;code&gt;expression&lt;/code&gt; is checked for a strict equality to the value from the first case (that is, value1) then to the second (value2) and so on.&lt;/li&gt;
&lt;li&gt;If the equality is found, switch starts to execute the code starting from the corresponding case, until the nearest break (or until the end of switch).&lt;/li&gt;
&lt;li&gt;If no case is matched then the default code is executed (if it exists).
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;switch (expression) {
    case value1:
        // Statements
        break;
    case value2:
        // Statements
        break;
    default:
        // Statements
        break;
} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mood = 'thirsty';

switch (mood) {
    case 'thirsty':
        drink = 'water';
        console.log(`I'm thirsty so I'm going to drink ${drink}.`);
        break;
    case 'hungry':
        food = 'burger';
        console.log(`I'm hungry so I'm going to eat a ${food}.`);
        break;
    case 'sleepy':
        action = 'sleep';
        console.log(`I'm sleepy so I'm going to ${action}.`);
        break;
}

// =&amp;gt; I'm thirsty so I'm going to drink water.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
    </item>
  </channel>
</rss>
