/
InteratingOverArrays.js
36 lines (24 loc) · 1.11 KB
/
InteratingOverArrays.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//MODULE 4: INTRODUCTION TO PROGRAMMING
//ITERATING OVER ARRAYS - Assignment
//Iterating over arrays with two common types of loops: the for and the while.
//Example:
//Store and organize gear for camping
const packingList = ["tent", "bug spray", "sleeping bag"];
//How print the items?1 by 1is laborious
console.log(packingList[0]);
console.log(packingList[1]);
console.log(packingList[2]);
//Therefore, use JavaScript's looping constructs:
//EXERCISE 1: Iterating Over an Array With for
const packingList = ["bowls", "plates", "pots", "pans", "eating utensils", "glasses", "cups", "cooking utensils"];
console.log("Kitchen stuff to pack:");
for (let i = 0; i < packingList.length; i++) {
console.log(packingList[i]);
}
//Declares a counter variable called i (short for "index") that starts at 0,
//Increments by 1 at every step of a loop until it matches the length of our array, that is packingList.
//Recall that to access an item in an array we index it.
packingList[3];
//Wanted to index every item,
//Build a loop that increments an index variable i,
//which can be used to look into packingList one item at a time.