Learning JavaScript
HTML Tables v. 2D Arrays:
| Name | Zip Code | |
|---|---|---|
| Coley | KP | Junghyung |
| glg15@email.com | talia1950@email.com | winterbear1997@gcf.com |
| 08215 | 06218 | 09129 |
let contactList = [
["Coley", "glg15@email.com", "08215"],
["KP", "talia1950@email.com", "06218"],
["Junghyung, "winterbear1997@gcf.com, "09129"]
]
Finding with find() and findIndex():
let favoriteAnimals = ["cat", "dog", "pig", "horse", "yak", "sea lion"];
let moreThanFourLetterAnimal = favoriteAnimals.find(animal => animal[4]);
console.log(moreThanFourLetterAnimal); //OUTPUT: horse
let locationAnimal = favoriteAnimals.findIndex(animal => animal[4]);
console.log(locationAnimal); //OUTPUT: 3
//The code within the parenthesis is a callback function (animal => animal[4])
Let's break this down:
- Create an array called 'favoriteAnimals' to hold a list of favorite animals.
- Declare a new variable 'moreThanFourLetterAnimal' to use .find(animal => animal[4]).
- Log 'moreThanFourLetterAnimal' to the console. This returns 'horse' because it is the first word more than 4 letters.
- Declare a new variable 'locationAnimal' to uses .findIndex(animal => animal[4]).
- Log 'locationAnimal' to the console. This returns the index value: 3 for the word 'horse' because of zero-indexing.
Notes:
Zero-Indexing
In this example, that means while there are 6 words total, the index values (position) starts at 0, as follows:
"cat" = 0, "dog" = 1, "pig" = 2, "horse" = 3, "yak" = 4, "sea lion" = 5
Logic behind .find()
The callback function uses the parameter 'animal' to keep track of the element being passed over, and [4] checks for a 5th character.
Logic behind .findIndex()
The callback function uses the parameter 'animal' to keep track of the element being passed over, and [4] checks for the index value of 'horse'.
Definitions of find() and findIndex()
.find() returns the first element that satifies a condition. Otherwise it returns undefined.
.findIndex() returns the index of the first element that satifies a condition. Otherwise it returns -1.
Using a for Loop:
for (let i = 0; i < 3; i++) {
console.log("Iteration: ", i);
}
Let's break this down:
First, let's look at the expression 'let i = 0':
This is called the initial expression, indicating what value we will start looping at and keep track of each cycle.
Second, take a look at the condition 'i < 3':
This is called the loop condition, meaning as long as i < 3, the loop will continue running.
Third, let's look at the expression 'i++':
This is called the update expression, which is the code that is being executed each cycle.
Remember:
- The loop condition is executed BEFORE each loop iteration
- The update expression is executed at the END of each loop iteration
Think about the logic like this:
i = 0 therefore 0 < 3 and the condition is met
Loop 1 prints iteration: 0 because (0 < 3) condition MET; continue looping
Loop 2 prints iteration: 1 because (1 < 3) condition MET; continue looping
Loop 3 prints iteration: 2 because (2 < 3) condition MET; continue looping
Loop 4 prints iteration: 3 because (3 < 3) condition MET; continue looping
Loop 5 prints iteration: 4 because (4 < 3) condition NOT MET; END LOOP