<?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: Ryan Barrera</title>
    <description>The latest articles on DEV Community by Ryan Barrera (@rbarrera9er).</description>
    <link>https://dev.to/rbarrera9er</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%2F896846%2F72484af8-a442-4dfc-814f-069dc8fd4757.png</url>
      <title>DEV Community: Ryan Barrera</title>
      <link>https://dev.to/rbarrera9er</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rbarrera9er"/>
    <language>en</language>
    <item>
      <title>Slideshow In JavaScript</title>
      <dc:creator>Ryan Barrera</dc:creator>
      <pubDate>Sat, 23 Jul 2022 22:12:00 +0000</pubDate>
      <link>https://dev.to/rbarrera9er/sideshow-in-javascript-2pe4</link>
      <guid>https://dev.to/rbarrera9er/sideshow-in-javascript-2pe4</guid>
      <description>&lt;p&gt;So, you've finished your website or app, but you notice something is missing... There is nothing that makes your website/ app approachable or interesting. Today, now more than ever the attention span of individuals is getting shorter by the day. Not that they are getting dumber, but there is so much content out there it is easy to get bored or find something different. Creating content on the internet is getting more complex by the day. Different generations are both using the web and its applications causing a wide range of demographics to create content for. But few things still remain the same. For example it is most important that your content is read!&lt;/p&gt;

&lt;p&gt;But how do you make sure your content is going to get read!?&lt;/p&gt;

&lt;p&gt;You don't want it to be too much that the reader doesn't realize there is more than what catches their eye. SO how do you approach this dilemma?&lt;/p&gt;

&lt;p&gt;For starters, I would approach this like a job interview. Yes, being qualified is important, but you need to look the part as well. If you don't "look" prepared, then it will be difficult to look past what you're saying. Not that the information is bad, but it's important to know your audience, atheistically that is.&lt;/p&gt;

&lt;p&gt;Often in web development you have a team of individuals working on a single project. Each person focusing on their task to make the web application the best it can be. These developers are using programs such as CSS, JavaScript and HTML and sometimes many other programs. Each program works differently, and when properly executed you have a fully functioning web application.&lt;/p&gt;

&lt;p&gt;The best way I can describe the three programs I mentioned previously and how they work together is to look at a car. Any car works for this analogy. In this analogy we are breaking the car into 3 part, our CSS, HTML and JavaScript.&lt;/p&gt;

&lt;p&gt;First we're gonna look at the body or frame of the car that is going to be our HTML.&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%2Fm7sxlop94fcinxkylk08.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%2Fm7sxlop94fcinxkylk08.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The HTML/Body can look different pending the type of car or website you using. Take the frame of my Honda Accord, and the frame of school bus, in comparison to to the web applications of Amazon.com and Netflix. Both cars drive on the the road they both web apps surf the internet, but they both have a different function (pun intended) and are used differently.&lt;/p&gt;

&lt;p&gt;Next is the look of the car, this is from the shell to the paint job.&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%2F067q84tgd2fkg2lf7dj3.jpeg" 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%2F067q84tgd2fkg2lf7dj3.jpeg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This isn't vitally important to either running properly, but as we will work on later, the appearance is equally important to everything involved.&lt;/p&gt;

&lt;p&gt;Then comes perhaps one of the more vital elements in web development, The JavaScript or in this case the engine. &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%2Fww4sy4c097ajiff0flcr.jpeg" 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%2Fww4sy4c097ajiff0flcr.jpeg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Without either to website like the car is just something cool to look at. Depending on what the car is going to be doing, engine parts will different pending the type of car. The same is with JavaScript. Depending what the web application will do, depends on what kind of events there will be eventListeners and the list goes one.&lt;/p&gt;

&lt;p&gt;This ultimately brings me to my point that I want to make with the web application I have created for myself.&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%2Fvzxtca9y06mh2xiyibfk.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%2Fvzxtca9y06mh2xiyibfk.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see I have a pretty pain and boring web page. I want to add something to it that makes it easier on the eyes and at the very least adds depth to what I am trying to accomplish with this web application.&lt;/p&gt;

&lt;p&gt;My goal is to add a slideshow off all the albums covers that are in my db.json file (there are 9 of them). I want this to be on the centered and I want it to be on a timer. But before I go into JavaScript and implement the code I need to tell JS where to put the code (HTML) and the format I want it to be in (CSS).&lt;/p&gt;

&lt;p&gt;In my HTML file inside the &lt;/p&gt; tags I want implement my image tag (&lt;img&gt;) for this it is important I give it a "name" and set it to something relevant to what I want it to do. In this case I chose "slide". I placed it towards the bottom of the of the  tag because I want this to appear at the bottom of the page.&lt;br&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;img name="slide" class="slidehow-container"/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;I then set the CSS for the image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  img {
      display: flex;
      width: 400px;
      height: 300px;
      margin-left: auto;
      margin-right: auto;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I say how big I want the slide show to be. You can get fancy here, I chose a more simple route.&lt;/p&gt;

&lt;p&gt;Now comes the JavaScript!&lt;/p&gt;

&lt;p&gt;First I will set the variables.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let i = 0;
let images = [];
let time = 4000;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are setting &lt;code&gt;i&lt;/code&gt; to zero(0) to show where we want to start.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let images = [];&lt;/code&gt; is set to empty array as we will create more files for images that will be in the slideshow.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let time = 4000;&lt;/code&gt; &lt;code&gt;time&lt;/code&gt; is our timer, JavaScript counts in milliseconds so setting &lt;code&gt;time = 4000&lt;/code&gt; is setting it to 4-seconds.&lt;/p&gt;

&lt;p&gt;now we set &lt;code&gt;images&lt;/code&gt; to an array and set that array to a link of the image we want a part of the slide show. Note the URLs are contained in strings &lt;code&gt;''&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;images[0] = 'https://upload.wikimedia.org/wikipedia/en/5/5e/Rhcp1.jpg';
    images[1] = 'https://upload.wikimedia.org/wikipedia/en/5/5b/Freakystyleyalbumcover.jpg';
    images[2] = 'https://upload.wikimedia.org/wikipedia/en/a/a2/UpliftMofoCover.jpg';
    images[3] = 'https://upload.wikimedia.org/wikipedia/en/9/98/Mother%27sMilkAlbumcover.jpg';
    images[4] = 'https://upload.wikimedia.org/wikipedia/en/5/5e/RHCP-BSSM.jpg';
    images[5] = 'https://upload.wikimedia.org/wikipedia/en/8/8a/Rhcp7.jpg';
    images[6] = 'https://upload.wikimedia.org/wikipedia/en/d/df/RedHotChiliPeppersCalifornication.jpg';
    images[7] = 'https://upload.wikimedia.org/wikipedia/en/2/23/Rhcp9.jpg';
    images[8] = 'https://upload.wikimedia.org/wikipedia/en/e/e6/Stadiumarcadium.jpg';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we have the images we want in the slide and set the timer variable we need to create a function that will execute  the slide show. For this we will need an &lt;code&gt;if&lt;/code&gt; &amp;amp; &lt;code&gt;else&lt;/code&gt; statement that will make the photos change every 4-seconds and when we get to end of the images, we want the slide show to start all over again.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function slideShow(){
    document.slide.src = images[i];

        if(i &amp;lt; images.length - 1){
           i++;
        } else{
        i = 0
        }

 setTimeout("slideShow()", time);

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

&lt;/div&gt;



&lt;p&gt;If you're like me, you'll spend too much time on naming a function. I do my best to use the KISS method Keep It Simple Stupid. Be clear and direct so if other coders were to read it, they know what the function should be doing. &lt;/p&gt;

&lt;p&gt;let's break this down!&lt;/p&gt;

&lt;p&gt;&lt;code&gt;document.slide.src = images[i];&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;document. is a anchor to the HTML page, &lt;code&gt;.slide&lt;/code&gt; goes to the &lt;a href="" class="article-body-image-wrapper"&gt;&lt;img&gt;&lt;/a&gt; tag in the HTML document, src is a reference to the file in my VS Code. So in short this line of the function is reaching into my HTML file finding the &lt;code&gt;name="slide"&lt;/code&gt; and setting it to &lt;code&gt;images[i];&lt;/code&gt;. and if you remember the variables we set at the beginning &lt;code&gt;images[i];&lt;/code&gt; = &lt;code&gt;images[0]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now we set the &lt;code&gt;if&lt;/code&gt; &amp;amp; &lt;code&gt;else&lt;/code&gt; statements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if(i &amp;lt; images.length - 1){
           i++;
        } else{
        i = 0
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In pseudo code this &lt;code&gt;if&lt;/code&gt; statement is saying;&lt;br&gt;
&lt;code&gt;if&lt;/code&gt; index of 0 is less than all the images and take away 1 from that. &lt;code&gt;if&lt;/code&gt; that is true then we want to increment by 1 (i++). When we reach to last index[i] we want the &lt;code&gt;else&lt;/code&gt; statement to reset the index to 0 and start the slideshow all over again.&lt;/p&gt;

&lt;p&gt;Now we implement the timer by invoking the function with the &lt;code&gt;time&lt;/code&gt; variable to set at the beginning like so;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function slideShow(){
    document.slide.src = images[i];

        if(i &amp;lt; images.length - 1){
           i++;
        } else{
        i = 0
        }

 setTimeout("slideShow()", time);

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

&lt;/div&gt;



&lt;p&gt;Note; &lt;code&gt;setTimeout("SlideShow"(), time);&lt;/code&gt; is still inside the &lt;code&gt;function slideShow()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And lastly we want this function to load when the page loads, So we'll add &lt;code&gt;window.onload = slideShow;&lt;/code&gt;. &lt;code&gt;window.onload&lt;/code&gt; ensure this function loads after the window has loaded. &lt;br&gt;
All together the JS code looks like;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let i = 0;
let images = [];
let time = 4000;


    images[0] = 'https://upload.wikimedia.org/wikipedia/en/5/5e/Rhcp1.jpg';
    images[1] = 'https://upload.wikimedia.org/wikipedia/en/5/5b/Freakystyleyalbumcover.jpg';
    images[2] = 'https://upload.wikimedia.org/wikipedia/en/a/a2/UpliftMofoCover.jpg';
    images[3] = 'https://upload.wikimedia.org/wikipedia/en/9/98/Mother%27sMilkAlbumcover.jpg';
    images[4] = 'https://upload.wikimedia.org/wikipedia/en/5/5e/RHCP-BSSM.jpg';
    images[5] = 'https://upload.wikimedia.org/wikipedia/en/8/8a/Rhcp7.jpg';
    images[6] = 'https://upload.wikimedia.org/wikipedia/en/d/df/RedHotChiliPeppersCalifornication.jpg';
    images[7] = 'https://upload.wikimedia.org/wikipedia/en/2/23/Rhcp9.jpg';
    images[8] = 'https://upload.wikimedia.org/wikipedia/en/e/e6/Stadiumarcadium.jpg';

function slideShow(){
    document.slide.src = images[i];

        if(i &amp;lt; images.length - 1){
           i++;
        } else{
        i = 0
        }

 setTimeout("slideShow()", time);

}

window.onload = slideShow;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Leaving us with this as the final result.&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%2Fpg8xo2eu6aoruncl4t7o.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpg8xo2eu6aoruncl4t7o.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I know it's not the flashy or super what I was completely looking for. But it's a step in the right direction!&lt;/p&gt;

&lt;p&gt;Hope you enjoyed,&lt;/p&gt;

&lt;p&gt;Ryan Barrera&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
