{"componentChunkName":"component---src-templates-legacy-tutorial-page-js","path":"/oa/tutorials/build-a-game-of-concentration-with-javascript/conclusion-amI=/","result":{"pageContext":{"tutorial":{"id":"T0E6OlR1dG9yaWFsLTE1MA==","slug":"build-a-game-of-concentration-with-javascript","title":"Build a Game of Concentration with Javascript","previewText":"Get started using Javascript to manipulate the DOM and add interactivity to webpages.\n","heroImagePath":"https://raw.githubusercontent.com/MakeSchool-Tutorials/sa-2018-concentration/master/cover.png","heroImageFile":{"childImageSharp":{"gatsbyImageData":{"layout":"constrained","placeholder":{"fallback":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAAsTAAALEwEAmpwYAAAEsElEQVQ4y52UfUzUdRzHf8qa1WZtgHgodwcCp3Cx2kpLhVJrWCkPiZkNkUxTsfKRqYGI4BBI8QkU5CSelOPwuAfuRDD1FB+Hlg+JLFNAxKzcELQD7o67V7sD0v7tj/e+n+373evz2r7fz1cQS+XUmKrYiQerbQKJCKQ+lvDqyD4EAVd8R/VgSYmB9dNgTRhPshbgZbUgAILNxgSgXFfDKJEEQSwNxmCqpgA5yTY3UhHIfPwmMv9eXh4OI9xgktyMJWsRpEVDSgRP9iQQbO1hGP0Itl7CgEPPgHIMpioUuLPZJpCOQHanmFdGWv81lHpZsGyNhU0fwcZwunOWMMbKoCGEuAwNQ8AJ6E21pLOfxdYkviKZZHMu2zP+YMvaP9m87i/2Zf6O7cAG7HuXw65lmEuS+MaiI8qhZJa1jBTOodRp8RRJEaTSQFSmBgIGuzm7htgB8yUwm6CnAbrO4shZjCPtE0iNoS83noNWEdkIZNgEqnkdlU6Jp8hvCHiGQCfQ0otgtxFi7eXpvUZsLWfpaz2P5e4F7JkLsCd/DN/Nonfn5+yy+pOEwHqbQAlTUOkqnwErT59B7ARidxnKsGHvugSdp6GrAR6dg5x4SIuA1Ej6c2P53ublehGr+wUUhDwzFPvJ0NWfJrERYozdxBz7m3WmJzTX3Ka5qolbVc38ZmjiXvpKWjcspC0xjrbslZzv3sQJcwJ13Uu4xR7UOjUeIl8EnwAZR9WnqJzWSY73XXaPbaVo8n12vvWANHE7aeIH5Ia2UDEplOJAGUW+AahnfABtV6HjKrQ0Qk8HWq0Od+ct+/jLMGpOURz5mK1BLWTI77FvZgd509vJlreSJW/nwId3UE2dRnmwnNLAILThM7G0NdB/30Rfy3EwN6NxAaUDhsZqE2UznrLNp51s8QMKpj4kb+JDsiUdZEkeUhjWSs2UN6gO8qYqwIu68Hfg0RXougKPLgLtaHVDhuNkGA0nKc9oI2vZT2xPuMaBlCb2b7zpqrevuE5h8g2+n5VL6pQsUt7OZPecPNqNSto1ClpU+Zgbj1NdrcHDaSj2G4++rp5NpwzEVRQSX1lEokHFKn2Fq/7iiII11RpEPjDMDYTh4CcB5bszKR3nj8JHimnefKqUKjzG+A4AdcfqSdYfJbagiLjCEtYq1Xx7SOWqFx4sZlWpDvFoCyMEG26CHdmYbtThs6l4LZgSmYyzC+M4MgSU+I1HW1vHSrWGiL37iMrLZ2nZYRYVlxGVV0B0fj6LFSpeeqEfQXC4Ztv9xaeUTwzjoGg0+909+TEykqqKygHgWF8ZdSdOcrHtDporl9D/fJkTTdc5/ss1V62/2kjd9ZvsyLSTnuxgSxLkZvdxu/gHmvfs4MaOLDoNGjRqDe7ezlkeF4jKeIaEWog5BPMrYZ4SPhvMfCXEq2GJ0UGcfiCxWgcxapirhdkqSDwHpSoDXt4SBP+AQBSaBoQkEJb2I6zgv/kaRNvAIx2E5c8lwYGQYEf40srIbVBw2IhorPP78gtEV3sS/a9QdLGH0ssWShufpWQwxYPr83vOs4oLZupb4LBKi6fTUBoQTOh77/PpnChioiP+V+bOiWZy6HQk/kH8AwjYIZSpK76eAAAAAElFTkSuQmCC"},"images":{"fallback":{"src":"/mediabook/static/c6cd91a6ad3093c648926e9084a26640/5aead/cover.png","srcSet":"/mediabook/static/c6cd91a6ad3093c648926e9084a26640/e9fba/cover.png 50w,\n/mediabook/static/c6cd91a6ad3093c648926e9084a26640/15e42/cover.png 100w,\n/mediabook/static/c6cd91a6ad3093c648926e9084a26640/5aead/cover.png 200w,\n/mediabook/static/c6cd91a6ad3093c648926e9084a26640/d6138/cover.png 400w","sizes":"(min-width: 200px) 200px, 100vw"},"sources":[{"srcSet":"/mediabook/static/c6cd91a6ad3093c648926e9084a26640/b79cb/cover.avif 50w,\n/mediabook/static/c6cd91a6ad3093c648926e9084a26640/6d0de/cover.avif 100w,\n/mediabook/static/c6cd91a6ad3093c648926e9084a26640/f2685/cover.avif 200w,\n/mediabook/static/c6cd91a6ad3093c648926e9084a26640/4ff31/cover.avif 400w","type":"image/avif","sizes":"(min-width: 200px) 200px, 100vw"},{"srcSet":"/mediabook/static/c6cd91a6ad3093c648926e9084a26640/dbc4a/cover.webp 50w,\n/mediabook/static/c6cd91a6ad3093c648926e9084a26640/d8057/cover.webp 100w,\n/mediabook/static/c6cd91a6ad3093c648926e9084a26640/2e34e/cover.webp 200w,\n/mediabook/static/c6cd91a6ad3093c648926e9084a26640/416c3/cover.webp 400w","type":"image/webp","sizes":"(min-width: 200px) 200px, 100vw"}]},"width":200,"height":200}}},"pages":{"nodes":[{"id":"T0E6OlBhZ2UtMTA0Mw==","title":"Introduction","slug":"intro-landing-page-Tqk=","sections":{"nodes":[{"id":"T0E6OlNlY3Rpb24tMzY1NA==","title":"Introduction","htmlContent":"<p>Welcome, welcome. You've reached the third tutorial of Make School's full-stack web series!</p><p>In this tutorial, we'll become friends with client-side JavaScript by building a game of concentration. We'll do the following:</p><ol>\n<li>Build the game UI using HTML, CSS, and Bootstrap</li>\n<li>Implement the game logic with client-side JavaScript</li>\n</ol><p>In this tutorial, we'll focus on using our front-end technologies: HTML, CSS and client-side JavaScript. Our game will be played in the web browser and won't have a back-end. All code will be retrieved locally on our personal computer.</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P00-Intro/assets/concentration_focus.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P00-Intro/assets/concentration_focus.jpg\" alt=\"Concentration Focus\" title=\"\">\n        </a></p><div class=\"info\">\n<p></p>\n\n<p>Client-side JavaScript refers to JavaScript code that implements logic on our client (in our case, the browser). We'll use JavaScript to implement our concentration game's logic. In the next tutorial, we'll start to learn about using JavaScript to build web apps that run on servers.</p>\n</div>"},{"id":"T0E6OlNlY3Rpb24tMzY1NQ==","title":"What is Concentration?","htmlContent":"<p>The game of concentration involves a board containing an even number of squares. Each square in the grid has a color that matches another square in the board. However, the color of all squares are hidden. The goal of the game is to match the color of each square with it's matching pair.</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P00-Intro/assets/completed_game.png\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P00-Intro/assets/completed_game.png\" alt=\"Completed Game\" title=\"\">\n        </a></p><p>Once the game starts, the player clicks on any square within the grid. Once clicked, the square will reveal it's hidden color.</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P00-Intro/assets/flipped_square.png\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P00-Intro/assets/flipped_square.png\" alt=\"Flipped Square\" title=\"\">\n        </a></p><p>The player then has the opportunity to reveal the color of another square. If the color of the second square matches the color of the first square, a match is made. If not, the colors of the two squares return to hidden and the player tries again.</p><p><video width=\"100%\" controls><source src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P00-Intro/assets/playing_concentration.mov\" type=\"video/mp4\"></source></video></p><p>The player tries to match all of the colored squares with their matching square as quickly as possible.</p>"},{"id":"T0E6OlNlY3Rpb24tMzY1Ng==","title":"Who Is This For?","htmlContent":"<p>Beginners who have some familiarity with using the following front-end technologies: HTML, CSS, JavaScript and  Bootstrap.</p><h2>What You Should Already Know</h2><p>This tutorial builds on key concepts from the first two Make School tutorials.</p><p>You should know:</p><ul>\n<li>how to build static webpages with HTML, CSS, and Bootstrap</li>\n</ul><p>If you can't, it's recommended to first complete or review the previous Make School tutorials.</p><h2>Estimated Completion Time</h2><p>4 hours</p>"},{"id":"T0E6OlNlY3Rpb24tMzY1Nw==","title":"What We're Building","htmlContent":"<p>In this tutorial, we'll build a game of concentration.</p><p>If you've been searching for one of those classic, old-school games to distract you from everything you should be doing, this one's for you! Please play responsibly.</p><p>Let's take a look at what our game will look like when it's complete:</p><p><video width=\"100%\" controls><source src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P00-Intro/assets/finish_full_game.mov\" type=\"video/mp4\"></source></video></p><p>To review the rules of gameplay: a player reveals the color of two squares at a time. If the color of the chosen squares are the same, a match is made. If not, the color of the square return to hidden. The game is won when the player matches every square with it's matching pair in the board.</p><!-- You can preview the finished concentration game in the browser here: --><!--  --><!-- TODO: turn into github page with the concentration game -->"},{"id":"T0E6OlNlY3Rpb24tMzY1OA==","title":"What You'll Learn","htmlContent":"<p>By the end of this tutorial, you will:</p><ul>\n<li>become more familiar with programming in JavaScript</li>\n<li>manipulate the DOM using client-side JavaScript</li>\n<li>learn to think through and implement more complex logic</li>\n<li>utilize the Chrome DevTools for debugging client-side JavaScript</li>\n</ul>"},{"id":"T0E6OlNlY3Rpb24tMzY1OQ==","title":"If You Get Stuck","htmlContent":"<p>Getting stuck when coding (and debugging) is a natural part of the programming process. If you find yourself stuck on a problem or lost, pause for a moment and take a breath. Maybe take a walk. Then retrace your steps (in the tutorial, not the walk). Make sure you've followed each step of the tutorial. It's easy to make typos or accidentally skip over important steps.</p><p>If you want to compare your code to the solution, you can find it here:</p><!-- TODO: insert link to github repo -->"}]},"next":{"id":"T0E6OlBhZ2UtMTA0NA==","slug":"dev-env-JEU=","title":"Setup Dev Environment"},"previous":{"id":"T0E6OlBhZ2UtMTA0NA==","slug":"dev-env-JEU=","title":"Setup Dev Environment"}},{"id":"T0E6OlBhZ2UtMTA0NA==","title":"Setup Dev Environment","slug":"dev-env-JEU=","sections":{"nodes":[{"id":"T0E6OlNlY3Rpb24tMzY2MA==","title":"Setup Dev Environment","htmlContent":"<p>To start, we'll setup our developer environment and create a new project.</p><!-- TODO: add review for what a dev env is --><p>We'll be using the same set of tools we previously learned about:</p><ul>\n<li>Atom: writing code</li>\n<li>Chrome: viewing and interacting with our UI</li>\n<li>Chrome DevTools: debugging and testing our game</li>\n</ul><p>You'll need to make sure you have both <em>Google Chrome</em> and the <em>Atom</em> text editor installed. If you've completed the previous tutorials, you will have done through this already.</p><div class=\"info\">\n<p></p>\n\n<p>If you don't have the required software installed, make sure you download and install the missing software before proceeding.</p>\n</div><p>With the correct tools, let's create a new project for our codebase.</p>"},{"id":"T0E6OlNlY3Rpb24tMzY2MQ==","title":"Setting Up a New Project","htmlContent":"<p>Whenever you're creating a new front-end project, you'll need to repeat the following steps:</p><div class=\"action\">\n<p></p>\n\n<p>Open the Atom text editor and create a new file:</p>\n\n<ol>\n<li>Create a new file by pressing the shortcut <code>CMD-n</code> or selecting the <em>New File</em> command in the <em>File menu</em>. <a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/new_file.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/new_file.jpg\" alt=\"New File\" title=\"\">\n        </a>\n</li>\n<li>Save the new <em>untitled</em> file you just created by pressing the shortcut <code>CMD-s</code> or selected the <em>Save</em> command in the <em>File menu</em>. <a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/save_file.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/save_file.jpg\" alt=\"Save File\" title=\"\">\n        </a>\n</li>\n<li>In the prompt, find a easy-to-find file path (i.e. <code>~/Code/html/</code>) on your computer to save all the files for your soon-to-be concentration game. Create a new project folder called <code>concentration</code> that will contain all your project-related files. <a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/new_project_folder.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/new_project_folder.jpg\" alt=\"New Project Folder\" title=\"\">\n        </a>\n</li>\n<li>Save your new file with the name <code>index.html</code>. It's important to include the extension <code>.html</code>. <a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/save_file_name.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/save_file_name.jpg\" alt=\"Save File Name\" title=\"\">\n        </a>\n</li>\n<li>Click the <code>Save</code> button to finish creating your new <code>index.html</code> file and enclosing project folder. <a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/click_save.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/click_save.jpg\" alt=\"Click Save\" title=\"\">\n        </a>\n</li>\n</ol>\n</div><p>You've now created a new project folder with a empty <code>index.html</code>. This will be the home of our new concentration game.</p><p>Next, let's make sure our Project Tree View is showing the correct project.</p><div class=\"action\">\n<p></p>\n\n<p>Let's make sure our Tree View has the correct project shown:</p>\n\n<ol>\n<li>Close the Atom window (don't quit the entire application) by clicking the red X in the left-hand corner. <a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/close_window.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/close_window.jpg\" alt=\"Close Window\" title=\"\">\n        </a>\n</li>\n<li>In the Atom <em>File menu</em>, select <em>Reopen Project</em> to open your concentration project. <a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/open_concentration_project.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/open_concentration_project.jpg\" alt=\"Open Concentration Project\" title=\"\">\n        </a>\n</li>\n</ol>\n</div><p>Once you're finished, you should see your <code>concentration</code> project folder and your new <code>index.html</code> file in the Atom editor Tree View.</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/concentration_project.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/concentration_project.jpg\" alt=\"Concentration Project\" title=\"\">\n        </a></p><p>Next, we'll add the boilerplate HTML code and Bootstrap to our <code>index.html</code>.</p>"},{"id":"T0E6OlNlY3Rpb24tMzY2Mg==","title":"New HTML File Boilerplate","htmlContent":"<p><code>Hello, World!</code> is always a good place to start. The Bootstrap docs will make this extremely easy for us.</p><div class=\"action\">\n<p></p>\n\n<p>Using the Bootstrap documentation's <a href=\"http://getbootstrap.com/docs/4.0/getting-started/introduction/#starter-template\" target=\"_blank\">starter template</a>, setup the code for an initial <code>Hello, World!</code> web page.</p>\n\n<p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/starter_template.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/starter_template.jpg\" alt=\"Starter Template\" title=\"\">\n        </a></p>\n</div><p>To make sure our HTML works, let's open it in the browser.</p><p>Remember how?</p><div class=\"challenge\">\n<p></p>\n\n<p>In your Chrome web browser, open your concentration <code>index.html</code> in a new tab.</p>\n</div><!-- break --><div class=\"solution\">\n<p></p>\n\n<p>To open your HTML document in Chrome, follow the steps below:</p>\n\n<ol>\n<li>Open the <em>Tree View</em> in <em>Atom</em> if it's not open already. You can toggle the <em>Tree View</em> with the shortcut <code>CMD-\\</code> or selecting the command under the <em>View menu</em>. <a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/toggle_tree_view.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/toggle_tree_view.jpg\" alt=\"Toggle Tree View\" title=\"\">\n        </a>\n</li>\n<li>With your <em>Tree View</em> open, right-click on your <code>index.html</code> file. In the contextual menu that pops up, click on <code>Copy Full Path</code>. <a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/copy_full_path.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/copy_full_path.jpg\" alt=\"Copy Full Path\" title=\"\">\n        </a>\n</li>\n<li>Open a <em>New Tab</em> in <em>Google Chrome</em> and paste the full file path into the address bar. <a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/chrome_full_file_path.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/chrome_full_file_path.jpg\" alt=\"Chrome Full File Path\" title=\"\">\n        </a>\n</li>\n<li>Once your <code>index.html</code> file path is in the address path, hit the <em>Return</em> key and you should see your <code>Hello, World!</code> web page. <a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/hello_world.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/hello_world.jpg\" alt=\"Hello World\" title=\"\">\n        </a>\n</li>\n</ol>\n</div><p>Off to a good start.</p>"},{"id":"T0E6OlNlY3Rpb24tMzY2Mw==","title":"Setting Up CSS","htmlContent":"<p>To finish our initial project setup, we'll also need to add our CSS and JavaScript.</p><p>We'll start with CSS.</p><div class=\"action\">\n<p></p>\n\n<ol>\n<li>Create a new folder in your <code>concentration</code> project folder named <code>css</code>.</li>\n<li>Create a new file in your <code>css</code> folder named <code>style.css</code>.</li>\n</ol>\n</div><p>Your Tree View should look like:</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/css_tree_view.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/css_tree_view.jpg\" alt=\"CSS Tree View\" title=\"\">\n        </a></p><p>Back in <code>index.html</code>, we'll need to link our new stylesheet.</p><div class=\"action\">\n<p></p>\n\n<p>In <code>index.html</code>, add the following tag inside your <code>&lt;head&gt;</code> element:</p>\n<pre><span class=\"nt\">&lt;head&gt;</span>\n  // ...\n\n  <span class=\"nt\">&lt;link</span> <span class=\"na\">rel=</span><span class=\"s\">\"stylesheet\"</span> <span class=\"na\">href=</span><span class=\"s\">\"css/style.css\"</span><span class=\"nt\">&gt;</span>\n<span class=\"nt\">&lt;/head&gt;</span></pre>\n</div><p>To make sure our CSS is properly connected, let's test it out.</p><div class=\"action\">\n<p></p>\n\n<p>In your new <code>style.css</code>, add the following CSS rule:</p>\n<pre><span class=\"nt\">h1</span> <span class=\"p\">{</span>\n  <span class=\"k\">color</span><span class=\"o\">:</span> <span class=\"nb\">orange</span><span class=\"p\">;</span>\n<span class=\"p\">}</span></pre>\n</div><p>Refresh your HTML doc in your browser and you should see <code>Hello, World!</code> in orange.</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/orange_hello_world.png\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/orange_hello_world.png\" alt=\"Orange Hello World\" title=\"\">\n        </a></p><p>If you see your <code>&lt;h1&gt;</code> in orange, you can rest assured that your CSS is properly linked. If not, go back through the previous steps again.</p><p>Next, we'll need a place to store our script files that we'll use to code our concentration game.</p>"},{"id":"T0E6OlNlY3Rpb24tMzY2NA==","title":"Configuring JavaScript","htmlContent":"<p>Similar to our CSS, we'll want to create a new file where we can write JavaScript. Then, we'll need to link our script back to our HTML document.</p><p>Let's create our first script file:</p><div class=\"action\">\n<p></p>\n\n<ol>\n<li>Create a new folder in your <code>concentration</code> project folder named <code>js</code>.</li>\n<li>Create a new file in your <code>js</code> folder named <code>main.js</code>.</li>\n</ol>\n</div><p>When you're done, your Atom Tree View should look like the following:</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/js_tree_view.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/js_tree_view.jpg\" alt=\"JS Tree View\" title=\"\">\n        </a></p><p>Like our CSS file, we'll need to link to <code>main.js</code> from our HTML document.</p><div class=\"action\">\n<p></p>\n\n<p>In <code>index.html</code>, add the following <code>&lt;script&gt;</code> element below Bootstrap's scripts but before the closing <code>&lt;body&gt;</code> tag:</p>\n<pre><span class=\"nt\">&lt;body&gt;</span>\n  <span class=\"c\">&lt;!-- ... previous HTML content --&gt;</span>\n  <span class=\"c\">&lt;!-- ... Bootstrap scripts --&gt;</span>\n\n  <span class=\"c\">&lt;!-- add line below --&gt;</span>\n  <span class=\"nt\">&lt;script </span><span class=\"na\">src=</span><span class=\"s\">\"js/main.js\"</span> <span class=\"na\">charset=</span><span class=\"s\">\"utf-8\"</span><span class=\"nt\">&gt;&lt;/script&gt;</span>\n<span class=\"nt\">&lt;/body&gt;</span></pre>\n</div><p>Finally, we'll want to test that our HTML doc is correctly configure with our new script file. To do so, we'll use Chrome's DevTools.</p><h2>Using the JavaScript Console</h2><p>To see if our <code>main.js</code> script is properly connectly to our HTML document, we'll need to switch back to our browser. First, we'll need to open our DevTools window.</p><div class=\"action\">\n<p></p>\n\n<p>Navigate to your Chrome browser. With your <code>index.html</code> web page open, open the Chrome DevTools by pressing the shortcut <code>opt-CMD-i</code> or select the Developer Tools command in the <em>View &gt; Developer menu</em>. <a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/view_menu_dev_tools.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/view_menu_dev_tools.jpg\" alt=\"View Menu Dev Tools\" title=\"\">\n        </a></p>\n</div><p>You'll either see a the DevTools open in a pane on your browser or in a new window.</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/dev_tools_window.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/dev_tools_window.jpg\" alt=\"DevTools Window\" title=\"\">\n        </a></p><div class=\"info\">\n<p></p>\n\n<p>If you want to change whether your DevTools is part of your browser or in a separate window, you can configure it the DevTool settings. <a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/dev_tools_settings.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/dev_tools_settings.jpg\" alt=\"DevTools Settings\" title=\"\">\n        </a></p>\n</div><p>With our DevTools open, let's navigate to the JavaScript console.</p><div class=\"action\">\n<p></p>\n\n<p>Click on the <em>Console</em> tab of your DevTools. <a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/javascript_console.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/javascript_console.jpg\" alt=\"JavaScript Console\" title=\"\">\n        </a></p>\n</div><p>This is the JavaScript console. In it, you can write and test basic JavaScript. For example we can use <code>console.log()</code> to print text into the console.</p><p>Let's do that now.</p><div class=\"action\">\n<p></p>\n\n<p>Write the following code in your JavaScript Console:</p>\n<pre><span class=\"nx\">console</span><span class=\"p\">.</span><span class=\"nx\">log</span><span class=\"p\">(</span><span class=\"s1\">'writing JavaScript in the console'</span><span class=\"p\">);</span></pre>\n<p>You should see the console print the text within the parenthesis: <a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/log_in_console.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/log_in_console.jpg\" alt=\"Log In Console\" title=\"\">\n        </a></p>\n</div><p>To test if our <code>main.js</code> script is connected to our <code>index.html</code> file, we can simply write JavaScript code in our <code>main.js</code> script and see if it's executed from the DevTools JavaScript Console.</p><div class=\"action\">\n<p></p>\n\n<p>Open <code>main.js</code> in your Atom editor and add the following code:</p>\n<pre><span class=\"nx\">console</span><span class=\"p\">.</span><span class=\"nx\">log</span><span class=\"p\">(</span><span class=\"s1\">'writing JavaScript with Atom'</span><span class=\"p\">);</span></pre>\n<p>Make sure to save <code>main.js</code> after you add the code above.</p>\n</div><p>Now, let's navigate back to our browser and refresh our web page. Keep an eye on your JavaScript console.</p><p>What happened? You'll notice that (if setup correctly) the code we wrote in <code>main.js</code> using our Atom text editor executed and was logged in our DevTools console.</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/log_with_atom.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P01-Setup-Dev-Environment/assets/log_with_atom.jpg\" alt=\"Log With Atom\" title=\"\">\n        </a></p><div class=\"info\">\n<p></p>\n\n<p>If set up incorrectly, you won't see any text printed in your console. Try to retrace your steps and figure out what you're missing.</p>\n</div>"},{"id":"T0E6OlNlY3Rpb24tMzY2NQ==","title":"Up Next","htmlContent":"<p>We've successfully set up our new project and the tools we'll use to build our game of concentration. In the next section, we'll start building the game's UI using our familiar friends HTML, CSS and Bootstrap.</p>"}]},"next":{"id":"T0E6OlBhZ2UtMTA0NQ==","slug":"building-ui","title":"Building the UI"},"previous":{"id":"T0E6OlBhZ2UtMTA0NQ==","slug":"building-ui","title":"Building the UI"}},{"id":"T0E6OlBhZ2UtMTA0NQ==","title":"Building the UI","slug":"building-ui","sections":{"nodes":[{"id":"T0E6OlNlY3Rpb24tMzg1MQ==","title":"Building the UI","htmlContent":"<p>Let's start building. Throughout this section, we'll be focused on implementing the HTML and CSS for our game of concentration. When we're done, our game should look like the following:</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/game_design.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/game_design.jpg\" alt=\"Game Design\" title=\"\">\n        </a></p><p>But first, we'll need to get rid of our previous <code>Hello, world!</code> code.</p>"},{"id":"T0E6OlNlY3Rpb24tMzg1Mg==","title":"Clearing Test Code","htmlContent":"<p>After successfully verifying that our new project is properly configured, we'll no longer need <code>Hello, world</code> or any of the other code we added in <code>style.css</code> and <code>main.js</code>.</p><p>To start building our board, we'll reset and delete the <code>Hello, world!</code> boilerplate code in our project.</p><div class=\"action\">\n<p></p>\n\n<p>In <code>index.html</code>, perform the following:</p>\n<pre><span class=\"cp\">&lt;!doctype html&gt;</span>\n<span class=\"nt\">&lt;html</span> <span class=\"na\">lang=</span><span class=\"s\">\"en\"</span><span class=\"nt\">&gt;</span>\n<span class=\"nt\">&lt;head&gt;</span>\n  <span class=\"nt\">&lt;meta</span> <span class=\"na\">charset=</span><span class=\"s\">\"utf-8\"</span><span class=\"nt\">&gt;</span>\n  <span class=\"nt\">&lt;meta</span> <span class=\"na\">name=</span><span class=\"s\">\"viewport\"</span> <span class=\"na\">content=</span><span class=\"s\">\"width=device-width, initial-scale=1, shrink-to-fit=no\"</span><span class=\"nt\">&gt;</span>\n\n  <span class=\"nt\">&lt;link</span> <span class=\"na\">rel=</span><span class=\"s\">\"stylesheet\"</span> <span class=\"na\">href=</span><span class=\"s\">\"https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css\"</span> <span class=\"na\">integrity=</span><span class=\"s\">\"sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm\"</span> <span class=\"na\">crossorigin=</span><span class=\"s\">\"anonymous\"</span><span class=\"nt\">&gt;</span>\n\n  <span class=\"c\">&lt;!-- 1. Change Title Text --&gt;</span>\n  <span class=\"nt\">&lt;title&gt;</span>Concentration &ndash; A Test of Memory<span class=\"nt\">&lt;/title&gt;</span>\n\n  <span class=\"nt\">&lt;link</span> <span class=\"na\">rel=</span><span class=\"s\">\"stylesheet\"</span> <span class=\"na\">href=</span><span class=\"s\">\"css/style.css\"</span><span class=\"nt\">&gt;</span>\n<span class=\"nt\">&lt;/head&gt;</span>\n\n<span class=\"nt\">&lt;body&gt;</span>\n  <span class=\"c\">&lt;!-- 2. Delete &lt;h1&gt; --&gt;</span>\n\n  <span class=\"nt\">&lt;script </span><span class=\"na\">src=</span><span class=\"s\">\"https://code.jquery.com/jquery-3.2.1.slim.min.js\"</span> <span class=\"na\">integrity=</span><span class=\"s\">\"sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN\"</span> <span class=\"na\">crossorigin=</span><span class=\"s\">\"anonymous\"</span><span class=\"nt\">&gt;&lt;/script&gt;</span>\n  <span class=\"nt\">&lt;script </span><span class=\"na\">src=</span><span class=\"s\">\"https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js\"</span> <span class=\"na\">integrity=</span><span class=\"s\">\"sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q\"</span> <span class=\"na\">crossorigin=</span><span class=\"s\">\"anonymous\"</span><span class=\"nt\">&gt;&lt;/script&gt;</span>\n  <span class=\"nt\">&lt;script </span><span class=\"na\">src=</span><span class=\"s\">\"https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js\"</span> <span class=\"na\">integrity=</span><span class=\"s\">\"sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl\"</span> <span class=\"na\">crossorigin=</span><span class=\"s\">\"anonymous\"</span><span class=\"nt\">&gt;&lt;/script&gt;</span>\n  <span class=\"nt\">&lt;script </span><span class=\"na\">src=</span><span class=\"s\">\"js/main.js\"</span> <span class=\"na\">charset=</span><span class=\"s\">\"utf-8\"</span><span class=\"nt\">&gt;&lt;/script&gt;</span>\n<span class=\"nt\">&lt;/body&gt;</span>\n<span class=\"nt\">&lt;/html&gt;</span></pre>\n<ol>\n<li>Delete the <code>&lt;h1&gt;</code> element that displays <code>Hello, world!</code>.</li>\n<li>Modify the title text inside the <code>&lt;title&gt;</code> element of your <code>&lt;head&gt;</code> to <code>Concentration &ndash; A Test of Memory</code>.</li>\n</ol>\n</div><p>Next, we'll need to remove the CSS we added.</p><div class=\"action\">\n<p></p>\n\n<p>In <code>style.css</code>, delete all CSS rules so that your stylesheet is empty.</p>\n</div><p>Finally, we'll remove the <code>console.log</code> that we added to our <code>main.js</code> script.</p><div class=\"action\">\n<p></p>\n\n<p>In <code>main.js</code>, delete all JavaScript code so that your file is empty.</p>\n</div><p>When you're done, your project, with exception of the HTML boilerplate and title, will be completely blank.</p>"},{"id":"T0E6OlNlY3Rpb24tMzg1Mw==","title":"Implementing the UI","htmlContent":"<p>For the rest of this section, we'll work on implementing the UI for our gameboard. When we're done, our game won't do anything, but it'll set the background for us to implement the code for concentration.</p><p>The design for our gameboard will be a 4x4 grid like such:</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/grid.png\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/grid.png\" alt=\"4x4 Grid\" title=\"\">\n        </a></p><p>The height and width of the gameboard will always be 370px by 370px:</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/grid_size.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/grid_size.jpg\" alt=\"Grid Size\" title=\"\">\n        </a></p><p>Finally, our gameboard will always take up the full 12 columns of the grid, but it will be centered regardless of the browser's width:</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/board_layout.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/board_layout.jpg\" alt=\"Board Layout\" title=\"\">\n        </a></p><p>With those details, we can start writing the HTML for our gameboard. We'll need to start by position the gameboard with the correct layout.</p><div class=\"action\">\n<p></p>\n\n<p>Create the <code>.container</code>, <code>.row</code>, and column <code>&lt;div&gt;</code> elements to position our gameboard:</p>\n<pre><span class=\"nt\">&lt;body&gt;</span>\n  <span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"container\"</span><span class=\"nt\">&gt;</span>\n    <span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"row\"</span><span class=\"nt\">&gt;</span>\n      <span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"col-12\"</span><span class=\"nt\">&gt;</span>\n\n        <span class=\"c\">&lt;!-- TODO: add HTML for gameboard --&gt;</span>\n\n      <span class=\"nt\">&lt;/div&gt;</span>\n    <span class=\"nt\">&lt;/div&gt;</span>\n  <span class=\"nt\">&lt;/div&gt;</span>\n\n  <span class=\"c\">&lt;!-- ... existing scripts --&gt;</span>\n<span class=\"nt\">&lt;/body&gt;</span></pre>\n</div><p>Notice that we haven't added any of our UI code yet. So far, we've implemented the container, row and column needed to position our gameboard, but they're all empty <code>&lt;div&gt;</code> elements.</p><h2>Creating the Gameboard</h2><p>If you look at the design of our gameboard, you'll see that it's a 4x4 grid.</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/grid.png\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/grid.png\" alt=\"4x4 Grid\" title=\"\">\n        </a></p><p>To create our gameboard UI, we'll nest another grid within the current grid that we're using for positioning. Let's start by creating a new <code>.container</code> <code>&lt;div&gt;</code> for our gameboard's grid.</p><div class=\"action\">\n<p></p>\n\n<p>Create a new <code>.container</code> <code>&lt;div&gt;</code> within your current grid:</p>\n<pre><span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"container\"</span><span class=\"nt\">&gt;</span>\n  <span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"row\"</span><span class=\"nt\">&gt;</span>\n    <span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"col-12\"</span><span class=\"nt\">&gt;</span>\n\n      <span class=\"c\">&lt;!-- nested container for gameboard --&gt;</span>\n      <span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"container\"</span><span class=\"nt\">&gt;</span>\n\n      <span class=\"nt\">&lt;/div&gt;</span>\n\n    <span class=\"nt\">&lt;/div&gt;</span>\n  <span class=\"nt\">&lt;/div&gt;</span>\n<span class=\"nt\">&lt;/div&gt;</span></pre>\n</div><p>If you refresh your page right now, you still won't see anything, but we're getting there. We'll start by defining the size of our nested grid.</p><div class=\"action\">\n<p></p>\n\n<p>Give your nested <code>.container</code> <code>&lt;div&gt;</code> a new class named <code>.board-container</code>:</p>\n<pre><span class=\"c\">&lt;!-- nested container for gameboard --&gt;</span>\n<span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"board-container container\"</span><span class=\"nt\">&gt;</span>\n\n<span class=\"nt\">&lt;/div&gt;</span></pre>\n<p>With our new <code>.board-container</code> class, we'll select our <code>&lt;div&gt;</code> with CSS and give it the fixed width and height of our gameboard. In addition, we'll add a <code>background-color</code> property so we can see the area of our new container. In <code>style.css</code>, add the following CSS rule:</p>\n<pre><span class=\"nc\">.board-container</span> <span class=\"p\">{</span>\n  <span class=\"k\">background-color</span><span class=\"o\">:</span> <span class=\"nb\">red</span><span class=\"p\">;</span>\n  <span class=\"k\">height</span><span class=\"o\">:</span> <span class=\"m\">370px</span><span class=\"p\">;</span>\n  <span class=\"k\">width</span><span class=\"o\">:</span> <span class=\"m\">370px</span><span class=\"p\">;</span>\n<span class=\"p\">}</span></pre>\n</div><p>Let's take a look. Refresh your web page in your browser. You should see the following:</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/board_container_red.png\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/board_container_red.png\" alt=\"Red Board Container\" title=\"\">\n        </a></p><p>If you adjust the width of your browser, you'll notice that our gameboard's container always remained centered in the middle of our outer grid.</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/board_container_red_med.png\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/board_container_red_med.png\" alt=\"Red Board Container Medium\" title=\"\">\n        </a></p><p>Next, let's create a <code>.row</code> <code>&lt;div&gt;</code> within our <code>.board-container</code>.</p><div class=\"action\">\n<p></p>\n<pre><span class=\"c\">&lt;!-- nested container for gameboard --&gt;</span>\n<span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"board-container container\"</span><span class=\"nt\">&gt;</span>\n  <span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"row\"</span><span class=\"nt\">&gt;</span>\n\n  <span class=\"nt\">&lt;/div&gt;</span>&gt;\n<span class=\"nt\">&lt;/div&gt;</span></pre>\n<p>Similar to our <code>.board-container</code>, let's give our row a blue <code>background-color</code> so we can see the area of the <code>&lt;div&gt;</code>. In your stylesheet, add the following <em>descendant selector</em>:</p>\n<pre><span class=\"nc\">.board-container</span> <span class=\"nc\">.row</span> <span class=\"p\">{</span>\n  <span class=\"k\">background-color</span><span class=\"o\">:</span> <span class=\"nb\">blue</span><span class=\"p\">;</span>\n<span class=\"p\">}</span></pre>\n</div><p>However, if you refresh your web page, you won't see the blue area of your <code>.row</code> <code>&lt;div&gt;</code>. What happened?</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/board_container_red.png\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/board_container_red.png\" alt=\"Red Board Container\" title=\"\">\n        </a></p><p>Our <code>.row</code> <code>&lt;div&gt;</code> doesn't automatically assume the height of it's container <code>&lt;div&gt;</code>. To implement this behavior, we'll have to add a little more CSS.</p><div class=\"action\">\n<p></p>\n\n<p>Give the <code>.row</code> container within your gameboard the same height it's container <code>&lt;div&gt;</code>:</p>\n<pre><span class=\"nc\">.board-container</span> <span class=\"nc\">.row</span> <span class=\"p\">{</span>\n  <span class=\"k\">background-color</span><span class=\"o\">:</span> <span class=\"nb\">blue</span><span class=\"p\">;</span>\n  <span class=\"k\">height</span><span class=\"o\">:</span> <span class=\"m\">100%</span><span class=\"p\">;</span>\n<span class=\"p\">}</span></pre>\n<p>The height percent (%) is based off of the height of the parent <code>&lt;div&gt;</code>. In this case, we give our <code>.row</code> 100% of the height of it's parent element. Which in our case, is the <code>.board-container</code> <code>&lt;div&gt;</code>.</p>\n</div><p>Now, refreshing our web page, we can see the blue area of our row as expect.</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/blue_board_row.png\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/blue_board_row.png\" alt=\"Blue Board Row\" title=\"\">\n        </a></p><p>Before we move further, let's go ahead and remove the <code>background-color</code> property for both the <code>.board-container</code> and it's <code>.row</code>. We've confirmed that each <code>&lt;div&gt;</code> is the correct size and position so we no longer need this CSS.</p><div class=\"action\">\n<p></p>\n\n<p>Remove the <code>background-color</code> property for both the <code>.board-container</code> and it's <code>.row</code>:</p>\n<pre><span class=\"nc\">.board-container</span> <span class=\"p\">{</span>\n  <span class=\"k\">height</span><span class=\"o\">:</span> <span class=\"m\">370px</span><span class=\"p\">;</span>\n  <span class=\"k\">width</span><span class=\"o\">:</span> <span class=\"m\">370px</span><span class=\"p\">;</span>\n<span class=\"p\">}</span>\n\n<span class=\"nc\">.board-container</span> <span class=\"nc\">.row</span> <span class=\"p\">{</span>\n  <span class=\"k\">height</span><span class=\"o\">:</span> <span class=\"m\">100%</span><span class=\"p\">;</span>\n<span class=\"p\">}</span></pre>\n</div><p>With our <code>.board-container</code> and <code>.row</code> container <code>&lt;div&gt;</code> complete, we'll want to start adding each of the gameboard's squares.</p><h2>Adding a Board Square</h2><p>Let's look at how we're going to code each square in our 4x4 gameboard. We'll start by figuring out how to implement a single square:</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/square_size.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/square_size.jpg\" alt=\"Square Size\" title=\"\">\n        </a></p><p>Each square will be 1/4 of the width and height of our gameboard.</p><p>In addition, each square can be either faceup or facedown. The square's color is hidden or revealed depending on if it's facedown or faceup.</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/square_faces.png\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/square_faces.png\" alt=\"Square Faces\" title=\"\">\n        </a></p><p>To imitate the behavior of a square having two faces, we'll do the following:</p><ol>\n<li>Inside each gameboard's square, we'll create a <code>.face-container</code> <code>&lt;div&gt;</code> that's double the height of each square. <a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/face_container.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/face_container.jpg\" alt=\"Face Container\" title=\"\">\n        </a>\n</li>\n<li>Within the <code>.face-container</code>, we'll add two <code>&lt;div&gt;</code> elements to represent each respective facedown and faceup state. <a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/face_container_children.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/face_container_children.jpg\" alt=\"Face Container Children\" title=\"\">\n        </a>\n</li>\n<li>The first <code>&lt;div&gt;</code> will cover the top half of the <code>.face-container</code> <code>&lt;div&gt;</code>. It'll be shown whenever the square is facedown. <a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/facedown_diagram.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/facedown_diagram.jpg\" alt=\"Facedown Diagram\" title=\"\">\n        </a>\n</li>\n<li>The second <code>&lt;div&gt;</code> in the <code>.face-container</code>, will cover the bottom half of the <code>&lt;div&gt;</code> element. It will be revealed when the square is faceup. <a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/faceup_diagram.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/faceup_diagram.jpg\" alt=\"Faceup Diagram\" title=\"\">\n        </a>\n</li>\n<li>We'll configure each square so that it can only show either the top or bottom half of the <code>.face-container</code>. This way, the square will always be either facedown or faceup. <a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/square_overflow.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/square_overflow.jpg\" alt=\"Square Overflow\" title=\"\">\n        </a>\n</li>\n</ol><p>With knowledge about the mechanics of how our square will work, let's start building it. First, we'll need to create a column that's 25% the width of the grid. We can use Bootstrap's column sizes for this.</p><div class=\"action\">\n<p></p>\n\n<p>Create a column that will be your gameboard square. This column should be 25% the width of the gameboard and have a class named <code>.board-square</code>:</p>\n<pre><span class=\"c\">&lt;!-- nested container for gameboard --&gt;</span>\n<span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"board-container container\"</span><span class=\"nt\">&gt;</span>\n  <span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"row\"</span><span class=\"nt\">&gt;</span>\n\n    <span class=\"c\">&lt;!-- gameboard squares --&gt;</span>\n    <span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"col-3 board-square\"</span><span class=\"nt\">&gt;</span>\n\n    <span class=\"nt\">&lt;/div&gt;</span>\n\n  <span class=\"nt\">&lt;/div&gt;</span>\n<span class=\"nt\">&lt;/div&gt;</span></pre>\n<p>We'll want to style our <code>.board-square</code> so that it's height is 25% of the gameboard. Add a CSS rule with the following declarations:</p>\n\n<ul>\n<li>Border: 2px solid red</li>\n<li>Height: 25% of row</li>\n<li>Padding: 0</li>\n</ul>\n<pre><span class=\"nc\">.board-square</span> <span class=\"p\">{</span>\n  <span class=\"k\">border</span><span class=\"o\">:</span> <span class=\"m\">2px</span> <span class=\"k\">solid</span> <span class=\"nb\">red</span><span class=\"p\">;</span>\n  <span class=\"k\">height</span><span class=\"o\">:</span> <span class=\"m\">25%</span><span class=\"p\">;</span>\n  <span class=\"k\">padding</span><span class=\"o\">:</span> <span class=\"m\">0</span><span class=\"p\">;</span>\n<span class=\"p\">}</span></pre>\n<p>We added a red border around our game square so that we can see it's area. We'll remove it later when we implement our final design.</p>\n</div><p>Refresh your web page and you should see your gameboard's first square.</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/red_border_square.png\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/red_border_square.png\" alt=\"Red Border Square\" title=\"\">\n        </a></p><p>But we're not done yet. Next, we'll need to implement the <code>.face-container</code> <code>&lt;div&gt;</code> inside our square with each respective faceup and facedown <code>&lt;div&gt;</code>.</p><div class=\"action\">\n<p></p>\n\n<p>Add a <code>&lt;div&gt;</code> inside your <code>.board-square</code> <code>&lt;div&gt;</code> with a class named <code>.face-container</code>:</p>\n<pre><span class=\"c\">&lt;!-- nested container for gameboard --&gt;</span>\n<span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"board-container container\"</span><span class=\"nt\">&gt;</span>\n  <span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"row\"</span><span class=\"nt\">&gt;</span>\n\n    <span class=\"c\">&lt;!-- gameboard squares --&gt;</span>\n    <span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"col-3 board-square\"</span><span class=\"nt\">&gt;</span>\n      <span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"face-container\"</span><span class=\"nt\">&gt;</span>\n\n      <span class=\"nt\">&lt;/div&gt;</span>\n    <span class=\"nt\">&lt;/div&gt;</span>\n\n  <span class=\"nt\">&lt;/div&gt;</span>\n<span class=\"nt\">&lt;/div&gt;</span></pre>\n<p>Next, we'll need to add some CSS so that our <code>.face-container</code> behaves as we expect it to. In addition, we'll add a temporary blue background color so that we can see the container <code>&lt;div&gt;</code> area.</p>\n<pre><span class=\"nc\">.face-container</span> <span class=\"p\">{</span>\n  <span class=\"k\">background-color</span><span class=\"o\">:</span> <span class=\"nb\">blue</span><span class=\"p\">;</span>\n  <span class=\"k\">height</span><span class=\"o\">:</span> <span class=\"m\">200%</span><span class=\"p\">;</span>\n  <span class=\"k\">width</span><span class=\"o\">:</span> <span class=\"m\">100%</span><span class=\"p\">;</span>\n<span class=\"p\">}</span></pre>\n</div><!-- TODO: explain CSS properties like position, top, and transition --><p>Refresh your web page and you should see both the red border of the <code>.board-square</code> <code>&lt;div&gt;</code> and the blue background color of the <code>.face-container</code> area.</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/face_container_area.png\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/face_container_area.png\" alt=\"Face Container Area\" title=\"\">\n        </a></p><p>Within our <code>.face-container</code>, we'll need to add both a facedown and faceup for our gameboard's square.</p><div class=\"action\">\n<p></p>\n\n<p>Within your <code>.face-container</code> <code>&lt;div&gt;</code>, add a <code>.facedown</code> and <code>.faceup</code> <code>&lt;div&gt;</code>:</p>\n<pre><span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"col-3 board-square\"</span><span class=\"nt\">&gt;</span>\n  <span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"face-container\"</span><span class=\"nt\">&gt;</span>\n    <span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"facedown\"</span><span class=\"nt\">&gt;&lt;/div&gt;</span>\n    <span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"faceup\"</span><span class=\"nt\">&gt;&lt;/div&gt;</span>\n  <span class=\"nt\">&lt;/div&gt;</span>\n<span class=\"nt\">&lt;/div&gt;</span></pre>\n<p>We'll need to add some more CSS for our facedown and faceup <code>&lt;div&gt;</code> elements:</p>\n<pre><span class=\"nc\">.faceup</span><span class=\"o\">,</span> <span class=\"nc\">.facedown</span> <span class=\"p\">{</span>\n  <span class=\"k\">height</span><span class=\"o\">:</span> <span class=\"m\">50%</span><span class=\"p\">;</span>\n<span class=\"p\">}</span>\n\n<span class=\"nc\">.facedown</span> <span class=\"p\">{</span>\n  <span class=\"k\">background-color</span><span class=\"o\">:</span> <span class=\"n\">lightGray</span><span class=\"p\">;</span>\n<span class=\"p\">}</span>\n\n<span class=\"nc\">.faceup</span> <span class=\"p\">{</span>\n  <span class=\"k\">background-color</span><span class=\"o\">:</span> <span class=\"nb\">coral</span><span class=\"p\">;</span>\n<span class=\"p\">}</span></pre>\n<p>Since the parent container of our <code>.facedown</code> and <code>.faceup</code> <code>&lt;div&gt;</code> elements is 200% the height of our <code>.board-square</code> <code>&lt;div&gt;</code>, we set the height for both <code>.facedown</code> and <code>.faceup</code> <code>&lt;div&gt;</code> elements to 50%. In addition, we give both facedown and faceup <code>&lt;div&gt;</code> elements a temporary color so we can test that our code is working as expected.</p>\n</div><p>Once again, refresh your web page in your browser. This time you'll see both the facedown and faceup colors of the gameboard square.</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/boardsquare_overflow.png\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/boardsquare_overflow.png\" alt=\"Boardsquare Overflow\" title=\"\">\n        </a></p><p>Last, we'll need to modify our square so that only one face (faceup or facedown) can be displayed at a time. To do this, we'll use a new property named <code>overflow</code>. This will hide any overflow content within our <code>.board-square</code> <code>&lt;div&gt;</code>.</p><div class=\"action\">\n<p></p>\n\n<p>In your stylesheet, add the <code>overflow</code> property to your <code>.board-square</code> <code>&lt;div&gt;</code>:</p>\n<pre><span class=\"nc\">.board-square</span> <span class=\"p\">{</span>\n  <span class=\"c\">/* ... previous CSS declarations */</span>\n\n  <span class=\"k\">overflow</span><span class=\"o\">:</span> <span class=\"k\">hidden</span><span class=\"p\">;</span>\n<span class=\"p\">}</span></pre>\n</div><p>Refresh your gameboard and you'll see that only the <code>.facedown</code> <code>&lt;div&gt;</code> shown in your <code>.board-square</code> <code>&lt;div&gt;</code>.</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/boardsquare_no_overflow.png\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/boardsquare_no_overflow.png\" alt=\"Boardsquare No Overflow\" title=\"\">\n        </a></p><p>The <code>.faceup</code> <code>&lt;div&gt;</code> is still there, it's just hidden because we specified that our <code>.board-square</code> should hide <em>overflow</em> content.</p>"},{"id":"T0E6OlNlY3Rpb24tMzg1NA==","title":"Implementing the Flip Transition","htmlContent":"<p>Throughout our game, squares will be flipped faceup and facedown. We'll handle the flipping of our board square using CSS.</p><p>Since each of our squares will have a <code>.faceup</code> and <code>.facedown</code> <code>&lt;div&gt;</code>, we'll flip between the two faces by changing the top position of our <code>.face-container</code>.</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/flipping_square.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/flipping_square.jpg\" alt=\"Flipping Square\" title=\"\">\n        </a></p><p>To implement the flipping transition, we'll need to be able to adjust the top position of our <code>.face-container</code>.</p><div class=\"action\">\n<p></p>\n\n<p>In <code>style.css</code>, add the following CSS declarations to <code>.face-container</code>:</p>\n<pre><span class=\"nc\">.face-container</span> <span class=\"p\">{</span>\n  <span class=\"c\">/* ... previous CSS declarations */</span>\n\n  <span class=\"k\">position</span><span class=\"o\">:</span> <span class=\"k\">absolute</span><span class=\"p\">;</span>\n  <span class=\"k\">top</span><span class=\"o\">:</span> <span class=\"m\">0</span><span class=\"p\">;</span>\n<span class=\"p\">}</span></pre>\n<p>By setting our <code>position</code> to <code>absolute</code>, we can now adjust the <code>.face-container</code> vertical position easily by changing the <code>top</code> property.</p>\n</div><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/flipping_with_top_percent.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/flipping_with_top_percent.jpg\" alt=\"Flipping With Top Percent\" title=\"\">\n        </a></p><p>Remember, the percent (%) is based on it's container element, in this case the <code>.board-square</code> <code>&lt;div&gt;</code>.</p><p>Next, we'll create a new CSS rule that will set the top position to <code>-100%</code> when applied to it's respective HTML element.</p><div class=\"action\">\n<p></p>\n\n<p>In <code>style.css</code>, add the following CSS rule:</p>\n<pre><span class=\"nc\">.flipped</span> <span class=\"nc\">.face-container</span> <span class=\"p\">{</span>\n  <span class=\"k\">top</span><span class=\"o\">:</span> <span class=\"m\">-100%</span><span class=\"p\">;</span>\n<span class=\"p\">}</span></pre>\n<p>Notice that our new rule has a higher CSS specificity value than our CSS rule for <code>.face-container</code>. This will ensure that if applied, our <code>.flipped .face-container</code> selector will have precedence.</p>\n</div><p>To finish up, we'll add the <code>transition</code> property to create a smoother animation when a square is flipped from facedown to faceup and vice versa.</p><div class=\"action\">\n<p></p>\n\n<p>In <code>style.css</code>, add the following declaration to your existing <code>.face-container</code> CSS rule:</p>\n<pre><span class=\"nc\">.face-container</span> <span class=\"p\">{</span>\n  <span class=\"c\">/* ... previous CSS declarations */</span>\n\n  <span class=\"n\">transition</span><span class=\"o\">:</span> <span class=\"m\">400</span><span class=\"n\">ms</span><span class=\"p\">;</span>\n<span class=\"p\">}</span></pre>\n</div><p>With our new CSS, whenever the <code>.flipped</code> class is applied to our <code>.board-square</code> <code>&lt;div&gt;</code>, the <code>.faceup</code> <code>&lt;div&gt;</code> of our <code>.face-container</code> will be revealed.</p><p>Let's try it now.</p><div class=\"action\">\n<p></p>\n\n<p>In <code>index.html</code>, add the <code>.flipped</code> class to your <code>.board-square</code> <code>&lt;div&gt;</code>:</p>\n<pre><span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"col-3 board-square flipped\"</span><span class=\"nt\">&gt;</span>\n  <span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"face-container\"</span><span class=\"nt\">&gt;</span>\n    <span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"facedown\"</span><span class=\"nt\">&gt;&lt;/div&gt;</span>\n    <span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"faceup\"</span><span class=\"nt\">&gt;&lt;/div&gt;</span>\n  <span class=\"nt\">&lt;/div&gt;</span>\n<span class=\"nt\">&lt;/div&gt;</span></pre>\n</div><p>Refresh your browser and take a look.</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/flipped_boardsquare.png\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P02-Building-The-UI/assets/flipped_boardsquare.png\" alt=\"Flipped Boardsquare\" title=\"\">\n        </a></p><p>As expected, our <code>.faceup</code> <code>&lt;div&gt;</code> is revealed in our <code>.board-square</code> and the <code>.facedown</code> <code>&lt;div&gt;</code> is hidden.</p><h2>Implementing the Remaining Squares</h2><p>To implement the remaining squares, we could simple copy and paste the <code>.board-square</code> HTML and it's nested elements 15 more times. However, this would lead to a lot of unnecessary duplication and would be hard to maintain if we wanted to make changes in the future. Instead, we'll move onto the next section to learn how to create the remaining squares in our gameboard using JavaScript.</p>"},{"id":"T0E6OlNlY3Rpb24tMzg1NQ==","title":"Cleaning Up","htmlContent":"<p>Before moving onto the next section, let's do some cleanup. We added some extra, unneeded code to better understand what was going on in our browser. Since we're finished, let's remove that extra code.</p><div class=\"action\">\n<p></p>\n\n<ol>\n<li>In <code>index.html</code>, remove the <code>.flipped</code> class from the <code>.board-square</code> <code>&lt;div&gt;</code> so that it returns to being <code>.facedown</code>.</li>\n<li>In <code>style.css</code>, modify your <code>.board-square</code> border to have a color of <code>#F7F7F7</code>, instead of <code>red</code>.</li>\n<li>In <code>style.css</code>, remove the <code>blue</code> background color from <code>.face-container</code>.</li>\n<li>In <code>style.css</code>, delete the CSS rule setting a <code>coral</code> background color for the <code>.faceup</code> selector.</li>\n<li>In <code>style.css</code>, change the background color of your <code>.facedown</code> selector from <code>lightGray</code> to <code>#D0D0D0</code>\n</li>\n</ol>\n</div><p>When you're done, move onto the next section and we'll begin learning how to manipulate the DOM with client-side JavaScript.</p>"}]},"next":{"id":"T0E6OlBhZ2UtMTA0Ng==","slug":"client-side-javascript","title":"Client-side JavaScript"},"previous":{"id":"T0E6OlBhZ2UtMTA0Ng==","slug":"client-side-javascript","title":"Client-side JavaScript"}},{"id":"T0E6OlBhZ2UtMTA0Ng==","title":"Client-side JavaScript","slug":"client-side-javascript","sections":{"nodes":[{"id":"T0E6OlNlY3Rpb24tMzY3MA==","title":"Client-side JavaScript","htmlContent":"<p>In the previous section, we worked on implementing the UI for our gameboard. You should have created a 4x4 grid gameboard with a single square.</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P03-Client-side-JavaScript/assets/starting_point.png\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P03-Client-side-JavaScript/assets/starting_point.png\" alt=\"Starting Point\" title=\"\">\n        </a></p><p>In this section, we'll learn to write client-side JavaScript to generate our concentration gameboard.</p>"},{"id":"T0E6OlNlY3Rpb24tMzY3MQ==","title":"Intro to the DOM","htmlContent":"<p>The Document Object Model (DOM) is a representation of HTML that can be changed and manipulated with code. For us, that means we'll be able to write JavaScript code that dynamically changes our HTML and generates our gameboard in the browser.</p><div class=\"info\">\n<p></p>\n\n<p>The DOM is a hard concept for beginners to grasp. We've greatly simplified our definition of the DOM to meet our needs. If you're interested in learning more about the DOM, check out the links below:</p>\n\n<ul>\n<li><a href=\"https://css-tricks.com/dom/\" target=\"_blank\">CSS Tricks</a></li>\n<li><a href=\"https://www.w3.org/TR/DOM-Level-2-Core/introduction.html\" target=\"_blank\">W3</a></li>\n<li><a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction\" target=\"_blank\">Mozilla Developer Network</a></li>\n<li><a href=\"https://en.wikipedia.org/wiki/Document_Object_Model\" target=\"_blank\">Wikipedia</a></li>\n</ul>\n</div><p>It's useful to generate our gameboard with code for the following reasons:</p><ul>\n<li>We won't have to copy and paste the same code for each square 16 times.</li>\n<li>If we wanted to modify the HTML for our <code>.board-square</code>, we won't have to propogate those changes manually for each square.</li>\n<li>We can randomly generate a matching pair of colors for each <code>.board-square</code> in our gameboard.</li>\n</ul><p>By writing JavaScript, we can manipulate the DOM and dynamically add HTML that isn't in our <code>index.html</code> document.</p><h2>Manipulating the DOM</h2><p>We'll start by generating the HTML for each of our 16 squares. To do so, we'll need to change the DOM and insert the following HTML for each of our 16 gameboard squares:</p><pre><span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"col-3 board-square\"</span><span class=\"nt\">&gt;</span>\n  <span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"face-container\"</span><span class=\"nt\">&gt;</span>\n    <span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"facedown\"</span><span class=\"nt\">&gt;&lt;/div&gt;</span>\n    <span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"faceup\"</span><span class=\"nt\">&gt;&lt;/div&gt;</span>\n  <span class=\"nt\">&lt;/div&gt;</span>\n<span class=\"nt\">&lt;/div&gt;</span></pre><p>We'll start by adding an <code>id</code> to the <code>.row</code> <code>&lt;div&gt;</code> of our gameboard. We'll need this to easily access the correct position in the DOM where we'll insert our 16 <code>.board-square</code> elements.</p><div class=\"action\">\n<p></p>\n\n<p>Add the following <code>id</code> to the <code>.row</code> <code>&lt;div&gt;</code> of your gameboard:</p>\n<pre><span class=\"c\">&lt;!-- nested container for gameboard --&gt;</span>\n<span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"board-container container\"</span><span class=\"nt\">&gt;</span>\n  <span class=\"c\">&lt;!-- add gameboard id below --&gt;</span>\n  <span class=\"nt\">&lt;div</span> <span class=\"na\">id=</span><span class=\"s\">\"gameboard\"</span> <span class=\"na\">class=</span><span class=\"s\">\"row\"</span><span class=\"nt\">&gt;</span>\n\n    <span class=\"c\">&lt;!-- ... existing HTML for .gameboard square --&gt;</span>\n\n  <span class=\"nt\">&lt;/div&gt;</span>\n<span class=\"nt\">&lt;/div&gt;</span></pre>\n</div><p>With our new <code>id</code>, we can access our <code>#gameboard</code> using the DOM. Let's access our <code>#gameboard</code> element and print it in the JavaScript console so we can better understand what's going on.</p><div class=\"action\">\n<p></p>\n\n<p>Open your empty <code>main.js</code> file and add the following lines of code:</p>\n<pre><span class=\"kr\">const</span> <span class=\"nx\">boardElement</span> <span class=\"o\">=</span> <span class=\"nb\">document</span><span class=\"p\">.</span><span class=\"nx\">getElementById</span><span class=\"p\">(</span><span class=\"s1\">'gameboard'</span><span class=\"p\">);</span>\n<span class=\"nx\">console</span><span class=\"p\">.</span><span class=\"nx\">log</span><span class=\"p\">(</span><span class=\"nx\">boardElement</span><span class=\"p\">);</span></pre>\n</div><p>Refresh your web page and navigate to your console (opt&ndash;CMD&ndash;j). If you expand the element printed, you should see the following:</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P03-Client-side-JavaScript/assets/gameboard_log.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P03-Client-side-JavaScript/assets/gameboard_log.jpg\" alt=\"Gameboard Log\" title=\"\">\n        </a></p><p>See how we used JavaScript <code>document.getElementById</code> function to access the DOM and print the <code>#gameboard</code> element in our HTML document?</p><p>Next, we'll want to manipulate the DOM by adding extra code for our gameboard squares. Let's start by modifying the code within our <code>#gameboard</code> to insert a simple paragraph <code>&lt;p&gt;</code> element.</p><div class=\"action\">\n<p></p>\n\n<p>In <code>main.js</code>, add the following code:</p>\n<pre><span class=\"kr\">const</span> <span class=\"nx\">boardElement</span> <span class=\"o\">=</span> <span class=\"nb\">document</span><span class=\"p\">.</span><span class=\"nx\">getElementById</span><span class=\"p\">(</span><span class=\"s1\">'gameboard'</span><span class=\"p\">);</span>\n<span class=\"nx\">boardElement</span><span class=\"p\">.</span><span class=\"nx\">innerHTML</span> <span class=\"o\">=</span> <span class=\"s1\">'&lt;p&gt;changing the DOM&lt;/p&gt;'</span>\n<span class=\"nx\">console</span><span class=\"p\">.</span><span class=\"nx\">log</span><span class=\"p\">(</span><span class=\"nx\">boardElement</span><span class=\"p\">);</span></pre>\n<p>It's important to note that our <code>&lt;p&gt;</code> element is a <code>String</code> as it's enclosed within a matching pair of single quotes <code>'</code>. When you change the DOM with JavaScript, your HTML must be written as a <code>String</code>.</p>\n</div><p>If you refresh the web page, you'll notice that our <code>.board-square</code> <code>&lt;div&gt;</code> has been replaced with a <code>&lt;p&gt;</code> element.</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P03-Client-side-JavaScript/assets/changing_DOM.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P03-Client-side-JavaScript/assets/changing_DOM.jpg\" alt=\"Changing DOM\" title=\"\">\n        </a></p><p>In addition, if you look in the JavaScript console, you'll see the DOM has been updated to the following:</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P03-Client-side-JavaScript/assets/changing_DOM_log.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P03-Client-side-JavaScript/assets/changing_DOM_log.jpg\" alt=\"Changing DOM Log\" title=\"\">\n        </a></p><p>We started by inserting a <code>&lt;p&gt;</code> element for example purposes. Now that we know how to change the DOM, we can move on to generating the HTML for the remaining squares of our gameboard.</p>"},{"id":"T0E6OlNlY3Rpb24tMzY3Mg==","title":"Generating a Single Square","htmlContent":"<p>We'll start off by trying to generate a single <code>.board-square</code> <code>&lt;div&gt;</code> using JavaScript. Let's give it a try.</p><div class=\"challenge\">\n<p></p>\n\n<p>Using what we've learned so far, use JavaScript to change the DOM so that the <code>#gameboard</code> element contains a single <code>.board-square</code> (with all of it's children elements).</p>\n</div><!-- break --><div class=\"solution\">\n<p></p>\n\n<p>In <code>main.js</code>, we can use the following code:</p>\n<pre><span class=\"kr\">const</span> <span class=\"nx\">boardElement</span> <span class=\"o\">=</span> <span class=\"nb\">document</span><span class=\"p\">.</span><span class=\"nx\">getElementById</span><span class=\"p\">(</span><span class=\"s1\">'gameboard'</span><span class=\"p\">);</span>\n\n<span class=\"nx\">boardElement</span><span class=\"p\">.</span><span class=\"nx\">innerHTML</span> <span class=\"o\">=</span> <span class=\"err\">`</span>\n    <span class=\"o\">&lt;</span><span class=\"nx\">div</span> <span class=\"kr\">class</span><span class=\"o\">=</span><span class=\"s2\">\"col-3 board-square\"</span><span class=\"o\">&gt;</span>\n      <span class=\"o\">&lt;</span><span class=\"nx\">div</span> <span class=\"kr\">class</span><span class=\"o\">=</span><span class=\"s2\">\"face-container\"</span><span class=\"o\">&gt;</span>\n        <span class=\"o\">&lt;</span><span class=\"nx\">div</span> <span class=\"kr\">class</span><span class=\"o\">=</span><span class=\"s2\">\"facedown\"</span><span class=\"o\">&gt;&lt;</span><span class=\"err\">/div&gt;</span>\n        <span class=\"o\">&lt;</span><span class=\"nx\">div</span> <span class=\"kr\">class</span><span class=\"o\">=</span><span class=\"s2\">\"faceup\"</span><span class=\"o\">&gt;&lt;</span><span class=\"err\">/div&gt;</span>\n      <span class=\"o\">&lt;</span><span class=\"err\">/div&gt;</span>\n    <span class=\"o\">&lt;</span><span class=\"err\">/div&gt;`;</span>\n\n<span class=\"nx\">console</span><span class=\"p\">.</span><span class=\"nx\">log</span><span class=\"p\">(</span><span class=\"nx\">boardElement</span><span class=\"p\">);</span></pre>\n<p>It's important to note that we can use a back tick (`) to start and end a multi-line string. You can also use <code>String</code> concatenation and the newline <code>\\n</code> character to format and organize the HTML we're trying to insert into the DOM.</p>\n</div><p>If you refresh your browser again, you'll notice that our <code>#gameboard</code> contains a single <code>.board-square</code> <code>&lt;div&gt;</code>.</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P03-Client-side-JavaScript/assets/single_boardsquare_log.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P03-Client-side-JavaScript/assets/single_boardsquare_log.jpg\" alt=\"Single Boardsquare Log\" title=\"\">\n        </a></p><p>Although the code is identical to our original <code>index.html</code> HTML, we modified the DOM and replaced the original <code>.board-square</code> HTML content with an exactly identical one. To better understand what's going on, let's delete the <code>.board-square</code> <code>&lt;div&gt;</code> in <code>index.html</code>.</p><div class=\"action\">\n<p></p>\n\n<p>Delete the existing <code>.board-square</code> <code>&lt;div&gt;</code> and it's children elements. When your done, your nested container should look like the following:</p>\n<pre><span class=\"c\">&lt;!-- nested container for gameboard --&gt;</span>\n<span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"board-container container\"</span><span class=\"nt\">&gt;</span>\n  <span class=\"nt\">&lt;div</span> <span class=\"na\">id=</span><span class=\"s\">\"gameboard\"</span> <span class=\"na\">class=</span><span class=\"s\">\"row\"</span><span class=\"nt\">&gt;</span>\n\n    <span class=\"c\">&lt;!-- generate board squares dynamically here --&gt;</span>\n\n  <span class=\"nt\">&lt;/div&gt;</span>\n<span class=\"nt\">&lt;/div&gt;</span></pre>\n</div><p>Refresh the page, and you'll see that even with the <code>.board-square</code> HTML content in <code>index.html</code> removed, our code in <code>main.js</code> still generated a <code>.board-square</code> <code>&lt;div&gt;</code> element in our DOM.</p><h2>Generating the Remaining Squares</h2><p>After generating our first board-square, we'll need to generate our remaining 15 squares to finish building our gameboard's HTML content.</p><p>See if you can figure out the code to generate 15 more identical <code>.board-square</code> <code>&lt;div&gt;</code> elements, including their children elements.</p><div class=\"challenge\">\n<p></p>\n\n<p>In <code>main.js</code>, write JavaScript to generate a 4x4 grid of <code>.board-square</code> elements (16 total) with the following HTML content:</p>\n<pre><span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"col-3 board-square\"</span><span class=\"nt\">&gt;</span>\n  <span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"face-container\"</span><span class=\"nt\">&gt;</span>\n    <span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"facedown\"</span><span class=\"nt\">&gt;&lt;/div&gt;</span>\n    <span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"faceup\"</span><span class=\"nt\">&gt;&lt;/div&gt;</span>\n  <span class=\"nt\">&lt;/div&gt;</span>\n<span class=\"nt\">&lt;/div&gt;</span></pre>\n</div><!-- break --><div class=\"solution\">\n<p></p>\n\n<p>To solve the challenge given, we can use a for-loop as follows:</p>\n<pre><span class=\"kr\">const</span> <span class=\"nx\">numberOfSquares</span> <span class=\"o\">=</span> <span class=\"mi\">16</span><span class=\"p\">;</span>\n<span class=\"kd\">let</span> <span class=\"nx\">squaresHTML</span> <span class=\"o\">=</span> <span class=\"s1\">''</span><span class=\"p\">;</span>\n\n<span class=\"c1\">// generate HTML for board squares</span>\n<span class=\"k\">for</span> <span class=\"p\">(</span><span class=\"kd\">let</span> <span class=\"nx\">i</span> <span class=\"o\">=</span> <span class=\"mi\">0</span><span class=\"p\">;</span> <span class=\"nx\">i</span> <span class=\"o\">&lt;</span> <span class=\"nx\">numberOfSquares</span><span class=\"p\">;</span> <span class=\"nx\">i</span><span class=\"o\">++</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n  <span class=\"nx\">squaresHTML</span> <span class=\"o\">+=</span> <span class=\"err\">`</span>\n    <span class=\"o\">&lt;</span><span class=\"nx\">div</span> <span class=\"kr\">class</span><span class=\"o\">=</span><span class=\"s2\">\"col-3 board-square\"</span><span class=\"o\">&gt;</span>\n      <span class=\"o\">&lt;</span><span class=\"nx\">div</span> <span class=\"kr\">class</span><span class=\"o\">=</span><span class=\"s2\">\"face-container\"</span><span class=\"o\">&gt;</span>\n        <span class=\"o\">&lt;</span><span class=\"nx\">div</span> <span class=\"kr\">class</span><span class=\"o\">=</span><span class=\"s2\">\"facedown\"</span><span class=\"o\">&gt;&lt;</span><span class=\"err\">/div&gt;</span>\n        <span class=\"o\">&lt;</span><span class=\"nx\">div</span> <span class=\"kr\">class</span><span class=\"o\">=</span><span class=\"s2\">\"faceup\"</span><span class=\"o\">&gt;&lt;</span><span class=\"err\">/div&gt;</span>\n      <span class=\"o\">&lt;</span><span class=\"err\">/div&gt;</span>\n    <span class=\"o\">&lt;</span><span class=\"err\">/div&gt;`;</span>\n<span class=\"p\">}</span>\n\n<span class=\"c1\">// insert squares HTML in DOM</span>\n<span class=\"kr\">const</span> <span class=\"nx\">boardElement</span> <span class=\"o\">=</span> <span class=\"nb\">document</span><span class=\"p\">.</span><span class=\"nx\">getElementById</span><span class=\"p\">(</span><span class=\"s1\">'gameboard'</span><span class=\"p\">);</span>\n<span class=\"nx\">boardElement</span><span class=\"p\">.</span><span class=\"nx\">innerHTML</span> <span class=\"o\">=</span> <span class=\"nx\">squaresHTML</span><span class=\"p\">;</span></pre>\n</div><p>After refreshing the page, you'll notice that our board has successfully been built programmatically using JavaScript.</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P03-Client-side-JavaScript/assets/full_grid.png\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P03-Client-side-JavaScript/assets/full_grid.png\" alt=\"Full Grid\" title=\"\">\n        </a></p><p>Before we move on, let's put the code we've written so far into a function to keep it a bit more organized.</p><div class=\"action\">\n<p></p>\n\n<p>In <code>main.js</code>, modify your code to the following:</p>\n<pre><span class=\"kd\">function</span> <span class=\"nx\">generateHTMLForBoardSquares</span><span class=\"p\">()</span> <span class=\"p\">{</span>\n  <span class=\"kr\">const</span> <span class=\"nx\">numberOfSquares</span> <span class=\"o\">=</span> <span class=\"mi\">16</span><span class=\"p\">;</span>\n  <span class=\"kd\">let</span> <span class=\"nx\">squaresHTML</span> <span class=\"o\">=</span> <span class=\"s1\">''</span><span class=\"p\">;</span>\n\n  <span class=\"c1\">// generate HTML for board squares</span>\n  <span class=\"k\">for</span> <span class=\"p\">(</span><span class=\"kd\">let</span> <span class=\"nx\">i</span> <span class=\"o\">=</span> <span class=\"mi\">0</span><span class=\"p\">;</span> <span class=\"nx\">i</span> <span class=\"o\">&lt;</span> <span class=\"nx\">numberOfSquares</span><span class=\"p\">;</span> <span class=\"nx\">i</span><span class=\"o\">++</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n    <span class=\"nx\">squaresHTML</span> <span class=\"o\">+=</span>\n      <span class=\"s1\">'&lt;div class=\"col-3 board-square\"&gt;\\n'</span> <span class=\"o\">+</span>\n      <span class=\"s1\">'&lt;div class=\"face-container\"&gt;\\n'</span> <span class=\"o\">+</span>\n      <span class=\"s1\">'&lt;div class=\"facedown\"&gt;&lt;/div&gt;\\n'</span> <span class=\"o\">+</span>\n      <span class=\"s1\">'&lt;div class=\"faceup\"&gt;&lt;/div&gt;\\n'</span> <span class=\"o\">+</span>\n      <span class=\"s1\">'&lt;/div&gt;\\n'</span> <span class=\"o\">+</span>\n      <span class=\"s1\">'&lt;/div&gt;\\n'</span><span class=\"p\">;</span>\n  <span class=\"p\">}</span>\n\n  <span class=\"c1\">// insert squares HTML in DOM</span>\n  <span class=\"kr\">const</span> <span class=\"nx\">boardElement</span> <span class=\"o\">=</span> <span class=\"nb\">document</span><span class=\"p\">.</span><span class=\"nx\">getElementById</span><span class=\"p\">(</span><span class=\"s1\">'gameboard'</span><span class=\"p\">);</span>\n  <span class=\"nx\">boardElement</span><span class=\"p\">.</span><span class=\"nx\">innerHTML</span> <span class=\"o\">=</span> <span class=\"nx\">squaresHTML</span><span class=\"p\">;</span>\n<span class=\"p\">}</span>\n\n<span class=\"nx\">generateHTMLForBoardSquares</span><span class=\"p\">();</span></pre>\n<p>Note that we call our function <code>generateHTMLForBoardSquares</code> on the last line of <code>main.js</code> for our function to execute.</p>\n</div><p>Moving on, we'll work on turning each of our board squares into an array of <code>GameSquare</code> objects that we can use when implementing the logic for our game.</p>"},{"id":"T0E6OlNlY3Rpb24tMzY3Mw==","title":"Adding the GameSquare Object","htmlContent":"<p>Before we start building the game logic for concentration, we'll need to create objects that represent each board square. We'll do this by defining a new class named <code>BoardSquare</code> in <code>main.js</code>.</p><div class=\"action\">\n<p></p>\n\n<p>In <code>main.js</code>, create a new JavaScript class by adding the following to the top of your script:</p>\n<pre><span class=\"kr\">class</span> <span class=\"nx\">BoardSquare</span> <span class=\"p\">{</span>\n\n<span class=\"p\">}</span>\n\n<span class=\"c1\">// ... existing code</span></pre>\n</div><p>Within our <code>BoardSquare</code> class definition, we'll need to add a constructor so we can create <code>BoardSquare</code> objects that represent each <code>.board-square</code> in our HTML document.</p><p>Some properties that we'll want to add and track in our <code>BoardSquare</code> class include:</p><ul>\n<li>\n<strong>element:</strong> The DOM element of each respective <code>BoardSquare</code>. This will allow us to easily manipulate and change each element as the player plays the game.</li>\n<li>\n<strong>color:</strong> The faceup color of the <code>BoardSquare</code>.</li>\n<li>\n<strong>isFaceUp:</strong> Whether the <code>BoardSquare</code> is currently faceup or facedown.</li>\n<li>\n<strong>isMatched:</strong> Whether the <code>BoardSquare</code> has already been matched with it's pair that shares the same faceup color.</li>\n</ul><p>Initially, each <code>BoardSquare</code> class will contain an element and a color.</p><div class=\"action\">\n<p></p>\n\n<p>In <code>main.js</code>, create a constructor that accepts an <code>element</code> and <code>color</code> as parameters:</p>\n<pre><span class=\"kr\">class</span> <span class=\"nx\">BoardSquare</span> <span class=\"p\">{</span>\n\n  <span class=\"nx\">constructor</span><span class=\"p\">(</span><span class=\"nx\">element</span><span class=\"p\">,</span> <span class=\"nx\">color</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n    <span class=\"k\">this</span><span class=\"p\">.</span><span class=\"nx\">element</span> <span class=\"o\">=</span> <span class=\"nx\">element</span><span class=\"p\">;</span>\n    <span class=\"k\">this</span><span class=\"p\">.</span><span class=\"nx\">color</span> <span class=\"o\">=</span> <span class=\"nx\">color</span><span class=\"p\">;</span>\n  <span class=\"p\">}</span>\n<span class=\"p\">}</span></pre>\n</div><p>The color parameter will be a <code>String</code> of a CSS class with a <code>background-color</code> set.</p><p>Let's go ahead and create 8 color classes in our <code>style.css</code> stylesheet.</p><div class=\"action\">\n<p></p>\n\n<p>In <code>style.css</code>, add the following to the bottom of your stylesheet:</p>\n<pre><span class=\"c\">/* colors */</span>\n\n<span class=\"nc\">.color-0</span> <span class=\"p\">{</span>\n  <span class=\"k\">background-color</span><span class=\"o\">:</span> <span class=\"nb\">aqua</span><span class=\"p\">;</span>\n<span class=\"p\">}</span>\n\n<span class=\"nc\">.color-1</span> <span class=\"p\">{</span>\n  <span class=\"k\">background-color</span><span class=\"o\">:</span> <span class=\"nb\">bisque</span><span class=\"p\">;</span>\n<span class=\"p\">}</span>\n\n<span class=\"nc\">.color-2</span> <span class=\"p\">{</span>\n  <span class=\"k\">background-color</span><span class=\"o\">:</span> <span class=\"nb\">blue</span><span class=\"p\">;</span>\n<span class=\"p\">}</span>\n\n<span class=\"nc\">.color-3</span> <span class=\"p\">{</span>\n  <span class=\"k\">background-color</span><span class=\"o\">:</span> <span class=\"nb\">blueviolet</span><span class=\"p\">;</span>\n<span class=\"p\">}</span>\n\n<span class=\"nc\">.color-4</span> <span class=\"p\">{</span>\n  <span class=\"k\">background-color</span><span class=\"o\">:</span> <span class=\"nb\">brown</span><span class=\"p\">;</span>\n<span class=\"p\">}</span>\n\n<span class=\"nc\">.color-5</span> <span class=\"p\">{</span>\n  <span class=\"k\">background-color</span><span class=\"o\">:</span> <span class=\"nb\">cadetblue</span><span class=\"p\">;</span>\n<span class=\"p\">}</span>\n\n<span class=\"nc\">.color-6</span> <span class=\"p\">{</span>\n  <span class=\"k\">background-color</span><span class=\"o\">:</span> <span class=\"nb\">chartreuse</span><span class=\"p\">;</span>\n<span class=\"p\">}</span>\n\n<span class=\"nc\">.color-7</span> <span class=\"p\">{</span>\n  <span class=\"k\">background-color</span><span class=\"o\">:</span> <span class=\"nb\">coral</span><span class=\"p\">;</span>\n<span class=\"p\">}</span></pre>\n</div><p>These colors will be used to set the faceup color when generating each <code>.board-square</code> element.</p><p>Using these CSS classes, we'll need to modify our <code>BoardSquare</code> class to set it's respective color class in the DOM.</p><p>Let's create a new function that sets the color of each <code>BoardSquare</code> DOM element.</p><div class=\"action\">\n<p></p>\n\n<p>In <code>main.js</code>, modify <code>BoardSquare</code> class definition to the following:</p>\n<pre><span class=\"kr\">class</span> <span class=\"nx\">BoardSquare</span> <span class=\"p\">{</span>\n\n  <span class=\"nx\">constructor</span><span class=\"p\">(</span><span class=\"nx\">element</span><span class=\"p\">,</span> <span class=\"nx\">color</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n    <span class=\"k\">this</span><span class=\"p\">.</span><span class=\"nx\">element</span> <span class=\"o\">=</span> <span class=\"nx\">element</span><span class=\"p\">;</span>\n    <span class=\"k\">this</span><span class=\"p\">.</span><span class=\"nx\">setColor</span><span class=\"p\">(</span><span class=\"nx\">color</span><span class=\"p\">);</span>\n  <span class=\"p\">}</span>\n\n  <span class=\"c1\">// 1</span>\n  <span class=\"nx\">setColor</span><span class=\"p\">(</span><span class=\"nx\">color</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n    <span class=\"c1\">// 2</span>\n    <span class=\"kr\">const</span> <span class=\"nx\">faceUpElement</span> <span class=\"o\">=</span> <span class=\"k\">this</span><span class=\"p\">.</span><span class=\"nx\">element</span><span class=\"p\">.</span><span class=\"nx\">getElementsByClassName</span><span class=\"p\">(</span><span class=\"s1\">'faceup'</span><span class=\"p\">)[</span><span class=\"mi\">0</span><span class=\"p\">];</span>\n\n    <span class=\"c1\">// 3</span>\n    <span class=\"k\">this</span><span class=\"p\">.</span><span class=\"nx\">color</span> <span class=\"o\">=</span> <span class=\"nx\">color</span><span class=\"p\">;</span>\n    <span class=\"c1\">// 4</span>\n    <span class=\"nx\">faceUpElement</span><span class=\"p\">.</span><span class=\"nx\">classList</span><span class=\"p\">.</span><span class=\"nx\">add</span><span class=\"p\">(</span><span class=\"nx\">color</span><span class=\"p\">);</span>\n  <span class=\"p\">}</span>\n<span class=\"p\">}</span></pre>\n<p>Let's break down the code we added above:</p>\n\n<ol>\n<li>Create a new function named <code>setColor</code> for some better organization and potential future reusability.</li>\n<li>Access the DOM element of the <code>.faceup</code> <code>&lt;div&gt;</code> for the respective <code>.board-square</code>.</li>\n<li>Set the <code>BoardSquare</code> object with the new color class.</li>\n<li>Add the CSS color class to the <code>.faceup</code> <code>&lt;div&gt;</code> DOM element to set the <code>.board-square</code> element's faceup color.</li>\n</ol>\n</div><p>Next, let's quickly add the following <code>Boolean</code> variables to our object. We'll be using these to implement the game logic for our <code>BoardSquare</code> in the next section.</p><div class=\"action\">\n<p></p>\n\n<p>In <code>main.js</code>, add a <code>isFaceUp</code> and <code>isMatched</code> boolean to your <code>BoardSquare</code> class:</p>\n<pre><span class=\"kr\">class</span> <span class=\"nx\">BoardSquare</span> <span class=\"p\">{</span>\n\n  <span class=\"nx\">constructor</span><span class=\"p\">(</span><span class=\"nx\">element</span><span class=\"p\">,</span> <span class=\"nx\">color</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n    <span class=\"k\">this</span><span class=\"p\">.</span><span class=\"nx\">element</span> <span class=\"o\">=</span> <span class=\"nx\">element</span><span class=\"p\">;</span>\n    <span class=\"k\">this</span><span class=\"p\">.</span><span class=\"nx\">isFaceUp</span> <span class=\"o\">=</span> <span class=\"kc\">false</span><span class=\"p\">;</span>\n    <span class=\"k\">this</span><span class=\"p\">.</span><span class=\"nx\">isMatched</span> <span class=\"o\">=</span> <span class=\"kc\">false</span><span class=\"p\">;</span>\n    <span class=\"k\">this</span><span class=\"p\">.</span><span class=\"nx\">setColor</span><span class=\"p\">(</span><span class=\"nx\">color</span><span class=\"p\">);</span>\n  <span class=\"p\">}</span>\n\n  <span class=\"c1\">// ... previous code</span>\n<span class=\"p\">}</span></pre>\n</div><p>With our new <code>BoardSquare</code> class, we'll need to find a way to randomly generate matching pairs of colors for each <code>BoardSquare</code>. We'll implement that next.</p>"},{"id":"T0E6OlNlY3Rpb24tMzY3NA==","title":"Randomly Generating Color Pairs","htmlContent":"<p>To randomly generate matching color pairs, we'll first create an array of all colors and their matching color pairs.</p><div class=\"action\">\n<p></p>\n\n<p>In <code>main.js</code>, at the bottom of the file, add the following code:</p>\n<pre><span class=\"kr\">const</span> <span class=\"nx\">colorPairs</span> <span class=\"o\">=</span> <span class=\"p\">[];</span>\n\n<span class=\"kd\">function</span> <span class=\"nx\">generateColorPairs</span><span class=\"p\">()</span> <span class=\"p\">{</span>\n  <span class=\"k\">if</span> <span class=\"p\">(</span><span class=\"nx\">colorPairs</span><span class=\"p\">.</span><span class=\"nx\">length</span> <span class=\"o\">&gt;</span> <span class=\"mi\">0</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n    <span class=\"k\">return</span> <span class=\"nx\">colorPairs</span><span class=\"p\">;</span>\n  <span class=\"p\">}</span> <span class=\"k\">else</span> <span class=\"p\">{</span>\n    <span class=\"c1\">// generates matching pair for each color</span>\n    <span class=\"k\">for</span> <span class=\"p\">(</span><span class=\"kd\">let</span> <span class=\"nx\">i</span> <span class=\"o\">=</span> <span class=\"mi\">0</span><span class=\"p\">;</span> <span class=\"nx\">i</span> <span class=\"o\">&lt;</span> <span class=\"mi\">8</span><span class=\"p\">;</span> <span class=\"nx\">i</span><span class=\"o\">++</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n      <span class=\"nx\">colorPairs</span><span class=\"p\">.</span><span class=\"nx\">push</span><span class=\"p\">(</span><span class=\"s1\">'color-'</span> <span class=\"o\">+</span> <span class=\"nx\">i</span><span class=\"p\">);</span>\n      <span class=\"nx\">colorPairs</span><span class=\"p\">.</span><span class=\"nx\">push</span><span class=\"p\">(</span><span class=\"s1\">'color-'</span> <span class=\"o\">+</span> <span class=\"nx\">i</span><span class=\"p\">);</span>\n    <span class=\"p\">}</span>\n\n    <span class=\"k\">return</span> <span class=\"nx\">colorPairs</span><span class=\"p\">;</span>\n  <span class=\"p\">}</span>\n<span class=\"p\">}</span></pre>\n<p>In the code above, we generate an array of matching pairs for each of our CSS color classes. Since concentration requires <code>BoardSquare</code> objects with matching faceup colors, we'll need to add two of each color to our array.</p>\n</div><p>Next, we'll add some code that randomly shuffles an array of elements based on the Fisher-Yates algorithm. This will be a helper function that helps us randomly shuffle our matching colors.</p><div class=\"action\">\n<p></p>\n\n<p>In <code>main.js</code>, add the following function to the bottom of your file:</p>\n<pre><span class=\"kd\">function</span> <span class=\"nx\">shuffle</span><span class=\"p\">(</span><span class=\"nx\">array</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n  <span class=\"kd\">let</span> <span class=\"nx\">currentIndex</span> <span class=\"o\">=</span> <span class=\"nx\">array</span><span class=\"p\">.</span><span class=\"nx\">length</span><span class=\"p\">;</span>\n  <span class=\"kd\">let</span> <span class=\"nx\">temporaryValue</span><span class=\"p\">,</span> <span class=\"nx\">randomIndex</span><span class=\"p\">;</span>\n\n  <span class=\"c1\">// While there remain elements to shuffle...</span>\n  <span class=\"k\">while</span> <span class=\"p\">(</span><span class=\"mi\">0</span> <span class=\"o\">!==</span> <span class=\"nx\">currentIndex</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n    <span class=\"c1\">// Pick a remaining element...</span>\n    <span class=\"nx\">randomIndex</span> <span class=\"o\">=</span> <span class=\"nb\">Math</span><span class=\"p\">.</span><span class=\"nx\">floor</span><span class=\"p\">(</span><span class=\"nb\">Math</span><span class=\"p\">.</span><span class=\"nx\">random</span><span class=\"p\">()</span> <span class=\"o\">*</span> <span class=\"nx\">currentIndex</span><span class=\"p\">);</span>\n    <span class=\"nx\">currentIndex</span> <span class=\"o\">-=</span> <span class=\"mi\">1</span><span class=\"p\">;</span>\n\n    <span class=\"c1\">// And swap it with the current element.</span>\n    <span class=\"nx\">temporaryValue</span> <span class=\"o\">=</span> <span class=\"nx\">array</span><span class=\"p\">[</span><span class=\"nx\">currentIndex</span><span class=\"p\">];</span>\n    <span class=\"nx\">array</span><span class=\"p\">[</span><span class=\"nx\">currentIndex</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"nx\">array</span><span class=\"p\">[</span><span class=\"nx\">randomIndex</span><span class=\"p\">];</span>\n    <span class=\"nx\">array</span><span class=\"p\">[</span><span class=\"nx\">randomIndex</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"nx\">temporaryValue</span><span class=\"p\">;</span>\n  <span class=\"p\">}</span>\n\n  <span class=\"k\">return</span> <span class=\"nx\">array</span><span class=\"p\">;</span>\n<span class=\"p\">}</span></pre>\n<p>You don't need to understand the specifics of what's going on in this function. Just know that it takes an array as input and returns the same element with it's elements shuffled as output.</p>\n\n<p>If you're interested in learning more about the specifics of the Fisher-Yates algorithm, you can learn more details and even see a visualization by <a href=\"https://bost.ocks.org/mike/shuffle/\" target=\"_blank\">clicking here.</a></p>\n</div><p>Next, we'll use the <code>generateColorPairs()</code> and <code>shuffle(array)</code> functions we created to return an array of shuffled colors.</p><div class=\"action\">\n<p></p>\n\n<p>In <code>main.js</code>, create a function that returns a shuffled array of matching color pairs:</p>\n<pre><span class=\"kd\">function</span> <span class=\"nx\">shuffleColors</span><span class=\"p\">()</span> <span class=\"p\">{</span>\n  <span class=\"kr\">const</span> <span class=\"nx\">colorPairs</span> <span class=\"o\">=</span> <span class=\"nx\">generateColorPairs</span><span class=\"p\">()</span>\n  <span class=\"k\">return</span> <span class=\"nx\">shuffle</span><span class=\"p\">(</span><span class=\"nx\">colorPairs</span><span class=\"p\">);</span>\n<span class=\"p\">}</span></pre>\n</div><p>With our new color-shuffling functions, we can finally add a function that sets up our game so that we can begin writing our game logic.</p>"},{"id":"T0E6OlNlY3Rpb24tMzY3NQ==","title":"Implementing the Game Setup","htmlContent":"<p>So far, we have bits and pieces of the code that we'll need to initially set up our game. In this last step, we'll bring the code we've written together. We'll need to accomplish the following:</p><ol>\n<li>Create and insert the HTML elements of each <code>.board-square</code> in the DOM</li>\n<li>Randomly shuffle and return 8 pairs of matching colors.</li>\n<li>Create an array of <code>BoardSquare</code> objects for each respective <code>.board-square</code> element</li>\n</ol><p>We've already completed step 1, however let's refactor our code into a function named <code>gameSetup()</code>.</p><div class=\"action\">\n<p></p>\n\n<p>In <code>main.js</code>, move the function call of <code>generateHTMLForBoardSquares()</code> into a new function named <code>gameSetup()</code>. Make sure this code is at the bottom of your <code>main.js</code> file.</p>\n<pre><span class=\"kd\">function</span> <span class=\"nx\">setupGame</span><span class=\"p\">()</span> <span class=\"p\">{</span>\n  <span class=\"nx\">generateHTMLForBoardSquares</span><span class=\"p\">();</span>\n<span class=\"p\">}</span>\n\n<span class=\"nx\">setupGame</span><span class=\"p\">();</span> <span class=\"c1\">// DO NOT ERASE THIS OR GAME WILL NOT RUN</span></pre>\n</div><p>Next, we'll need to use our helper functions to generate an array of randomly shuffled color pairs.</p><div class=\"action\">\n<p></p>\n\n<p>In <code>main.js</code>, use the <code>shuffleColors()</code> method to create a constant of randomly shuffled color pairs:</p>\n<pre><span class=\"kd\">function</span> <span class=\"nx\">setupGame</span><span class=\"p\">()</span> <span class=\"p\">{</span>\n  <span class=\"nx\">generateHTMLForBoardSquares</span><span class=\"p\">();</span>\n\n  <span class=\"kr\">const</span> <span class=\"nx\">randomColorPairs</span> <span class=\"o\">=</span> <span class=\"nx\">shuffleColors</span><span class=\"p\">();</span>\n<span class=\"p\">}</span></pre>\n</div><p>Finally, we'll need to create each <code>BoardSquare</code> object from each <code>.board-square</code> element in our DOM.</p><div class=\"action\">\n<p></p>\n\n<p>Create an array of <code>BoardSquare</code> objects using each <code>.board-square</code> element in the DOM:</p>\n<pre><span class=\"kr\">const</span> <span class=\"nx\">boardSquares</span> <span class=\"o\">=</span> <span class=\"p\">[];</span>\n\n<span class=\"kd\">function</span> <span class=\"nx\">setupGame</span><span class=\"p\">()</span> <span class=\"p\">{</span>\n  <span class=\"nx\">generateHTMLForBoardSquares</span><span class=\"p\">();</span>\n\n  <span class=\"kr\">const</span> <span class=\"nx\">randomColorPairs</span> <span class=\"o\">=</span> <span class=\"nx\">shuffleColors</span><span class=\"p\">();</span>\n  <span class=\"c1\">// 1</span>\n  <span class=\"kr\">const</span> <span class=\"nx\">squareElements</span> <span class=\"o\">=</span> <span class=\"nb\">document</span><span class=\"p\">.</span><span class=\"nx\">getElementsByClassName</span><span class=\"p\">(</span><span class=\"s2\">\"board-square\"</span><span class=\"p\">);</span>\n\n  <span class=\"c1\">// 2</span>\n  <span class=\"k\">for</span> <span class=\"p\">(</span><span class=\"kd\">let</span> <span class=\"nx\">i</span> <span class=\"o\">=</span> <span class=\"mi\">0</span><span class=\"p\">;</span> <span class=\"nx\">i</span> <span class=\"o\">&lt;</span> <span class=\"nx\">squareElements</span><span class=\"p\">.</span><span class=\"nx\">length</span><span class=\"p\">;</span> <span class=\"nx\">i</span><span class=\"o\">++</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n    <span class=\"kr\">const</span> <span class=\"nx\">element</span> <span class=\"o\">=</span> <span class=\"nx\">squareElements</span><span class=\"p\">[</span><span class=\"nx\">i</span><span class=\"p\">];</span>\n    <span class=\"kr\">const</span> <span class=\"nx\">color</span> <span class=\"o\">=</span> <span class=\"nx\">randomColorPairs</span><span class=\"p\">[</span><span class=\"nx\">i</span><span class=\"p\">];</span>\n    <span class=\"c1\">// 3</span>\n    <span class=\"kr\">const</span> <span class=\"nx\">square</span> <span class=\"o\">=</span> <span class=\"k\">new</span> <span class=\"nx\">BoardSquare</span><span class=\"p\">(</span><span class=\"nx\">element</span><span class=\"p\">,</span> <span class=\"nx\">color</span><span class=\"p\">)</span>\n\n    <span class=\"c1\">// 4</span>\n    <span class=\"nx\">boardSquares</span><span class=\"p\">.</span><span class=\"nx\">push</span><span class=\"p\">(</span><span class=\"nx\">square</span><span class=\"p\">);</span>\n  <span class=\"p\">}</span>\n<span class=\"p\">}</span></pre>\n<p>Breaking down the code step-by-step:</p>\n\n<ol>\n<li>We use <code>document.getElementsByClassName</code> to retrieve an array of all the <code>.board-square</code> elements in our DOM. We'll need these in order to create each <code>BoardSquare</code> object.</li>\n<li>Using a for-loop, we loop through each <code>.board-square</code> element in our <code>#gameboard</code> to create it's respective <code>BoardSquare</code> object.</li>\n<li>Create a new <code>BoardSquare</code> object using the DOM elements and shuffled colors at the specified position <code>i</code>.</li>\n<li>Add the new <code>BoardSquare</code> object that was created into the <code>boardSquares</code> array.</li>\n</ol>\n</div><p>At this point, we've finished implementing the setup for our gameboard. It's still not functional, and we won't be able to play concentration until our game logic is implemented, but let's take a look at what we've built so far.</p><p>Refresh your landing page and take a look at your gameboard.</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P03-Client-side-JavaScript/assets/full_grid.png\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P03-Client-side-JavaScript/assets/full_grid.png\" alt=\"Full Grid\" title=\"\">\n        </a></p><p>If we want to verify the faceup colors are being shuffled and set for each of our <code>.board-square</code> elements, we can temporarily add the <code>.flipped</code> class to each <code>.board-square</code> <code>&lt;div&gt;</code> that we generate.</p><div class=\"action\">\n<p></p>\n\n<p>In <code>main.js</code>, modify your <code>generateHTMLForBoardSquares()</code> function by adding the <code>.flipped</code> class to the HTML code of each <code>.board-square</code> <code>&lt;div&gt;</code>:</p>\n<pre><span class=\"kd\">function</span> <span class=\"nx\">generateHTMLForBoardSquares</span><span class=\"p\">()</span> <span class=\"p\">{</span>\n  <span class=\"kr\">const</span> <span class=\"nx\">numberOfSquares</span> <span class=\"o\">=</span> <span class=\"mi\">16</span><span class=\"p\">;</span>\n  <span class=\"kd\">let</span> <span class=\"nx\">squaresHTML</span> <span class=\"o\">=</span> <span class=\"s1\">''</span><span class=\"p\">;</span>\n\n  <span class=\"k\">for</span> <span class=\"p\">(</span><span class=\"kd\">let</span> <span class=\"nx\">i</span> <span class=\"o\">=</span> <span class=\"mi\">0</span><span class=\"p\">;</span> <span class=\"nx\">i</span> <span class=\"o\">&lt;</span> <span class=\"nx\">numberOfSquares</span><span class=\"p\">;</span> <span class=\"nx\">i</span><span class=\"o\">++</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n    <span class=\"nx\">squaresHTML</span> <span class=\"o\">+=</span>\n      <span class=\"c1\">// add .flipped class to &lt;div&gt; element</span>\n      <span class=\"s1\">'&lt;div class=\"col-3 board-square flipped\"&gt;\\n'</span> <span class=\"o\">+</span>\n      <span class=\"s1\">'&lt;div class=\"face-container\"&gt;\\n'</span> <span class=\"o\">+</span>\n      <span class=\"s1\">'&lt;div class=\"facedown\"&gt;&lt;/div&gt;\\n'</span> <span class=\"o\">+</span>\n      <span class=\"s1\">'&lt;div class=\"faceup\"&gt;&lt;/div&gt;\\n'</span> <span class=\"o\">+</span>\n      <span class=\"s1\">'&lt;/div&gt;\\n'</span> <span class=\"o\">+</span>\n      <span class=\"s1\">'&lt;/div&gt;\\n'</span><span class=\"p\">;</span>\n  <span class=\"p\">}</span>\n\n  <span class=\"kr\">const</span> <span class=\"nx\">boardElement</span> <span class=\"o\">=</span> <span class=\"nb\">document</span><span class=\"p\">.</span><span class=\"nx\">getElementById</span><span class=\"p\">(</span><span class=\"s1\">'gameboard'</span><span class=\"p\">);</span>\n  <span class=\"nx\">boardElement</span><span class=\"p\">.</span><span class=\"nx\">innerHTML</span> <span class=\"o\">=</span> <span class=\"nx\">squaresHTML</span><span class=\"p\">;</span>\n<span class=\"p\">}</span></pre>\n<p>Don't forget you'll need to remove the <code>.flipped</code> class from your code later. This is just for illustrative purposes to show that our gameboard setup is working properly.</p>\n</div><p>When you're finished, refresh your web page and take a look:</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P03-Client-side-JavaScript/assets/full_grid_faceup.png\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P03-Client-side-JavaScript/assets/full_grid_faceup.png\" alt=\"Full Grid Faceup\" title=\"\">\n        </a></p><p>To make sure your colors are being shuffled correct, refresh the pages a couple times. The color of each square should be randomized each time.</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P03-Client-side-JavaScript/assets/faceup_refresh.png\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P03-Client-side-JavaScript/assets/faceup_refresh.png\" alt=\"Faceup Refreshes\" title=\"\">\n        </a></p><p>When you've verified that your gameboard is being setup properly, remove the <code>.flipped</code> class from your <code>generateHTMLForBoardSquares()</code> function in <code>.main.js</code>.</p><div class=\"action\">\n<p></p>\n\n<p>In <code>main.js</code>, remove the <code>.flipped</code> class from being set in <code>generateHTMLForBoardSquares()</code> function:</p>\n<pre><span class=\"kd\">function</span> <span class=\"nx\">generateHTMLForBoardSquares</span><span class=\"p\">()</span> <span class=\"p\">{</span>\n  <span class=\"kr\">const</span> <span class=\"nx\">numberOfSquares</span> <span class=\"o\">=</span> <span class=\"mi\">16</span><span class=\"p\">;</span>\n  <span class=\"kd\">let</span> <span class=\"nx\">squaresHTML</span> <span class=\"o\">=</span> <span class=\"s1\">''</span><span class=\"p\">;</span>\n\n  <span class=\"k\">for</span> <span class=\"p\">(</span><span class=\"kd\">let</span> <span class=\"nx\">i</span> <span class=\"o\">=</span> <span class=\"mi\">0</span><span class=\"p\">;</span> <span class=\"nx\">i</span> <span class=\"o\">&lt;</span> <span class=\"nx\">numberOfSquares</span><span class=\"p\">;</span> <span class=\"nx\">i</span><span class=\"o\">++</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n    <span class=\"nx\">squaresHTML</span> <span class=\"o\">+=</span>\n      <span class=\"c1\">// remove .flipped class to &lt;div&gt; element</span>\n      <span class=\"s1\">'&lt;div class=\"col-3 board-square\"&gt;\\n'</span> <span class=\"o\">+</span>\n      <span class=\"s1\">'&lt;div class=\"face-container\"&gt;\\n'</span> <span class=\"o\">+</span>\n      <span class=\"s1\">'&lt;div class=\"facedown\"&gt;&lt;/div&gt;\\n'</span> <span class=\"o\">+</span>\n      <span class=\"s1\">'&lt;div class=\"faceup\"&gt;&lt;/div&gt;\\n'</span> <span class=\"o\">+</span>\n      <span class=\"s1\">'&lt;/div&gt;\\n'</span> <span class=\"o\">+</span>\n      <span class=\"s1\">'&lt;/div&gt;\\n'</span><span class=\"p\">;</span>\n  <span class=\"p\">}</span>\n\n  <span class=\"kr\">const</span> <span class=\"nx\">boardElement</span> <span class=\"o\">=</span> <span class=\"nb\">document</span><span class=\"p\">.</span><span class=\"nx\">getElementById</span><span class=\"p\">(</span><span class=\"s1\">'gameboard'</span><span class=\"p\">);</span>\n  <span class=\"nx\">boardElement</span><span class=\"p\">.</span><span class=\"nx\">innerHTML</span> <span class=\"o\">=</span> <span class=\"nx\">squaresHTML</span><span class=\"p\">;</span>\n<span class=\"p\">}</span></pre>\n</div><p>When you're done, refreshing your browser will return you back to your 16 gray, facedown boardsquares.</p>"},{"id":"T0E6OlNlY3Rpb24tMzY3Ng==","title":"Moving On","htmlContent":"<p>In this section, we learned about the DOM and how to manipulate it by dynamically generating the squares for our gameboard. Coming up next, we'll start building out the game logic so that we can start playing concentration!</p>"}]},"next":{"id":"T0E6OlBhZ2UtMTA0Nw==","slug":"game-logic-91c=","title":"Game Logic"},"previous":{"id":"T0E6OlBhZ2UtMTA0Nw==","slug":"game-logic-91c=","title":"Game Logic"}},{"id":"T0E6OlBhZ2UtMTA0Nw==","title":"Game Logic","slug":"game-logic-91c=","sections":{"nodes":[{"id":"T0E6OlNlY3Rpb24tMzY3Nw==","title":"Game Logic","htmlContent":"<p>With our game board successfully setup, we'll move onto implementing the game logic. In other words, we'll start writing code so that the rules of the game are followed.</p><p>In this section, we'll need to implement JavaScript for the following:</p><ul>\n<li>monitoring when <code>.board-square</code> <code>&lt;div&gt;</code> elements are clicked by the player</li>\n<li>flipping squares from face down toface upp and vice versa</li>\n<li>checking if two squares have matching face up colors</li>\n</ul>"},{"id":"T0E6OlNlY3Rpb24tMzY3OA==","title":"Handling Square Click Events","htmlContent":"<p>We'll begin with monitoring and handling <em>click</em> events whenever a player clicks on a square. To do so, we'll use a method named <code>addEventListener()</code> to send a notification to your <code>BoardSquare</code> object whenever a <em>click</em> occurs.</p><div class=\"action\">\n<p></p>\n\n<p>In <code>main.js</code>, add the following line of code to your <code>BoardSquare</code> constructor:</p>\n<pre><span class=\"kr\">class</span> <span class=\"nx\">BoardSquare</span> <span class=\"p\">{</span>\n  <span class=\"nx\">constructor</span><span class=\"p\">(</span><span class=\"nx\">element</span><span class=\"p\">,</span> <span class=\"nx\">color</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n    <span class=\"k\">this</span><span class=\"p\">.</span><span class=\"nx\">element</span> <span class=\"o\">=</span> <span class=\"nx\">element</span><span class=\"p\">;</span>\n\n    <span class=\"c1\">// 1</span>\n    <span class=\"k\">this</span><span class=\"p\">.</span><span class=\"nx\">element</span><span class=\"p\">.</span><span class=\"nx\">addEventListener</span><span class=\"p\">(</span><span class=\"s2\">\"click\"</span><span class=\"p\">,</span> <span class=\"k\">this</span><span class=\"p\">,</span> <span class=\"kc\">false</span><span class=\"p\">);</span>\n\n    <span class=\"k\">this</span><span class=\"p\">.</span><span class=\"nx\">isFaceUp</span> <span class=\"o\">=</span> <span class=\"kc\">false</span><span class=\"p\">;</span>\n    <span class=\"k\">this</span><span class=\"p\">.</span><span class=\"nx\">isMatched</span> <span class=\"o\">=</span> <span class=\"kc\">false</span><span class=\"p\">;</span>\n    <span class=\"k\">this</span><span class=\"p\">.</span><span class=\"nx\">setColor</span><span class=\"p\">(</span><span class=\"nx\">color</span><span class=\"p\">);</span>\n  <span class=\"p\">}</span>\n\n  <span class=\"c1\">// ... previous code</span>\n<span class=\"p\">}</span></pre>\n<p>In the syntax above, we add an event listen to our <code>.board-square</code> DOM element so that whenever it's clicked, our <code>BoardSquare</code> object is sent a notification.</p>\n</div><p>Next, we'll add a special function in our <code>BoardSquare</code> class to handle the <em>click</em> notification that's sent.</p><div class=\"action\">\n<p></p>\n\n<p>In <code>main.js</code>, add the follow function within your <code>BoardSquare</code> class:</p>\n<pre><span class=\"kr\">class</span> <span class=\"nx\">BoardSquare</span> <span class=\"p\">{</span>\n\n  <span class=\"c1\">// ...</span>\n\n  <span class=\"c1\">// 1</span>\n  <span class=\"nx\">handleEvent</span><span class=\"p\">(</span><span class=\"nx\">event</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n    <span class=\"c1\">// 2</span>\n    <span class=\"k\">switch</span> <span class=\"p\">(</span><span class=\"nx\">event</span><span class=\"p\">.</span><span class=\"nx\">type</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n      <span class=\"k\">case</span> <span class=\"s2\">\"click\"</span><span class=\"o\">:</span>\n        <span class=\"c1\">// 3</span>\n        <span class=\"nx\">console</span><span class=\"p\">.</span><span class=\"nx\">log</span><span class=\"p\">(</span><span class=\"k\">this</span><span class=\"p\">.</span><span class=\"nx\">color</span> <span class=\"o\">+</span> <span class=\"s1\">' square was clicked'</span><span class=\"p\">);</span>\n    <span class=\"p\">}</span>\n  <span class=\"p\">}</span>\n\n  <span class=\"c1\">// ...</span>\n<span class=\"p\">}</span></pre>\n<p>Let's go through the steps above together:</p>\n\n<ol>\n<li>We add a new method to our <code>BoardSquare</code> class definition named <code>handleEvent()</code>.</li>\n<li>Check that the event that we're handling is a <em>click</em> event. If the event is not a <em>click</em> event, we don't handle it.</li>\n<li>Log to the JavaScript console that the game board square was clicked.</li>\n</ol>\n</div><p>Refresh your browser and click on a couple squares. Navigate to your JavaScript console in your DevTools window and verify that each game board square is handling <em>click</em> events.</p><p><video width=\"100%\" controls><source src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P04-Game-Logic/assets/log_click_events.mov\" type=\"video/mp4\"></source></video></p>"},{"id":"T0E6OlNlY3Rpb24tMzY3OQ==","title":"Game Logic","htmlContent":"<p>After handling the <em>click</em> event of our squares, we can move on to actually implementing the code that will allow us to play concentration. Let's go over the rules of what happens when a user clicks on a square.</p><h3>Clicking on a Square</h3><p>When a player clicks on a square, the following should happen:</p><ol>\n<li>Check that the square isn't already face up or matched with its pair.\n\n<ol>\n<li>If so, don't do anything.</li>\n<li>If not, flip the square face up and set it's <code>isFaceUp</code> property to true.</li>\n</ol>\n</li>\n<li>Handle the game logic for when a square is flipped.</li>\n</ol><p>Let's breakdown step 3 further.</p><h3>3. Handling Flipped Squares</h3><p>When a square is flipped, the following should happen:</p><p>a. If the square is the first to be flipped face up, save a reference to it and do nothing else.\nb. If the square is the second to be flipped face up, check if it'sface upp color matches with the first square.\n    c. If so, set the squares to matched and clear the reference of the first square.\n    d. If not, flip both squares back to facedown and clear the reference to the first square.</p><p>We can start by implementing the logic for when a player clicks on a square.</p><h2>Implementing Game Logic</h2><div class=\"action\">\n<p></p>\n\n<p>In <code>main.js</code>, within the <code>handleEvent</code> method of your <code>BoardSquare</code> class definition, replace your <code>console.log()</code> with the following:</p>\n<pre><span class=\"nx\">handleEvent</span><span class=\"p\">(</span><span class=\"nx\">event</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n  <span class=\"k\">switch</span> <span class=\"p\">(</span><span class=\"nx\">event</span><span class=\"p\">.</span><span class=\"nx\">type</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n    <span class=\"k\">case</span> <span class=\"s2\">\"click\"</span><span class=\"o\">:</span>\n      <span class=\"c1\">// 1</span>\n      <span class=\"k\">if</span> <span class=\"p\">(</span><span class=\"k\">this</span><span class=\"p\">.</span><span class=\"nx\">isFaceUp</span> <span class=\"o\">||</span> <span class=\"k\">this</span><span class=\"p\">.</span><span class=\"nx\">isMatched</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n        <span class=\"c1\">// 2</span>\n        <span class=\"k\">return</span><span class=\"p\">;</span>\n      <span class=\"p\">}</span>\n\n      <span class=\"c1\">// 3</span>\n      <span class=\"k\">this</span><span class=\"p\">.</span><span class=\"nx\">isFaceUp</span> <span class=\"o\">=</span> <span class=\"kc\">true</span><span class=\"p\">;</span>\n      <span class=\"k\">this</span><span class=\"p\">.</span><span class=\"nx\">element</span><span class=\"p\">.</span><span class=\"nx\">classList</span><span class=\"p\">.</span><span class=\"nx\">add</span><span class=\"p\">(</span><span class=\"s1\">'flipped'</span><span class=\"p\">);</span>\n\n      <span class=\"c1\">// 4</span>\n      <span class=\"nx\">squareFlipped</span><span class=\"p\">(</span><span class=\"k\">this</span><span class=\"p\">);</span>\n  <span class=\"p\">}</span>\n<span class=\"p\">}</span></pre>\n<p>We follow our logic above and implement the following:</p>\n\n<ol>\n<li>Check that both <code>isFaceUp</code> and <code>isMatched</code> are false.</li>\n<li>If either is true, return and do nothing else.</li>\n<li>If both are <code>false</code>, set <code>isFaceUp</code> to <code>true</code> and add <code>.flipped</code> to the <code>.board-square</code> DOM element.</li>\n<li>Call a function to handle the game logic for flipping a square. If you're wondering, we haven't implemented this yet.</li>\n</ol>\n</div><p>Next, we'll move on to adding the code to handle flipped squares. But before that, let's add some helper functions in our <code>BoardSquare</code> class so that we can easily:</p><ul>\n<li>set a <code>BoardSquare</code> as matched</li>\n<li>reset a <code>BoardSquare</code> back to it's default (facedown) state</li>\n</ul><div class=\"action\">\n<p></p>\n\n<p>In <code>main.js</code>, in your <code>BoardSquare</code> class, add the following methods:</p>\n<pre><span class=\"kr\">class</span> <span class=\"nx\">BoardSquare</span> <span class=\"p\">{</span>\n\n  <span class=\"c1\">// ...</span>\n\n  <span class=\"nx\">reset</span><span class=\"p\">()</span> <span class=\"p\">{</span>\n    <span class=\"k\">this</span><span class=\"p\">.</span><span class=\"nx\">isFaceUp</span> <span class=\"o\">=</span> <span class=\"kc\">false</span><span class=\"p\">;</span>\n    <span class=\"k\">this</span><span class=\"p\">.</span><span class=\"nx\">isMatched</span> <span class=\"o\">=</span> <span class=\"kc\">false</span><span class=\"p\">;</span>\n    <span class=\"k\">this</span><span class=\"p\">.</span><span class=\"nx\">element</span><span class=\"p\">.</span><span class=\"nx\">classList</span><span class=\"p\">.</span><span class=\"nx\">remove</span><span class=\"p\">(</span><span class=\"s1\">'flipped'</span><span class=\"p\">);</span>\n  <span class=\"p\">}</span>\n\n  <span class=\"nx\">matchFound</span><span class=\"p\">()</span> <span class=\"p\">{</span>\n    <span class=\"k\">this</span><span class=\"p\">.</span><span class=\"nx\">isFaceUp</span> <span class=\"o\">=</span> <span class=\"kc\">true</span><span class=\"p\">;</span>\n    <span class=\"k\">this</span><span class=\"p\">.</span><span class=\"nx\">isMatched</span> <span class=\"o\">=</span> <span class=\"kc\">true</span><span class=\"p\">;</span>\n  <span class=\"p\">}</span>\n\n  <span class=\"c1\">// ...</span>\n<span class=\"p\">}</span></pre>\n</div><p>After adding our new methods to our <code>BoardSquare</code> class, we can move on to implementing the remaining game logic for concentration.</p><div class=\"action\">\n<p></p>\n\n<p>In <code>main.js</code>, at the bottom of your script, add the following code:</p>\n<pre><span class=\"c1\">// 1</span>\n<span class=\"kd\">let</span> <span class=\"nx\">firstFaceupSquare</span> <span class=\"o\">=</span> <span class=\"kc\">null</span><span class=\"p\">;</span>\n\n<span class=\"kd\">function</span> <span class=\"nx\">squareFlipped</span><span class=\"p\">(</span><span class=\"nx\">square</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n  <span class=\"c1\">// 2</span>\n  <span class=\"k\">if</span> <span class=\"p\">(</span><span class=\"nx\">firstFaceupSquare</span> <span class=\"o\">===</span> <span class=\"kc\">null</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n    <span class=\"nx\">firstFaceupSquare</span> <span class=\"o\">=</span> <span class=\"nx\">square</span><span class=\"p\">;</span>\n    <span class=\"k\">return</span>\n  <span class=\"p\">}</span>\n\n  <span class=\"c1\">// 3</span>\n  <span class=\"k\">if</span> <span class=\"p\">(</span><span class=\"nx\">firstFaceupSquare</span><span class=\"p\">.</span><span class=\"nx\">color</span> <span class=\"o\">===</span> <span class=\"nx\">square</span><span class=\"p\">.</span><span class=\"nx\">color</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n    <span class=\"c1\">// 4</span>\n    <span class=\"nx\">firstFaceupSquare</span><span class=\"p\">.</span><span class=\"nx\">matchFound</span><span class=\"p\">();</span>\n    <span class=\"nx\">square</span><span class=\"p\">.</span><span class=\"nx\">matchFound</span><span class=\"p\">();</span>\n\n    <span class=\"nx\">firstFaceupSquare</span> <span class=\"o\">=</span> <span class=\"kc\">null</span><span class=\"p\">;</span>\n  <span class=\"p\">}</span> <span class=\"k\">else</span> <span class=\"p\">{</span>\n    <span class=\"c1\">// 5</span>\n    <span class=\"kr\">const</span> <span class=\"nx\">a</span> <span class=\"o\">=</span> <span class=\"nx\">firstFaceupSquare</span><span class=\"p\">;</span>\n    <span class=\"kr\">const</span> <span class=\"nx\">b</span> <span class=\"o\">=</span> <span class=\"nx\">square</span><span class=\"p\">;</span>\n\n    <span class=\"nx\">firstFaceupSquare</span> <span class=\"o\">=</span> <span class=\"kc\">null</span><span class=\"p\">;</span>\n\n    <span class=\"nx\">setTimeout</span><span class=\"p\">(</span><span class=\"kd\">function</span><span class=\"p\">()</span> <span class=\"p\">{</span>\n      <span class=\"nx\">a</span><span class=\"p\">.</span><span class=\"nx\">reset</span><span class=\"p\">();</span>\n      <span class=\"nx\">b</span><span class=\"p\">.</span><span class=\"nx\">reset</span><span class=\"p\">();</span>\n    <span class=\"p\">},</span> <span class=\"mi\">400</span><span class=\"p\">);</span>\n  <span class=\"p\">}</span>\n<span class=\"p\">}</span></pre>\n<p>Walking through the steps above:</p>\n\n<ol>\n<li>First, we create a variable to hold a reference to the first faceup square. This will help us keep track of whether the square is the first or second square to be flipped.</li>\n<li>Check if the square is the first square to be flipped faceup. If it is, set a reference to it using the <code>firstFaceupSquare</code> variable and return from the function.</li>\n<li>If the square is the second square to be flipped, check if it's faceup color matches the first faceup square's color.</li>\n<li>If both faceup colors for each square is the same, a match is made. Set both <code>BoardSquare</code> objects to matched using the <code>matchFound()</code> function and clear the <code>firstFaceupSquare</code> variable so the player can keep playing.</li>\n<li>If the faceup colors aren't the same, reset each square to it's default (facedown) state.</li>\n</ol>\n</div><p>When you're finished, refresh your landing page, and start matching away. Your game should be fully working at this point!</p><p><video width=\"100%\" controls><source src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P04-Game-Logic/assets/finish_game_logic.mov\" type=\"video/mp4\"></source></video></p>"},{"id":"T0E6OlNlY3Rpb24tMzY4MA==","title":"Up Next","htmlContent":"<p>In this section, we broke down and implement the logic for our game of concentration. In the next section, we'll finish the implementation of our game by adding some final touchups to our UI.</p>"}]},"next":{"id":"T0E6OlBhZ2UtMTA0OA==","slug":"final-touch-ups","title":"Final Touch-Ups"},"previous":{"id":"T0E6OlBhZ2UtMTA0OA==","slug":"final-touch-ups","title":"Final Touch-Ups"}},{"id":"T0E6OlBhZ2UtMTA0OA==","title":"Final Touch-Ups","slug":"final-touch-ups","sections":{"nodes":[{"id":"T0E6OlNlY3Rpb24tMzY4MQ==","title":"Final Touch-Ups","htmlContent":"<p>In the last section, we finished coding the logic for our game of concentration. In this section, we'll finish up the final touches in our design by adding a title <code>&lt;h1&gt;</code> and a reset button.</p><p>When we're done, we'll want our game to match the following:</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P05-Final-Touch-Ups/assets/game_design.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P05-Final-Touch-Ups/assets/game_design.jpg\" alt=\"Game Design\" title=\"\">\n        </a></p><p>Our section title <code>&lt;h1&gt;</code>, will give users a context what they're looking at.</p><p>The <em>Reset button</em> will allow users to reset the game so they can play again at any moment. It'll re-shuffle the colors of each gameboard square and clear all existing matches.</p><p>Let's start with the easier of the two: the section title.</p>"},{"id":"T0E6OlNlY3Rpb24tMzY4Mg==","title":"Adding a Section Title","htmlContent":"<p>At this point, you should be familiar with HTML elements and how to create a new <code>&lt;h1&gt;</code> element. Let's do that now by adding a new section title to our web page.</p><div class=\"challenge\">\n<p></p>\n\n<p>Add a section title <code>&lt;h1&gt;</code> element, right above the gameboard with the text <code>Concentration</code>. The text should be center aligned.</p>\n\n<p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P05-Final-Touch-Ups/assets/section_title.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P05-Final-Touch-Ups/assets/section_title.jpg\" alt=\"Section Title\" title=\"\">\n        </a></p>\n</div><p>When you're done, check your solution below.</p><div class=\"solution\">\n<p></p>\n\n<p>Within your outer <code>.container</code>, add the following <code>&lt;h1&gt;</code> element just before your <code>.board-container</code> <code>&lt;div&gt;</code>.</p>\n<pre><span class=\"nt\">&lt;body&gt;</span>\n  <span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"container\"</span><span class=\"nt\">&gt;</span>\n    <span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"row\"</span><span class=\"nt\">&gt;</span>\n      <span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"col-12\"</span><span class=\"nt\">&gt;</span>\n\n        <span class=\"c\">&lt;!-- add new h1 below --&gt;</span>\n        <span class=\"nt\">&lt;h1</span> <span class=\"na\">class=</span><span class=\"s\">\"text-center\"</span><span class=\"nt\">&gt;</span>Concentration<span class=\"nt\">&lt;/h1&gt;</span>\n\n        <span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"board-container container\"</span><span class=\"nt\">&gt;</span>\n\n          <span class=\"c\">&lt;!-- ... existing gameboard HTML --&gt;</span>\n\n        <span class=\"nt\">&lt;/div&gt;</span>\n      <span class=\"nt\">&lt;/div&gt;</span>\n    <span class=\"nt\">&lt;/div&gt;</span>\n  <span class=\"nt\">&lt;/div&gt;</span>\n\n  <span class=\"c\">&lt;!-- ... scripts --&gt;</span>\n\n<span class=\"nt\">&lt;/body&gt;</span></pre>\n<p>To center our new <code>&lt;h1&gt;</code> element, we add the <code>.text-center</code> Bootstrap class.</p>\n</div>"},{"id":"T0E6OlNlY3Rpb24tMzY4Mw==","title":"Adding a Reset Button","htmlContent":"<p>Next, we'll introduce a new HTML element: the <code>&lt;button&gt;</code>. We'll use a <code>&lt;button&gt;</code> to create a button that will be triggered to execute code in our <code>main.js</code> script whenever the user clicks on it.</p><p>Let's start with the HTML. We'll give it some of that built-in Bootstrap styling.</p><div class=\"action\">\n<p></p>\n\n<p>In <code>index.html</code>, add a <em>Reset button</em> right below the closing tag of your <code>.board-container</code> <code>&lt;div&gt;</code>:</p>\n<pre><span class=\"nt\">&lt;body&gt;</span>\n  <span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"container\"</span><span class=\"nt\">&gt;</span>\n    <span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"row\"</span><span class=\"nt\">&gt;</span>\n      <span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"col-12\"</span><span class=\"nt\">&gt;</span>\n        <span class=\"nt\">&lt;h1</span> <span class=\"na\">class=</span><span class=\"s\">\"text-center\"</span><span class=\"nt\">&gt;</span>Concentration<span class=\"nt\">&lt;/h1&gt;</span>\n\n        <span class=\"nt\">&lt;div</span> <span class=\"na\">class=</span><span class=\"s\">\"board-container container\"</span><span class=\"nt\">&gt;</span>\n\n          <span class=\"c\">&lt;!-- ... existing gameboard HTML --&gt;</span>\n\n        <span class=\"nt\">&lt;/div&gt;</span>\n\n        <span class=\"c\">&lt;!-- add reset button below --&gt;</span>\n        <span class=\"nt\">&lt;button</span> <span class=\"na\">id=</span><span class=\"s\">\"reset-button\"</span> <span class=\"na\">class=</span><span class=\"s\">\"btn btn-lg btn-primary btn-block\"</span><span class=\"nt\">&gt;</span>reset<span class=\"nt\">&lt;/button&gt;</span>\n      <span class=\"nt\">&lt;/div&gt;</span>\n    <span class=\"nt\">&lt;/div&gt;</span>\n  <span class=\"nt\">&lt;/div&gt;</span>\n\n  <span class=\"c\">&lt;!-- ... scripts --&gt;</span>\n\n<span class=\"nt\">&lt;/body&gt;</span></pre>\n<p>We've give our <code>&lt;button&gt;</code> an <em>id</em> of <code>#reset-button</code> so that we can easily access it using the DOM in our JavaScript code. In addition, we've added some Bootstrap classes to give it some styling.</p>\n</div><p>To finish up, we'll need to add a bit more CSS for our <code>#reset-button</code>.</p><div class=\"action\">\n<p></p>\n\n<p>In <code>style.css</code>, add the following CSS rule so that your <em>Reset button</em> is the correct width and centered within your outer <code>.container</code> <code>&lt;div&gt;</code>.</p>\n<pre><span class=\"nf\">#reset-button</span> <span class=\"p\">{</span>\n  <span class=\"k\">margin</span><span class=\"o\">:</span> <span class=\"m\">15px</span> <span class=\"k\">auto</span> <span class=\"m\">0</span><span class=\"p\">;</span>\n  <span class=\"k\">width</span><span class=\"o\">:</span> <span class=\"m\">370px</span><span class=\"p\">;</span>\n<span class=\"p\">}</span></pre>\n</div><p>When your done, your UI should match the design. Refresh your web page and you should see the following:</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P05-Final-Touch-Ups/assets/game_design.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P05-Final-Touch-Ups/assets/game_design.jpg\" alt=\"Game Design\" title=\"\">\n        </a></p><p>However, if you click on your <em>Reset button</em>, nothing happens. That's because we haven't added any code to happen when the user clicks the button. Let's implement that next.</p><h2>Handling Button Click Events</h2><p>To handle <em>Click</em> events from our <code>&lt;button&gt;</code> element, we'll need to add some additional code in our script file.</p><div class=\"action\">\n<p></p>\n\n<p>In <code>main.js</code>, add the following JavaScript code:</p>\n<pre><span class=\"c1\">// 1</span>\n<span class=\"kr\">const</span> <span class=\"nx\">resetButton</span> <span class=\"o\">=</span> <span class=\"nb\">document</span><span class=\"p\">.</span><span class=\"nx\">getElementById</span><span class=\"p\">(</span><span class=\"s2\">\"reset-button\"</span><span class=\"p\">);</span>\n\n<span class=\"c1\">// 2</span>\n<span class=\"nx\">resetButton</span><span class=\"p\">.</span><span class=\"nx\">addEventListener</span><span class=\"p\">(</span><span class=\"s1\">'click'</span><span class=\"p\">,</span> <span class=\"p\">()</span> <span class=\"o\">=&gt;</span> <span class=\"p\">{</span>\n  <span class=\"c1\">// 3</span>\n  <span class=\"nx\">console</span><span class=\"p\">.</span><span class=\"nx\">log</span><span class=\"p\">(</span><span class=\"s1\">'reset button clicked'</span><span class=\"p\">);</span>\n<span class=\"p\">});</span></pre>\n<p>Breaking down the steps in the code above:</p>\n\n<ol>\n<li>Get a reference to the DOM element using the <code>#reset-button</code> id.</li>\n<li>Add an event listener that monitors the element for <code>click</code> events.</li>\n<li>Log <code>reset button clicked</code> to the JavaScript console each time a <code>click</code> event occurs.</li>\n</ol>\n</div><p>Let's test this out.</p><div class=\"action\">\n<p></p>\n\n<p>To see if our <em>Reset button</em> is receiving and handling <code>click</code> events, do the following:</p>\n\n<ol>\n<li>Refresh your web page.</li>\n<li>Open the JavaScript console in Chrome DevTools by clicking <code>opt-CMD-j</code>.</li>\n<li>Click on the <em>Reset button</em> and see if the log is printed in the console.</li>\n</ol>\n\n<p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P05-Final-Touch-Ups/assets/reset_log.jpg\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P05-Final-Touch-Ups/assets/reset_log.jpg\" alt=\"Reset Log\" title=\"\">\n        </a></p>\n</div><p>When you've confirmed that your <em>Reset button</em> is handling <code>click</code> events, we can move on to implementing the logic for resetting the game.</p><h2>Reset Game Logic</h2><p>Let's create a new function that will reset our concentration game.</p><div class=\"action\">\n<p></p>\n\n<p>In <code>main.js</code>, add the following code to the bottom of your script:</p>\n<pre><span class=\"kd\">function</span> <span class=\"nx\">resetGame</span><span class=\"p\">()</span> <span class=\"p\">{</span>\n  <span class=\"c1\">// 1</span>\n  <span class=\"nx\">firstFaceupSquare</span> <span class=\"o\">=</span> <span class=\"kc\">null</span><span class=\"p\">;</span>\n\n  <span class=\"c1\">// 2</span>\n  <span class=\"nx\">boardSquares</span><span class=\"p\">.</span><span class=\"nx\">forEach</span><span class=\"p\">((</span><span class=\"nx\">square</span><span class=\"p\">)</span> <span class=\"o\">=&gt;</span> <span class=\"p\">{</span>\n    <span class=\"nx\">square</span><span class=\"p\">.</span><span class=\"nx\">reset</span><span class=\"p\">()</span>\n  <span class=\"p\">});</span>\n\n  <span class=\"c1\">// 3</span>\n  <span class=\"nx\">setTimeout</span><span class=\"p\">(()</span> <span class=\"o\">=&gt;</span> <span class=\"p\">{</span>\n    <span class=\"c1\">// 4</span>\n    <span class=\"kr\">const</span> <span class=\"nx\">randomColorPairs</span> <span class=\"o\">=</span> <span class=\"nx\">shuffleColors</span><span class=\"p\">();</span>\n\n    <span class=\"c1\">// 5</span>\n    <span class=\"k\">for</span> <span class=\"p\">(</span><span class=\"kd\">let</span> <span class=\"nx\">i</span> <span class=\"o\">=</span> <span class=\"mi\">0</span><span class=\"p\">;</span> <span class=\"nx\">i</span> <span class=\"o\">&lt;</span> <span class=\"nx\">boardSquares</span><span class=\"p\">.</span><span class=\"nx\">length</span><span class=\"p\">;</span> <span class=\"nx\">i</span><span class=\"o\">++</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n      <span class=\"kr\">const</span> <span class=\"nx\">newColor</span> <span class=\"o\">=</span> <span class=\"nx\">randomColorPairs</span><span class=\"p\">[</span><span class=\"nx\">i</span><span class=\"p\">];</span>\n      <span class=\"kr\">const</span> <span class=\"nx\">square</span> <span class=\"o\">=</span> <span class=\"nx\">boardSquares</span><span class=\"p\">[</span><span class=\"nx\">i</span><span class=\"p\">];</span>\n\n      <span class=\"nx\">square</span><span class=\"p\">.</span><span class=\"nx\">setColor</span><span class=\"p\">(</span><span class=\"nx\">newColor</span><span class=\"p\">);</span>\n    <span class=\"p\">}</span>\n  <span class=\"p\">},</span> <span class=\"mi\">500</span><span class=\"p\">);</span>\n<span class=\"p\">}</span></pre>\n<p>Walking through each step together:</p>\n\n<ol>\n<li>Reset the <code>firstFaceupSquare</code> variable back to <code>null</code>.</li>\n<li>Use the <code>reset()</code> method of <code>BoardSquare</code> to set both <code>isFaceUp</code> and <code>isMatched</code> back to <code>false</code>, as well as setting each square back to facedown.</li>\n<li>Use the <code>setTimeout()</code> function to delay the execution of the code within <code>setTimeout()</code> by 500 milliseconds. We do this because calling the <code>reset()</code> on each <code>BoardSquare</code> object will trigger the <code>.face-container</code> animation transition property. By delaying the shuffling of colors by 500ms, we don't interrupt the flipping animation.</li>\n<li>Shuffle and randomize a new order for the color pairs.</li>\n<li>Set each <code>BoardSquare</code> object in our <code>boardSquares</code> array with a new color based on our new shuffled colors.</li>\n</ol>\n</div><p>Before we can move on, we'll need to add one additional line of code.</p><div class=\"action\">\n<p></p>\n\n<p>In <code>main.js</code>, inside your <code>setColor()</code> method of your <code>BoardSquare</code> class, add the following line of code:</p>\n<pre><span class=\"kr\">class</span> <span class=\"nx\">BoardSquare</span> <span class=\"p\">{</span>\n\n  <span class=\"nx\">setColor</span><span class=\"p\">(</span><span class=\"nx\">color</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n    <span class=\"kr\">const</span> <span class=\"nx\">faceUpElement</span> <span class=\"o\">=</span> <span class=\"k\">this</span><span class=\"p\">.</span><span class=\"nx\">element</span><span class=\"p\">.</span><span class=\"nx\">getElementsByClassName</span><span class=\"p\">(</span><span class=\"s1\">'faceup'</span><span class=\"p\">)[</span><span class=\"mi\">0</span><span class=\"p\">];</span>\n\n    <span class=\"c1\">// remove the previous color if it exists</span>\n    <span class=\"nx\">faceUpElement</span><span class=\"p\">.</span><span class=\"nx\">classList</span><span class=\"p\">.</span><span class=\"nx\">remove</span><span class=\"p\">(</span><span class=\"k\">this</span><span class=\"p\">.</span><span class=\"nx\">color</span><span class=\"p\">);</span>\n\n    <span class=\"k\">this</span><span class=\"p\">.</span><span class=\"nx\">color</span> <span class=\"o\">=</span> <span class=\"nx\">color</span><span class=\"p\">;</span>\n    <span class=\"nx\">faceUpElement</span><span class=\"p\">.</span><span class=\"nx\">classList</span><span class=\"p\">.</span><span class=\"nx\">add</span><span class=\"p\">(</span><span class=\"nx\">color</span><span class=\"p\">);</span>\n  <span class=\"p\">}</span>\n<span class=\"p\">}</span></pre>\n<p>In the code above, we remove the previous color before setting a new color. We need to add this line of code since we're re-using the same <code>BoardSquare</code> objects when we reset our game.</p>\n</div><p>Last, we'll modify the event listener to call the <code>resetGame()</code> function instead of logging text to the console.</p><div class=\"action\">\n<p></p>\n\n<p>In <code>main.js</code>, modify your <code>addEventListener()</code> function to the following:</p>\n<pre><span class=\"nx\">resetButton</span><span class=\"p\">.</span><span class=\"nx\">addEventListener</span><span class=\"p\">(</span><span class=\"s1\">'click'</span><span class=\"p\">,</span> <span class=\"p\">()</span> <span class=\"o\">=&gt;</span> <span class=\"p\">{</span>\n  <span class=\"nx\">resetGame</span><span class=\"p\">();</span>\n<span class=\"p\">});</span></pre>\n</div><p>Now, whenever our <em>Reset button</em> is clicked, our concentration will be reset. Let's refresh our web page and test it out.</p><p><video width=\"100%\" controls><source src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P05-Final-Touch-Ups/assets/reset_game.mov\" type=\"video/mp4\"></source></video></p>"},{"id":"T0E6OlNlY3Rpb24tMzY4NA==","title":"Wrapping Up","htmlContent":"<p>Nicee! We've implemented the last feature of our concentration game. Feel free to celebrate by spending endless amounts of time playing your new favorite matching game. In the last section, we'll review what we've learned and discuss where to go from here.</p>"}]},"next":{"id":"T0E6OlBhZ2UtMTA0OQ==","slug":"conclusion-amI=","title":"Conclusion"},"previous":{"id":"T0E6OlBhZ2UtMTA0OQ==","slug":"conclusion-amI=","title":"Conclusion"}},{"id":"T0E6OlBhZ2UtMTA0OQ==","title":"Conclusion","slug":"conclusion-amI=","sections":{"nodes":[{"id":"T0E6OlNlY3Rpb24tMzY4NQ==","title":"Conclusion","htmlContent":"<p>In this tutorial, we learned about client-side JavaScript and used our previous knowledge of HTML and CSS to build a game concentration.</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P06-Conclusion/assets/completed_game.png\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P06-Conclusion/assets/completed_game.png\" alt=\"Completed Game\" title=\"\">\n        </a></p><p>Through the tutorial, we learned about many new concepts, especially in regards to using client-side JavaScript to build dynamic web pages. Along the way, you've picked up some new skills:</p><ul>\n<li>become more familiar with programming in JavaScript</li>\n<li>manipulate the DOM using client-side JavaScript</li>\n<li>learn to think through and implement more complex logic </li>\n<li>utilize the Chrome DevTools for debugging client-side JavaScript</li>\n</ul><p>We've also go to review front-end basics:</p><ul>\n<li>building web pages with HTML elements</li>\n<li>styling HTML elements with CSS</li>\n<li>using Bootstrap's grid system to layout HTML content</li>\n</ul><p>Another web page built. You're well on your way to becoming an awesome full-stack developer (we'll still need to dive into back-end development which we'll do next).</p><p>Stop and take a moment to appreciate how far you've come!</p><h2>Where To Go From Here?</h2><p>In the previous tutorials, we've only focused on front-end development. In the upcoming tutorials, we'll move onto back-end development and learning about how to build full-stack web apps.</p><div class=\"info\">\n<p></p>\n\n<p>Keep in mind that as we move on from front-end development to back-end development, our focus will shift to more complex JavaScript logic and the NodeJS ecosystem. This means, in future tutorials, we won't cover as in-depth on HTML and CSS. </p>\n\n<p>If you ever feel like you need a refresher, refer back to this and the previous tutorials!</p>\n</div>"}]},"next":null,"previous":null}]}},"page":{"id":"T0E6OlBhZ2UtMTA0OQ==","title":"Conclusion","slug":"conclusion-amI=","sections":{"nodes":[{"id":"T0E6OlNlY3Rpb24tMzY4NQ==","title":"Conclusion","htmlContent":"<p>In this tutorial, we learned about client-side JavaScript and used our previous knowledge of HTML and CSS to build a game concentration.</p><p><a href=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P06-Conclusion/assets/completed_game.png\" target=\"_blank\">\n          <img src=\"https://cdn.rawgit.com/MakeSchool-Tutorials/sa-2018-concentration/master/P06-Conclusion/assets/completed_game.png\" alt=\"Completed Game\" title=\"\">\n        </a></p><p>Through the tutorial, we learned about many new concepts, especially in regards to using client-side JavaScript to build dynamic web pages. Along the way, you've picked up some new skills:</p><ul>\n<li>become more familiar with programming in JavaScript</li>\n<li>manipulate the DOM using client-side JavaScript</li>\n<li>learn to think through and implement more complex logic </li>\n<li>utilize the Chrome DevTools for debugging client-side JavaScript</li>\n</ul><p>We've also go to review front-end basics:</p><ul>\n<li>building web pages with HTML elements</li>\n<li>styling HTML elements with CSS</li>\n<li>using Bootstrap's grid system to layout HTML content</li>\n</ul><p>Another web page built. You're well on your way to becoming an awesome full-stack developer (we'll still need to dive into back-end development which we'll do next).</p><p>Stop and take a moment to appreciate how far you've come!</p><h2>Where To Go From Here?</h2><p>In the previous tutorials, we've only focused on front-end development. In the upcoming tutorials, we'll move onto back-end development and learning about how to build full-stack web apps.</p><div class=\"info\">\n<p></p>\n\n<p>Keep in mind that as we move on from front-end development to back-end development, our focus will shift to more complex JavaScript logic and the NodeJS ecosystem. This means, in future tutorials, we won't cover as in-depth on HTML and CSS. </p>\n\n<p>If you ever feel like you need a refresher, refer back to this and the previous tutorials!</p>\n</div>"}]},"next":null,"previous":null}}},"staticQueryHashes":[]}