<?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: Christopher Ninman</title>
    <description>The latest articles on DEV Community by Christopher Ninman (@alternate_robot).</description>
    <link>https://dev.to/alternate_robot</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%2F748005%2Fefbda3f9-62e8-4b4a-a389-86c42cf627e5.jpeg</url>
      <title>DEV Community: Christopher Ninman</title>
      <link>https://dev.to/alternate_robot</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alternate_robot"/>
    <language>en</language>
    <item>
      <title>Consider This Before Starting Your Spotify API Project</title>
      <dc:creator>Christopher Ninman</dc:creator>
      <pubDate>Tue, 14 Jun 2022 17:46:23 +0000</pubDate>
      <link>https://dev.to/alternate_robot/consider-this-before-starting-your-spotify-api-project-p34</link>
      <guid>https://dev.to/alternate_robot/consider-this-before-starting-your-spotify-api-project-p34</guid>
      <description>&lt;p&gt;The Spotify API is home to an incredible wealth of information about artists, albums, and songs, as well as individual Spotify users. If you are a Spotify user, you know obviously know how amazing it is to have access to almost any music ever released in one place. As a developer, it is exciting to know that all that information is available for you to use, too. But not in the snap of a finger. If you have not tried your hand yet at developing an app using the Spotify API, this article will attempt to tackle some of what you can expect, and hopefully help you decide if the effort is worth your time.&lt;/p&gt;

&lt;h1&gt;CONSIDERATION #1: You Need a Spotify Account&lt;/h1&gt;

&lt;p&gt;Head to the &lt;a href="https://developer.spotify.com/dashboard/"&gt;Spotify Developer Dashboard&lt;/a&gt; and log into your Spotify Account. Once logged in, you'll have the opportunity to "Create An App". You will find that you have a "Client ID", and a hidden "Client Secret". These will be used when making requests to the API. The process of registering your App to get started is remarkably simple.&lt;/p&gt;

&lt;h1&gt;CONSIDERATION #2: Using OAuth&lt;/h1&gt;

&lt;p&gt;If you are unfamiliar with OAuth, you may be in for a world of frustration here, though it is no doubt a good skill to add to your arsenal. Basically, what you need to do is create a multi-faceted custom link which contains your client ID, multiple scopes, and a redirect link. A user will click and be sent to this link, where they will log into Spotify and give permission for your app to access various information from them, which you have declared in your scopes. Once they have logged in and granted permission, they will be sent to your redirect link and the URL will now contain and "code" param which you'll need to use to get an access token.&lt;/p&gt;

&lt;p&gt;NOTE: Make sure any redirect link is registered on your app's dashboard under the Edit Settings &amp;gt; Redirect_URIs section.&lt;/p&gt;

&lt;h1&gt;CONSIDERATION #3: Using .env Variables&lt;/h1&gt;

&lt;p&gt;You obviously don't want to share your &lt;strong&gt;Client ID&lt;/strong&gt; and especially your &lt;strong&gt;Secret Client ID&lt;/strong&gt; with just anyone who's using your app. You'll need to keep them hidden in a .env variable, another new concept for coding beginners. And the setup of a .env variable will almost certainly differ from your development phase to when you deploy it.&lt;/p&gt;

&lt;h1&gt;CONSIDERATION #4: Development vs. Deployment&lt;/h1&gt;

&lt;p&gt;Just as your .env setup will differ, you'll also have to consider how your redirect links from Spotify will work. When developing, you'll be redirecting Spotify to "localhost:3000" or something similar, which will change once you have deployed your app and are sending it to the "herokuapp.com" url. Seems simple in theory, but in my experience caused more headache than expected.&lt;/p&gt;

&lt;h1&gt;CONSIDERATION #5: Getting an Access Token&lt;/h1&gt;

&lt;p&gt;In order to make a request that returns data from the Spotify API, you'll need an access token. You'll need to exchange the "code" param that was returned earlier to get an access token. This "code" will be used in a fetch request along with your Client ID and Secret Client Id (which you'll want in a .env variable). The Spotify documentation states that you will need to Base64 encode and set the 'Content-Type' to 'application/x-www-form-urlencoded'.&lt;/p&gt;

&lt;p&gt;As a student at a programming bootcamp, my requirement was to use Rails as the backend of my project. Days of frustration over trying to properly Base64 encode my IDs, only to finally succeed when finding a blog that I had previously dismissed because it did not have any Base64 encoding done, and seemed to be outdated as it doesn't seem to match up with the &lt;a href="https://developer.spotify.com/documentation/general/guides/authorization/code-flow/"&gt;Spotify documentation&lt;/a&gt;. Which leads to:&lt;/p&gt;

&lt;h1&gt;CONSIDERATION #6: Using a Library&lt;/h1&gt;

&lt;p&gt;Depending on which programming language you're planning to use, someone may have already made the process easier for you. Spotify has actually compiled &lt;a href="https://developer.spotify.com/documentation/web-api/libraries/"&gt;a list of libraries&lt;/a&gt; in their docs which can be a huge help. I recommend checking out any libraries specific to your planned language.&lt;/p&gt;

&lt;h1&gt;CONSIDERATION #7: Follow Along With a Youtube Video&lt;/h1&gt;

&lt;p&gt;There are many walkthroughs on Youtube for how to implement the Spotify Authorization Flow and create an app. There is a lot to explain, so be prepared to be flown through the essentials. And you may get lost if you stray from their flow, or are using a different setup.&lt;/p&gt;

&lt;h1&gt;CONSIDERATION #8: The Access Token is Only Valid for One Hour&lt;/h1&gt;

&lt;p&gt;You will rejoice once you finally receive an access token and can make your first API call. But be warned, that access token will only work for 1 hour, and will then expire. &lt;/p&gt;

&lt;p&gt;When you receive your access token, you will also receive a refresh token. Whenever your user's access token is expired (or almost expired) you'll need to use the refresh token to make a request to the Spotify API to receive another access token. So you'll have to figure out a clever way to make sure that your user never ends up with an expired access token where their request for information returns a 401 error and they wonder why they ever decided to use your app.&lt;/p&gt;

&lt;h1&gt;CONSIDERATION #9: Development Mode&lt;/h1&gt;

&lt;p&gt;You finally have your app deployed and out there for the world to use. So why can't they use it? &lt;/p&gt;

&lt;p&gt;Your app is automatically considered to be in the development phase by Spotify, which means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your application is limited to 25 users (Spotify accounts)&lt;/li&gt;
&lt;li&gt;Each of those users must be added to the "Users and Access" section on the dashboard&lt;/li&gt;
&lt;li&gt;A limited number of requests can be made to the API within any given 30 seconds&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;CONSIDERATION #10: Extended Quota Mode&lt;/h1&gt;

&lt;p&gt;In order for your app to be used by anyone (at least to make calls to Spotify API), you'll need to submit a "Quota Extension Request". "Submitting a quota extension request will trigger a review by Spotify to ensure your app complies with the &lt;a href="https://developer.spotify.com/policy/"&gt;Spotify Developer Policy&lt;/a&gt;."&lt;/p&gt;

&lt;p&gt;This means that Spotify has the right to disable or block your app from being used by the general public if it isn't up to their standards. Additionally, any changes made to the your app's OAuth scopes will require another Quota Extension Request, and you are currently limited to one such request.&lt;/p&gt;

&lt;h1&gt;FINAL VERDICT&lt;/h1&gt;

&lt;p&gt;Navigating the Spotify API will consume a great deal of your time, unless you are already a seasoned veteran. If you are planning a small project or just interested in toying around with the Spotify API, you may find yourself investing way more time and energy than it is worth.&lt;/p&gt;

&lt;p&gt;However, if you are passionate about your music and really interested and dedicated to creating an app that allows people (and yourself) to experience music in a new way, you will be hard-pressed to find a more expansive and organized database to help you accomplish your vision. And you will be certain to learn many new coding concepts along the way. &lt;/p&gt;

&lt;p&gt;Sounds appealing?&lt;/p&gt;

&lt;p&gt;Photo by &lt;a href="https://unsplash.com/photos/SwKf1x2_hRo?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditShareLink"&gt;Thibault Penin&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/students?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

</description>
      <category>spotify</category>
      <category>webdev</category>
      <category>api</category>
    </item>
    <item>
      <title>CSS Styled React Components</title>
      <dc:creator>Christopher Ninman</dc:creator>
      <pubDate>Tue, 14 Jun 2022 15:53:56 +0000</pubDate>
      <link>https://dev.to/alternate_robot/css-styled-react-components-3cce</link>
      <guid>https://dev.to/alternate_robot/css-styled-react-components-3cce</guid>
      <description>&lt;p&gt;You're making React Components that have all the information that you had planned. The problem is that they're not looking how you had envisioned. We'll be taking a look at the options of styling your components without the need to download outside libraries. &lt;/p&gt;

&lt;h1&gt;What We're Starting With&lt;/h1&gt;

&lt;p&gt;For the remainder of this article, we'll be using a React Component with an &lt;strong&gt;image&lt;/strong&gt;, an &lt;strong&gt;h1&lt;/strong&gt;, and an &lt;strong&gt;h2&lt;/strong&gt;. It will remain constant throughout, and we'll style it the same each time. We'll just use different methods to accomplish that.&lt;/p&gt;

&lt;p&gt;Our barebones React Component without any styling:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import casaDelArbol from '../images/CSS-in-React.JPG'

function StyledReactComponent() {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;img src={casaDelArbol}/&amp;gt;
      &amp;lt;h1&amp;gt;Casa Del Arbol&amp;lt;/h1&amp;gt;
      &amp;lt;h2&amp;gt;Banos, Ecuador&amp;lt;/h2&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default StyledReactComponent;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which gives us:&lt;/p&gt;

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

&lt;p&gt;We have a &lt;strong&gt;div&lt;/strong&gt; that contains three elements: our &lt;strong&gt;img&lt;/strong&gt;, &lt;strong&gt;h1&lt;/strong&gt;, and &lt;strong&gt;h2&lt;/strong&gt;. Our image is named "CSS-in-React.JPG" and is located in a folder called "images". We are importing it from there into our &lt;strong&gt;StyledReactComponent&lt;/strong&gt; and giving it the name &lt;em&gt;casaDelArbol&lt;/em&gt;.&lt;/p&gt;

&lt;h1&gt;How We Want Our Component To Look&lt;/h1&gt;

&lt;p&gt;Here's what we want to change in our component:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DIV&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Change the background color&lt;/li&gt;
&lt;li&gt;Give it a border&lt;/li&gt;
&lt;li&gt;Round the corners&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;IMG&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Make it smaller, not covering entire div&lt;/li&gt;
&lt;li&gt;Give it margins&lt;/li&gt;
&lt;li&gt;Round the corners&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;H1&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Center the text&lt;/li&gt;
&lt;li&gt;Underline the text&lt;/li&gt;
&lt;li&gt;Change the color&lt;/li&gt;
&lt;li&gt;Decrease the margins&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;H2&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Center the text&lt;/li&gt;
&lt;li&gt;Italicize the text&lt;/li&gt;
&lt;li&gt;Change the color&lt;/li&gt;
&lt;li&gt;Decrease the margins&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;OPTION #1: Inline Styling&lt;/h1&gt;

&lt;p&gt;We can style each element right inside the JSX of that component. We'll need to use the following syntax to achieve that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;style={{}}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A few things to note:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Make sure to use double curly brackets&lt;/li&gt;
&lt;li&gt;CSS attributes are camelCase (text-align becomes textAlign)&lt;/li&gt;
&lt;li&gt;A colon follows the attribute name&lt;/li&gt;
&lt;li&gt;Attribute values are inside quotes&lt;/li&gt;
&lt;li&gt;Multiple attributes can be used, separated by a comma&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's our component using inline styling:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function StyledReactComponent() {
    return (
        &amp;lt;div style={{backgroundColor: ' #F8F0E3', width: '400px', border: '5px solid #6C5B7B', borderRadius: '10px'}}&amp;gt;
            &amp;lt;img style={{width: '90%', marginRight: '5%', marginLeft: '5%', marginTop: '10px', borderRadius: '5px'}} src={casaDelArbol}/&amp;gt;
            &amp;lt;h1 style={{textAlign: 'center', margin: '3px', color: '#355C7D', textDecoration: 'underline'}}&amp;gt;Casa Del Arbol&amp;lt;/h1&amp;gt;
            &amp;lt;h2 style={{textAlign: 'center', margin: '3px', color: '#6C5B7B', fontStyle: 'italic'}}&amp;gt;Banos, Ecuador&amp;lt;/h2&amp;gt;
        &amp;lt;/div&amp;gt;
    );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which returns: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--H5MQ4Vtm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4qsjr090pf79jgis76va.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--H5MQ4Vtm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4qsjr090pf79jgis76va.png" alt="Styled treehouse on horizon component" width="866" height="510"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can hit enter and rearrange our code to place attributes on separate lines, but if we're using multiple attributes, inline styling can quickly make our code much more difficult to read. Inline styling is the quickest and easiest method to implement, but is best served in limited use. A possible example would be when you just want to change the bottom margin of an element. &lt;/p&gt;

&lt;h1&gt;OPTION #2: Inline Styling Using Variables&lt;/h1&gt;

&lt;p&gt;We can clear up our code a bit by creating a variable for each styled element with all our style attributes.&lt;br&gt;
&lt;/p&gt;

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

  const styledDiv = {
    backgroundColor: ' #F8F0E3', 
    width: '400px', 
    border: '5px solid #6C5B7B', 
    borderRadius: '10px'
  }
  const styledImg = {
    width: '90%', 
    marginRight: '5%', 
    marginLeft: '5%', 
    marginTop: '10px', 
    borderRadius: '5px'
  }
  const styledH1 = {
    textAlign: 'center', 
    margin: '3px', 
    color: '#355C7D', 
    textDecoration: 'underline'
  }
  const styledH2 = {
    textAlign: 'center', 
    margin: '3px', 
    color: '#6C5B7B', 
    fontStyle: 'italic'
  }

  return (
    &amp;lt;div style={styledDiv}&amp;gt;
      &amp;lt;img style={styledImg} src={casaDelArbol}/&amp;gt;
      &amp;lt;h1 style={styledH1}&amp;gt;Casa Del Arbol&amp;lt;/h1&amp;gt;
      &amp;lt;h2 style={styledH2}&amp;gt;Banos, Ecuador&amp;lt;/h2&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: Within our JSX, when we define our &lt;em&gt;style={variable}&lt;/em&gt;, we need to use single brackets.&lt;/p&gt;

&lt;h1&gt;OPTION #3: Define a className and use CSS file&lt;/h1&gt;

&lt;p&gt;This is the method recommended in the React Docs, in which they state that "CSS classes are generally better for performance than inline styles."&lt;/p&gt;

&lt;p&gt;Inside our CSS file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.styled-div {
  background-color: #F8F0E3;
  width: 400px;
  border: 5px solid #6C5B7B;
  border-radius: 10px;
}

.styled-img {
  width: 90%; 
  margin-right: 5%; 
  margin-left: 5%; 
  margin-top: 10px; 
  border-radius: 5px;
}

.styled-h1 {
  text-align: center; 
  margin: 3px; 
  color: #355C7D; 
  text-decoration: underline;
}

.styled-h2 {
  text-align: center; 
  margin: 3px; 
  color: #6C5B7B; 
  font-style: italic;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And our squeaky clean React component:&lt;br&gt;
&lt;/p&gt;

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

  return (
    &amp;lt;div className='styled-div'&amp;gt;
      &amp;lt;img className='styled-img' src={casaDelArbol}/&amp;gt;
      &amp;lt;h1 className='styled-h1'&amp;gt;Casa Del Arbol&amp;lt;/h1&amp;gt;
      &amp;lt;h2 className='styled-h2'&amp;gt;Banos, Ecuador&amp;lt;/h2&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A few things to note:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;className is camelCased in our React component&lt;/li&gt;
&lt;li&gt;className is declared inside quotes&lt;/li&gt;
&lt;li&gt;The dot prior to the class-name in our CSS file denotes that it is a class&lt;/li&gt;
&lt;li&gt;Attributes in CSS have a dash when containing multiple words&lt;/li&gt;
&lt;li&gt;CSS does not contain quotes or commas&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Make sure to import your CSS file into your React project. If you are using one file, such as the &lt;em&gt;index.css&lt;/em&gt; file to define all your styling for the project, you can import it at the top of your &lt;strong&gt;App&lt;/strong&gt; component, the highest level component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import '../index.css';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import './index.css';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;depending on the location of the CSS file.&lt;/p&gt;

&lt;p&gt;Another option is to create a CSS file specifically for this component, which contains all styles relevant to it. This CSS file can then be imported at the top of our &lt;strong&gt;StyledReactComponent&lt;/strong&gt; file.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>react</category>
      <category>css</category>
      <category>beginners</category>
    </item>
    <item>
      <title>VSCode Like a Pro on Your Mac</title>
      <dc:creator>Christopher Ninman</dc:creator>
      <pubDate>Mon, 13 Jun 2022 21:21:56 +0000</pubDate>
      <link>https://dev.to/alternate_robot/vscode-like-a-pro-on-your-mac-44f1</link>
      <guid>https://dev.to/alternate_robot/vscode-like-a-pro-on-your-mac-44f1</guid>
      <description>&lt;p&gt;You’ve made the big decision to learn how to code. Your workspace is set up and you have started learning some of the big concepts of programming. You watch as your instructors or favorite YouTube teachers jump through their code with speed and wonder how they did it. &lt;/p&gt;

&lt;p&gt;Well, you are a programmer now, and you should look and feel like it. In time, your mousepad will begin to feel like it slows you down and you’ll do most of your navigating on your new best friend, your keyboard. Here is a walkthrough of some of the commands you’ll be using the most on your journey using VSCode on your Mac. &lt;/p&gt;

&lt;h1&gt;Flip Through Open Apps&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AClkrB-M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u5ubcr8a5009p5vhcnuv.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AClkrB-M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u5ubcr8a5009p5vhcnuv.gif" alt="Flip through apps gif" width="880" height="198"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Command-tab&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This command will bring up a list of all open applications. Hold &lt;strong&gt;&lt;em&gt;Command&lt;/em&gt;&lt;/strong&gt; and press &lt;strong&gt;&lt;em&gt;tab&lt;/em&gt;&lt;/strong&gt; to select the next program to the right. Release &lt;strong&gt;&lt;em&gt;Command&lt;/em&gt;&lt;/strong&gt; when you have selected the desired app. Apps will be arranged from left to right in the order that they were last opened. Using &lt;strong&gt;&lt;em&gt;Command-shift-tab&lt;/em&gt;&lt;/strong&gt; will cycle through open applications from right to left.&lt;/p&gt;

&lt;h1&gt;Flip Through Tabs&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wiNNX6vZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z4ihl1q2cjsfkse482s1.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wiNNX6vZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z4ihl1q2cjsfkse482s1.gif" alt="Flip through tabs gif" width="239" height="200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Control-tab&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Once you are in VSCode or in your browser, hold &lt;strong&gt;&lt;em&gt;Control&lt;/em&gt;&lt;/strong&gt; and press &lt;strong&gt;&lt;em&gt;tab&lt;/em&gt;&lt;/strong&gt; to cycle through all open tabs. In VSCode, this will be all files that are currently open. In Google Chrome, or your browser of choice, it will cycle through all open tabs. Use &lt;strong&gt;&lt;em&gt;Control-Shift-tab&lt;/em&gt;&lt;/strong&gt; to cycle from right to left.&lt;/p&gt;

&lt;h1&gt;Zoom In/Out&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Command-plus&lt;/em&gt;&lt;/strong&gt; / &lt;strong&gt;&lt;em&gt;Command-minus&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This command will work in VSCode or your browser, as well as many other programs. If you are creating a screen recording, this can be a particularly helpful command in order to quickly zoom in or out and more easily see what you are working on.&lt;/p&gt;

&lt;h1&gt;Undo&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Command-Z&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In my short life as a programmer, this is the most used command in my arsenal - UNDO!!! Trying to code a new idea or refactor one that’s already there, only to discover that it really was not a good idea and you want humanity to forget that it ever happened. Hold &lt;strong&gt;&lt;em&gt;Command&lt;/em&gt;&lt;/strong&gt; and press &lt;strong&gt;&lt;em&gt;Z&lt;/em&gt;&lt;/strong&gt; to your heart’s content. Alternately, &lt;strong&gt;&lt;em&gt;Command-Shift-Z&lt;/em&gt;&lt;/strong&gt; will allow you to redo any changes that you undid. This is not strictly for VSCode. &lt;strong&gt;&lt;em&gt;Command-Z&lt;/em&gt;&lt;/strong&gt; will work in your browser/Google Docs/etc…&lt;/p&gt;

&lt;h1&gt;Save&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Command-S&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Save often!!! Seriously.&lt;/p&gt;

&lt;h1&gt;Go To Top/Bottom of Document&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Command-Up&lt;/em&gt;&lt;/strong&gt; / &lt;strong&gt;&lt;em&gt;Command-Down&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Quickly head to the first line or the last line of the current document in a fraction of a second.&lt;/p&gt;

&lt;h1&gt;Go To First/Last Character Of The Line&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rw07RkDj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rs8wcdtu5lql59j1qrgc.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rw07RkDj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rs8wcdtu5lql59j1qrgc.gif" alt="Beginning of line gif" width="500" height="121"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Command-left&lt;/em&gt;&lt;/strong&gt; / &lt;strong&gt;&lt;em&gt;Command-right&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This command has increased my productivity tenfold(ish). Let’s say that I need to remove a bracket at the end a long line of code. Rather than switching over to my mousepad and clicking at the end of the line, all I need to do is to make sure to navigate my cursor to the proper line and press &lt;strong&gt;&lt;em&gt;Command-right&lt;/em&gt;&lt;/strong&gt; to send my cursor to the last character of the line. Or if I need to indent a line of code to make it more readable, once I’m on the correct line, I can press &lt;strong&gt;&lt;em&gt;Command-left&lt;/em&gt;&lt;/strong&gt; to move my cursor to the beginning of the line.&lt;/p&gt;

&lt;h1&gt;Highlight A Section Of Code&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2GIfmZvF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w5cqexrg5kym9sabb5ep.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2GIfmZvF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w5cqexrg5kym9sabb5ep.gif" alt="Highlight Code gif" width="500" height="279"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Shift-direction&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Holding &lt;strong&gt;&lt;em&gt;Shift&lt;/em&gt;&lt;/strong&gt; plus &lt;strong&gt;&lt;em&gt;up&lt;/em&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;em&gt;down&lt;/em&gt;&lt;/strong&gt; will highlight an entire line of code. If you have a five-line function that you need to copy or move, once your cursor is positioned at the beginning of the function, hold &lt;strong&gt;&lt;em&gt;Shift&lt;/em&gt;&lt;/strong&gt; and press &lt;strong&gt;&lt;em&gt;down&lt;/em&gt;&lt;/strong&gt; five times until you have arrived at the end of the function. Hold &lt;strong&gt;&lt;em&gt;Shift&lt;/em&gt;&lt;/strong&gt; and press &lt;strong&gt;&lt;em&gt;right&lt;/em&gt;&lt;/strong&gt; to highlight the next character to the right (or unhighlight if it already is.)&lt;/p&gt;

&lt;h1&gt;Highlight All Text Until the End Of The Line Or File&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Command-Shift-direction&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This command will allow you to navigate in the same manner as using &lt;strong&gt;&lt;em&gt;Command-direction&lt;/em&gt;&lt;/strong&gt;, but will simultaneously highlight the text as well. &lt;/p&gt;

&lt;p&gt;Let’s say you want to select all text until the end of the line without including the closing bracket. From the start of the line, press &lt;strong&gt;&lt;em&gt;Command-Shift-right&lt;/em&gt;&lt;/strong&gt; to select the entire line, then press &lt;strong&gt;&lt;em&gt;Shift-left&lt;/em&gt;&lt;/strong&gt; to unhighlight the bracket which is the final character of the line.&lt;/p&gt;

&lt;h1&gt;Search For A File&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Command-P&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You’ve moved beyond a single JavaScript file for your project and need to move between many files. &lt;strong&gt;&lt;em&gt;Command-P&lt;/em&gt;&lt;/strong&gt; allows you to search for a file based on its name.&lt;/p&gt;

&lt;h1&gt;Search For A Term Within a File&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Command-F&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now where did I call that function? Press &lt;strong&gt;&lt;em&gt;Command-F&lt;/em&gt;&lt;/strong&gt; to open the search bar for the current file.&lt;/p&gt;

&lt;h1&gt;Search For A Term Within All Files&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Command-Shift-F&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I remember declaring a variable with a certain classname somewhere, but it could be anywhere. Press*&lt;em&gt;&lt;em&gt;Command-Shift-F&lt;/em&gt;&lt;/em&gt;* to open the search bar for all files in your application.&lt;/p&gt;

&lt;h1&gt;Move Line Up/Down&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6cpxR0Ej--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ck5g8lt7zbj28079ubws.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6cpxR0Ej--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ck5g8lt7zbj28079ubws.gif" alt="Move Line gif" width="640" height="211"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Option-Up/Down&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There’s no need to cut and paste every time you need to move some text to another location. Press &lt;strong&gt;&lt;em&gt;Option&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;Up&lt;/em&gt;&lt;/strong&gt; (&lt;strong&gt;&lt;em&gt;Down&lt;/em&gt;&lt;/strong&gt;) to move a line of code up (down). If you have multiple lines of code highlighted, all of those lines will be moved.&lt;/p&gt;

&lt;h1&gt;Toggle Word Wrap&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--egE4v7a_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qq0wgifnwfubg9m1np75.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--egE4v7a_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qq0wgifnwfubg9m1np75.gif" alt="Word Wrap gif" width="500" height="130"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Option-Z&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Lines of code can get pretty long. If there isn’t a good space to hit &lt;strong&gt;&lt;em&gt;Enter&lt;/em&gt;&lt;/strong&gt; and start a new line, you may want to use word wrap to make each line of code wrap onto the following line whenever it goes beyond the width of your VSCode window. &lt;strong&gt;&lt;em&gt;Option-Z&lt;/em&gt;&lt;/strong&gt; allows you to switch quickly between wrapping the text or not.&lt;/p&gt;

&lt;h1&gt;View/Change User Settings&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Command-comma(,)&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One of the beautiful things about programming is that there is rarely one correct way to do it. Within the confines of a computer language, you can find your own style. &lt;strong&gt;&lt;em&gt;Command&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;,&lt;/em&gt;&lt;/strong&gt; will bring up all your VSCode personal settings. One option for personalization is selecting your default number of spaces when pressing &lt;strong&gt;&lt;em&gt;tab&lt;/em&gt;&lt;/strong&gt;. Default is 4 spaces, I (many others) prefer 2 spaces. Whatever makes sense for you is just fine.&lt;/p&gt;

&lt;h1&gt;Toggle Sidebar Visibility&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Command-B&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;At times you need to see all the files you have in the sidebar, but once you have selected one, they take up too much space and are distracting. &lt;strong&gt;&lt;em&gt;Command-B&lt;/em&gt;&lt;/strong&gt; allows you to quickly toggle between showing or hiding the sidebar .&lt;/p&gt;

&lt;h1&gt;Find All Matching Terms In The File&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PfXW5PjG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wch0p9qx2y5e71r6gvgp.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PfXW5PjG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wch0p9qx2y5e71r6gvgp.gif" alt="Select All Matching Terms gif" width="500" height="136"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Command-D&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Another command to greatly increase productivity. Let’s say you have a variable that is used 15 times in a file. You realized, for whatever reason, that you want/need to change its name. If you forget to correctly change a single one of those fifteen instances, your program will not work as planned (or at all). Select the text you want to change, then hold &lt;strong&gt;&lt;em&gt;Command&lt;/em&gt;&lt;/strong&gt; and press &lt;strong&gt;&lt;em&gt;D&lt;/em&gt;&lt;/strong&gt; to select the next instance in which that exact text is used. Each time you press &lt;strong&gt;&lt;em&gt;D&lt;/em&gt;&lt;/strong&gt;, the next instance of that text will be highlighted as well. Now you can change all fifteen instances of that variable at the same time.&lt;/p&gt;

&lt;p&gt;Best of luck in all your future coding endeavors!!!&lt;/p&gt;

&lt;p&gt;Photo by &lt;a href="https://unsplash.com/photos/Im7lZjxeLhg?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditShareLink"&gt;Ales Nesetril&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/students?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

</description>
      <category>vscode</category>
      <category>mac</category>
      <category>codenewbie</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Building a Rails API to Track Student Progress</title>
      <dc:creator>Christopher Ninman</dc:creator>
      <pubDate>Fri, 08 Apr 2022 01:03:01 +0000</pubDate>
      <link>https://dev.to/alternate_robot/building-a-rails-api-to-track-student-progress-3a09</link>
      <guid>https://dev.to/alternate_robot/building-a-rails-api-to-track-student-progress-3a09</guid>
      <description>&lt;p&gt;What is the most difficult thing about being a teacher? Actually, that's a trick question. Everything is difficult, hence, my switch to becoming a software developer. But one of those difficult teacher tasks is gathering data, as a means to guide your instruction, along with a way to show parents that you know their children well. As a teacher, I was always hoping to find the balance of a fun, engaging app that actually taught my students, while giving me some information about them after they finished.&lt;/p&gt;

&lt;p&gt;As a 3rd grade teacher, I tried to make sure every one of my students was able to walk away with the ability to recite their multiplication facts in no time. But it was difficult to keep track of what each of them knew. So, after a switch to programming, I began my quest to create an application that could track students' knowledge of the times tables.&lt;/p&gt;

&lt;h1&gt;The Database&lt;/h1&gt;

&lt;p&gt;I'll be covering the set up of the database using Ruby on Rails and ActiveRecord. Setting up the frontend is a whole other story, but what we're really after here, and what a teaching application should create, is data.&lt;/p&gt;

&lt;p&gt;The structure of a classroom lends itself easily to being created using Active Record associations. Here is how we can set up our models:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Teacher&lt;/strong&gt; model - has_many :classrooms, has_many: students, through: :classrooms&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Classroom&lt;/strong&gt; model - has_many :students, belongs_to: teacher&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Student&lt;/strong&gt; model - belongs_to: classroom&lt;/p&gt;

&lt;p&gt;As a teacher, sometimes you need data on individual students, other times you'll need data on your whole classroom. Think about how you'll want to access the data throughout whatever app you have planned.&lt;/p&gt;

&lt;p&gt;If you want the teacher to have simple access to all of their students, you can include that association.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class TeacherSerializer &amp;lt; ActiveModel::Serializer
  attributes :id, :username, :is_teacher

  has_many :classrooms
  has_many :students, through: :classrooms

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

&lt;/div&gt;



&lt;p&gt;With this set up through your ActiveRecord associations, an array of students will now be available at the first level of your data that you fetch, along with all of the data they hold. Though you can serialize their data and return their classroom_id, they will not be nested in the second level within a classroom. You'll need to handle that logic on the front end.&lt;/p&gt;

&lt;p&gt;Another option is to take away the &lt;em&gt;has_many :students, through: :classrooms&lt;/em&gt; association, and instead serialize your classroom data to include the students.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class TeacherSerializer &amp;lt; ActiveModel::Serializer
  attributes :id, :username, :is_teacher
  has_many :classrooms
end


class ClassroomSerializer &amp;lt; ActiveModel::Serializer

  belongs_to :teacher
  has_many :students

  attributes :id, :classroom_name, :students
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;Create a Problem model&lt;/h1&gt;

&lt;p&gt;In my case, building an app to track student knowledge of the times tables, I created a &lt;strong&gt;Problem&lt;/strong&gt; model, with 100 entries, each one containing a problem (string: "6 x 7"), and an answer (integer: 42). &lt;/p&gt;

&lt;p&gt;In order to create student data to track, I needed to create a joins table through my ActiveRecord associations to keep tabs on a student's progress. &lt;/p&gt;

&lt;h1&gt;Create a Mastery model&lt;/h1&gt;

&lt;p&gt;The &lt;strong&gt;Mastery&lt;/strong&gt; model is a table created to house a student's data on each of the entries in the &lt;strong&gt;Problem&lt;/strong&gt; model. (You could alternately think of this as a &lt;strong&gt;Quiz&lt;/strong&gt; model, rather than one single problem.)&lt;/p&gt;

&lt;p&gt;Now to set up the joins the table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Problem &amp;lt; ApplicationRecord
  has_many :masteries
  has_many :students, through: :masteries
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Student &amp;lt; ApplicationRecord
  has_many :masteries
  has_many :problems, through: :masteries
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Mastery &amp;lt; ApplicationRecord
  belongs_to :student
  belongs_to :problem
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each time a student takes a quiz, you can create a new instance of a Mastery (or Quiz), giving it attributes such as :times_answered, :times_correct, :mastery_level, or custom methods, such as :mastery_percentage. And thanks to our associations, each instance of a &lt;strong&gt;Mastery&lt;/strong&gt; will also have a :student_id and a :problem_id.&lt;/p&gt;

&lt;p&gt;In the case of the multiplication tables, students will review facts multiple times, so their scores can be updated regularly to reflect how many times each fact was answered and how many times it was answered correctly.&lt;/p&gt;

&lt;h1&gt;Customizing our Data&lt;/h1&gt;

&lt;p&gt;Now comes the fun part. What sort of data do we want our teachers to have access to? We can build custom methods to return whatever we think will be valuable information about the students.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Student &amp;lt; Application Record

  def mastery_percentage 
    self.masteries.average(:total_score).to_f
  end

  def difficult_facts
    self.masteries.where("total_score" &amp;lt; ?", 20) 
  end

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

&lt;/div&gt;



&lt;p&gt;In the first method, we make a call to the database, which will return to us a method for each student, searching through each of the student's &lt;strong&gt;Masteries&lt;/strong&gt;, and returning the average of all of them. In the second custom method, we're returning a custom list that searches through each student's masteries and finds all cases where their :total_score is less than our chosen number.&lt;/p&gt;

&lt;p&gt;Just make sure you add your custom defined method into your &lt;strong&gt;Student&lt;/strong&gt; serializer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class StudentSerializer &amp;lt; ActiveModel::Serializer
  attributes :id, :username, :mastery_percentage,  
  :difficult_facts, :classroom_id, :masteries,

  belongs_to :classroom
  has_many :masteries

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

&lt;/div&gt;



&lt;p&gt;Teachers deserve all the help they can get. Educational apps with well thought out databases are just what those teachers are looking for. &lt;/p&gt;

&lt;p&gt;Photo by &lt;a href="https://unsplash.com/@johnschno?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;John Schnobrich&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/students?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

</description>
      <category>rails</category>
      <category>activerecord</category>
      <category>ruby</category>
      <category>sinatra</category>
    </item>
    <item>
      <title>Sinatra: Accessing Data and Methods with {include:}</title>
      <dc:creator>Christopher Ninman</dc:creator>
      <pubDate>Thu, 10 Feb 2022 22:36:52 +0000</pubDate>
      <link>https://dev.to/alternate_robot/sinatra-accessing-data-and-methods-with-include-2dhi</link>
      <guid>https://dev.to/alternate_robot/sinatra-accessing-data-and-methods-with-include-2dhi</guid>
      <description>&lt;p&gt;So you've been using ActiveRecord/Sinatra for your backend and have come up with a clever method to filter your database and achieve world peace. Or maybe you just created a &lt;code&gt;Class&lt;/code&gt; method to calculate the average rating of a particular location in your app, like me. Good for you!!! But now what? How can you access that brilliant/less brilliant but useful information?  &lt;/p&gt;

&lt;h1&gt;My Project&lt;/h1&gt;

&lt;p&gt;I like to have something tangible as an example when trying to learn a new concept. So here's a brief scenario (part of a project that I created during bootcamp) to be our spirit guide:&lt;/p&gt;

&lt;p&gt;The backend contains these &lt;code&gt;Model&lt;/code&gt;s:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;City&lt;/strong&gt;  has_many :users, has_many: locations&lt;br&gt;
&lt;strong&gt;User&lt;/strong&gt;  belongs_to :city, has_many :reviews, has_many :locations, through: :reviews&lt;br&gt;
&lt;strong&gt;Location&lt;/strong&gt;  belongs_to :city, has_many :reviews, has_many :users, through: :reviews&lt;br&gt;
&lt;strong&gt;Review&lt;/strong&gt;  belongs_to :location, belongs_to :user&lt;/p&gt;

&lt;p&gt;Each user can write a review for a location. This &lt;strong&gt;review&lt;/strong&gt; includes a rating, which is an &lt;code&gt;integer&lt;/code&gt; between 1 and 5.&lt;/p&gt;

&lt;p&gt;Several &lt;strong&gt;Users&lt;/strong&gt; have written &lt;strong&gt;Reviews&lt;/strong&gt; for the &lt;strong&gt;Location&lt;/strong&gt;, so now our location has several &lt;strong&gt;Reviews&lt;/strong&gt;. Luckily, ActiveRecord has given us a very non-original, but ridiculously simple, way to calculate a &lt;strong&gt;Location&lt;/strong&gt;'s rating. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;Class.average(:attribute)&lt;/code&gt;&lt;/p&gt;

&lt;h1&gt;Creating Our Method&lt;/h1&gt;

&lt;p&gt;Time to put the &lt;code&gt;Class.average&lt;/code&gt; method to use in our current scenario&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Location &amp;lt; ActiveRecord::Base
  belongs_to :city
  has_many :reviews
  has_many :users, through: :reviews

  def average_rating
    self.reviews.average(:rating).to_f
  end
end

// Assuming you'll want to use that average as a decimal,
// we're calling `to_f` on our 'average_rating'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Previously we had access to instance methods on instances of locations, such as &lt;code&gt;Location.first.reviews&lt;/code&gt;, &lt;code&gt;Location.first.users&lt;/code&gt;, &lt;code&gt;Location.first.reviews.first&lt;/code&gt;, etc. Now we have an additional method we can call on our &lt;strong&gt;Location&lt;/strong&gt; instances: &lt;code&gt;Location.first.average_rating&lt;/code&gt;. This will return us the average of all the ratings that have been given in the &lt;strong&gt;Reviews&lt;/strong&gt; of that instance.&lt;/p&gt;

&lt;h1&gt;How Can I Get That to My Front-End?&lt;/h1&gt;

&lt;p&gt;Let's begin by taking a look at our Locations Controller, and what we can access when we make a fetch request to our backend.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class LocationsController &amp;lt; ApplicationController

  def serialize_locations(objects)
    objects.to_json(include: [:reviews, :users, :city])
  end

  get '/locations' do
    serialize_locations(Location.all)
  end
end

// Rather than calling 
// Location.all.to_json(include: [:.......]),
// we are using the serialize_locations function to do 
// that work, as we may want to reuse that logic in other
// fetch requests we make, such as '/locations/:id', or 
// post, patch, and delete requests

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

&lt;/div&gt;



&lt;p&gt;Since we may want access to the &lt;strong&gt;Location&lt;/strong&gt; instance's reviews, users, and city information, we're using &lt;code&gt;include:&lt;/code&gt; and an array of it's connected models. We could now call something like &lt;code&gt;Location.first.city.name&lt;/code&gt; and have access to the name of the city where the &lt;strong&gt;Location&lt;/strong&gt; belongs. &lt;/p&gt;

&lt;p&gt;The problem is, we still can't access the average rating of a &lt;strong&gt;Location&lt;/strong&gt; instance.&lt;/p&gt;

&lt;h1&gt;Time to Access the Method&lt;/h1&gt;

&lt;p&gt;We can dig further and further into our data and create nested routes using &lt;code&gt;include:&lt;/code&gt;, example: calling &lt;code&gt;include&lt;/code&gt; inside of the &lt;strong&gt;Location's&lt;/strong&gt; &lt;strong&gt;Users&lt;/strong&gt; and including the &lt;strong&gt;Location's&lt;/strong&gt; &lt;strong&gt;User's&lt;/strong&gt; &lt;strong&gt;Review&lt;/strong&gt;. &lt;em&gt;Check out this &lt;a href="https://medium.com/@coywreid/using-include-in-your-sinatra-application-controller-methods-fc42f297b560"&gt;great post&lt;/a&gt; for more info.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;But there is something else that we can include: &lt;strong&gt;methods&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;class LocationsController &amp;lt; ApplicationController

  def serialize_locations(objects)
    objects.to_json(include: [:reviews, :users, :city], methods: :average_rating)
  end

  get '/locations' do
    serialize_locations(Location.all)
  end
end

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

&lt;/div&gt;



&lt;p&gt;We have included an array of related tables, along with the named method (average_rating) inside our &lt;strong&gt;Locations&lt;/strong&gt; class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;id: 1,
location_name: "Prospect Park",
city_id: 2,
neighborhood: "Paradise Creek",
times_visited: 8,
average_rating: 3.25,
+reviews: [...],
+users: [...],
+city: {...} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Oh, you actually have several methods that you created in your &lt;strong&gt;Location&lt;/strong&gt; class that you want? Let's include an array of methods!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  def serialize_locations(objects)
    objects.to_json(include: [:reviews, :users, :city], methods: [:average_rating, :sum_times_visited])
  end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Photo by &lt;a href="https://unsplash.com/@fabioha?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;fabio&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/data?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

</description>
      <category>activerecord</category>
      <category>sinatra</category>
      <category>ruby</category>
    </item>
    <item>
      <title>React Quiz with Unlimited Questions</title>
      <dc:creator>Christopher Ninman</dc:creator>
      <pubDate>Sun, 12 Dec 2021 02:02:15 +0000</pubDate>
      <link>https://dev.to/alternate_robot/react-quiz-with-unlimited-questions-32pi</link>
      <guid>https://dev.to/alternate_robot/react-quiz-with-unlimited-questions-32pi</guid>
      <description>&lt;p&gt;Wouldn't it be awesome if you could create a quiz app that wouldn't require you to write hundreds of questions along with a correct response and multiple incorrect responses? Though this is by no means the only way to make this happen, here is an solution to make it work.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Before you continue on to find out how, here is the caveat: This method requires the same question each time, and in turn generates multiple buttons with one correct response and all others incorrect. I'll give you an example of how I used this method, and then explain how.&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;My Project&lt;/h1&gt;

&lt;p&gt;My 4-year-old son is currently obsessed with world flags, which inspired me to create a React App with information about flags, population, capitals, and continents. After exploring the site, users can take quizzes for each category.&lt;/p&gt;

&lt;p&gt;I used the &lt;a href="https://restcountries.com/"&gt;Rest Countries API&lt;/a&gt; to get data about each country for each of the categories above.&lt;/p&gt;

&lt;p&gt;In order to create a quiz on flags, I used the countries data to create a flag image, along with four buttons, one with the country that corresponds to the flag image, and three other random countries. In another &lt;code&gt;div&lt;/code&gt; is the 'Next' button.&lt;/p&gt;

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

&lt;h3&gt;Data Fetch&lt;/h3&gt;

&lt;p&gt;Using your API of choice, make a GET request and save the data, or whichever part of the data that will be useful to you, to a state variable. A regular old variable should work for what we're doing here as well. If you decide to make a state variable, import and run useEffect to make your data fetch, and include an empty dependencies array so that your fetch only runs once.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [countries, setCountries] = useState([])

useEffect (() =&amp;gt; {
  fetch('https://restcountries.com/v3.1/all')
    .then(res =&amp;gt; res.json())
    .then(data =&amp;gt; {
      const unMemberCountries = data.filter((country) {
        country.unMember === true 
      }
    setCountries(unMemberCountries)
  })
}, [] )

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

&lt;/div&gt;



&lt;h3&gt;Additional State Variables&lt;/h3&gt;

&lt;p&gt;We're going to need a lot more state variables. In my  case, I added seven more. One for the country that is the correct answer, and three incorrect ones, along with state variables to keep track of the question number, score, and whether or not a question has been answered. The four countries are set with a function to choose a random country from the array of countries saved in the countries 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 [flagQuizCountry, setFlagQuizCountry] = useState(countries[Math.floor(Math.random()*countries.length)])
  const [incorrectFlagOne, setIncorrectFlagOne] = useState(countries[Math.floor(Math.random()*countries.length)])
  const [incorrectFlagTwo, setIncorrectFlagTwo] = useState(countries[Math.floor(Math.random()*countries.length)])
  const [incorrectFlagThree, setIncorrectFlagThree] = useState(countries[Math.floor(Math.random()*countries.length)])
  const [flagResponseGiven, setFlagResponseGiven] = useState(false)
  const [currentFlagQuestion, setCurrentFlagQuestion] = useState(0)
  const [flagQuizScore, setFlagQuizScore] = useState (0)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;Create your buttons&lt;/h3&gt;

&lt;p&gt;In your JSX, create four buttons. Each button should have its value set to equal the text inside the button. So if I'm asking "Which country's flag is this?" I have four buttons that are set to my state variables: flagQuizCountry (correct answer), incorrectOne, incorrectTwo, and incorrectThree. Then in a following &lt;code&gt;div&lt;/code&gt;, add your 'Next' button.&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;div'&amp;gt;
  &amp;lt;ul style={{display: 'flex', flexDirection: 'row', justifyContent: 'center'}} 
    &amp;lt;li&amp;gt;
      &amp;lt;button 
        value={flagQuizCountry.name}
        onClick={handleFlagAnswer} &amp;gt; 
        {flagQuizCountry.name} 
      &amp;lt;/button&amp;gt;
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
      &amp;lt;button 
        value={incorrectOne.name}
        onClick={handleFlagAnswer} &amp;gt; 
        {incorrectOne.name} 
      &amp;lt;/button&amp;gt;
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
      &amp;lt;button 
        value={incorrectTwo.name}
        onClick={handleFlagAnswer} &amp;gt; 
        {incorrectTwo.name} 
      &amp;lt;/button&amp;gt;
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
      &amp;lt;button 
        value={incorrectThree.name}
        onClick={handleFlagAnswer} &amp;gt; 
        {incorrectThree.name} 
      &amp;lt;/button&amp;gt;
    &amp;lt;/li&amp;gt;
  &amp;lt;/ul
&amp;lt;/div&amp;gt;
&amp;lt;div&amp;gt;
  &amp;lt;button onClick={flagOnClick}&amp;gt;Next&amp;lt;/button&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And here comes someone else creative solution that I found to randomize the button order. My gratitude to the wise thinker who posted this solution &lt;a href="https://techwelkin.com/randomize-html-list-display-html-list-items-randomly"&gt;here&lt;/a&gt;. Create each of your buttons inside a &lt;code&gt;li&lt;/code&gt; tag, and place all the buttons inside a &lt;code&gt;ul&lt;/code&gt; tag. Then use this code to randomize the order of the items in the list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function randomizeItems(items) {
  let cached = items.slice(0), temp, i = cached.length, rand;
  while(--i) {
    rand = Math.floor(i * Math.random());
    temp = cached[rand];
    cached[rand] = cached[i];
    cached[i] = temp;
  }
  return cached;
}

function randomizeList() {
  let list = document.getElementById("capital-quiz-buttons");
  let nodes = list.children, i = 0;
  nodes = Array.prototype.slice.call(nodes);
  nodes = randomizeItems(nodes);
  while(i &amp;lt; nodes.length) {
    list.appendChild(nodes[i]);
    ++i;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Style your buttons as you see fit, but make sure that you style your &lt;code&gt;ul&lt;/code&gt; in the following fashion to take away the bullet points.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ul {
  list-style-type: none;
  padding: 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add an &lt;code&gt;onClick&lt;/code&gt; event to each button which calls something similar to the following function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function handleFlagAnswer (event) {
    if (flagResponseGiven === false) {
      if (event.target.value === flagQuizCountry.name.common) {
        setFlagQuizScore(flagQuizScore + 1)
      } 
    setFlagResponseGiven(true)
    }
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function will only run if no response has been given yet, and we will set the responseGiven to true onClick. If the button's value equals the &lt;code&gt;flagQuizCountry&lt;/code&gt;, a point will be added the &lt;code&gt;flagQuizScore&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now we need a function that will run when we click the 'Next' button. We'll set each of our four buttons to a new random country. We'll increase the 'currentFlagQuestion' by one, reset 'flagResponseGiven' to false, and set limits for however many questions we want on the quiz, and then decide what you want to do once the end has been reached.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function setNextFlagQuestion () {
  setFlagQuizCountry (countryData[Math.floor(Math.random()*countryData.length)])
  setIncorrectFlagOne (countryData[Math.floor(Math.random()*countryData.length)])
  setIncorrectFlagTwo (countryData[Math.floor(Math.random()*countryData.length)])
  setIncorrectFlagThree (countryData[Math.floor(Math.random()*countryData.length)])

  let nextQuestion = currentFlagQuestion + 1
  setCurrentFlagQuestion(nextQuestion)
  setFlagResponseGiven(false)
  resetButtonColors()
  if (currentFlagQuestion &amp;gt;= 25){
      // insert desired outcome
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you have a quiz that can use the same question each time, you now have a limitless number of randomized quiz questions. &lt;/p&gt;

&lt;p&gt;Photo by &lt;a href="https://unsplash.com/@lamunix?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Ana Municio&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/question?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>DOM Selectors in JavaScript</title>
      <dc:creator>Christopher Ninman</dc:creator>
      <pubDate>Mon, 08 Nov 2021 21:50:26 +0000</pubDate>
      <link>https://dev.to/alternate_robot/dom-selectors-in-javascript-344i</link>
      <guid>https://dev.to/alternate_robot/dom-selectors-in-javascript-344i</guid>
      <description>&lt;p&gt;While creating interactive content with JavaScript, you will often find yourself needing to access various HTML elements in order to make changes to them. We'll go through some of the main ways you can grab elements in the DOM, and how you can make use of what they hold.&lt;/p&gt;

&lt;h1&gt;Different Types of Selectors&lt;/h1&gt;

&lt;h3&gt;ID&lt;/h3&gt;

&lt;p&gt;If you want to manipulate one single element, the easiest and most straight-forward approach is to find that element by &lt;code&gt;id&lt;/code&gt;. Only one HTML element can have a specific &lt;code&gt;id&lt;/code&gt; attribute, and each ID is unique to one element.&lt;/p&gt;

&lt;h3&gt;Class&lt;/h3&gt;

&lt;p&gt;Multiple elements can be grouped together when they are given the same &lt;code&gt;class&lt;/code&gt; name. An example would be if you have multiple buttons that serve the purpose of deleting content, and you want all of those buttons to be manipulated in the same manner.&lt;/p&gt;

&lt;h3&gt;Tag&lt;/h3&gt;

&lt;p&gt;Some common tags in HTML are &lt;code&gt;body&lt;/code&gt;, &lt;code&gt;h1&lt;/code&gt;, &lt;code&gt;p&lt;/code&gt;, &lt;code&gt;div&lt;/code&gt;, &lt;code&gt;span&lt;/code&gt;, and &lt;code&gt;img&lt;/code&gt;, though there are numerous others. Another DOM selector is &lt;code&gt;tagName&lt;/code&gt;, which selects elements based on the type of element they are.&lt;/p&gt;

&lt;h1&gt;Accessing an Element by ID&lt;/h1&gt;

&lt;p&gt;In order to access an element with a specific &lt;code&gt;id&lt;/code&gt;, you have a few options. You can either use:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Either option will search the entire document and return the element with a matching &lt;code&gt;id&lt;/code&gt; as an object. If no element with the given &lt;code&gt;id&lt;/code&gt; is found, &lt;code&gt;null&lt;/code&gt; will be returned. If you encounter a &lt;code&gt;TypeError: Cannot read properties of null&lt;/code&gt;, it is likely that no element with that &lt;code&gt;id&lt;/code&gt; was found.&lt;/p&gt;

&lt;p&gt;You can also use these methods to create a variable, and then manipulate the DOM by calling that variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let foundElement = document.getElementById('first-header')
foundElement.style.color = 'blue'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If an element does not exist in the HTML file and is created using JavaScript, you can still give that element an &lt;code&gt;id&lt;/code&gt; using JavaScript with the following syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let createdWithJsElement = document.createElement('p')
createdWithJsElement.id = 'js-element'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;Accessing an Element by ClassName&lt;/h1&gt;

&lt;p&gt;Similar to accessing an element by &lt;code&gt;id&lt;/code&gt;, you have a few choices for selecting elements by &lt;code&gt;class&lt;/code&gt;. You can access elements by &lt;code&gt;class&lt;/code&gt; name with the following methods.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;This will return only the &lt;strong&gt;first&lt;/strong&gt; element that matches the specified &lt;code&gt;class&lt;/code&gt; name.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;The use of &lt;code&gt;getElementsByClassName&lt;/code&gt; will return a live &lt;code&gt;HTMLCollection&lt;/code&gt; of all the elements matching that &lt;code&gt;class&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;document.querySelectorAll('.element-class')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using the &lt;code&gt;querySelectorAll&lt;/code&gt; method will return a static &lt;code&gt;NodeList&lt;/code&gt; of all the elements matching that class. Distinguishing between a &lt;code&gt;NodeList&lt;/code&gt; and an &lt;code&gt;HTMLCollection&lt;/code&gt; is a whole other topic which you can explore &lt;a href="https://unicorntears.dev/posts/queryselectorall-vs-getelementsbyclassname/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note the use of a &lt;strong&gt;dot&lt;/strong&gt; prior to the &lt;code&gt;class&lt;/code&gt; name, as opposed to the &lt;strong&gt;hash-tag&lt;/strong&gt; prior to doing a query selector on an &lt;code&gt;id&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In order to manipulate every element that is returned from the &lt;code&gt;getElementsByClassName&lt;/code&gt; or &lt;code&gt;querySelectorAll&lt;/code&gt; methods, you'll need to run a &lt;code&gt;for loop&lt;/code&gt; to iterate the array-like object and make the desired changes to each element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let allButtons = document.getElementsByClassName('button')

function testButtons () {
  for (let i = 0; i &amp;lt; allButtons.length; i++) {
    console.log(`I am button ${[i+1]}`);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can create or change an element's &lt;code&gt;class&lt;/code&gt;, or other &lt;code&gt;attribute&lt;/code&gt;, by accessing that element by its &lt;code&gt;[index]&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;allButtons[5].className = 'delete-button'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;Accessing an Element by TagName&lt;/h1&gt;

&lt;p&gt;Yet another way to select elements in the DOM is to select the elements by their &lt;code&gt;tag&lt;/code&gt; name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let allParagraphs = document.getElementsByTagName('p')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can get even more specific with your search for specific elements based on &lt;code&gt;tag name&lt;/code&gt;. Suppose you want to change the color of the text in all the paragraphs of a "Skills" section. You can search for elements by &lt;code&gt;tag name&lt;/code&gt; within a specific div.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mySkills = document.getElementById("skills-div");
const skillParagraphs = mySkills.getElementsByTagName("p");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Like when the &lt;code&gt;getElementsByClassName&lt;/code&gt; method is called, &lt;code&gt;getElementsByTagName&lt;/code&gt; will return a live HTML collection of elements. This can be iterated over with a &lt;code&gt;for loop&lt;/code&gt; just like the list of elements gathered with the &lt;code&gt;getElementsByClassName&lt;/code&gt; method.&lt;/p&gt;

&lt;h1&gt;Wrapping It Up&lt;/h1&gt;

&lt;p&gt;If you can't precisely locate elements, you'll have difficulty making your JavaScript code come to life. These selectors are just the tip of the iceberg for making your creations dynamic.&lt;/p&gt;

&lt;p&gt;Photo by &lt;a href="https://unsplash.com/@max_duz?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Max Duzij&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/programming?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

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