edit-task
Home
Up
Delete
Task Name:
Task Description:
recursion -
TaskGroup ID:
Start Date:
Start Time:
Duration:
Priority:
Status:
To Do
Completed
In Process
Add Photo:
Owner ID:
Content:
use HTML
Edit Content
<h1 style="text-align: center;">CREATE-article# #</h1> <h2>[previously]</h2> <ol> <li><strong>[reference]</strong></li> <ol> <li>] <a href="http://www.codecademy.com/courses/javascript-lesson-205/1/5" target="_blank">http://www.codecademy.com/courses/javascript-lesson-205/1/5</a> </li> <li>] <a href="https://medium.com/functional-javascript/recursion-282a6abbf3c5" target="_blank">https://medium.com/functional-javascript/recursion-282a6abbf3c5</a> </li> </ol> <li><strong>[2015-06-27] NEW task IN tech-dev-sw/pln/language-javascript/</strong><ol> <li>] # 5327 - CREATE-article# # - recursion</li> </ol></li> <li><strong>> [09:00] NEW task IN</strong></li> <ol> <li>i] # 5327 CREATE-article# -recursion</li> <li>i] codecademy tutorial 10/24, +] ref </li> </ol></ol> <h2>[currently]</h2> <ol> <li>] </li> </ol> <h2>[next]</h2> <ol> <li>]</li> </ol> <p> </p> <p><strong>4] selections - recursion</strong></p> <p>> write loop to do something</p> <p>- example multiple num * by 10</p> <p>> put loop into function ( pass in arg)</p> <p> </p> <p><strong>5] recursion </strong></p> <p>function factorial(n) {<br /> if (n === 0) {<br /> return 1;<br /> }<br /> <br /> // This is it! Recursion!!<br /> return n * factorial(n - 1);<br />}</p> <p> </p> <p>There are a few key features of recursion that must be included in order for it to work properly.</p> <p>The first is a <em>base case</em>: this is a statement, usually within a conditional clause like <code class="java"><span class="keyword">if</span></code>, that stops the recursion. If you don't have a base case, the recursion will go on infinitely and your program will crash. Crash = bad.</p> <p>The second is a <em>recursive case</em>: this is the statement where the recursion actually happens: where the recursive function is called on itself.</p> <p>function factorial(n) { <br /> // This is our Base Case - when n is equal to 0, we stop the recursion<br /> if (n === 0) {<br /> return 1;<br /> }<br /> <br /> // This is our Recursive Case<br /> // It will run for all other conditions except when n is equal to 0<br /> return n * factorial(n - 1);<br />}</p> <p> </p> <p><strong>7]</strong> One other useful (and often necessary) feature of a recursive function is <strong>a <em>termination condition</em>.</strong></p> <p>This is a specific statement that will explicitly stop the recursion. The <em>base case</em> is a form of termination condition, though for our purposes here I will use <em>termination condition</em> to describe a statement that will cancel recursion in the event of bad input or other potential error.</p> <p>To put this into practice, look at the factorial function. What would happen if we called it on a negative integer? Since the recursion will only stop when <code class="undefined">n</code> is equal to <code class="undefined">0</code>, and that would never happen with a negative integer, our program would crash.</p> <p>In order to protect against this, we use a <em>termination condition</em> to ensure that the value passed to the function is valid, and will not crash our program. As a programmer, you must constantly be thinking about how to prepare for any type of situation and ensure that your code can handle it correctly.</p> <p>function factorial(n) {<br /> if (n < 0) {<br /> // Termination condition to prevent infinite recursion<br /> console.log("NO negative numbers");<br /> return;<br /> }<br /> // Base case<br /> if (n === 0) {<br /> return 1;<br /> }<br /> // Recursive case<br /> return n * factorial(n -1);<br />}</p> <p>factorial(-1);<br />factorial(5);</p> <p><strong>8] arguements in recursion</strong></p> <p>When building your recursive case (the code that will be repeated), one of the rules of thumb is to <em>ensure that the arguments you use for the recursion will lead to your base case</em>.</p> <p>If the value we pass to the recursive function call is the same as the initial value, chances are our code will enter an infinite loop. And then, crash. Bad.</p> <p>So, the question you have to ask yourself is "does the recursive case modify my arguments in such a way that each recursion brings it one step closer to the base case?"</p> <p> </p> <p> // What's wrong with this picture? Why won't this recursion work?<br /> return n * factorial(n);</p> <p>// CORRECTED </p> <p>return n * factorial(n-1);</p> <p><strong> 9] try from scratch</strong> </p> <p>Now that we've covered the bare essentials, try rebuilding the <code class="undefined">factorial()</code> function we've been working on, but this time write it all from scratch. To help you along, here are five questions that you can use whenever using recursion in your code:</p> <ul> <li>What is/are the <em>base case(s)</em>?</li> <li>What is/are the <em>recursive case(s)</em>?</li> <li>Have I included any other necessary <em>termination condition(s)</em>?</li> <li>Do the statements in the function lead to the base case?</li> <li>Does the recursion build on the base case until the desired result is returned by the function?</li> </ul> <p><strong>// recursive function with arg</strong><br />function factorial(n) {<br /> console.log(n);<br /><strong>// termination condition</strong><br /> if ( n < 1){<br /> console.log("no negative numbers")<br /> }<br /><strong> // base case</strong><br /> if (n === 0) {<br /> return 1; <br /> }<br /><strong> // return </strong> <br /> // ERROR return factorial(n-1);</p> <p>return n * factorial(n-1);</p> <p><br />}</p> <div class="lesson-checkpoint__name"><strong>10] Iterative recursion</strong></div> <div class="article__content"> <p>To start deconstructing recursion, let's first take a look at the difference between loops and recursive functions. In many situations, they are effectively interchangeable.</p> <p>In fact, many programming languages don't have loops like <code class="java"><span class="keyword">for</span></code> or <code class="java"><span class="keyword">while</span></code>, but use recursion for everything.</p> <p>In this example, each function does the same thing. One uses a loop, the other uses recursion.</p> </div> <p> </p> <p> </p>