<?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>Forem: Hsabes</title>
    <description>The latest articles on Forem by Hsabes (@hsabes).</description>
    <link>https://forem.com/hsabes</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%2F863374%2F76810363-9d7f-4d9f-b58a-976ad50d779c.jpeg</url>
      <title>Forem: Hsabes</title>
      <link>https://forem.com/hsabes</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/hsabes"/>
    <language>en</language>
    <item>
      <title>Ruby Code Challenge: Sum of all primes within an array</title>
      <dc:creator>Hsabes</dc:creator>
      <pubDate>Tue, 15 Nov 2022 05:09:00 +0000</pubDate>
      <link>https://forem.com/hsabes/ruby-code-challenge-sum-of-all-primes-within-an-array-58hf</link>
      <guid>https://forem.com/hsabes/ruby-code-challenge-sum-of-all-primes-within-an-array-58hf</guid>
      <description>&lt;p&gt;A prime number is a number that is only divisible by itself and 1. Today I'll be walking through a fundamental algorithm problem you may see in coding assessments: Finding all the prime numbers in an array, and generating their sum. &lt;/p&gt;

&lt;h2&gt;
  
  
  Input
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr = [2, 5, 10, 12, 7, 2, 11, 4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;--&amp;gt; 27
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Find a single prime
&lt;/h2&gt;

&lt;p&gt;Before you can find a multitude of primes within an array, we need the capability of determining whether or not a single integer is a prime number.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;num = 5

def is_prime?(num)
    # Your code here
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First step is to create an array of values we will be dividing our integer by. Since we don't need to check 1 and the integer we're testing, we can leave those out of our range.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def is_prime?(num)
    (2...num).to_a
end

--&amp;gt; [2, 3, 4]

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

&lt;/div&gt;



&lt;p&gt;We've created an array of values from 2 to the number we are testing, now we need to select the values that DO divide into our integer in question.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def is_prime?(num)
    (2...num).to_a.select{ |divisor| num % divisor === 0 }
end

--&amp;gt; []
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If our integer truly is a prime number, our return should be an empty array. If it isn't prime, it will be filled with a bunch of values that divide into the non-prime integer. Lastly, all we have to do is evaluate presence of values inside the array to determine whether or not we have a prime number.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def is_prime?(num)
    (2...num).to_a.select{ |divisor| num % divisor === 0 }.length === 0 ? num : false
end

--&amp;gt; true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, since this is only the first step, we will need the actual number instead of just a boolean telling us it's true. If the method we've written evaluates to true (meaning it's a prime number), we'll need that value for later.&lt;/p&gt;

&lt;h2&gt;
  
  
  Find multiple primes
&lt;/h2&gt;

&lt;p&gt;Create your method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def find_and_sum_primes(arr)
    # Your code here
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This process is very similar to the previous one, but the method we've already written is going to do all the work for us. First, create an empty array inside the method. This will soon house all the prime values in the array being passed as an argument. Then iterate through the input array using select:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def find_and_sum_primes(arr)
    primes = []
    arr.select{ |num| # find if num is prime }
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thankfully, we can just call the method that we've already written to determine if the specific value being selected is in fact prime. Once we have a prime number, push it to our primes array and sum the values&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def find_and_sum_primes(arr)
    primes = []
    arr.select{ |num| 
        if (is_prime?(num))
            primes &amp;lt;&amp;lt; num
        end
    }
    primes.sum
end

--&amp;gt; 27
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

</description>
      <category>ruby</category>
      <category>algorithms</category>
      <category>math</category>
    </item>
    <item>
      <title>Code challenge: Make it make sense</title>
      <dc:creator>Hsabes</dc:creator>
      <pubDate>Tue, 01 Nov 2022 06:14:26 +0000</pubDate>
      <link>https://forem.com/hsabes/code-challenge-make-it-make-sense-4ahp</link>
      <guid>https://forem.com/hsabes/code-challenge-make-it-make-sense-4ahp</guid>
      <description>&lt;p&gt;You're given a string that contains several words, each word interjected with a number. Your task is to write a function that sorts the words based on the numbers that are contained within them. The function should return the sorted words still containing their respective numbers.&lt;/p&gt;

&lt;p&gt;Input:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"doi4ng y3ou 5today h1ow ar2e"&lt;/code&gt;&lt;/p&gt;

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

&lt;p&gt;&lt;code&gt;h1ow ar2e y3ou doi4ng 5today&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Solution:&lt;/p&gt;

&lt;p&gt;This is one of my favorite problem because you can use an iterating and sorting technique I find very fun. The first step is to convert our string into something we can work with by splitting it into an array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function order(string){
  return wordsObj.split(" ")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output: &lt;code&gt;["doi4ng", "y3ou", "5today", "h1ow", "ar2e"]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The objective here is fairly simple, we need to sort the numbers from lowest to highest. How can we do that when they are in a string? Regex and iteration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function order(words){
  return wordsObj = words.split(" ").map((element) =&amp;gt; ({ num: element.replace(/[^0-9]+/g, ""), word: element}))
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We've mapped through our array and for each word we've created an object with a number and its associated word. Using regex, we can extract the letters from each element, leaving us with just the number. Since we want our final output to contain the number, we'll leave the word alone. This will return:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  { num: '2', word: 'is2' },
  { num: '1', word: 'Thi1s' },
  { num: '4', word: 'T4est' },
  { num: '3', word: '3a' }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now all that remains is sorting these objects by their number, and then extract out words.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function order(words){
  const wordsObj = words.split(" ").map((element, i) =&amp;gt; ({ num: element.replace(/[^0-9]+/g, ""), word: element}))
  return wordsObj.sort((a, b) =&amp;gt; a.num - b.num)
    .map((object) =&amp;gt; object.word)
    .join(" ")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output: &lt;code&gt;"Thi1s is2 3a T4est"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Clean refactor:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function order(words){
  return words.split(" ")
    .map((element, i) =&amp;gt; ({ num: element.replace(/[^0-9]+/g, ""), word: element}))
    .sort((a, b) =&amp;gt; a.num - b.num)
    .map((object) =&amp;gt; object.word)
    .join(" ")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://www.codewars.com/kata/55c45be3b2079eccff00010f/javascript"&gt;Source&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Create a chess board with Vanilla JS</title>
      <dc:creator>Hsabes</dc:creator>
      <pubDate>Mon, 31 Oct 2022 05:47:40 +0000</pubDate>
      <link>https://forem.com/hsabes/create-a-chess-board-with-vanilla-js-1hcm</link>
      <guid>https://forem.com/hsabes/create-a-chess-board-with-vanilla-js-1hcm</guid>
      <description>&lt;p&gt;In this tutorial I'll run you through the steps for creating a chess board. Creating a chess board covers very fundamental skills for a JavaScript developer, including conditionals, loops, and iterator methods. Enjoy!&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 1 (Assuming you have your files set up)
&lt;/h1&gt;

&lt;p&gt;Create your HTML boilerplate. Here's mine:&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;title&amp;gt;Chess&amp;lt;/title&amp;gt;
        &amp;lt;link rel="stylesheet" href="styles/styles.css" /&amp;gt;
        &amp;lt;meta charset="UTF-8" /&amp;gt;
        &amp;lt;script src="scripts/index.js" defer&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;/head&amp;gt;
    &amp;lt;body&amp;gt;
        &amp;lt;div class="chessBoard"&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;This is a very basic boilerplate for our HTML file. Notice we have linked our CSS and JS files with the &lt;code&gt;&amp;lt;link&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tags. It's important to note that I've included a defer attribute. If this is not included your scripts will not execute.&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 2
&lt;/h1&gt;

&lt;p&gt;Style the ".chessBoard":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;div.chessBoard {
    width: 800px;
    height: 800px;
    border: 5px solid #000;
    margin: auto; // centers board
    line-height: 0px; // no line space on inline-blocks 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we've styled the border for the chess board which will inhabit all of the tiles for the board. The board itself is 800x800 pixels, and each tile will be 100x100 pixels. &lt;code&gt;margin: auto;&lt;/code&gt; will center the board. Since the squares themselves will be inline-blocks to get them to wrap, they will behave as text does and automatically create vertical space above and below them. To prevent this, we add &lt;code&gt;line-height: 0px;&lt;/code&gt; to the parent element of the squares.&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 3
&lt;/h1&gt;

&lt;p&gt;Render the squares:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function renderSquares(){
    let numOfSquares = 0;
    const chessBoard = document.querySelector(".chessBoard")
    const chessSquareArray = []
    while (numOfSquares &amp;lt; 64){
        chessSquare = document.createElement("div")
        chessSquare.classList.add("chessSquare")
        chessSquareArray.push(chessSquare)
        numOfSquares += 1;
    }
    chessSquareArray.forEach((element, i) =&amp;gt; {
        determineSquareColor(element, i)
        // determines square color in separate function
    })
    return chessSquareArray.forEach((element) =&amp;gt; {
        return chessBoard.append(element)
    })
}

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

&lt;/div&gt;



&lt;p&gt;Here we have written a function that handles creating the squares and appending it to the board. We declare a variable called &lt;code&gt;numOfSquares&lt;/code&gt;, and initialize it to 0. Then we grab the chess board, and create an empty array that will begin to house the squares momentarily. We write a while loop that conditionally runs based off the value of &lt;code&gt;numOfSquares&lt;/code&gt;. Since we want 64 squares, that's what we base our loop off of. Each loop we are creating a div element, assigning it a class name of &lt;code&gt;"chessSquare"&lt;/code&gt;, and pushing it to the array (don't forget the increment!). Now that we have an array of all 64 squares, we iterate through it and color each one based on its position in the array. Take a look at the function below. After this, for each element in our now-styled array of squares, append it to the chess board.&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 4
&lt;/h1&gt;

&lt;p&gt;Add logic for coloring the squares:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function determineSquareColor(element, i){
    if ((i &amp;gt;= 0 &amp;amp;&amp;amp; i &amp;lt;= 7) || (i &amp;gt;= 16 &amp;amp;&amp;amp; i &amp;lt;= 23) || (i &amp;gt;= 32 &amp;amp;&amp;amp; i &amp;lt;= 39) || (i &amp;gt;= 48 &amp;amp;&amp;amp; i &amp;lt;= 55)){
        return i % 2 === 0 ? element.style.backgroundColor = "#FFF" : element.style.backgroundColor = "#000";
    }
    if ((i &amp;gt;= 8 &amp;amp;&amp;amp; i &amp;lt;= 15) || (i &amp;gt;= 24 &amp;amp;&amp;amp; i &amp;lt;= 31) || (i &amp;gt;= 40 &amp;amp;&amp;amp; i &amp;lt;= 47) || (i &amp;gt;= 56 &amp;amp;&amp;amp; i &amp;lt;= 63)){
        return i % 2 !== 0 ? element.style.backgroundColor = "#FFF" : element.style.backgroundColor = "#000";
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The logic that handles coloring the tiles. The first row contains 8 tiles, beginning with a white tile and ending with a black tile, and vice versa each row. It's not enough to just evaluate the position of each tile based on its odd or even placement in the array. This is because each row begins and ends with a different color. We need to write two if statements. The first one determines the colors of each square for the rows that begin with a white tile. The second one determines the colors for the rows that begin with a black tile.&lt;/p&gt;

&lt;p&gt;Remember, the reason we have access to the elements we're coloring is because we passed them in as arguments from &lt;code&gt;renderSquares()&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 5
&lt;/h1&gt;

&lt;p&gt;Add dimensions to squares:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;div.chessSquare {
    width: 100px;
    height: 100px;
    display: inline-block;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Adding these properties and values will make your squares flush with the edges, and wrap them perfectly so all 64 squares fit to the board.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;And there you have it! This is a great way to exercise the basics of JavaScript such as conditionals, loops, and iterator methods.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Code Challenge: Sort only odd or even integers</title>
      <dc:creator>Hsabes</dc:creator>
      <pubDate>Mon, 24 Oct 2022 18:19:12 +0000</pubDate>
      <link>https://forem.com/hsabes/code-challenge-sort-collection-of-integers-based-on-even-or-odd-value-4i00</link>
      <guid>https://forem.com/hsabes/code-challenge-sort-collection-of-integers-based-on-even-or-odd-value-4i00</guid>
      <description>&lt;p&gt;This problem addresses a skill in coding that caused me much confusion early on in my career; segregating values based on their value (odd, even, letters, symbols, etc.), and not their data type (number, string, etc.). This problem exercises that skill, and I will walk you through step by step how to solve it. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Task&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You will be given an array of numbers. You have to sort the odd numbers in ascending order while leaving the even numbers at their original positions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;[1, 5, 2, 4, 71, 432, 0, 3, 9]&lt;/p&gt;

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

&lt;p&gt;[1, 3, 2, 4, 5, 432, 0, 9, 71]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The first thing we need to think about is how we're going to modify only the odd numbers. We can achieve this by declaring a new array with the filter method, and filtering out the odd numbers.&lt;/p&gt;

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

&lt;p&gt;&lt;code&gt;// -&amp;gt; [1, 5, 71, 3, 9]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now when we call our function, we should return an array that contains just the odd values of the original array. Now we need to sort them.&lt;/p&gt;

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

&lt;p&gt;&lt;code&gt;// -&amp;gt; [1, 3, 5, 9, 71]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Great, now we have our array with odd numbers that are sorted in ascending order. I want you to think ahead here, what is your final return going to be? Are you going to be returning the original array? Or this new array?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Final step&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Mapping through an array can be extremely valuable. JavaScript's map method can take up to three arguments: the element in the array, the index of the element, and an array. For this we'll only be utilizing the element and its index. In this case, we will only be accessing the elements that are even.&lt;/p&gt;

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

&lt;p&gt;So now we have access to the even elements and the index they were at in the original array, exactly what we need. The last thing we need to do is insert them into the array of sorted odd values we created earlier, and we have our solution.&lt;/p&gt;

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

&lt;p&gt;The splice method works here, because we can use it to insert values by specifying 0 as the second argument. The first argument of splice accepts the index the value is being inserted at, the second argument accepts an integer whose value is used to determine how many elements will be deleted past the insertion index, and the last argument accepts the value being inserted. If we pass 0 in, splice will not delete any values past the value being inserted.&lt;/p&gt;

&lt;p&gt;Now all we have to do is return our oddArr, and smile at our solution.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;// -&amp;gt; [1, 3, 2, 4, 5, 432, 0, 9, 71]&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Stretch deliverable
&lt;/h2&gt;

&lt;p&gt;Let's say instead of passing in an array, we have a string of values that can contain anything under the sun. Letters, numbers, and symbols/special characters (assume numbers are only 1 digit). For example:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"3f6em8[10}2$*./4,1!~5d"&lt;/code&gt;&lt;/p&gt;

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

&lt;p&gt;[1, 6, 8, 1, 0, 2, 4, 3, 5]&lt;/p&gt;

&lt;p&gt;This may seem very daunting at first, but let me introduce you to the magic of regex. There's a few extra steps we need to take because we're dealing not only with different values, but a different data type entirely; a string instead of an array. &lt;/p&gt;

&lt;p&gt;The first step is to get rid of everything that we don't want, in this case letters and symbols. We can do this using regex since we're dealing with a string, and then split it into an array:&lt;/p&gt;

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

&lt;p&gt;&lt;code&gt;// -&amp;gt; ['3', '6', '8', '1', '0', '2', '4', '1', '5']&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Great, now we have something to work with. Since we're able to sort numbers as string the way we normally would, everything stays the same up until the very end.&lt;/p&gt;

&lt;p&gt;We'll start by creating our oddArr again, then inserting the even numbers into that array at their respective index. &lt;/p&gt;

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

&lt;p&gt;And there's our solution!&lt;/p&gt;

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

&lt;p&gt;This output is correct, but not entirely. When we originally obtained just our numbers, the regex extracted the &lt;em&gt;integers&lt;/em&gt; but the split converted them into an array of &lt;em&gt;strings&lt;/em&gt;! All we have to do to remedy this problem is parseInt the values of our array.&lt;/p&gt;

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

&lt;p&gt;I hope you found this guide insightful and maybe you can use this kind of solution in other problems you face in the future!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.codewars.com/kata/578aa45ee9fd15ff4600090d"&gt;Source&lt;/a&gt;&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>javascript</category>
      <category>intermediate</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>React and Rails: Using Active Storage to update a user's profile picture</title>
      <dc:creator>Hsabes</dc:creator>
      <pubDate>Wed, 24 Aug 2022 23:25:00 +0000</pubDate>
      <link>https://forem.com/hsabes/react-and-rails-using-active-storage-to-update-a-users-profile-picture-300p</link>
      <guid>https://forem.com/hsabes/react-and-rails-using-active-storage-to-update-a-users-profile-picture-300p</guid>
      <description>&lt;p&gt;Ever heard of this thing called FaceBook? Every once in a while we snap a pic that features our good side so much we just have to put it on display and make it our profile picture. When you hit that "Edit Profile Picture", it opens upon a window on our local machine to upload a file, bypassing the need to utilize a web URL from something like Imgur. I'll be walking you through how to do this using active storage via a patch request to a user in our database. This guide assumes a few things: You are using React front-end, Rails back-end, and have a database with a User that has a profile-picture type column to hold the file (we can add this last one if you don't). If those three things check out for you, then let's get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up Rails
&lt;/h2&gt;

&lt;p&gt;The first thing we need to do is install Active Storage. In your terminal, run this command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails active_storage:install&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will create some tables that handle most of the leg work for you. Check your migrations, you should see something like this:&lt;/p&gt;

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

&lt;p&gt;All we need to do from here is migrate the changes.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails db:migrate&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For my project, I created a column in my User table called "avatar", with a data type of a string. The column can be called anything, but I suggest using something that indicates it should be some sort of file. If you check your schema, you'll see the migrated tables reflected from your migration.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Serializer
&lt;/h2&gt;

&lt;p&gt;All you need to do here is replace the word avatar in my serializer with whatever column in your user table is supposed to hold the image file.&lt;/p&gt;

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

&lt;p&gt;Do not forget to &lt;code&gt;include Rails.application.routes.url_helpers&lt;/code&gt;!&lt;/p&gt;

&lt;h2&gt;
  
  
  Model association macro
&lt;/h2&gt;

&lt;p&gt;In your user model, create a &lt;strong&gt;has_one_attached :avatar&lt;/strong&gt; association. &lt;/p&gt;

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

&lt;h2&gt;
  
  
  Controller
&lt;/h2&gt;

&lt;p&gt;Your controller can stay pretty much the same, just make sure in your params method you include the image attribute from your User table.&lt;/p&gt;

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

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

&lt;h2&gt;
  
  
  What if I don't have an image in my user table?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;rails g migration addImageToUsers image&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails db:migrate&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will create a new table that adds the image column to our user table, and updates the schema when we migrate these changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up React
&lt;/h2&gt;

&lt;p&gt;For my project, I used MUI for styling. It doesn't change too much if you aren't using MUI, but this guide will show you the React setup with MUI.&lt;/p&gt;

&lt;h2&gt;
  
  
  State
&lt;/h2&gt;

&lt;p&gt;This is all you need to hold the data when it comes to a patch request:&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Form
&lt;/h2&gt;

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

&lt;p&gt;There's a few things going on here for the form, so let's go line by line.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Line 131: Create the form element.&lt;/li&gt;
&lt;li&gt;Line 132: This grid item holds the button that we use to select the file from our local machine.&lt;/li&gt;
&lt;li&gt;Lines 133-140: The text in this button will change depending on if an image has been selected using a ternary expression. If the image has been selected, display the name of that image. If not, display "Change Avatar"&lt;/li&gt;
&lt;li&gt;Line 136: Input. Here we declare what type of input it is (136), what kind of file it can accept (137), the fact that it should be hidden (138), and the onChange function that will change our state variable to the image that we have selected (139).&lt;/li&gt;
&lt;li&gt;Line 142: Here is another ternary. If an image has been selected, then we will display the submit button that is responsible for running the function that makes our patch request. If no image is selected, no button will appear.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Making the request
&lt;/h2&gt;

&lt;p&gt;Now it's time to write out our handleSumbit function that is responsible for carrying out our request. You will need a current user object in the component you're making request so Rails knows which user you are sending the patch request to.&lt;/p&gt;

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

&lt;p&gt;In our &lt;code&gt;new FormData()&lt;/code&gt; variable, we're setting the value of our image column on the database to the state variable holding the file, in this case a user's avatar. The string in our append represents what's in our schema, and the variable next to it represents the file that's been uploaded to our state variable.&lt;/p&gt;

&lt;p&gt;For debugging purposes, if you want to check to see if a file has been uploaded before making the patch request, &lt;code&gt;console.log(avatarData)&lt;/code&gt; &lt;strong&gt;outside&lt;/strong&gt; of the patch request. When the file has been uploaded (but not sent), in your browser console you should see a file object holding the information about the file being uploaded.&lt;/p&gt;

&lt;h2&gt;
  
  
  That's it!
&lt;/h2&gt;

&lt;p&gt;This is all you need to get Active Storage up and running. Having said that, getting Active Storage to run properly the first time you've learned it is... challenging. So here's some resources you can use to clarify any potential problems:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://edgeguides.rubyonrails.org/active_storage_overview.html"&gt;https://edgeguides.rubyonrails.org/active_storage_overview.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/jblengino510/uploading-files-in-a-react-rails-app-using-active-storage-201c"&gt;https://dev.to/jblengino510/uploading-files-in-a-react-rails-app-using-active-storage-201c&lt;/a&gt;&lt;/p&gt;

</description>
      <category>rails</category>
      <category>react</category>
    </item>
    <item>
      <title>Rails: Self Referential Tables</title>
      <dc:creator>Hsabes</dc:creator>
      <pubDate>Thu, 04 Aug 2022 19:30:00 +0000</pubDate>
      <link>https://forem.com/hsabes/rails-self-referrential-tables-4mhl</link>
      <guid>https://forem.com/hsabes/rails-self-referrential-tables-4mhl</guid>
      <description>&lt;p&gt;For Sunday Night Football we crack open some cold ones with the boys, our friends. In other words, we're interacting with our friends. Virtually every social media application that has ever existed has some form of interaction between a user and their friends. You can message them, play games with them, tag them in posts, or add new friends... but what's actually happening on the back end? From a rails stand point, you can use Self Referrential Tables.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are self referrential tables?
&lt;/h2&gt;

&lt;p&gt;Take my previous Sunday Night Football example; we're interacting with friends right? But the word "friend" is a word we use to explain the &lt;em&gt;relationship between two people&lt;/em&gt;. This is the basis of self referrential tables. A user has_many friends, but in technical terms, a user has_many &lt;em&gt;other users&lt;/em&gt;. Self referrential tables are very confusing at first, so I'll give you some examples on how this data dance looks on the backend.&lt;/p&gt;

&lt;h2&gt;
  
  
  Models, serializers, and controllers... oh my!
&lt;/h2&gt;

&lt;p&gt;For a self referrential table, we need two models (assuming a social media application for this example): The users, and the relationship: friends. Through this friends table you'll be able to connect users different ways. In this example we'll be looking at a following system. Take a look at this User.rb model from a recent project of mine:&lt;/p&gt;

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

&lt;p&gt;User.rb has_many catpanions, that's the name of the self referrential table connecting users. So what's this &lt;code&gt;has_many :friends, through: :catpanions&lt;/code&gt; business? Well, let's now take a look at the Catpanions.rb model:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UJzBV153--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7n17bel35l91dg2r086s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UJzBV153--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7n17bel35l91dg2r086s.png" alt="catpanion model" width="756" height="268"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So a catpanion belongs_to a user, but &lt;em&gt;also&lt;/em&gt; belongs_to a friend. We haven't made a friend model, so what's going on here? We've essentially told Ruby that there's other instances called friends that are really just instances of the User class. Now we need our foreign_keys.&lt;/p&gt;

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

&lt;p&gt;These IDs are accessible because we declared our relationships when creating the resources:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KU8-AzIM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0rk25fce45fiicf4uh02.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KU8-AzIM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0rk25fce45fiicf4uh02.png" alt="catpanions create table" width="880" height="269"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With all this set up, in order to create a friendship between two users you'll have to create the route in the controller handling the self referrential table. &lt;/p&gt;

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

&lt;p&gt;When we fetch this route, we're telling the engine to first see if the user we're trying to add already exists in our friendships. If so, throw an error. If not, you have a new friend! The params used for the create should be the user_id and the friend_id. If you create a route that shows all the friendships, it should look something like this:&lt;/p&gt;

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

&lt;p&gt;Keep in mind, the user_id is the current user, and the friend_id is the person that they have followed. For the final touches, we want a way to access the people we've followed, right? In your user serializer, add the attribute &lt;code&gt;:friends&lt;/code&gt;:&lt;/p&gt;

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

&lt;p&gt;Now if we take a look at a specific user, our data will reflect their friendships with the users they have followed:&lt;/p&gt;

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

&lt;p&gt;This kind of data can be extremely powerful when establishing some form of connection between users. If you're feeling a little overwhelmed don't worry, I had to play around with this concept for days to feel comfortable enough to use it in a project!&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>sql</category>
      <category>database</category>
    </item>
    <item>
      <title>Let's talk about burnout</title>
      <dc:creator>Hsabes</dc:creator>
      <pubDate>Thu, 21 Jul 2022 06:33:09 +0000</pubDate>
      <link>https://forem.com/hsabes/lets-talk-about-burnout-3bmi</link>
      <guid>https://forem.com/hsabes/lets-talk-about-burnout-3bmi</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Let's take a moment and talk about something every programmer ever has and will experience. As the tags for this blog suggest, burnout is accrued by any programmer no matter what stage in their journey they are at. Whether you're just starting to learn or have been in the biz for years, ignoring workplace indicators, symptoms, and approaches to resolving burnout will incur a tax to your digital lifetime. Let's face it, sweeping this problem under the rug isn't enough fix it. So, let's talk about burnout.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is it, and what causes it?
&lt;/h2&gt;

&lt;p&gt;Burnout is a form of mental exhaustion caused by constantly feeling overwhelmed. Generally speaking, burnout is exclusive to workplace duties, in the form of prolonged mental or physical distress. Outside of the world of technology, not much is understood about the projects we build for every day use. This can lead to pretty unrealistic expectations when building websites or applications for clients. That said, this relentless exhaustion can be the result of various culprits. A &lt;a href="https://haystack-books.s3.amazonaws.com/Study+to+understand+the+impact+of+COVID-19+on+Software+Engineers+-+Full+Report.pdf"&gt;2021 study&lt;/a&gt; by Haystack shed some light on the reality of burnout:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;83% of software developers experience burnout&lt;/li&gt;
&lt;li&gt;The top reasons for burnout: 47% said it was due to high workloads, 31% cited inefficient proccess, and 29% answered unclear goals/expectations.&lt;/li&gt;
&lt;li&gt;And many, many more reasons:&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;The glaring thing about these reasons isn't the most common ones, it's &lt;strong&gt;the least&lt;/strong&gt; common one. Only 1% of software developers &lt;em&gt;didn't&lt;/em&gt; know what the cause of their burnout was.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Signs of burnout and ways to address it
&lt;/h2&gt;

&lt;p&gt;According to &lt;a href="https://www.webmd.com/mental-health/burnout-symptoms-signs#:~:text=Burnout%20is%20a%20form%20of,up%20with%20life's%20incessant%20demands."&gt;WebMD&lt;/a&gt;, the signs of burnout can include exhaustion, alienation from workplace activities, reduced performance, and lack of enthusiasm. Ideally, these are all things we want to avoid in the field of work we love, so how can we address these problems for a healthier mental state? &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Setting expectations with your supervisor can be a great start if you are unclear about the goals they have set with you, or are constantly moving goal posts. Our job is hard enough, being confused about the finish line is the last thing we need.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;During my studies at &lt;a href="https://flatironschool.com/"&gt;Flatiron School&lt;/a&gt;, my cohort would meet at the end of the day and talk about our roadblocks. Without a shed of doubt in my mind I can confidently say the most common roadblock in my cohort was lack of sleep. Getting good sleep not only keeps us physically in tune, but mentally sharp. Being down in the dumps after a 4 hour sleep night will take a pretty big toll on your performance (especially if you're working in an office and can't take the cherished nap). &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Exercise. Exercise. Exercise. Have I mentioned exercise? This helps trumendously with a wide variety of issues programmers face day to day. Mental sharpness, posture, and motivation... to name just a few. If you can't make it to the gym, get outside for some fresh air and walk around the block. I find that this is particularly helpful if I'm stuck on a tough problem. Stepping away from the computer for a breath of fresh air for 10 minutes somehow gives me the ability to solve a problem I've been working on for hours. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Work/Life balance. Don't compromise on the things that you love! Whether it's an evening hobby or cracking open some cold ones with the boys for Monday Night Football, we can't completely sacrifice the things we love to do in the name of hitting that deadline (within reason). What are some of the things you do in your spare time to take your mind off work? (It's disc golf for me)&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Burnout is a very real reaction to common workplace struggles especially as a developer, don't ignore it, fight it! &lt;/p&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>intermediate</category>
      <category>advanced</category>
    </item>
    <item>
      <title>Directed Graphs, Strongly Connected Components, and Kosaraju's Algorithm</title>
      <dc:creator>Hsabes</dc:creator>
      <pubDate>Fri, 15 Jul 2022 20:17:36 +0000</pubDate>
      <link>https://forem.com/hsabes/directed-graphs-strongly-connected-components-and-kosarajus-algorithm-4fm3</link>
      <guid>https://forem.com/hsabes/directed-graphs-strongly-connected-components-and-kosarajus-algorithm-4fm3</guid>
      <description>&lt;p&gt;As software engineers, one of the most daunting part of our career is facing algorithm problems in a technical interview. Algorithms can range from simple, testing only basic knowledge of a programming language, to advanced. The topic of this blog post will be about Strongly Connected Components and Directed Graphs, something you may experience in a technical interview.&lt;/p&gt;

&lt;h2&gt;
  
  
  Directed Graphs
&lt;/h2&gt;

&lt;p&gt;Simplified, a directed graph is a type of graph the utilizes vertexes connected by arrows, or edges. These edges can be referred to in two different ways, either indegree or outdegree. A vertex that has 1 indegree edge has an edge that points to that vertex. If it also has 1 outdegree edge, it has an edge pointing outward to another vertex. Here is a simple directed graph:&lt;/p&gt;

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

&lt;p&gt;If you look at vertex 11, you'll see it is has several edges pointing to it, and several edges pointing to other vertexes. In total, there are 3 outdegree edges and 2 indegree edges for vertex 11. This is the basic structure of a directed graph.&lt;/p&gt;

&lt;h2&gt;
  
  
  Strongly Connected Components
&lt;/h2&gt;

&lt;p&gt;So what are strongly connected components? Well, we've all seen a graph like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hWPQpBie--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b2hqbg3c9u0n6cmi282t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hWPQpBie--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b2hqbg3c9u0n6cmi282t.png" alt="Two components with no common vertex" width="880" height="552"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have two separate components, both with a few individual nodes in each component. These components are not connected in any way, because there is not common node between the two components. As such, there is no way for any node in the left component to reach any node in the right component. But what happens if they do share a common node?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CMlHPDV0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a1o44dxnwcfbtdii106l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CMlHPDV0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a1o44dxnwcfbtdii106l.png" alt="Two separate components with common vertex" width="880" height="548"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that they have a common node, any node within the left component can be reached from the right component, and vice versa. Here is another way to visualize this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Qc6UdiZS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z7gfsyuu0jz0kln5tj1n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Qc6UdiZS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z7gfsyuu0jz0kln5tj1n.png" alt="directed graph in two components" width="880" height="605"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What previously was two separate components connected by a common vertex in a directed graph is now joined into one strongly connected component, and there is no need to visualize it as two separate components but rather one strongly connected component:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VwONPpkp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8eljxv6ojdgcjke8md1d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VwONPpkp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8eljxv6ojdgcjke8md1d.png" alt="one strongly connected component" width="880" height="852"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Kosaraju's Algorithm
&lt;/h2&gt;

&lt;p&gt;To first understand Kosaraju's Algorithm, we'll need to understand further how strongly connected components exist in a directed graph. The following screen shots shows several vertexes. Can you identify which vertexes are strongly connected? How would you re-write this directed graph to represent the existence of strongly connected components? How many strongly connected components are there?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vTowH9Ow--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i46gngext273o7in07tw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vTowH9Ow--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i46gngext273o7in07tw.png" alt="Directed graph w/o strongly connected components" width="880" height="614"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Take a moments and think about it!&lt;/p&gt;




&lt;p&gt;Here's the solution:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fx-hA36c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ox1r2wtv4v82f8q13ca.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fx-hA36c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ox1r2wtv4v82f8q13ca.png" alt="Directed graph w/ strongly connected components" width="880" height="518"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The solution to the problem is 2 strongly connected components. But what if we have a directed graph like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8wC4_0Li--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cx5vi4ng4ogvrumywoch.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8wC4_0Li--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cx5vi4ng4ogvrumywoch.gif" alt="Complex directed graph" width="404" height="325"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It may be much harder to visualize the amount of strongly connected components in a directed graph in which each vertex has several indegree and outdegree edges. Luckily for us, this is where Kosaraju's Algorithm comes into play. The purpose of the algorithm is to find the amount of strongly connected components in a directed graph. Kosaraju's Algorithm is extremely complex, so here's a resource that implements it in a practical situation: &lt;/p&gt;

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

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Hopefully this blog will serve you in some way when you begin interviewing for software engineering positions. You never know, you may come across a directed graph problem!&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Three beginner CSS tips to make your life easier</title>
      <dc:creator>Hsabes</dc:creator>
      <pubDate>Sat, 18 Jun 2022 00:38:48 +0000</pubDate>
      <link>https://forem.com/hsabes/three-beginner-css-tips-to-make-your-life-easier-49no</link>
      <guid>https://forem.com/hsabes/three-beginner-css-tips-to-make-your-life-easier-49no</guid>
      <description>&lt;p&gt;When I started learning how to code, the first two languages I learned were HTML and CSS. Like most people, I found HyperText Markup Language to be fairly straight forward. The element names were intuitive, the structure made sense, and was fairly easy to memorize the more discrete details. Cascading Style Sheets was starkly different in comparison, especially for a beginner. I found myself caught in a lot of frustration trying to figure out why elements were behaving one way when I was expecting them to act in a different way. Here are a few tips that I found helpful when learning CSS.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tip 1: Selectors
&lt;/h2&gt;

&lt;p&gt;The first thing you're going to learn when jumping into CSS is how do I actually style my webpage? In order to style the elements in your HTML document, you'll need to use selectors. With CSS selectors you can style one element, a collection of elements, everything at once, or even style certain elements on an event that occurs when a user is interacting with your webpage (we'll get to that last one a little later). Here's your basic HTML webpage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 &amp;lt;!DOCTYPE html&amp;gt;
2
3 &amp;lt;html lang="en"&amp;gt;
4    &amp;lt;head&amp;gt;
5        &amp;lt;title&amp;gt;Hello World&amp;lt;/title&amp;gt;
6        &amp;lt;link rel="stylesheet" href="./styles.css" /&amp;gt;
7        &amp;lt;meta charset="UTF-8"&amp;gt;
8    &amp;lt;/head&amp;gt;
9    &amp;lt;body&amp;gt;
10       &amp;lt;h1&amp;gt;Hello World!&amp;lt;/h1&amp;gt;
11   &amp;lt;/body&amp;gt;
12 &amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's say we want to get spicy and change the color of our &lt;code&gt;h1&lt;/code&gt; tag to red. If you notice on line 6, we've linked our CSS file, titled &lt;em&gt;styles.css&lt;/em&gt;, to our HTML file. This grants our CSS file access to modify our HTML elements. Now we can use our selectors to target our &lt;code&gt;h1&lt;/code&gt; element:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 h1 {
2     color: red;
3 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But what if we have more than one &lt;code&gt;h1&lt;/code&gt; tag, and want them to be different colors? Using IDs!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10 &amp;lt;h1 id="beans"&amp;gt;Potatoes&amp;lt;/h1&amp;gt;
11 &amp;lt;h1 id="greens"&amp;gt;Potatoes&amp;lt;/1&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our CSS will have a slightly different syntax to target the specific ID's of those h1 tags:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 #beans {
2     color: red;
3 }
4 
5 #greens {
6     color: blue;
7 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;CSS uses hashtags to tell the engine that it's targeting an element with an ID of whatever follows the hashtag. Pretty nifty. &lt;/p&gt;

&lt;p&gt;Selecting elements can go even further with classes. Lets say you want the first &lt;code&gt;h1&lt;/code&gt; tag to be red, the second &lt;code&gt;h1&lt;/code&gt; tag to be blue, but you want &lt;strong&gt;both&lt;/strong&gt; of them to have a border? We can keep our IDs, but this time we'll add on classes to our &lt;code&gt;h1&lt;/code&gt; tags:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10 &amp;lt;h1 id="beans" class="tomatoes"&amp;gt;Potatoes&amp;lt;/h1&amp;gt;
11 &amp;lt;h1 id="greens" class="tomatoes"&amp;gt;Potatoes&amp;lt;/1&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To target our the class of "tomatoes", our CSS will need to use dotted notation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;9  .tomatoes {
10     font-size: 100px; /* larger size for readability */
11     text-shadow: -1px 0 black, 0 1px black, 1px 0 black ,0 -1px black;
12 } 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now your headers should have a neat little border around them! &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%2Fdtk0heidq6vitb6z6kzz.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%2Fdtk0heidq6vitb6z6kzz.png" alt="styling using id and class"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Already things are looking stylish.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tip 2: Properties
&lt;/h2&gt;

&lt;p&gt;If you want to be a pro at CSS, knowing your properties and how they interact with your webpage is key. There are over 200 properties, of which have multiple values, so committing all of them to memory &lt;em&gt;and&lt;/em&gt; their value pairs &lt;em&gt;&lt;strong&gt;AND&lt;/strong&gt;&lt;/em&gt; what they all do would be quite the task. To get you started, here's a few that can instantly turn a web page into a more organized experience for a user:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Display&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Display is a good property to understand and implement when you need to. By default, each element has a display of either block or inline, although there are other values that can be useful such as inline-block or none. The following elements' display are as follows:&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Inline&lt;/u&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;span&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Block level elements (like our h1 tags) by default go from top to bottom by making a new line, and take up the full width of the web page unless specified otherwise. If you notice in our Potatoes headers, they are stacked on top of eacher. But what would happen if we changed those h1 tags to a span tag?&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%2Fvvntcidd0xyvjh4o0mf9.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%2Fvvntcidd0xyvjh4o0mf9.png" alt="inline elements"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now they're being displayed inline! Knowing this distinction between block and inline displays, and which elements are either by default will save you a lot of confusion moving forward.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Block&lt;/u&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; through &lt;code&gt;&amp;lt;h6&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;form&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;header&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;footer&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;section&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;/ul&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%2Ff9vhktp6f4zywjrjy2iy.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%2Ff9vhktp6f4zywjrjy2iy.png" alt="inline divs"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the picture above we have 4 div elements inside a parent div. Each one has a specific id to change the color, and all of them have a class name that modify their width, font-size, and padding. You may have noticed one other thing about these block elements that seem to contradict what I said earlier... can you identify what it is?&lt;/p&gt;

&lt;p&gt;Block elements by default go from top to bottom, but these ones are going from left to right, or &lt;code&gt;inline&lt;/code&gt;. The key difference between block and inline elements isn't just the direction they render, it's your ability to style them as well. The height/width of an inline element &lt;em&gt;cannot&lt;/em&gt; be altered, only block level elements. So if we want to alter the height/width of our block level elements &lt;strong&gt;and&lt;/strong&gt; have them be inline, you can use &lt;code&gt;display: inline-block;&lt;/code&gt; or &lt;code&gt;float: left;&lt;/code&gt;. Both of these will achieve the same thing, but typically &lt;code&gt;display: inline-block&lt;/code&gt; will be better. This is because when developers are maintaining your code at a later point, &lt;code&gt;display: inline-block;&lt;/code&gt; will make it much more obvious as to what you're trying to achieve. Another option is &lt;code&gt;display: flex;&lt;/code&gt;. This gives you access to more styling options:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;justify-content&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So we have all of our inline divs pushed to the left of our cotainer. What if we have a header, and each of those colored divs are a button we can press that will navigate us to a new page? Having them all pushed to the left could be fine, but we may want them all centered across the screen. &lt;code&gt;justify-content&lt;/code&gt; gives us a few ways to alter our header:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;justify-content: flex-end&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This will push the content in our parent div to the right, or the end:&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%2Fpsv3swdvmn73xrit7edr.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%2Fpsv3swdvmn73xrit7edr.png" alt="flex end"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;justify-content: space-between&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This will separate your content so the space between all of them are equal:&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%2Fiuc61qbuneq1moimbgy8.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%2Fiuc61qbuneq1moimbgy8.png" alt="space between"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;justify-content: space-evenly&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This will create equal space between the edges of all divs, including the outermost child divs and the parent:&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%2Fog75icbpotvzgi93hhud.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%2Fog75icbpotvzgi93hhud.png" alt="space evenly"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;justify-content: space-around&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Similar to space eveny, but spaces on either edge have half-sized spaces:&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%2Fpt8uf73tr264lb25m8dv.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%2Fpt8uf73tr264lb25m8dv.png" alt="space around"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Tip 3: Hover
&lt;/h2&gt;

&lt;p&gt;The hover selector is a fantastic and simple selector tool you can easily implement into your CSS. Consider this HTML:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;9    &amp;lt;body&amp;gt;
10       &amp;lt;p&amp;gt;Hello World!&amp;lt;/p&amp;gt;
11   &amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;...and this CSS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 p {
2     font-size: 100px;
3     color: red;
4 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What if we wanted to change the styling of our paragraph tag when the user hovers over the element with their mouse? Well, we can simply create another selector that's called on the same element, but with the &lt;code&gt;:hover&lt;/code&gt; selector attached!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 p {
2     font-size: 100px;
3     color: red;
4 }
5
6 p:hover {
7     color: blue;
8 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are many ways this can be implemented, the one we've done here is very basic. But here's an exercise, go to any web page. When you hover over a button, does the styling change? Is there a border when you hover over it? Does the color change or is there a shadow? &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bonus Tip&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Transition&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Transition is one of my favorite properties in CSS. It makes things look very smooth when a user interacts with a web page! Let's take our previous example, but add a transition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 p {
2     font-size: 100px;
3     color: red;
4 }
5
6 p:hover {
7     color: blue;
9     transition: 1s;
8 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you tried out the hover in our last example, you probably noticed it switch color instantly. With the transition property, the change in styling will take effect over the course of 1 second, or however many seconds you want it to take. Try it out! We're still not done though... what if you don't want the styling to instantly revert back to it's normal state? Simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10 p:not(:hover) {
11     transition: 1s;
12 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we've told CSS that we want it to revert back to our old styling over the course of one second when the user is no long hovering over the element.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;CSS is basic in syntax, but can get extremely complex in functionality. There are very advanced implementations of CSS out there, but this is a great starting point for beginners.&lt;/p&gt;

&lt;p&gt;For more information on CSS properties, visit &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference" rel="noopener noreferrer"&gt;MDN&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>css</category>
      <category>html</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
