{"id":2199,"date":"2020-04-06T15:02:55","date_gmt":"2020-04-06T15:02:55","guid":{"rendered":"http:\/\/newmediaproduction.mynmi.net\/?page_id=2199"},"modified":"2022-07-02T02:35:52","modified_gmt":"2022-07-02T02:35:52","slug":"javascript-5-fcc-21","status":"publish","type":"page","link":"https:\/\/nmi.cool\/web\/javascript-5-fcc-21\/","title":{"rendered":"JavaScript 5 &#8211; FCC 20"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"2560\" height=\"1706\" src=\"https:\/\/nmi.cool\/newmediaproduction\/wp-content\/uploads\/sites\/3\/2020\/04\/david-streit-BumOnw4oEZo-unsplash-scaled-1.jpg\" alt=\"\" class=\"wp-image-2201\" srcset=\"https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/04\/david-streit-BumOnw4oEZo-unsplash-scaled-1.jpg 2560w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/04\/david-streit-BumOnw4oEZo-unsplash-scaled-1-300x200.jpg 300w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/04\/david-streit-BumOnw4oEZo-unsplash-scaled-1-1024x682.jpg 1024w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/04\/david-streit-BumOnw4oEZo-unsplash-scaled-1-768x512.jpg 768w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/04\/david-streit-BumOnw4oEZo-unsplash-scaled-1-1536x1024.jpg 1536w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/04\/david-streit-BumOnw4oEZo-unsplash-scaled-1-2048x1365.jpg 2048w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/04\/david-streit-BumOnw4oEZo-unsplash-scaled-1-1568x1045.jpg 1568w\" sizes=\"auto, (max-width: 2560px) 100vw, 2560px\" \/><figcaption>Photo by&nbsp;<a href=\"https:\/\/unsplash.com\/@daviidstreit?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\">David Streit<\/a>.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Understanding Loops <\/h2>\n\n\n\n<p>Loops allow you to run the same code block multiple times until a desired result is reached. They are a kind of short hand, so that you don&#8217;t have to write the same code block over and over again. With a loop, you&#8217;re basically saying: when this condition is true, do this thing over and over again until the condition is not true. Here&#8217;s a diagram to help illustrate what we mean: <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1411\" height=\"783\" src=\"https:\/\/nmi.cool\/newmediaproduction\/wp-content\/uploads\/sites\/3\/2020\/04\/Screen-Shot-2020-04-07-at-11.35.16-AM.png\" alt=\"\" class=\"wp-image-2216\" srcset=\"https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/04\/Screen-Shot-2020-04-07-at-11.35.16-AM.png 1411w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/04\/Screen-Shot-2020-04-07-at-11.35.16-AM-300x166.png 300w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/04\/Screen-Shot-2020-04-07-at-11.35.16-AM-1024x568.png 1024w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/04\/Screen-Shot-2020-04-07-at-11.35.16-AM-768x426.png 768w\" sizes=\"auto, (max-width: 1411px) 100vw, 1411px\" \/><figcaption>Loop flow chart.<\/figcaption><\/figure>\n\n\n\n<p>There are several common loop statements that we&#8217;ll look at, including the <code>while<\/code> loop, the <code>for<\/code> loop and the <code>do-while<\/code> loop. <\/p>\n\n\n\n<p>The best way to get a handle on this is to look at some examples, so stick with us if this feels a little abstract right now. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>while<\/code> loop<\/h3>\n\n\n\n<p>The first loop we will look at is a <code>while<\/code> loop.  Let&#8217;s start out by breaking down the basic syntax of a <code>while <\/code>statement. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">while(condition){\n  \/\/code block to be executed\n}<\/pre>\n\n\n\n<p>Just like in the diagram above, we&#8217;ll start by evaluating the <code>condition<\/code>. The <code>condition<\/code> is a boolean statement, so it will either be <code>true<\/code> or <code>false<\/code>. If the statement is <code>true<\/code>, the code in the body (the bit between the two curly braces) will be executed; if the statement is <code>false<\/code> the loop terminates and the code in the body isn&#8217;t executed. The loop will repeat (or iterate) over and over again until the condition is false. At that point, the loop will terminate, and you&#8217;ll move on to the next bit of code. <\/p>\n\n\n\n<p>Here&#8217;s an example with some code: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var loopArray = [];\nvar b = 0;\n\nwhile(b &lt; 5) {\n  loopArray.push(b);\n  b++;\n}<\/pre>\n\n\n\n<p>This is telling us that we start with an empty array called <code>loopArray<\/code> and a variable <code>b<\/code> that has the value of <code>0<\/code>. The <code>condition<\/code> in the <code>while<\/code> loop asks if <code>b<\/code> is less than <code>5<\/code>. If that&#8217;s true, we will execute the body of the loop, which tells us to push the current value of <code>b<\/code> into the array (<code>loopArray.push(b)<\/code>). Then, the value of <code>b<\/code> will be incremented (<code>b++<\/code>).<a href=\"#footnote-1-2199\" id=\"note-1-2199\" rel=\"footnote\">1<\/a> <\/p>\n\n\n\n<p>This is where things get&#8230; loopy. We will continue to execute that code, as long as the condition returns a value of <code>true<\/code>, or, in other words, as long as <code>b<\/code> is less than<code> 5<\/code>. <\/p>\n\n\n\n<p>Once <code>b<\/code> equals <code>5<\/code>, the loop will terminate and we will move on to whatever bit of code is next. When the <code>while<\/code> loop is complete, <code>loopArray<\/code> will be <code>[0, 1, 2, 3, 4]<\/code>.   <\/p>\n\n\n\n<p>It can some times be a bit tricky to visualize what&#8217;s happening in a loop so you might find it helpful to draw a quick chart. The idea here is that you&#8217;ll start with three columns, one for the array, the variable value, and the condition statement.  Fill it in with what we know about the loop:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>What is in the loopArray?<\/td><td>What is the value of the variable?<\/td><td> State of the Condition<\/td><\/tr><tr><td><code>loopArray<\/code><\/td><td><code>b<\/code><\/td><td><code>b &lt; 5<\/code><\/td><\/tr><tr><td>We start with an empt array: []<\/td><td>0<\/td><td> 0 is less than 5: true! <\/td><\/tr><\/tbody><\/table><figcaption>This is how we can start a variable table&#8230; keep going on your own!<\/figcaption><\/figure>\n\n\n\n<p>On each iteration, you&#8217;ll evaluate the <code>condition<\/code> in the third column of the chart, then you&#8217;ll execute the body by adding a new value to the array in the first column, and then update the value of the variable <code>b<\/code> in the middle column. Once finished we can clearly see where the loop terminates and what the array contains when that happens. This might seem a little tedious, but it can actually be a really helpful way to visualize what&#8217;s happening inside a <code>loop<\/code> statement! <\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><code>loopArray<\/code><\/td><td><code>b<\/code><\/td><td><code>b &lt; 5<\/code><\/td><\/tr><tr><td><code>[]<\/code><\/td><td><code>0<\/code><\/td><td><code>true<\/code><\/td><\/tr><tr><td><code>[0]<\/code><\/td><td><code>1<\/code><\/td><td><code>true<\/code><\/td><\/tr><tr><td><code>[0, 1]<\/code><\/td><td><code>2<\/code><\/td><td><code>true<\/code><\/td><\/tr><tr><td><code>[0, 1, 2]<\/code><\/td><td><code>3<\/code><\/td><td><code>true<\/code><\/td><\/tr><tr><td><code>[0, 1, 2, 3]<\/code><\/td><td><code>4<\/code><\/td><td><code>true<\/code><\/td><\/tr><tr><td><code>[0, 1, 2, 3, 4]<\/code><\/td><td><code>5<\/code><\/td><td><code>false<\/code><\/td><\/tr><\/tbody><\/table><figcaption>A complete variable table.<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><code>do... while<\/code> loop<\/h3>\n\n\n\n<p>This loop is very similar to the the <code>while<\/code> loop, but instead of first checking to see if the <code>condition<\/code> is met before you execute the code block, you&#8217;ll execute the code block first&#8211;<strong>just the first time<\/strong>&#8211;and then check the condition to decide if you should continue to execute the code block.  It&#8217;s basically saying <em><strong>do<\/strong><\/em> the code block first. Here&#8217;s an example: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var ourArray = [];\nvar b = 0;\ndo {\n  ourArray.push(b);\n  b++;\n} while (b &lt; 5);<\/pre>\n\n\n\n<p>Let&#8217;s break that down. We start off with an empty array called <code>ourArray<\/code> and a variable <code>b<\/code> that has the value of <code>0<\/code>. The first thing we&#8217;re going to do is execute the code block; we&#8217;ll push b (which has the value of <code>0<\/code>) into <code>ourArray<\/code> and then we will increment it by 1. That means that now <code>b = 1<\/code>. <\/p>\n\n\n\n<p>You&#8217;ll notice that we are always going to execute the code block&#8211; at least once&#8211; with a <code>do... while<\/code> loop. By executing the code block, our array has been updated and the value for <code>b<\/code> has changed. This is different than a regular <code>while<\/code> loop because with a regular <code>while<\/code> loop we will only execute the code block if the condition is true, so it&#8217;s possible, if the condition is false, that we will never execute the code block.  With a <code>do... while<\/code> loop we will always execute it once. <\/p>\n\n\n\n<p>So we start off by executing the code block, but what next? Now, we will evaluate the <code>condition<\/code> and ask if it is <code>true<\/code> or <code>false<\/code>. Remember, our value for <code>b<\/code> was updated, so now we&#8217;re going to ask if <code>b<\/code> (which has a value of <code>1<\/code>) is less than <code>5<\/code>. That is true, so we will execute the code block a second time, and repeat the process of checking the <code>condition<\/code> and executing the code block until the <code>condition<\/code> is no longer true (when <code>b<\/code> has the value of <code>5<\/code>). When we&#8217;re finished, ourArray will have <code>[0, 1, 2, 3, 4]<\/code>.  <\/p>\n\n\n\n<p>Again, the benefit of the <code>do... while<\/code> loop is that it gives you the opportunity to execute the code block once before iterating through the loop. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>for<\/code> loop<\/h3>\n\n\n\n<p>A <code>for<\/code> loop will feel familiar after you get comfortable with a <code>while<\/code> loop. A <code>for<\/code> loop will run <em>for<\/em> a set amount of time and we will pass it three specific instructions. Free Code Camp refers to them as the <code>initialization<\/code>, the <code>condition<\/code>, and the <code>final-expression<\/code>. <\/p>\n\n\n\n<p>The <code>initialization<\/code> is basically the set up for the loop; it&#8217;s where we might set the variable before the loop starts. <\/p>\n\n\n\n<p>The <code>condition<\/code> statement tells you what needs to be true in order for the loop to run. If the <code>condition<\/code> is true, the code block will be executed, but it the <code>condition<\/code> is false, the loop will be terminated. Just like before, we will revaluate the <code>condition<\/code> each time the loop runs. <\/p>\n\n\n\n<p>The <code>final-expression<\/code> is usually used to increment or decrement the loop count; you might also see it called an update statement. The <code>final-expression<\/code> should be executed at the end of each loop, before you check the <code>condition<\/code> statement again. <\/p>\n\n\n\n<p>It&#8217;s easiest to understand this with an example.  Here&#8217;s the syntax of a <code>for<\/code> loop: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for (initialization; condition; final-expression){\n\/\/code block to be executed\n}<\/pre>\n\n\n\n<p>The <code>initialization<\/code>, <code>condition<\/code>, and <code>final-expression<\/code> are separated by semicolons inside the parenthesis. Aside from that, the <code>for<\/code> loop looks similar to the <code>while<\/code> loop. Here&#8217;s an example with code: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>var ourArray = [];<\/code>\n<code> <\/code>\n<code>for (var i = 0; i &lt; 3; i++) { <\/code>\n   <code>ourArray.push(i); <\/code>\n<code>}<\/code><\/pre>\n\n\n\n<p>What we&#8217;re seeing is an empty array called <code>ourArray<\/code>. The <code>initialization<\/code> statement in the <code>for<\/code> loop introduces the variable <code>i<\/code> and sets it to <code>0<\/code>, then the <code>condition<\/code> statement reads that <code>i<\/code> has to be less than <code>3<\/code> in order for the code block to execute.  Since it is true that <code>0<\/code> is less than <code>3<\/code> we will execute the code block, which asks us to  push the value of <code>i<\/code> into the array. Next the <code>final-expression<\/code> asks us to increment <code>i<\/code> by  <code>1<\/code>. We&#8217;ll repeat the <code>for<\/code> statement, but this time <code>i<\/code> will equal <code>1<\/code>. We will continue to repeat this loop process until  <code>i &lt; 3<\/code> is no longer true. When we are finished, the array will contain <code>[0, 1, 2]<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Iterating through an Array<\/h2>\n\n\n\n<p>In the examples we have been looking at so far, we have been adding values to empty arrays. But often we&#8217;ll want to iterate through the contents of the array&#8211; or, in other words, we will want to access the array elements one at a time. The <code>for<\/code> loop can help us with that task. Let&#8217;s look at an example and then we will unpack it: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var arr = [7, 6, 5, 4, 3];\nfor (var a = 0; a &lt; arr.length; a++) {\n  console.log(arr[a]);\n}<\/pre>\n\n\n\n<p>This is a very common <code>for<\/code> loop so it&#8217;s a good idea to know what&#8217;s going on inside it. <\/p>\n\n\n\n<p>First we have an array with five values&#8211; simple enough! Then, we have our <code>for<\/code> loop. Inside, it&#8217;s saying that <code>var a = 0<\/code> (that&#8217;s our <code>initialization<\/code>), then we have the <code>condition<\/code> that we have to evaluate: <code>a &lt; arr.length<\/code>. It&#8217;s been a minute since we&#8217;ve seen something that looks like <code>arr.length<\/code>, so let&#8217;s unpack it: <code>arr.length<\/code> is the length of the array, or the number of values in it. In other words, the condition is checking if  the value for <code>a<\/code> is less than the length of the array. We&#8217;ve got five values in the array so <code>a<\/code> has to be less than 5. So at this point we have to ask ourselves if <code>0 &lt; 5<\/code>, and it is&#8211; so then we can execute the code block.  <\/p>\n\n\n\n<p>The code block is asking us to look at the contents of the array and log whatever value is indexed at <code>a<\/code>. We&#8217;ve already established that <code>a = 0<\/code>, so we will log the value of the array at index <code>0<\/code><a href=\"#footnote-2-2199\" id=\"note-2-2199\" rel=\"footnote\">2<\/a> so the value that we will log is <code>7<\/code> (the first value in the array). <\/p>\n\n\n\n<p>Next we need to address the <code>final-expression<\/code> statement, which asks us to add one to <code>a<\/code>. When we do that, we update the value of <code>a<\/code> to <code>1<\/code>. Now we&#8217;ll go through the condition again (<code>1 &lt; 5<\/code> is still true), and execute the code block again (it will return a value of <code>6<\/code>)<a href=\"#footnote-3-2199\" id=\"note-3-2199\" rel=\"footnote\">3<\/a> and so on, until the condition is no longer true; in other words, until <code>a<\/code> is no longer less than the length of the array.  Eventually, we will log the entire array (<code>arr = [7, 6, 5, 4, 3]<\/code>) which could be really useful if we weren&#8217;t sure what was in the array. <\/p>\n\n\n\n<p>Sometimes you may want to iterate through the loop backwards (i.e.  back to front) instead of forwards. This is another common use of loops that you will encounter.  Let&#8217;s look at an example: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>var ourArray = [];<\/code>\n\n<code>for (var i = 0; i &lt; 3; i++) {<\/code>\n<code>ourArray.push(i);<\/code>\n<code>}<\/code><\/pre>\n\n\n\n<p>You will see that the initialization statement says that <code>var i = 0<\/code> and the looping condition says that <code>i &lt; 3<\/code>. We can build out a variable table to help visualize this&#8211; here&#8217;s what it looks like when finished: <\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>ourArray<\/td><td><code>i<\/code><\/td><td><code>i &lt; 3<\/code><\/td><\/tr><tr><td><code>[]<\/code><\/td><td><code>0<\/code><\/td><td>true<\/td><\/tr><tr><td><code>[0]<\/code><\/td><td><code>1<\/code><\/td><td>true<\/td><\/tr><tr><td><code>[0, 1]<\/code><\/td><td><code>2<\/code><\/td><td>true<\/td><\/tr><tr><td><code>[0, 1, 2]<\/code><\/td><td><code>3<\/code><\/td><td>false<\/td><\/tr><\/tbody><\/table><figcaption>A complete variable table.<\/figcaption><\/figure>\n\n\n\n<p>Each row on the table corresponds to an iteration of the loop: so when we start the loop the array is empty, <code>i is 0<\/code>, and the conditional statement is true, so we&#8217;ll execute the code block by pushing <code>0<\/code> to the array and then incrementing <code>i<\/code> by <code>1<\/code>. Now we can start the loop over again, and move through the table. <\/p>\n\n\n\n<p>If you look at the &#8220;<code>i<\/code>&#8221; column on the table you can see that the order that we added our elements into the array was (<code>0, 1, 2<\/code>), but if we&#8217;re trying to iterate through the loop backwards we will want to add them like this: (<code>2 , 1, 0<\/code>) . If we look at the &#8220;<code>i<\/code>&#8221; column in the variable table from last row to first, we recognize the order we actually wanted. So, we can use the same loop body (pushing <code>i<\/code> into the array each iteration), except we&#8217;ll start with <code>i = 3<\/code> and continued through until <code>i = 0<\/code>, then we would have achieved our goal! Let&#8217;s look at how we can achieve that: <br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var ourArray = [];\n  for (var i = 3; i = 0; i--) {\n  ourArray.push(i);\n}<\/pre>\n\n\n\n<p>Notice how our initialization condition says that <code>i<\/code> starts at <code>3<\/code> instead of <code>0<\/code>, our looping condition says to loop until <code>i<\/code> is equal to   <code>0<\/code>, and we are decrementing <code>i<\/code> instead of incrementing it. This will ultimately give us an array in descending or backward order. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Recursion<\/h2>\n\n\n\n<p>Recursion is a slightly advanced programming concept that turns up in our Free Code Camp lessons.  It can be a bit frustrating to grasp, but it&#8217;s really just a function that calls itself. <\/p>\n\n\n\n<p>You can replace <code>for<\/code> loops with a recursive version of the same function, which can make your code easier to read. It&#8217;s important to note that the outcome will be the same if you&#8217;re using loops or using recursion&#8211; it&#8217;s really just a more elegant. <\/p>\n\n\n\n<p>To give you a hand with this concept, check out <a href=\"https:\/\/www.freecodecamp.org\/news\/quick-intro-to-recursion\/\" target=\"_blank\" rel=\"noreferrer noopener\">A Quick Intro to Recursion in JavaScript<\/a> by our friends at Free Code Camp. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Random Number Generators<\/h2>\n\n\n\n<p>Next we are going to take a look at a handy JavaScript function that we can use to generate random fractions: <code>Math.random()<\/code>.  <\/p>\n\n\n\n<p>If you run a value through <code>Math.random()<\/code> you will get a random decimal value<a href=\"#footnote-4-2199\" id=\"note-4-2199\" rel=\"footnote\">4<\/a> between <code>0<\/code> (inclusive) and <code>1<\/code> (exclusive), meaning that the value you get from <code>Math.random()<\/code> might be as low as <code>0<\/code> but will never be as high as <code>1<\/code>.<\/p>\n\n\n\n<p>With a little bit of work you can even use the random number generator function to generate random integers<a href=\"#footnote-5-2199\" id=\"note-5-2199\" rel=\"footnote\">5<\/a>.<\/p>\n\n\n\n<p>To get a random number greater than <code>1<\/code>, you can multiply the function by an integer<a href=\"#footnote-6-2199\" id=\"note-6-2199\" rel=\"footnote\">6<\/a>. Here&#8217;s what that would look like: <code>Math.random() * 10<\/code>. <\/p>\n\n\n\n<p>The result would be a random number with a decimal that could be anything between <code>0<\/code> and just under <code>10<\/code>.<a href=\"#footnote-7-2199\" id=\"note-7-2199\" rel=\"footnote\">7<\/a><\/p>\n\n\n\n<p>To convert that number into an integer, we will use the <code>Math.floor<\/code> function, which converts the value to an integer by rounding down to the closest integer. Here&#8217;s what it would all look like put together: <code>Math.floor(Math.random() * 10)<\/code> <\/p>\n\n\n\n<p>So if <code>Math.random() * 10<\/code> was equal to <code>8.2<\/code>, then <code>Math.floor(Math.random() *10)<\/code> would be equal to 8. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Generate a Random Number from a Range<\/h3>\n\n\n\n<p>Lastly, we can also generate a random number that falls within a specific range. This might look a little complicated at first, but stick with it! Here&#8217;s the formula: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Math.floor(Math.random() * (max - min + 1)) + min<\/pre>\n\n\n\n<p>The easiest way to get a handle on this is to see it in action. We&#8217;ll use that formula in the body of a function to generate a random number between <code>2<\/code> and <code>15<\/code>. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function randomRange(min, max) {\nreturn Math.floor(Math.random() * (max - min + 1)) + min\n}\nvar randomNum = randomRange(2, 15)<\/pre>\n\n\n\n<p>Okay, so here we have a function called <code>randomRange<\/code> that has two parameters: a <code>min<\/code> and a <code>max<\/code>. We want our variable <code>randomNum<\/code> to equal the output of <code>randomRange<\/code> when we pass the arguments <code>2<\/code> and <code>15<\/code> through the function. This will generate a random number between 2 and 15.<\/p>\n\n\n\n<p>Let&#8217;s walk through the rest of the function step by step. First will fill in the formula with our min and max values. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">return Math.floor(Math.random() * (15 - 2 + 1)) + 2<\/pre>\n\n\n\n<p>We&#8217;ll start by solving the inner most parentheses first: <code>(15 - 2 + 1)<\/code> is equal to <code>14<\/code>. <code>Math.random<\/code> will generate a random decimal&#8211; let&#8217;s say it gives us <code>.2<\/code>. We will multiply that by <code>14<\/code> which gives us <code>2.8<\/code>.  Next we&#8217;ll use <code>Math.floor<\/code> to turn that into an integer by rounding down, so it becomes <code>2<\/code>. Lastly, we will add that value to our minimum (which also happens to be <code>2<\/code>) to complete the function, which gives us <code>4<\/code>. In other words, <code>var randomNum = 4<\/code>. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Parsing Strings to Integers<\/h2>\n\n\n\n<p>The <code>parseInt()<\/code> function allows you to turn a string into a number.<a href=\"#footnote-8-2199\" id=\"note-8-2199\" rel=\"footnote\">8<\/a> For example: <code>var b = parseInt(\"0028\")<\/code> would give you the number <code>28<\/code>. Here&#8217;s another one: <code>var b = parseInt(\"44\")<\/code>. This converts the string <code>\"44<\/code>&#8221; to the number <code>44<\/code>. <\/p>\n\n\n\n<p>Why might we want to do this? Remember, we can only preform mathematical operations on numbers; we can&#8217;t perform them on strings, even if the string looks like a number.<\/p>\n\n\n\n<p>What would happen if we try to run a string that didn&#8217;t &#8220;look like&#8221; a number through the <code>parseInt()<\/code>? It would return <code>NaN<\/code>, which stands for &#8220;not a number.&#8221; So, <code>parseInt(\"bananas\")<\/code> would return <code>NaN<\/code>. <\/p>\n\n\n\n<p>You&#8217;ve actually already seen examples of the <code>parseInt()<\/code> function without even realizing it : <code>5==\"5\"<\/code> is true because Javascript is automatically parsing the string <code>\"5\"<\/code> into the number <code>5<\/code> for you! If you want you can even parse strings which represent numbers in various different bases (beyond base 10) by passing another argument to the <code>parseInt()<\/code> function called a <code>radix<\/code>, though that&#8217;s a bit advanced for what we&#8217;re covering here&#8230;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Conditional (Ternary) Operator<\/h2>\n\n\n\n<p>There is a special operator in Javascript called the conditional operator &#8211; or some times the ternary operator. Do you know what unary means? It means consisting of a single element. Binary? Consisting of two elements. What about ternary? If you guessed consisting of three elements you&#8217;re right! The ternary operator takes three elements and provides one result. Here&#8217;s the syntax for a ternary operator: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">condition ? val1 : val2 <\/pre>\n\n\n\n<p>It can be helpful to think of the ternary operator as related to the <code>else if<\/code> statement, because it&#8217;s ultimately doing the same thing: it&#8217;s saying if a condition is true return <code>val 1<\/code>, else if a condition is false return <code>val 2<\/code>. <\/p>\n\n\n\n<p>The <code>condition<\/code> of a ternary statement can be any boolean statement (something that returns a boolean value). If this sounds familiar to you, it&#8217;s most likely because the ternary operator is just a short way of saying this: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">if (condition) return val1;\nelse return val2;<\/pre>\n\n\n\n<p>You can actually nest multiple ternary statements together so that if the first condition is false, you&#8217;ll evaluate a second condition rather than just returning a value. Here&#8217;s what that might look like: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function findGreaterOrEqual(a, b) { \nreturn (a === b) ? \"a and b are equal\" \n  : (a &gt; b) ? \"a is greater\" \n  : \"b is greater\"; }<\/pre>\n\n\n\n<p>In this example we are asking if a is strictly equal to b. If it is, we return the value &#8220;a and b are equal.&#8221; If it&#8217;s not, we look at a second condition: is a greater than b? If it is, we return &#8220;a is greater.&#8221; It&#8217;s it&#8217;s not, we return &#8220;b is greater.&#8221; <\/p>\n\n\n\n<p><\/p>\n<div class=\"footnotes\"><hr \/><ol><li id=\"footnote-1-2199\" class=\"footnote\"><p>Note: we can have multiple lines of code in the body of a loop statement! Just make sure that each line ends with a semicolon.<a href=\"#note-1-2199\" class=\"footnote-return\">&#8617;<\/a><\/p><\/li><!--\/#footnote-1.footnote--><li id=\"footnote-2-2199\" class=\"footnote\"><p>Remember, we start indexing arrays at zero instead of one, because computers count from zero<a href=\"#note-2-2199\" class=\"footnote-return\">&#8617;<\/a><\/p><\/li><!--\/#footnote-2.footnote--><li id=\"footnote-3-2199\" class=\"footnote\"><p>Again, when we&#8217;re indexing arrays, we start counting at zero. So when <code>a = 1<\/code> we&#8217;re actually looking for the second value in the array, which is <code>6<\/code>.<a href=\"#note-3-2199\" class=\"footnote-return\">&#8617;<\/a><\/p><\/li><!--\/#footnote-3.footnote--><li id=\"footnote-4-2199\" class=\"footnote\"><p>Also called a float value<a href=\"#note-4-2199\" class=\"footnote-return\">&#8617;<\/a><\/p><\/li><!--\/#footnote-4.footnote--><li id=\"footnote-5-2199\" class=\"footnote\"><p>Quick math refresher: whole numbers are integers. Numbers with a decimal place are not integers. I know, it&#8217;s been a minute.<a href=\"#note-5-2199\" class=\"footnote-return\">&#8617;<\/a><\/p><\/li><!--\/#footnote-5.footnote--><li id=\"footnote-6-2199\" class=\"footnote\"><p> This is called scaling.<a href=\"#note-6-2199\" class=\"footnote-return\">&#8617;<\/a><\/p><\/li><!--\/#footnote-6.footnote--><li id=\"footnote-7-2199\" class=\"footnote\"><p> Why couldn&#8217;t it be <code>10<\/code>? Because Math.random generates a decimal value that is less than <code>1<\/code>.<a href=\"#note-7-2199\" class=\"footnote-return\">&#8617;<\/a><\/p><\/li><!--\/#footnote-7.footnote--><li id=\"footnote-8-2199\" class=\"footnote\"><p>Remember that strings and numbers are two different data types. Strings will always be in quotation marks.<a href=\"#note-8-2199\" class=\"footnote-return\">&#8617;<\/a><\/p><\/li><!--\/#footnote-8.footnote--><\/ol><\/div><!--\/#footnotes-->","protected":false},"excerpt":{"rendered":"<p>Understanding Loops Loops allow you to run the same code block multiple times until a desired result is reached. They are a kind of short hand, so that you don&#8217;t have to write the same code block over and over again. With a loop, you&#8217;re basically saying: when this condition is true, do this thing &hellip; <a href=\"https:\/\/nmi.cool\/web\/javascript-5-fcc-21\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">JavaScript 5 &#8211; FCC 20<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2199","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/pages\/2199","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/comments?post=2199"}],"version-history":[{"count":3,"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/pages\/2199\/revisions"}],"predecessor-version":[{"id":3693,"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/pages\/2199\/revisions\/3693"}],"wp:attachment":[{"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/media?parent=2199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}