owned this note
owned this note
Published
Linked with GitHub
###### tags: `JavaScript` `Algo App`
# Algo app examples
## Links
[Codepen to practice: ](https://codepen.io/WolfsVeteran/pen/ExNYyax?editors=0012)
## Breaking down the code
```
var x = [2,4,6];
console.log(x);
```
1. Creating a variable, naming it x, and setting it to an array of numbers
2. Logging out the array
##### Result = [2,4,5]
## Understanding index referances
Given the following code:
```
var y = [1,3,4];
console.log(y[0]);
```
First lets break it down to make sure we understand what is going on
1. Creating a variable, naming it y, and setting it to an array of numbers
2. Logging out index 0 from the array
##### Result = 1
What is index 0?
When looking at an array every item has a assigned index or position. It will always start at 0. So in the above code the item at index 0 is 1
## Using index to change the array
Given the following code:
```
var z [5,6,9];
console.log(z);
z[0] = z[2];
console.log(z);
```
Lets break this down as well
1. Creating a variable, naming it z, and setting it to an array
2. Pulling from the array the 1st index [0] and making it equal to the 3rd index [2]; Or z[0] which is 5 make this equal to z[2] which is 9
3. Logging out the array; In this case it would now print [9,6,9] because we told it to make index 0 equal to index 2
##### Result [5,6,9] and then [9,6,9]
## Using .length
.length is how we say what is the length of a given item. Typically how many index's does an array have
```
var a = [6,24,50];
var b = a.length;
console.log(b);
```
1. Creating a variable, naming it a, and setting it to an array of numbers
2. Creating a new variable, naming it b, and setting it equal to the length of variable a
3. Logging the results of variable b
##### Result 3
Here we would get a result of 3. Because there are 3 items in the array for variable a. If we used the following code would we get the same answer?
```
var c = ["Hello", "There", "Houston"];
var d = c.length;
console.log(d);
```
Well lets break it down
1. Creating a variable, naming it c, and setting it to an array of strings
2. Creating a variable, naming it d, and setting it equal to the length of variable c
3. Logging out the results of variable d
##### Result 3
The breakdown looks the same and since there are 3 words or sets of "" then yes it would print the same thing.
```
var e = ["Hello", "I", "am", 42];
var f = e.length;
console.log(f);
console.log(e);
```
1. Variable e is and array of items.
2. variable f is the length of the e array
3. Log out the results of variable f
4. Log out the array of e
##### Result 4 and then ["Hello", "I", "am", 42]
## Push and Pop
```
var h = [1,3,4,5];
h.push(24);
console.log(h);
```
1. Variable h is an array of 4 items
2. Add 24 to the end of the array
3. log out the whole array
##### Result [1, 3, 4, 5, 24]
Using the same array
```
// h.pop();
console.log(h);
```
1. Remove the last item from the array
2. Log out the whole array
##### Result [1, 3, 4, 5]
Using the same array again
```
h.push(24);
h.push(8);
console.log(h);
h.pop();
console.log(h);
```
1. Add 24 to the end of the array
2. Add 8 to the end of the array
3. Log out the results of the array [1, 3, 4, 5, 24, 8]
4. Remove the last item from the array
5. Log out the results of the array [1, 3, 4, 5, 24]
## If else Statements
```
var counter = 4;
if(counter < 10) {
console.log('less than 10')
} else {
console.log('greater than or equal to 10')
}
```
1. Set counter to 4
2. If counter is less than 10
3. Log out 'Less than 10'
4. Otherwise (NOT OR)
5. Log out 'greater than or equal to 10'
##### Result 'Less than 10'
```
var j = [2,5,7,0,6];
var counter = 2;
if(j[counter] > 10) {
console.log('HI')
} else {
console.log('Goodbye')
}
```
1. Variable j is set to an array
2. Variable counter is set to 2
3. If at index 2 in array j is greater than 10
4. Log 'HI'
5. Otherwise
6. Log 'Goodbye'
##### Result 'Goodbye'