{"id":3756,"date":"2023-07-18T01:12:30","date_gmt":"2023-07-18T01:12:30","guid":{"rendered":"https:\/\/nmi.cool\/advweb\/?page_id=3756"},"modified":"2026-02-09T16:23:49","modified_gmt":"2026-02-09T16:23:49","slug":"exercise-build-a-slideshow","status":"publish","type":"page","link":"https:\/\/nmi.cool\/advweb\/unit-two-javascript\/week-four\/exercise-build-a-slideshow\/","title":{"rendered":"Exercise: Build a Slideshow"},"content":{"rendered":"\n<p>In the previous exercise we learned how to use two arrays to display a group of pictures and text inside of an html document. In this exercise we are going to use a very similar technique that uses the same arrays to produce a slideshow that displays information for just one player at a time.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Much of the work for this lesson was already taken care of in the previous lesson, so we might as well take advantage of it. So duplicate your work and move it into a new folder (maybe a Week Five folder).<\/li>\n\n\n\n<li>Open up the new folder in VS Code, and open roster.html and roster.js side by side.<\/li>\n\n\n\n<li>Comment out the ordered list in roster.html. Add a div and two buttons:<img decoding=\"async\" src=\"https:\/\/apweb.quest\/wp-content\/uploads\/2018\/10\/img_5bc797b7d4544.png\" alt=\"\"> <pre class=\"wp-block-code\"><code>&lt;div id=\"slideshow\"&gt;&lt;\/div&gt;\n&lt;button id=\"previous\"&gt;Previous&lt;\/button&gt;\n&lt;button id=\"next\"&gt;Next&lt;\/button&gt;<\/code><\/pre><\/li>\n\n\n\n<li>In roster.js, comment out or delete everything after the arrays. Create a variable named slideshow and connect it to #slideshow. Also, let&#8217;s go ahead and establish a starting point by declaring a currentIndex variable. This variable holds our spot in the array. <img decoding=\"async\" src=\"https:\/\/apweb.quest\/wp-content\/uploads\/2018\/10\/img_5bc79853c6647.png\" alt=\"\"><pre class=\"wp-block-code\"><code>let slideshow = document.querySelector('#slideshow'); <br>let currentIndex = 0;<span style=\", serif\"><\/span><\/code><\/pre><\/li>\n\n\n\n<li>Create a function to display the current player. <img decoding=\"async\" src=\"https:\/\/apweb.quest\/wp-content\/uploads\/2018\/10\/img_5bc799a08ea4a.png\" alt=\"\"><pre class=\"wp-block-code\"><code>function showCurrentPlayer() {\n    slideshow.innerHTML = rosterNames[currentIndex] + \"&lt;br&gt;&lt;img src='roster\/\" + rosterPix[currentIndex] + \"'&gt;\";\n}<\/code><\/pre><\/li>\n\n\n\n<li>Now, we&#8217;re going to create a function that updates the index. Let&#8217;s call the function <br>canShow(plusMinus). <img decoding=\"async\" src=\"https:\/\/apweb.quest\/wp-content\/uploads\/2018\/10\/img_5bc79a61b5475.png\" alt=\"\"><pre class=\"wp-block-code\"><code>function canShow(plusMinus) {<br>    currentIndex = currentIndex + plusMinus;<br><br>    <strong>\/\/ Keep the index within bounds<\/strong><br>    if (currentIndex &lt; 0) {<br>        currentIndex = 0;<br>    }<br>    if (currentIndex &gt;= rosterNames.length) {<br>        currentIndex = rosterNames.length - 1;<br>    }<br><br>    showCurrentPlayer();<br>}<\/code><\/pre><\/li>\n\n\n\n<li>Next let&#8217;s have the current player appear as soon as the page loads by adding this after the closing bracket of the canShow function. <pre class=\"wp-block-code\"><code>showCurrentPlayer();<\/code><\/pre><\/li>\n\n\n\n<li>It&#8217;s time to talk to the buttons. Let&#8217;s connect the next button first.<img decoding=\"async\" src=\"https:\/\/apweb.quest\/wp-content\/uploads\/2018\/10\/img_5bc7a08051031.png\" alt=\"\"><pre class=\"wp-block-code\"><code>document.querySelector('#next').onclick = function () {\n    canShow(1);\n}<\/code><\/pre><\/li>\n\n\n\n<li>Try connecting the previous button on your own. <\/li>\n\n\n\n<li>Voila! You should have a functioning slideshow. <\/li>\n<\/ol>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>Come back here for a hint when you need it!<\/summary>\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>document.querySelector('#previous').onclick = function () {\n    canShow(-1);\n}<\/code><\/pre>\n<\/details>\n\n\n\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-ad2f72ca wp-block-group-is-layout-flex\">\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>How can I make the slideshow loop?<\/summary>\n<p>Change the for loop!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function canShow(plusMinus) {\n    currentIndex = currentIndex + plusMinus;\n\n    <strong>\/\/ Loop back to the end if too far left<\/strong>\n    if (currentIndex &lt; 0) {\n        currentIndex = rosterNames.length - 1;\n    }\n\n    <strong>\/\/ Loop to the start if too far right<\/strong>\n    if (currentIndex &gt;= rosterNames.length) {\n        currentIndex = 0;\n    }\n\n    showCurrentPlayer();\n}<\/code><\/pre>\n<\/details>\n\n\n\n<p><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In the previous exercise we learned how to use two arrays to display a group of pictures and text inside of an html document. In this exercise we are going to use a very similar technique that uses the same arrays to produce a slideshow that displays information for just one player at a time.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3690,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3756","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3756","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/comments?post=3756"}],"version-history":[{"count":15,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3756\/revisions"}],"predecessor-version":[{"id":4767,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3756\/revisions\/4767"}],"up":[{"embeddable":true,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3690"}],"wp:attachment":[{"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/media?parent=3756"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}