Added old URL paths as aliases and cleaned up some code sections of old posts

This commit is contained in:
Brandon Rozek 2020-11-07 23:09:16 -05:00
parent 8e175e60e4
commit b86c103ade
27 changed files with 1023 additions and 966 deletions

View file

@ -5,6 +5,8 @@ date: 2015-10-25T13:48:41+00:00
author: Brandon Rozek
layout: post
guid: https://brandonrozek.com/?p=350
aliases:
- /2015/10/functions/
permalink: /2015/10/functions/
medium_post:
- 'O:11:"Medium_Post":11:{s:16:"author_image_url";N;s:10:"author_url";N;s:11:"byline_name";N;s:12:"byline_email";N;s:10:"cross_link";N;s:2:"id";N;s:21:"follower_notification";N;s:7:"license";N;s:14:"publication_id";N;s:6:"status";N;s:3:"url";N;}'
@ -27,33 +29,33 @@ Ever had a snippet of code that appears multiple times in different places in yo
To make a function
<pre><code class="language-javascript">
```javascript
var doSomething = function() {
doStuff;
}
</code></pre>
```
To call the above function to execute
<pre><code class="language-javascript">
```javascript
doSomething();
</code></pre>
```
### <a href="#arguments" name="arguments"></a>Arguments {#arguments}
You can also add in arguments (parameters that go inside the paraenthesis next to the word function) for the functions to use.
<pre><code class="language-javascript">
```javascript
var add = function(number1, number2) {
return number1 + number2;
}
</code></pre>
```
And when you use the `return` keyword, like the function above. You can store the value in a variable for future use.
<pre><code class="language-javascript">
```javascript
var total = add(1, 3);
</code></pre>
```
<code class="language-javascript">total</code> here will equal `4`
@ -63,16 +65,16 @@ Functions create their own scope, which means that variables created inside the
The snippet below will output an error like <code class="language-javascript">total is not defined</code>
<pre><code class="language-javascript">
```javascript
var add = function(number1, number2) {
var total = number1 + number2;
}
console.log(total);
</code></pre>
```
Below is a correct example of the concept
<pre><code class="language-javascript">
```javascript
//Function below converts km/hr to m/s
var convert = function(speed) {
var metersPerHour = speed * 1000;
@ -80,13 +82,13 @@ Below is a correct example of the concept
return metersPerSecond;
}
var currentSpeed = convert(5);
</code></pre>
```
Its also important to note that functions can use variables outside of it; granted it resides in the same scope.
Here is an example of a variable that doesn&#8217;t reside in the same scope as the function. (The below code will fail)
<pre><code class="language-javascript">
```javascript
var something = function() {
var x = 5;
var y = 2;
@ -96,17 +98,17 @@ Here is an example of a variable that doesn&#8217;t reside in the same scope as
console.log(x + y);
}
addXandY();
</code></pre>
```
Below, is an example of where the variable does reside in the same scope as the function. Which allows this snippet to execute properly.
<pre><code class="language-javascript">
```javascript
var x = 5;
var addX = function(a) {
return a + x;
}
var sum = addX(6);
</code></pre>
```
<code class="language-javascript">sum</code> here will equal <code class="language-javascript">11</code>