At the end of this class, you will be able to:
JSD
on your machine.Documents
folder: ~/Documents/JSD
cd
into your JSD
foldergit clone
Access by pressing cmd + alt + j
Google is your friend - Don't be afraid to Google JavaScript methods or syntax.
[ ]
length
property is a number 1 greater than the final index number.length !==
number of elements in the array.
let a = ['dog', 'cat', 'hen'];
undefined
a[0];
"dog"
a[1];
"cat"
a[2];
"hen"
array.length
isn't necessarily the number of items in the array
a[100] = 'fox';
a.length; // 101
Key Objective
Type of Exercise
Location
Timing
8 mins |
|
Method | Use |
---|---|
toString() | Returns a single string consisting of the array elements converted to strings and separated by commas |
join() | Same as |
concat() | Merges two or more arrays together |
pop() | Removes and returns the item at the end of the array |
push(item1, …, itemN) | Adds one or more items to the end of the array |
reverse() | Reverses the array |
shift() | Removes and returns the item at the start of the array |
unshift(item1, …, itemN) | Adds one or more items to the start of the array |
Open up: 1-loops-codealong
Iterating is a way of incrementally repeating a task, one at a time.
while
is a loop statement that will run while a condition is true
while (true) {
// an infinite loop!
}
Using a do-while
loop makes sure that the body of the loop is executed at least once.
while()
isn't evaluated until after the block of code runs.
let input = 0;
do {
console.log(input++);
} while (input < 10);
It is possible to iterate over an array with a while loop:
let a = [1, 2, 3, 4, 5]
let len = a.length;
let i = 0;
while(i < len) {
console.log(i);
i++;
}
You can also iterate over an array with:
let teams = ['Liverpool', 'Arsenal', 'Chelsea'];
for (let i = 0; i < teams.length; i++) {
console.log(teams[i]);
}
// Liverpool
// Arsenal
// Chelsea
Type of Exercise
Location
Timing
10 mins |
|
Another way of iterating over an array that was added with ECMAScript 5 is forEach()
:
let teams = ['Tottenham', 'QPR', 'Watford'];
teams.forEach(function(element)) {
console.log(element);
};
Type of Exercise
Location
Timing
6 mins |
|
Method | Use |
---|---|
forEach() | Executes a provided function once per array element |
every() | Tests whether all elements in the array pass the test implemented by the provided function |
some() | Tests whether some element in the array passes the text implemented by the provided function |
filter() | Creates a new array with all elements that pass the test implemented by the provided function |
map() | Creates a new array with the results of calling a provided function on every element in this array |
Open up: 3-array-iterators-codealong
Type of Exercise
Location
Timing
5 mins |
|
Type of Exercise
Location
Timing
Until 20:45 |
|
(Lesson 03)