Algo app examples
Links
Codepen to practice:
Breaking down the code
- Creating a variable, naming it x, and setting it to an array of numbers
- Logging out the array
Result = [2,4,5]
Understanding index referances
Given the following code:
First lets break it down to make sure we understand what is going on
- Creating a variable, naming it y, and setting it to an array of numbers
- 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:
Lets break this down as well
- Creating a variable, naming it z, and setting it to an array
- 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
- 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
- Creating a variable, naming it a, and setting it to an array of numbers
- Creating a new variable, naming it b, and setting it equal to the length of variable a
- 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?
Well lets break it down
- Creating a variable, naming it c, and setting it to an array of strings
- Creating a variable, naming it d, and setting it equal to the length of variable c
- 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.
- Variable e is and array of items.
- variable f is the length of the e array
- Log out the results of variable f
- Log out the array of e
Result 4 and then ["Hello", "I", "am", 42]
Push and Pop
- Variable h is an array of 4 items
- Add 24 to the end of the array
- log out the whole array
Result [1, 3, 4, 5, 24]
Using the same array
- Remove the last item from the array
- Log out the whole array
Result [1, 3, 4, 5]
Using the same array again
- Add 24 to the end of the array
- Add 8 to the end of the array
- Log out the results of the array [1, 3, 4, 5, 24, 8]
- Remove the last item from the array
- Log out the results of the array [1, 3, 4, 5, 24]
If else Statements
- Set counter to 4
- If counter is less than 10
- Log out 'Less than 10'
- Otherwise (NOT OR)
- Log out 'greater than or equal to 10'
Result 'Less than 10'
- Variable j is set to an array
- Variable counter is set to 2
- If at index 2 in array j is greater than 10
- Log 'HI'
- Otherwise
- Log 'Goodbye'
Result 'Goodbye'