-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path057-for-of-loop.js
More file actions
142 lines (113 loc) · 2.85 KB
/
057-for-of-loop.js
File metadata and controls
142 lines (113 loc) · 2.85 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// WHAT IS THE FOR...OF LOOP?
// for(const elements of iterable){
// // code to execute for each element
// }
// const fruits = ['apple', 'banana', 'orange','mango']
// for(const fruit of fruits){
// console.log(fruit)
// }
// COMPARING FOR...OF WITH OTHER LOOPS
// const numbers = [10, 20, 30, 40];
// for (let i = 0; i < numbers.length; i++) {
// console.log(numbers[i]);
// }
// for (const num of numbers) {
// console.log(num);
// }
// for(const [index, fruit] of fruits.entries()){
// console.log(`${index}: ${fruit}`)
// }
// WHAT ARE ITERABLES?
// const uniqueNumbers = new Set([1, 2, 3, 2, 1]);
// for (const num of uniqueNumbers) {
// console.log(num);
// }
// Output:
// 1
// 2
// 3
// const userRoles = new Map([
// ['john', 'admin'],
// ['jane', 'editor'],
// ['bob', 'viewer']
// ]);
// for (const [username, role] of userRoles) {
// console.log(`${username} is an ${role}`);
// }
// Output:
// john is an admin
// jane is an editor
// bob is an viewer
// WHAT'S NOT ITERABLE?
const person = {
name: 'Alice',
age: 30,
city: 'New York'
};
// This will throw an error!
// for (const prop of person) {
// console.log(prop);
// }
// Iterate over keys
for (const key of Object.keys(person)) {
console.log(key);
}
// Iterate over values
for (const value of Object.values(person)) {
console.log(value);
}
// Iterate over key-value pairs
for (const [key, value] of Object.entries(person)) {
console.log(`${key}: ${value}`);
}
// PRACTICAL EXAMPLES
// Example 1: Processing Shopping Cart Items
const cartItems = [
{ name: 'Laptop', price: 999 },
{ name: 'Mouse', price: 25 },
{ name: 'Keyboard', price: 75 }
];
let totalPrice = 0;
for (const item of cartItems) {
totalPrice += item.price;
console.log(`Added ${item.name}: $${item.price}`);
}
console.log(`Total: $${totalPrice}`);
// Example 2: Validating User Input
const userInputs = ['john@email.com', '', 'jane@email.com', 'invalid'];
for (const email of userInputs) {
if (email && email.includes('@')) {
console.log(`Valid email: ${email}`);
} else {
console.log(`Invalid email: ${email}`);
}
}
// BREAK AND CONTINUE WITH FOR...OF
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// Find first number greater than 5
for (const num of numbers) {
if (num > 5) {
console.log(`Found it: ${num}`);
break;
}
}
// Skip even numbers
for (const num of numbers) {
if (num % 2 === 0) {
continue;
}
console.log(`Odd number: ${num}`);
}
// FOR...OF VS FOR...IN - KEY DIFFERENCES
const languages = ['JavaScript', 'Python', 'Java'];
// for...in gives you indices (keys)
console.log('for...in:');
for (const index in languages) {
console.log(index); // '0', '1', '2' (as strings!)
}
// for...of gives you values
console.log('for...of:');
for (const language of languages) {
console.log(language); // 'JavaScript', 'Python', 'Java'
}
// WHEN TO USE FOR...OF