<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Mooseshark</title>
    <description>The latest articles on Forem by Mooseshark (@moosesharkgames).</description>
    <link>https://forem.com/moosesharkgames</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F341933%2F0ede8ecd-bca3-4186-a2a4-0a99edab44f1.jpeg</url>
      <title>Forem: Mooseshark</title>
      <link>https://forem.com/moosesharkgames</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/moosesharkgames"/>
    <language>en</language>
    <item>
      <title>Super Basic Intro to Using HTML and JavaScript Together</title>
      <dc:creator>Mooseshark</dc:creator>
      <pubDate>Tue, 07 Nov 2023 03:12:53 +0000</pubDate>
      <link>https://forem.com/moosesharkgames/super-basic-intro-to-using-html-and-javascript-together-43mb</link>
      <guid>https://forem.com/moosesharkgames/super-basic-intro-to-using-html-and-javascript-together-43mb</guid>
      <description>&lt;p&gt;One of the biggest problems I, and I think a lot of new developers have come across while learning web development is a disjointed understanding of how HTML and JavaScript work together. Many tutorials focus on one or the other and it can lead to some confusion around how to actually use HTML and JavaScript together to create apps and web pages. This tutorial will go in depth to break down a very simple project. We'll make a small Rock, Paper, Scissors game that takes user input, determines the outcome of the selections, and prints the result to the user.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites for this Tutorial
&lt;/h2&gt;

&lt;p&gt;While the point of this is to be as beginner friendly as possible, the focus is much more on using HTML and JavaScript together, and less on HTML and JavaScript basics. I think an extremely introductory level of skill should be had beforehand. Doing the HTML and JavaScript courses someplace like Codecademy would be ideal and would provide enough knowledge to follow along without much issue. You should also have something to write code in. An IDE like VSCode is best, but something like Notepad will be fine. If you're reading this, then you probably already have a web browser, so no concerns there. Or you are a Technomancer, in which case you can probably disregard this tutorial.&lt;/p&gt;

&lt;p&gt;Throughout I’ll be including example images where relevant. If you're coding along with this tutorial, you should be seeing similar results to the images, though they will be few and far between once we reach the JavaScript portion of the tutorial.&lt;/p&gt;

&lt;h2&gt;
  
  
  With that, Let's Begin with HTML
&lt;/h2&gt;

&lt;p&gt;We'll start with the most basic of basic things, an empty HTML page. Open your code writing app of choice and let's create a new file and call it &lt;strong&gt;rps.html&lt;/strong&gt;. This will be what holds all of our HTML for our Rock, Paper, Scissors game. Inside we'll be adding &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; tags to get started.&lt;/p&gt;

&lt;p&gt;Our &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt; tags will encase everything we need for our game. &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; is where you'd normally be adding a title, and little more info about our game. We're going to forgo adding things to the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; in the interested of simplicity in this tutorial. &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; will house our game inputs and results.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;

&amp;lt;/head&amp;gt;

&amp;lt;body&amp;gt;

&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can open this page by navigating to where you've saved your &lt;strong&gt;.html&lt;/strong&gt; file and double clicking it. If it doesn't open in your default web browser, try right clicking and selecting an &lt;strong&gt;Open With&lt;/strong&gt; option or dragging and dropping it into your browser. We'll be looking at this page a lot as we go so it's worthwhile to keep it open in your browser.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's Jump Right In to Making a Game
&lt;/h2&gt;

&lt;p&gt;Now, this isn't going to get too fancy. We want to keep it simple and easy to understand. Rock, Paper, Scissors doesn't require a complex set up. We need a way for two players to make a selection and not much else. Because of that, we're opting for a way to make that selection easy, a &lt;code&gt;&amp;lt;form&amp;gt;&lt;/code&gt; with two &lt;code&gt;&amp;lt;select&amp;gt;&lt;/code&gt; fields. Our &lt;code&gt;&amp;lt;select&amp;gt;&lt;/code&gt; fields will have three options (Rock, Paper, Scissors) each and there's 2 of them for the typical player count of Rock, Paper, Scissors.&lt;/p&gt;

&lt;p&gt;Inside our &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; tags, let's create a form with opening and closing &lt;/p&gt; tags. Our form tags are going to create a nice organizational element to hold our &lt;strong&gt;select&lt;/strong&gt; fields, their labels and our button input we'll eventually add.&lt;br&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;form&amp;gt;
&amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;In-between our &lt;code&gt;&amp;lt;form&amp;gt;&lt;/code&gt; tags, we are going to add our &lt;code&gt;&amp;lt;select&amp;gt;&lt;/code&gt; fields. These are going to be made up of three elements: a &lt;code&gt;&amp;lt;label&amp;gt;&lt;/code&gt; to show what the &lt;strong&gt;select&lt;/strong&gt; field is for, a  to create the drop down list portions of our &lt;strong&gt;select&lt;/strong&gt; field, and some &lt;code&gt;&amp;lt;option&amp;gt;&lt;/code&gt; tags to give us something to select in the field.&lt;/p&gt;

&lt;p&gt;We'll start by creating our first &lt;strong&gt;select&lt;/strong&gt; field for &lt;strong&gt;Player 1&lt;/strong&gt;. Let's make opening and closing &lt;code&gt;&amp;lt;label&amp;gt;&lt;/code&gt; tags.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;label&amp;gt;&amp;lt;/label&amp;gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Since we want this label to be for player one, let's slap that in-between the two tags so we know who's selecting what. Of course, feel free to call your players or selections whatever you want. We'll be keeping it pretty generic here to make it easy to follow.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;label&amp;gt;Player 1: &amp;lt;/label&amp;gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gLZkz4wt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zcyypfz2cqyewkvnm0vl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gLZkz4wt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zcyypfz2cqyewkvnm0vl.png" alt="Image description" width="265" height="179"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, let's start our &lt;strong&gt;select&lt;/strong&gt; field proper with some opening and closing select tags. We're going to stack anything we'll need to add more elements to for some easy formatting.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;select&amp;gt;
&amp;lt;/select&amp;gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Our &lt;strong&gt;select&lt;/strong&gt; field is pretty barren, and we're going to need something to reference when we get to the JavaScript portion of things, so let's add an &lt;strong&gt;id&lt;/strong&gt; attribute to it. Your &lt;strong&gt;id&lt;/strong&gt; attribute can be anything, but we want it to be descriptive so we know what we're looking at, so let's call it &lt;strong&gt;player1&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;select id="player1"&amp;gt;
&amp;lt;/select&amp;gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Now that we have an &lt;strong&gt;id&lt;/strong&gt; on our &lt;strong&gt;select&lt;/strong&gt; field, we need to go back and amend our &lt;code&gt;&amp;lt;label&amp;gt;&lt;/code&gt;. We're going to add a &lt;strong&gt;for&lt;/strong&gt; attribute to the label. The &lt;strong&gt;for&lt;/strong&gt; attribute lets us say what element the label is for by referencing that element's &lt;strong&gt;id&lt;/strong&gt;. Since this is for our element with the id of &lt;strong&gt;player1&lt;/strong&gt;, we'll add the for attribute as such.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;label for="player1"&amp;gt;Player 1: &amp;lt;/label&amp;gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Now we can finish off our &lt;strong&gt;select&lt;/strong&gt; field with some options. Inside the &lt;code&gt;&amp;lt;select&amp;gt;&lt;/code&gt; tags, we're going to add three options. Each option uses its own &lt;code&gt;&amp;lt;option&amp;gt;&lt;/code&gt; opening and closing tags. These tags will have a &lt;strong&gt;value&lt;/strong&gt; attribute attached to them, and inside the tags, we'll put some text to say what we want them to say. Let's make an example with Rock.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;option value="Rock"&amp;gt;Rock&amp;lt;/select&amp;gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;You can see we made the value and the display text the same. The value could be anything you want to represent someone picking Rock (a number, letter, different words), but we are going to stick with Rock in order to make use of the value later in our JavaScript.&lt;/p&gt;

&lt;p&gt;We can now add the options for Paper and Scissors in the same way. At the end, we'll have a little &lt;strong&gt;select&lt;/strong&gt; field snippet that looks something like this:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;label for="player1"&amp;gt;Player 1: &amp;lt;/label&amp;gt;
  &amp;lt;select id="player1"&amp;gt;
    &amp;lt;option value="Rock"&amp;gt;Rock&amp;lt;/option&amp;gt;
    &amp;lt;option value="Paper"&amp;gt;Paper&amp;lt;/option&amp;gt;
    &amp;lt;option value="Scissors"&amp;gt;Scissors&amp;lt;/option&amp;gt;
  &amp;lt;/select&amp;gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OPsE-bIx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/je9ttn5l61v6ytnyyus1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OPsE-bIx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/je9ttn5l61v6ytnyyus1.png" alt="Image description" width="240" height="138"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We've created a first player, but we still need a second. After all, playing Rock, Paper, Scissors with only one person is about as fun as simulating it on a computer with two drop down menus. We're going to do what developers have been trying to do for decades and make our lives easier by just copying and pasting Player 1, and changing a few things so that we have a Player 2.&lt;/p&gt;

&lt;p&gt;Almost everything is identical, but we need to update our &lt;strong&gt;for&lt;/strong&gt;, &lt;strong&gt;id&lt;/strong&gt;, and &lt;strong&gt;label&lt;/strong&gt; text to reference 2 instead of 1.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;label for="player2"&amp;gt;Player 2: &amp;lt;/label&amp;gt;
  &amp;lt;select id="player2"&amp;gt;
    &amp;lt;option value="Rock"&amp;gt;Rock&amp;lt;/option&amp;gt;
    &amp;lt;option value="Paper"&amp;gt;Paper&amp;lt;/option&amp;gt;
    &amp;lt;option value="Scissors"&amp;gt;Scissors&amp;lt;/option&amp;gt;
  &amp;lt;/select&amp;gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Well, we did it! We have two players to play the game. But we need one more thing before we can move on to our JavaScript and be done with our &lt;/p&gt; We're going to need to create a button to submit our choices in order to determine who will come out as victorious in our high stakes Rock, Paper, Scissors simulator.
&lt;h2&gt;
  
  
  Time to Make Our Form Capable of Doing Work
&lt;/h2&gt;

&lt;p&gt;Forms are special in that we could assign an &lt;strong&gt;action&lt;/strong&gt; attribute to our form and create an &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt; to submit our form to that action, but we're going to keep it a little simpler than that. Instead we are going to create an &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt; with a few attributes. We'll need a &lt;strong&gt;type&lt;/strong&gt;, a &lt;strong&gt;value&lt;/strong&gt;, and also an event called &lt;strong&gt;onclick&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's start with the small stuff, the attributes. &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt; is a little unique in that we can give it a &lt;strong&gt;type&lt;/strong&gt; and depending on that type it can be and do different things. We could make text fields and number fields or checkboxes or a vast assortment of things and they all have different properties to them. However, we want to submit a form and for that, we're going to want to click a button. So, big shock, we're going to give our &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt; a &lt;strong&gt;type&lt;/strong&gt; of &lt;strong&gt;button&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;input type="button"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tOP-CLZS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ofzs6w1n868jh61vm5ws.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tOP-CLZS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ofzs6w1n868jh61vm5ws.png" alt="Image description" width="313" height="152"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We also want our &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt; to say something so the user knows clicking it is required to play. A &lt;strong&gt;value&lt;/strong&gt; attribute will let us add some text to our button and spruce it up a bit. Since this is Rock, Paper, Scissors, I think it's appropriate to call that &lt;strong&gt;value&lt;/strong&gt; "Shoot!". Feel free to put whatever you like as your &lt;strong&gt;value&lt;/strong&gt;. It is strictly visual in this case and won't impact how the game functions. I will be referring to it as the "Shoot!" button throughout this tutorial. Afterwards you'll end up with something like this:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;input type="button" value="Shoot!"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4pKPWITg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eneh2i5zoy8as6fadqqb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4pKPWITg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eneh2i5zoy8as6fadqqb.png" alt="Image description" width="231" height="153"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now I mentioned one other thing we need and that's an &lt;strong&gt;event&lt;/strong&gt; known as &lt;strong&gt;onclick&lt;/strong&gt;. But what is an &lt;strong&gt;onclick&lt;/strong&gt; event? It's kind of exactly what it sounds like. It's an event that triggers when you click on an element. In this case, our button is what's being clicked on and the event we're going to be firing is the JavaScript code we'll write. An onclick event can execute code in a variety of ways, but what we'll be focusing on is probably the most common, calling a &lt;strong&gt;function&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We haven't written any JavaScript code just yet, so we don't even know what we're going to be calling our &lt;strong&gt;function&lt;/strong&gt;. Well, we're going to cheat a little. We know we're playing a game, so we're going to continue to keep it simple and call it &lt;strong&gt;playGame()&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;input type="button" value="Shoot!" onclick="playGame()"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Now, if you look at your form in a web browser, you'll notice things are looking a little cramped. Let's space it out a bit with a few break tags (&lt;code&gt;&amp;lt;br&amp;gt;&lt;/code&gt;) after each closing &lt;code&gt;&amp;lt;/select&amp;gt;&lt;/code&gt; tag. It'll add a new line between our elements and make things look a bit nicer&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;/select&amp;gt;
&amp;lt;!--- here for break tags ---&amp;gt;
&amp;lt;br&amp;gt;
&amp;lt;br&amp;gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;With all said and done, we should have our full form complete. That means we have everything we need to start playing with JavaScript and making our game do something. You're full HTML file should look something like this at this point&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
&amp;lt;/head&amp;gt;

&amp;lt;body&amp;gt;
  &amp;lt;form&amp;gt;
    &amp;lt;label for="player1"&amp;gt;Player 1: &amp;lt;/label&amp;gt;
      &amp;lt;select id="player1"&amp;gt;
        &amp;lt;option value="Rock"&amp;gt;Rock&amp;lt;/option&amp;gt;
        &amp;lt;option value="Paper"&amp;gt;Paper&amp;lt;/option&amp;gt;
        &amp;lt;option value="Scissors"&amp;gt;Scissors&amp;lt;/option&amp;gt;
      &amp;lt;/select&amp;gt;
    &amp;lt;br&amp;gt;
    &amp;lt;br&amp;gt;
    &amp;lt;label for="player2"&amp;gt;Player 2: &amp;lt;/label&amp;gt;
      &amp;lt;select id="player2"&amp;gt;
        &amp;lt;option value="Rock"&amp;gt;Rock&amp;lt;/option&amp;gt;
        &amp;lt;option value="Paper"&amp;gt;Paper&amp;lt;/option&amp;gt;
        &amp;lt;option value="Scissors"&amp;gt;Scissors&amp;lt;/option&amp;gt;
       &amp;lt;/select&amp;gt;
    &amp;lt;br&amp;gt;
    &amp;lt;br&amp;gt;
    &amp;lt;input type="button" value="Shoot!" onclick="playGame();"&amp;gt;
  &amp;lt;/form&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZMAEPM49--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rjjii1a29zfukr89oyso.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZMAEPM49--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rjjii1a29zfukr89oyso.png" alt="Image description" width="245" height="279"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Time to Make a Game Playable with JavaScript
&lt;/h2&gt;

&lt;p&gt;First things first, we're going to need a new JavaScript file. Let's make a new file in the same directory as our HTML file and call it &lt;strong&gt;rps.js&lt;/strong&gt;. Go ahead and open it up if it's not already open and we'll add our first bit of JavaScript.&lt;/p&gt;

&lt;p&gt;We're going to start by creating a new &lt;strong&gt;function&lt;/strong&gt;. A &lt;strong&gt;function&lt;/strong&gt; is just a snippet of code that lets us set up some repeatable steps for our application to execute. In our situation, we are writing the steps that allow our game to be playable, but functions are a very powerful tool and let us repeat actions without the need to rewrite them over and over again. Each time we want to perform an action, we can just &lt;strong&gt;call&lt;/strong&gt; our &lt;strong&gt;function&lt;/strong&gt;. In our HTML file, we already set up what the name of our function should be in our &lt;strong&gt;onclick&lt;/strong&gt; event so we can go ahead add that &lt;strong&gt;function&lt;/strong&gt; to our JS file using the function keyword like so:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function playGame(){
}
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Great! We have some JavaScript. Now what? Well, we're going to do something that's not always necessary, but for the purposes of learning, let's just test our &lt;strong&gt;onclick&lt;/strong&gt; event and see if it works.&lt;/p&gt;

&lt;p&gt;For some basic testing, let's add a &lt;strong&gt;console.log()&lt;/strong&gt; to our function so we can print some text to our console and see the &lt;strong&gt;onclick&lt;/strong&gt; event in action.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function playGame(){
  console.log('testing our game');
}
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;So, what does a &lt;strong&gt;console.log()&lt;/strong&gt; do? Well, it prints out information. In our current situation, we just want to make sure the function is working when we click our button. To do so, we just added a bit of text as a string to the function call. When we click our Shoot! button, we should see that text print out in the browsers development console.&lt;/p&gt;

&lt;p&gt;Oh, and what is a development console? It's just part of the development tool kit that's available on virtually every browser. To open it (at least on a keyboard with function keys), you can just simply press the &lt;strong&gt;F12&lt;/strong&gt; key and select the &lt;strong&gt;Console&lt;/strong&gt; tab if it's not already selected. If you don't have an F12 key, you can usually right click on the page and select &lt;strong&gt;Inspect&lt;/strong&gt; and then select the &lt;strong&gt;Console&lt;/strong&gt; tab. Alternatively, you may have to check the options available on your browser and look for something along the lines of &lt;strong&gt;Development Tools&lt;/strong&gt; or &lt;strong&gt;Dev Tools&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;If you haven't already, open your &lt;strong&gt;rps.html&lt;/strong&gt; file in your default browser, open the development console, and let's try running the game by making two selections and clicking the Shoot! button.&lt;/p&gt;

&lt;p&gt;And… oh noooo! It didn't work. So, what happened? I expect that you got something along the lines of:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Li7jc78u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wz9iws1bw9qs8o9i8egg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Li7jc78u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wz9iws1bw9qs8o9i8egg.png" alt="Image description" width="523" height="161"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;No problem! Nothing to worry about. We got this error because we did not link our JavaScript file to our HTML file. Let's take care of that now and then talk about errors and how we can decipher them.&lt;/p&gt;

&lt;p&gt;Let's go back to our &lt;strong&gt;rps.html&lt;/strong&gt; file and add a &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag. We can add this in a couple locations, the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt;, or the &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt;. While adding to the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; works and in our case isn't going to cause any issues, best practice is to add JavaScript &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tags at the bottom of the &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt;, just before the closing &lt;code&gt;&amp;lt;/body&amp;gt;&lt;/code&gt; tag.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;We're going to add two attributes to our &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag. We want to specify a &lt;strong&gt;type&lt;/strong&gt;, which in this case will be &lt;strong&gt;text/javascript&lt;/strong&gt;, and we want to add a &lt;strong&gt;source&lt;/strong&gt; attribute (&lt;strong&gt;src&lt;/strong&gt;) to point to the JavaScript file we created based on the path from the &lt;strong&gt;HTML&lt;/strong&gt; file. In our case, we will link directly to &lt;strong&gt;rps.js&lt;/strong&gt; because it should be in the same directory as &lt;strong&gt;rps.html&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script type="text/javascript" src="rps.js"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Now let's go back to the web browser, refresh, and try clicking the &lt;strong&gt;Shoot!&lt;/strong&gt; button again. This time we should see the text from our &lt;strong&gt;console.log()&lt;/strong&gt; in the console.&lt;/p&gt;

&lt;p&gt;Success! You should now see something along the lines of this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mDo3Otve--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xwhs3wvl4l4ubyujppnz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mDo3Otve--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xwhs3wvl4l4ubyujppnz.png" alt="Image description" width="727" height="143"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's take a slight detour and do a quick breakdown on the error we received.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--O1Hxw07f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fn0w7t2peqx0wm5rm3ms.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--O1Hxw07f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fn0w7t2peqx0wm5rm3ms.png" alt="Image description" width="516" height="125"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This first part is letting us know what's wrong. We got an error and that error is that &lt;strong&gt;playGame&lt;/strong&gt; is not defined. It's not defined because according to our HTML page, that &lt;strong&gt;function&lt;/strong&gt; doesn't exist.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ushrZ07E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3l1khsllvovtthowi0y6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ushrZ07E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3l1khsllvovtthowi0y6.png" alt="Image description" width="485" height="118"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We know that the HTML page is looking for that &lt;strong&gt;function&lt;/strong&gt; and can't find it because we specifically told it to use a &lt;strong&gt;function&lt;/strong&gt; called &lt;strong&gt;playGame&lt;/strong&gt; in our &lt;strong&gt;onclick&lt;/strong&gt; event. We know the error is happening as a result of that &lt;strong&gt;onclick&lt;/strong&gt; event because &lt;strong&gt;HTMLInputElement.onclick&lt;/strong&gt; is saying the error happens on this html element, on it's &lt;strong&gt;onclick&lt;/strong&gt; event. &lt;strong&gt;(rps.html:23:75)&lt;/strong&gt; is saying, "Hey, the error is in your &lt;strong&gt;rps.html&lt;/strong&gt; file at &lt;strong&gt;line 23&lt;/strong&gt;, &lt;strong&gt;75 spaces&lt;/strong&gt; from the start of that line. Sometimes, the spaces into the line aren't super important to troubleshooting, but the line number is almost always helpful for tracking down a problem.&lt;/p&gt;

&lt;p&gt;How do we know that, based on this error, we need to add a &lt;strong&gt;&lt;/strong&gt; tag for our JavaScript file? Really, it's something that will come with experience. If you get that error, and you use what we just discussed to determine where it's happening, it'll be easy to search for what the cause is online. For us, we could determine that our &lt;strong&gt;onclick&lt;/strong&gt; event is looking for our &lt;strong&gt;function&lt;/strong&gt; and saying it's undefined. With that knowledge, we could search the world wide web for something like, "&lt;strong&gt;onclick event throws error function is not defined&lt;/strong&gt;", and we'll likely come across a post on &lt;strong&gt;stackoverflow&lt;/strong&gt; or somewhere where someone has asked a similar or the same question and we can go through answers and try to determine what we are missing and what's wrong.&lt;/p&gt;

&lt;p&gt;It's a bit frustrating and it's not ideal, but it's also a big part of current development and utilizing resources like this to resolve issues is something you'll be doing throughout your development career.&lt;/p&gt;

&lt;h2&gt;
  
  
  Now, Back To Our Show
&lt;/h2&gt;

&lt;p&gt;That was a big ol' side quest from our tutorial, but it's an important thing to understand and utilize. You'll be seeing a lot of errors and some of them aren't going to be very helpful at all. The more you experience and resolve the more likely you are to spend less time on them in the future.&lt;/p&gt;

&lt;p&gt;Let's get this show back on the road. We have some &lt;strong&gt;select&lt;/strong&gt; fields in our HTML file, and those fields have values that we want. Not only do we want them, but we want them at the ready to be used as much as we need, and that means we need them set to a variable. Let's create our first variable, assign it a value, then talk through what it is exactly that we're doing.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let player1 = document.getElementById('player1').value;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;You're probably at least familiar with variables and variable keywords, but we'll do a deep dive on this one and break out each piece.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;let&lt;/strong&gt; is our keyword for defining a variable that we plan to potentially overwrite or we expect the value of it to change at some point.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;player1&lt;/strong&gt; is the name of our &lt;strong&gt;variable&lt;/strong&gt;. We've called it &lt;strong&gt;player1&lt;/strong&gt; to match with our first select field for &lt;strong&gt;Player 1&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;=&lt;/strong&gt; to say we are setting player1 equal to something.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;document&lt;/strong&gt; is generally the page that the JavaScript is running on. In this case, our HTML page is the document.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;.getElementById('player1')&lt;/strong&gt; is a built-in function for &lt;strong&gt;document&lt;/strong&gt; that allows us to search our HTML page for an element based on its &lt;strong&gt;id&lt;/strong&gt; attribute. In this case, the element we are looking for is our first &lt;strong&gt;select&lt;/strong&gt; field for &lt;strong&gt;Player 1&lt;/strong&gt;. We gave that field an &lt;strong&gt;id&lt;/strong&gt; attribute of &lt;strong&gt;player1&lt;/strong&gt;. So, we are referencing that &lt;strong&gt;id&lt;/strong&gt; and using it to get us the element so that way we can retrieve information from it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;.value&lt;/strong&gt; gives us the &lt;strong&gt;value&lt;/strong&gt; of the element. In this case, &lt;strong&gt;value&lt;/strong&gt; is the value of the &lt;strong&gt;option&lt;/strong&gt; we selected in the &lt;strong&gt;select&lt;/strong&gt; field.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;;&lt;/strong&gt; to close out the JS line. While potentially not necessary, it is still good practice to include.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now that we've broken down how we plan to get a value from our &lt;strong&gt;select&lt;/strong&gt; field, try doing the same for &lt;strong&gt;Player 2&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;At this point, our JavaScript file should look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function playGame(){
  let player1 = document.getElementById('player1').value;
  let player2 = document.getElementById('player2').value;

  console.log('testing our game');
}
&lt;/code&gt;&lt;/pre&gt;



&lt;h2&gt;
  
  
  Looking Pretty Good
&lt;/h2&gt;

&lt;p&gt;We're off to a good start here. Now we have access to the &lt;strong&gt;values&lt;/strong&gt; from our &lt;strong&gt;select&lt;/strong&gt; fields. That means we can do something with them, right? It suuuure does. Now, we can work out our win conditions and add a little text to say who comes out on top.&lt;/p&gt;

&lt;p&gt;First, let's add one more &lt;strong&gt;variable&lt;/strong&gt;. We'll call it &lt;strong&gt;result&lt;/strong&gt; and we'll set it equal to an empty string using two single quotes ('').&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let result = '';
&lt;/code&gt;&lt;/pre&gt;



&lt;h2&gt;
  
  
  Adding Some Conditional Logic
&lt;/h2&gt;

&lt;p&gt;OK, let's get deep into this and figure out how to figure out who wins in a game of rock, paper, scissors. If you don't know the rules, no worries. It's pretty simple. Rock beats scissors, scissors beats paper, paper beats rock. That's it. Not a whole lot to the game really.&lt;/p&gt;

&lt;p&gt;So how do we choose the best way to determine our win conditions?&lt;/p&gt;

&lt;h2&gt;
  
  
  MENTAL EXERCISE TIME!!!!!!
&lt;/h2&gt;

&lt;p&gt;Yep, this is a tutorial about using JavaScript with HTML, but maybe we can expand the JavaScript part a bit more. No need to write any code or anything. But, before continuing, maybe take a minute to consider how you would approach this problem programmatically. Think of the possible win/lose combinations for rock, paper, scissors and consider how you would approach logically figuring out who wins each game. The important thing is that even if your solution is different than below, it can still be correct. Lots of problems have multiple solutions and you may come up with a good way to solve this on your own. You can also consider that not every solution would work for each situation. Don't worry if you're not sure. No harm in not coming up with anything and no harm in not having a full solution. You can always come back to this step and try out other solutions when we're done, or even expand on what we write in the tutorial.&lt;/p&gt;

&lt;h2&gt;
  
  
  For Now…
&lt;/h2&gt;

&lt;p&gt;We're going to go with my favorite solution, the short and easy route. We'll just use a few &lt;strong&gt;if/else if/else&lt;/strong&gt; conditional statements and try to keep things nice and clean. We really only care about two types of conditions: a &lt;strong&gt;tie&lt;/strong&gt;, and &lt;strong&gt;player1&lt;/strong&gt; winning. If our game doesn't result in either of those conditions, then we can assume &lt;strong&gt;player2&lt;/strong&gt; is the winner. With that logic, we don't need to overcomplicate our comparisons.&lt;/p&gt;

&lt;p&gt;Let's start with our first condition. We'll start with an &lt;strong&gt;if&lt;/strong&gt; statement which lets us make some comparison and determine if the logic makes sense. We can use some really simple logic that says if the value of &lt;strong&gt;player1 equals&lt;/strong&gt; (represented by two equals signs ==) the value of &lt;strong&gt;player2&lt;/strong&gt; then the game is a tie.&lt;/p&gt;

&lt;p&gt;This is where we'll first get to use our &lt;strong&gt;result&lt;/strong&gt; variable as well. When we defined result, we created it as a placeholder with the value of an empty string. We can now assign it a new value that we would like to use to display the results of the game. We'll do this similarly to how we initially defined it, but without the let keyword. And now we'll also set it to a string value that will mean something to our player, like &lt;strong&gt;'Tie!'&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We should come up with something like this:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (player1 == player2) {
  result = 'Tie!';
}
&lt;/code&gt;&lt;/pre&gt;



&lt;h2&gt;
  
  
  A Quick Test
&lt;/h2&gt;

&lt;p&gt;Before we continue on with our logical steps, let's reutilize some existing code and test that our conditional statement is working as expected. In the &lt;strong&gt;console.log()&lt;/strong&gt; we already have in our function, remove the text &lt;strong&gt;'testing our game'&lt;/strong&gt; and replace it with our &lt;strong&gt;result&lt;/strong&gt; variable.&lt;/p&gt;

&lt;p&gt;Now, refresh your browser, make sure your console is open, and select the same value for &lt;strong&gt;player1&lt;/strong&gt; and &lt;strong&gt;player2&lt;/strong&gt; and press the &lt;strong&gt;Shoot!&lt;/strong&gt; button. You should see &lt;strong&gt;Tie!&lt;/strong&gt; in your console window if your &lt;strong&gt;if&lt;/strong&gt; conditional statement is working properly.&lt;/p&gt;

&lt;h2&gt;
  
  
  But What Else
&lt;/h2&gt;

&lt;p&gt;Let's continue on our journey to logical conclusion by skipping a bunch of steps in favor of pairing up the &lt;strong&gt;if&lt;/strong&gt; conditions best partner in crime, an &lt;strong&gt;else&lt;/strong&gt; condition. With an &lt;strong&gt;else&lt;/strong&gt; condition we're basically saying that any logic not caught by the if &lt;strong&gt;must&lt;/strong&gt; make the &lt;strong&gt;else&lt;/strong&gt; statement the true statement. In our case, and temporarily to the benefit of &lt;strong&gt;player2&lt;/strong&gt;, our &lt;strong&gt;else&lt;/strong&gt; condition is going to say that &lt;strong&gt;player2&lt;/strong&gt;, for a few brief fleeting moments (unless you decide to skip testing this code change), wins if the game isn't a tie.&lt;/p&gt;

&lt;p&gt;An important side note, an &lt;strong&gt;if&lt;/strong&gt; can exist without an &lt;strong&gt;else&lt;/strong&gt; but, not the other way around.&lt;/p&gt;

&lt;p&gt;To append an &lt;strong&gt;else&lt;/strong&gt; to our &lt;strong&gt;if&lt;/strong&gt; we just simply write else after our closing curly brace (&lt;strong&gt;}&lt;/strong&gt;) for the &lt;strong&gt;if&lt;/strong&gt; conditional, and add a new opening and closing curly brace for the contents of the &lt;strong&gt;else&lt;/strong&gt; to reside in.&lt;/p&gt;

&lt;p&gt;We'll also add some new text for our &lt;strong&gt;result&lt;/strong&gt; variable to be set to. Seems fitting we say that &lt;strong&gt;result&lt;/strong&gt; is equal to &lt;strong&gt;'Player 2 Wins!'&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When we're done we'll have something like this:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (player1 == player2) {
  result = 'Tie!';
} else {
  result = 'Player 2 Wins!';
}
&lt;/code&gt;&lt;/pre&gt;



&lt;h2&gt;
  
  
  Continuing Logic
&lt;/h2&gt;

&lt;p&gt;Now we can finish off our little logic block here with a fancy bit of &lt;strong&gt;else if&lt;/strong&gt; conditional statements. There's work like adding another &lt;strong&gt;if&lt;/strong&gt; condition into the mix before our final &lt;strong&gt;else&lt;/strong&gt; condition and we can use them to check if &lt;strong&gt;player1&lt;/strong&gt; should be the winner given a certain selection from each player.&lt;/p&gt;

&lt;p&gt;In our &lt;strong&gt;else if&lt;/strong&gt; statements we need to know a bit more information than we did in our &lt;strong&gt;if&lt;/strong&gt; statement. We need to know what &lt;strong&gt;player1&lt;/strong&gt; and &lt;strong&gt;player2&lt;/strong&gt; selected, and more importantly, we need to know if &lt;strong&gt;player1&lt;/strong&gt; selected something that will beat &lt;strong&gt;player2&lt;/strong&gt;. Luckily, we know the rules to rock, paper, scissors, so we can simply check if each player selected a specific &lt;strong&gt;value&lt;/strong&gt; that would result in &lt;strong&gt;player1&lt;/strong&gt; being the winner.&lt;/p&gt;

&lt;p&gt;We'll do this by checking two statements and determining if they are both &lt;strong&gt;true&lt;/strong&gt;. If both are &lt;strong&gt;true&lt;/strong&gt; then we'll be able to conclude that &lt;strong&gt;player1&lt;/strong&gt; is the winner. Let's walk through the win condition for if &lt;strong&gt;player1&lt;/strong&gt; picks &lt;strong&gt;Rock&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We'll start similar to our &lt;strong&gt;else&lt;/strong&gt; condition by appending &lt;strong&gt;else if&lt;/strong&gt; after the closing curly brace (&lt;strong&gt;}&lt;/strong&gt;) of our &lt;strong&gt;if&lt;/strong&gt; conditional. Now we'll need to add opening and closing parentheses after &lt;strong&gt;else if&lt;/strong&gt;, just like with our &lt;strong&gt;if&lt;/strong&gt; conditional. Inside those parentheses is where we'll write our conditional logic.&lt;/p&gt;

&lt;p&gt;In this instance, we know that we want to check if &lt;strong&gt;player1&lt;/strong&gt; picked &lt;strong&gt;Rock&lt;/strong&gt;. we can do so by checking that our value for &lt;strong&gt;player1&lt;/strong&gt; equals the string &lt;strong&gt;'Rock'&lt;/strong&gt;. If it does, then that means our first statement is &lt;strong&gt;true&lt;/strong&gt;, which is what we want.&lt;/p&gt;

&lt;p&gt;Now we need to add an additional condition into the mix. To do so we're going to use a conditional operator for &lt;strong&gt;AND&lt;/strong&gt;. It looks like &lt;strong&gt;&amp;amp;&amp;amp;&lt;/strong&gt; and says that an entire statement is &lt;strong&gt;true&lt;/strong&gt; if each of the statements separated by the &lt;strong&gt;&amp;amp;&amp;amp;&lt;/strong&gt; operator are &lt;strong&gt;true&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Next, we need to know that &lt;strong&gt;player2&lt;/strong&gt; would need to pick something that would lose to &lt;strong&gt;Rock&lt;/strong&gt;, so they would have picked &lt;strong&gt;Scissors&lt;/strong&gt;. We'll essentially be doing the same check we did for &lt;strong&gt;player1&lt;/strong&gt;, but for &lt;strong&gt;player2&lt;/strong&gt; and making sure they selected &lt;strong&gt;'Scissors'&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now outside of the parentheses, we can add our open and closing curly braces to house our &lt;strong&gt;result&lt;/strong&gt; which will be us similarly setting the &lt;strong&gt;result&lt;/strong&gt; variable to something like &lt;strong&gt;'Player 1 Wins!'&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In the end you'll have something that looks a bit like this:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;else if (player1 == 'Rock' &amp;amp;&amp;amp; player2 == 'Scissors') {
  result = 'Player 1 Wins!';
}
&lt;/code&gt;&lt;/pre&gt;



&lt;h2&gt;
  
  
  And The Logical Conclusion
&lt;/h2&gt;

&lt;p&gt;We're moving ever so close to being done with the logic portion of this. Now we just need to write two more &lt;strong&gt;else if&lt;/strong&gt; conditions to follow up our first one. One where &lt;strong&gt;player1&lt;/strong&gt; picks &lt;strong&gt;Paper&lt;/strong&gt; and &lt;strong&gt;player2&lt;/strong&gt; picks &lt;strong&gt;Rock&lt;/strong&gt; and one for when &lt;strong&gt;player1&lt;/strong&gt; picks &lt;strong&gt;Scissors&lt;/strong&gt; and &lt;strong&gt;player2&lt;/strong&gt; picks &lt;strong&gt;Paper&lt;/strong&gt;. I'll let you write these two out on your own, but below will be what your &lt;strong&gt;function&lt;/strong&gt; should look like so far. When you're done, go ahead and try a few games with the console window open. Since we still have our &lt;strong&gt;console.log(result);&lt;/strong&gt; code at the bottom of our &lt;strong&gt;function&lt;/strong&gt;, you should be able to see the game in action.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function playGame(){
  let player1 = document.getElementById('player1').value;
  let player2 = document.getElementById('player2').value;
  let result = '';

  if (player1 == player2) {
    result = 'Tie!';
  } else if (player1 == 'Rock' &amp;amp;&amp;amp; player2 == 'Scissors') {
    result = 'Player 1 Wins!';
  } else if (player1 == 'Paper' &amp;amp;&amp;amp; player2 == 'Rock') {
    result = 'Player 1 Wins!';
  } else if (player1 == 'Scissors' &amp;amp;&amp;amp; player2 == 'Paper') {
    result = 'Player 1 Wins!';
  } else {
    result = 'Player 2 Wins!';
  }

  console.log(result);
}
&lt;/code&gt;&lt;/pre&gt;



&lt;h2&gt;
  
  
  Displaying a Win to Players
&lt;/h2&gt;

&lt;p&gt;Now we have a functioning game. You're making selections in &lt;strong&gt;HTML&lt;/strong&gt; and then using &lt;strong&gt;JavaScript&lt;/strong&gt; to figure out some logic and print out who's the game winner in the console. But, people don't tend to go to the console window looking for information. They want to see results up on the screen, so that's what we're going to make our code do.&lt;/p&gt;

&lt;h2&gt;
  
  
  Back to HTML
&lt;/h2&gt;

&lt;p&gt;We're going to start by going back to our &lt;strong&gt;HTML&lt;/strong&gt; page. We want to add a little placeholder element where we can display our &lt;strong&gt;result&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Under the closing tag for &lt;strong&gt;form&lt;/strong&gt; (&lt;code&gt;&amp;lt;/form&amp;gt;&lt;/code&gt;), put a space for us to add our new tag to hold our result value. For simplicity, we're going to use an &lt;code&gt;&amp;lt;h2&amp;gt;&lt;/code&gt; tag. We want our result to be large and visible. We could do this in a variety of other ways, but in the interest of keeping it simple, we'll let that tag handle all that for us.&lt;/p&gt;

&lt;p&gt;Inside our &lt;code&gt;&amp;lt;h2&amp;gt;&lt;/code&gt; tag we'll need to include an &lt;strong&gt;id&lt;/strong&gt; attribute just like we did with our &lt;code&gt;&amp;lt;select&amp;gt;&lt;/code&gt; fields. We'll set our &lt;strong&gt;id&lt;/strong&gt; to &lt;strong&gt;resultEl&lt;/strong&gt; since it's the element we want to use to display our result.&lt;/p&gt;

&lt;p&gt;In the end, your HTML should just have this one additional line between the closing &lt;code&gt;&amp;lt;form&amp;gt;&lt;/code&gt; tag and the &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag, like this:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;/form&amp;gt;
&amp;lt;h2 id="result"&amp;gt;&amp;lt;/h2&amp;gt;
&amp;lt;script type="text/javascript" src="tmp.js"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;h2&gt;
  
  
  And Back to the JavaScript
&lt;/h2&gt;

&lt;p&gt;Yep, that's it. That's all the &lt;strong&gt;HTML&lt;/strong&gt; we'll need to add. Now things are going to get a bit more tricky and we're going to actually be manipulating our &lt;strong&gt;HTML&lt;/strong&gt; with &lt;strong&gt;JavaScript&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;First things first, we no longer need &lt;strong&gt;console.log(result);&lt;/strong&gt; in our code. We can go ahead and remove that, since soon we’ll be seeing our results printed on the screen and won’t need the console for displaying them.&lt;/p&gt;

&lt;p&gt;We're going to start with adding two additional variables. We need a variable to get our result element from our &lt;strong&gt;HTML&lt;/strong&gt;, the same way we got the elements for &lt;strong&gt;player1&lt;/strong&gt; and &lt;strong&gt;player2&lt;/strong&gt;. We'll just need to get that element by its &lt;strong&gt;id&lt;/strong&gt; attribute, &lt;strong&gt;resultEl&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Next we need an element to hold an &lt;strong&gt;HTML&lt;/strong&gt; element we're going to let &lt;strong&gt;JavaScript&lt;/strong&gt; create for us using a function similar to how we used &lt;strong&gt;document.getElementById()&lt;/strong&gt;. For now we can just call it &lt;strong&gt;resultTextNode&lt;/strong&gt; and we don't need to assign it a value.&lt;/p&gt;

&lt;p&gt;Your two new variables should be fairly similar to this:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let resultEl = document.getElementById('resultEl');
let resultTextNode;
&lt;/code&gt;&lt;/pre&gt;



&lt;h2&gt;
  
  
  Creating a TextNode
&lt;/h2&gt;

&lt;p&gt;Now that we have our variables we can use Javascript to create some things, display some things, and also, remove some things. We'll start by making a new text node. We'll need this in order to display the text we've been assigning to &lt;strong&gt;result&lt;/strong&gt;. To do so, we're going to just set our new variable, &lt;strong&gt;resultTextNode&lt;/strong&gt;, equal to &lt;strong&gt;document.createTextNode(result)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;That should result in a bit of code like this at the bottom of your JavaScript file:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;resultTextNode = document.createTextNode(result);
&lt;/code&gt;&lt;/pre&gt;



&lt;h2&gt;
  
  
  Displaying Our Text Node
&lt;/h2&gt;

&lt;p&gt;This is the moment we've been leading up to. The reason we previously spent all those minutes creating a placeholder. It's time to display our &lt;strong&gt;result&lt;/strong&gt; to the player. This part gets a little complicated and I'm going to be keeping it relatively simple for the sake of this tutorial, but know that attributes of DOM elements can also have &lt;strong&gt;child&lt;/strong&gt; elements and they can nest relatively deep. We're just going to scratch the surface here, but it's something you'll definitely be encountering going forward and something you'll slowly learn to manage, understand, and manipulate.&lt;/p&gt;

&lt;p&gt;To display our &lt;strong&gt;result&lt;/strong&gt; value, we are going to take our &lt;strong&gt;resultEl&lt;/strong&gt; element and append our &lt;strong&gt;resultTextNode&lt;/strong&gt; to it as a &lt;strong&gt;child&lt;/strong&gt;. In our case, this is going to be really simple and smooth. JavaScript has us covered with an &lt;strong&gt;appendChild()&lt;/strong&gt; function for our element.&lt;/p&gt;

&lt;p&gt;We should end up with a tiny snippet like so at the bottom of the JavaScript file under everything else:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;resultEl.appendChild(resultTextNode);
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Go ahead and test it out, then we'll have one final hurdle to tackle.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cSVqkKpx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fog0yx666ug461omr8bc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cSVqkKpx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fog0yx666ug461omr8bc.png" alt="Image description" width="800" height="187"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  One Last Problem to Solve
&lt;/h2&gt;

&lt;p&gt;So, if your testing went as good as I expect, you've noticed that the results don't go away and in fact, they keep getting added onto each other. Not to worry, We have a semi-simple solution that will clear this up no problem.&lt;/p&gt;

&lt;p&gt;Our issue is that we are not removing the old text node before appending a new one. So we just keep on appending new children to our little placeholder and making a sloppy mess for our player.&lt;/p&gt;

&lt;p&gt;Well just like how we can append a child, we can also remove a child. This is another one of those problems that may have multiple solutions. Ours is not going to be the most sophisticated, but it will work for our scenario. More complex sites will likely require more complex solutions. Just something to keep in mind as we wrap up.&lt;/p&gt;

&lt;p&gt;Our &lt;strong&gt;resultEl&lt;/strong&gt; element has a fancy attribute on it called &lt;strong&gt;firstChild&lt;/strong&gt;. That &lt;strong&gt;firstChild&lt;/strong&gt; in our case will be our &lt;strong&gt;resultTextNode&lt;/strong&gt; if we've appended it to &lt;strong&gt;resultEl&lt;/strong&gt; already. Otherwise, &lt;strong&gt;firstChild&lt;/strong&gt; is nothing. We want to do two things, check if there's a &lt;strong&gt;firstChild&lt;/strong&gt;, then, if there is, remove it before we try to append a new child to our &lt;strong&gt;resultEl&lt;/strong&gt; element.&lt;/p&gt;

&lt;p&gt;The first part is a simple &lt;strong&gt;if&lt;/strong&gt; condition where we just want to know if &lt;strong&gt;resultEl.firstChild&lt;/strong&gt; contains anything. Checking &lt;strong&gt;if (resultEl.firstChild) {}&lt;/strong&gt; will return &lt;strong&gt;true&lt;/strong&gt; if there is a child, and &lt;strong&gt;false&lt;/strong&gt; if there is not.&lt;/p&gt;

&lt;p&gt;If we get a &lt;strong&gt;true&lt;/strong&gt; result, then we want to remove that child. We can do so similarly to how we appended a child with a function called &lt;strong&gt;removeChild()&lt;/strong&gt;. This gets a little awkward though. We can't just do &lt;strong&gt;removeChild(resultTextNode)&lt;/strong&gt; because &lt;strong&gt;resultTextNode&lt;/strong&gt; is now a different value.&lt;/p&gt;

&lt;p&gt;We have a few options. We can save the old value to another variable that we set at some point so that way we can reference it when the time comes. However, we can also simply reference the old value as &lt;strong&gt;resultEl.firstChild&lt;/strong&gt; since that's what we want to remove.&lt;/p&gt;

&lt;p&gt;We end up with a funky little snippet of code that looks about like this:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;resultTextNode = document.createTextNode(result);

if (resultEl.firstChild) {
  resultEl.removeChild(resultEl.firstChild);
}

resultEl.appendChild(resultTextNode);
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Now you should be able to go back and test again and see that the result is successfully cleared each round.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EjtcSHxR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9s5douenqsiijid29pnp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EjtcSHxR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9s5douenqsiijid29pnp.png" alt="Image description" width="320" height="344"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  That's It
&lt;/h2&gt;

&lt;p&gt;And that's it. That's the whole tutorial. It's a lot of words for just a little bit of code, but that's what programming is all about. Taking something complicated and making something simple with it. Hopefully this gives a bit of understanding on how &lt;strong&gt;HTML&lt;/strong&gt; and &lt;strong&gt;JavaScript&lt;/strong&gt; work together to make web pages and applications function, and how the two can work together to create interesting experiences for a user, or in our case, player.&lt;/p&gt;

&lt;p&gt;Below I'll include the full &lt;strong&gt;HTML&lt;/strong&gt; and &lt;strong&gt;JavaScript&lt;/strong&gt; code in case you missed anything or something about your code just isn't working right and you need to compare it with the finished product. Now that you have something functional, why not improve it. Change the inputs, add some CSS to the mix, add new options, anything you want to do. The best way to learn is to explore and try to find what's possible!&lt;/p&gt;

&lt;h2&gt;
  
  
  HTML
&lt;/h2&gt;



&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
&amp;lt;/head&amp;gt;

&amp;lt;body&amp;gt;
  &amp;lt;form&amp;gt;
    &amp;lt;label for="player1"&amp;gt;Player 1: &amp;lt;/label&amp;gt;
        &amp;lt;select id="player1"&amp;gt;
        &amp;lt;option value="Rock"&amp;gt;Rock&amp;lt;/option&amp;gt;
        &amp;lt;option value="Paper"&amp;gt;Paper&amp;lt;/option&amp;gt;
        &amp;lt;option value="Scissors"&amp;gt;Scissors&amp;lt;/option&amp;gt;
      &amp;lt;/select&amp;gt;
      &amp;lt;br&amp;gt;
      &amp;lt;br&amp;gt;
    &amp;lt;label for="player2"&amp;gt;Player 2: &amp;lt;/label&amp;gt;
        &amp;lt;select id="player2"&amp;gt;
        &amp;lt;option value="Rock"&amp;gt;Rock&amp;lt;/option&amp;gt;
        &amp;lt;option value="Paper"&amp;gt;Paper&amp;lt;/option&amp;gt;
        &amp;lt;option value="Scissors"&amp;gt;Scissors&amp;lt;/option&amp;gt;
      &amp;lt;/select&amp;gt;
      &amp;lt;br&amp;gt;
      &amp;lt;br&amp;gt;
    &amp;lt;input type="button" value="Shoot!" onclick="playGame();"&amp;gt;
  &amp;lt;/form&amp;gt;
  &amp;lt;h2 id="resultEl"&amp;gt;&amp;lt;/h2&amp;gt;
    &amp;lt;script type="text/javascript" src="tmp.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;h2&gt;
  
  
  JavaScript
&lt;/h2&gt;



&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function playGame(){
  let player1 = document.getElementById('player1').value;
  let player2 = document.getElementById('player2').value;
  let resultEl = document.getElementById('resultEl');
  let resultTextNode;
  let result = '';

  if (player1 == player2) {
    result = 'Tie!';
  } else if (player1 == 'Rock' &amp;amp;&amp;amp; player2 == 'Scissors') {
    result = 'Player 1 Wins!';
  } else if (player1 == 'Paper' &amp;amp;&amp;amp; player2 == 'Rock') {
    result = 'Player 1 Wins!';
  } else if (player1 == 'Scissors' &amp;amp;&amp;amp; player2 == 'Paper') {
    result = 'Player 1 Wins!';
  } else {
    result = 'Player 2 Wins!';
  }

  resultTextNode = document.createTextNode(result);

  if (resultEl.firstChild) {
    resultEl.removeChild(resultEl.firstChild);
  }

  resultEl.appendChild(resultTextNode);
}
&lt;/code&gt;&lt;/pre&gt;



</description>
      <category>javascript</category>
      <category>html</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Studying with Leetcode</title>
      <dc:creator>Mooseshark</dc:creator>
      <pubDate>Tue, 18 Aug 2020 00:28:56 +0000</pubDate>
      <link>https://forem.com/moosesharkgames/studying-with-leetcode-1em</link>
      <guid>https://forem.com/moosesharkgames/studying-with-leetcode-1em</guid>
      <description>&lt;p&gt;Last year I had the opportunity to interview with Google. I never expected they would pick me for a technical interview when I applied. I just assumed it would be a "well, at least I tried" type of scenario. To say I was shocked would be an understatement. It left me with one big hurdle to make it over that I hadn't had to deal with yet, whiteboarding. I'd been a developer for almost 6 years at the time. I didn't need to whiteboard for my job, so I never really thought about it. I quickly got to work studying and testing myself with practice problems from leetcode.com. In that time, I came up with a solid way to study, that allowed me to test my own knowledge, and expand upon what I already knew simply by solving leetcode problems.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'll Cover
&lt;/h2&gt;

&lt;p&gt;I'm going to go over everything you'll need to follow along with my process and teach yourself while solving problems. First, I'll cover my set up for solving problems and taking notes. Next We'll discuss the challenge question, and potential scenarios that whiteboarding may cover. We'll talk about the submission and potential re-submission of your personal answers, and then go into ways to expand your knowledge through Google searches and learning from other peoples solutions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting Up for Studying
&lt;/h2&gt;

&lt;p&gt;My setup may be different than what you'll need to do depending on the language you choose to solve problems with. I like to use JavaScript as it's the language I'm most familiar with.&lt;/p&gt;

&lt;p&gt;I start by choosing a leetcode problem. Obviously, we need a problem to solve before we can start solving it. For this exercise, I chose the problem that has the highest clear rate on the site, just to keep things simple. I suggest choosing a variety of questions while studying in order to give yourself a lot of different information to study.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZpLlo0C1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/vjo76gkuyqmv9jco76ea.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZpLlo0C1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/vjo76gkuyqmv9jco76ea.PNG" alt="Alt Text" width="719" height="725"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then, I create a new HTML file with just a head and body in it, and I link to a newly created JavaScript file where I'll write my test functions. In the JS file, I'll copy the problem question, and set up an example set of data, then call it inside of a console.log() so I can view my results in the console. It makes for a quick and easy set up, and I don't have to run my tests or make a bunch of submissions on leetcode itself. It also allows me to use comments to make notes on my work for further studying in the future. Typically, I'll keep notes in comments in the .JS file and I'll save previous work just so I know what I did and why. You may opt to take notes a different way and I suggest you do what works best for you.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DERvaes1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/hkki9xr1kuk0k76lphk9.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DERvaes1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/hkki9xr1kuk0k76lphk9.PNG" alt="Alt Text" width="352" height="324"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Finding a Solution
&lt;/h2&gt;

&lt;p&gt;The first thing I try to do is mimic an interview scenario where I'd be solving a whiteboarding problem. That means, no Google, no books, no personal notes, no material to help whatsoever. I try to come up with my own solution based on my brain and my brain alone. I want to try to solve the problem to the best of my ability at the time. It doesn't have to be the best solution, but I try to get something down without searching. Typically, I'll put a time limit on this though. If I don't have a working example in 30 minutes or so, I'll move on from this step and start googling. If I have an idea but can't finalize it, Google will usually help me bring it home. For this problem I was able to come up with a quick and easy solution luckily.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nCAYBqHo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/vvphfsgsnebxa8toaemk.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nCAYBqHo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/vvphfsgsnebxa8toaemk.PNG" alt="Alt Text" width="720" height="378"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And we can check our results in the console of our browser. I chose to highlight the data set used for testing as well as the result from the solution.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--z50Q-Xx9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/hddu7ms89nlofsbub0m8.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--z50Q-Xx9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/hddu7ms89nlofsbub0m8.PNG" alt="Alt Text" width="207" height="169"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Where to go After Finding a Solution?
&lt;/h2&gt;

&lt;p&gt;For this instance, we're going to assume you solved the problem without needing to look anything up and it's working with the example data from the question. What next?&lt;/p&gt;

&lt;p&gt;Typically during whiteboarding interviews, the interviewer will want you to try to solve the problem with additional sets of data. They may also want to know what you would do giving specific data types being passed in or in various scenarios you may encounter in the real world. Since this is JavaScript and arrays can contain anything and in this instance, we are doing math, we should make sure we're checking that only numerical values are being passed in. We should also cover a case where no data is passed in, though in this instance, it should be unneeded as the for loop will have nothing to loop over if the array is empty.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QnR0ujZq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/dlm9dgrwwn503pbh99qx.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QnR0ujZq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/dlm9dgrwwn503pbh99qx.PNG" alt="Alt Text" width="660" height="215"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Keep in mind, error handling may or may not be necessary for your submission on leetcode. I've encountered problems that have a lot of test scenarios that cause errors, as well as ones such as this that require no error handling at all.&lt;/p&gt;

&lt;h2&gt;
  
  
  Submitting the Solution
&lt;/h2&gt;

&lt;p&gt;Now that a test has been written, tested, and retested, it's time to submit it and see how we did.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LgBlXueY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/21gu2g93y50w8fkorukk.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LgBlXueY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/21gu2g93y50w8fkorukk.PNG" alt="Alt Text" width="800" height="371"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first submission was a success! However, sometimes it's worth checking the performance without error handling if it's not required. So, I tested it a few more times.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0q6mtrn_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/db27uh5bs9oq92rehdvg.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0q6mtrn_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/db27uh5bs9oq92rehdvg.PNG" alt="Alt Text" width="800" height="177"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can note that there's a difference between runtime and memory for the first and second instances. These are both runs without the error handling. It's important to know that leetcode may give you different results for the same code on different runs. So, don't take these results to heart. Since we're studying to become better developers, it's OK if your code is faster than 100% or 0% of other solutions tested. If you've written a successful submission, you're already doing great, and if you haven't, keep going and you'll get there!&lt;/p&gt;

&lt;h2&gt;
  
  
  Expanding our Knowledge
&lt;/h2&gt;

&lt;p&gt;So, it's safe to assume that a successful solution has been submitted. If it hasn't, don't worry, we're going to go digging to learn how other people solved the problem. If you didn't solve it on your own or with Google searches, then this is your chance to learn a few different ways to solve the problem so you can write your own answer next time.&lt;/p&gt;

&lt;p&gt;My first suggestion is always Google. Or Bing. Or DuckDuckGo. Whoever your favorite search engine is, that's the one you should use. You can search for the title of the question, or some information from the description or anything descriptive enough to lead you down the path of finding someone else who's solved a similar problem. Take a look at solutions, questions, errors people encountered, and the answers they received. All of this information can give you insight into 1. what to expect when presented with this problem and 2. what others have done in order to solve the problem.  &lt;/p&gt;

&lt;p&gt;The next thing to do is to check the leetcode "Discuss" tab on the question. This will bring you to a list of discussions from other users. Typically, these posts are questions about the problem, but more often than not, people use this area to share their own solutions. Now comes the tricky part, going on the hunt for other solutions that are different from yours. Usually you'll find several different solutions than what you've come up with.&lt;/p&gt;

&lt;p&gt;I found this map solution by user trueK.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KT3txwBi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/suqhk9aruftsd8zlwz40.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KT3txwBi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/suqhk9aruftsd8zlwz40.PNG" alt="Alt Text" width="268" height="151"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And this reduce solution from user torilov123&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FAju6lb0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/pqg615bmfequz25vbxhu.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FAju6lb0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/pqg615bmfequz25vbxhu.PNG" alt="Alt Text" width="375" height="213"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Both resulting in similar performance to mine, with the map solution by trueK being slightly faster at 68ms.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--InYDwwJV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/a7jw4pxp3srxcpiji64n.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--InYDwwJV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/a7jw4pxp3srxcpiji64n.PNG" alt="Alt Text" width="800" height="173"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What to do With These Other Solutions?
&lt;/h2&gt;

&lt;p&gt;Now that we have some other solutions that we know are working, what do we do with them? Well, that depends on what solutions you've found. If you understand what others have done, you just didn't think of them yourself, then you move on to the next question. However, let's say in this case, you have no idea what the map function does. In that case, it's back to your trusty search engine to explore this function. Find out how it works. Write some working examples with it. Build a small app using it. Do what works best for you to learn this new function. By better understanding it, you'll better understand how to use it to solve problems in the future, thus expanding your knowledge and learning something new, all from having solved a problem on leetcode.com.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Hopefully this gives some insight into how I've been using leetcode to study, and maybe you'll find that this method of studying works great for you as well. It's helped me learn some Computer Science related things I never learned in school, new functions, new sorts, new concepts, and tons of other information about development. For my Google interview, I spent about 4 weeks doing this process every day about 4-7 hours a day and in that time managed to learn a ton of new information. It's a lot of research, a lot of note taking and a whole lot of thinking, but you'll come out learning something new that will help you in the future. &lt;/p&gt;

&lt;p&gt;As a disclaimer, I did not get a job at Google, but I was able to answer one of the two whiteboarding problems they presented me with. I was able to land a job later on at another company where I once again had to go through the whiteboarding process. Studying this way really did help improve my skills as a developer and I found myself reading more and challenging myself more frequently because of it. Like all walks of life, some people on leetcode will not be kind to you, but others will offer real friendly and helpful advice. So don't get too distraught if you have a bad experience one day, you'll have many good ones as well.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
