Skip to content

Commit 2187c3a

Browse files
committed
complete for of loop section
1 parent 51fc8e3 commit 2187c3a

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

09-Data-Structure-Modern-Operators-and-Strings/README.md

+34
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,37 @@ console.log(guestCOrrect);
187187
This works because the Nullish operator works with the concept of Nullish values instead of falsy values. So it works with `Undefined` and `Null` as falsy values and considers empty string and 0 as truthy values.
188188
189189
In other words, the condition will work only if the first value is null or undefined. Since 0 is not a falsy value in the Nullish Coalescing operator, the condition will move to the next option.
190+
191+
# Looping Arrays - The For Of Loop
192+
193+
The for...of statement executes a loop that operates on a sequence of values sourced from an iterable object.
194+
195+
Let's say we want to loop the array below, a typical `For` loop will look like this:
196+
197+
```js
198+
const newMenu2 = [...restaurant.starterMenu, ...restaurant.mainMenu];
199+
200+
for (let i = 0; i < newMenu2.lenght; i++) {
201+
console.log(newMenu2[i]);
202+
}
203+
```
204+
205+
But there is an easier way to loop between this array without having to specify all these conditions. This method is called the `For Of loop` and it was introduced in ES6.
206+
207+
```js
208+
for (const foo of newMenu2) console.log(foo);
209+
```
210+
211+
You can also use the `continue` and `break` keywords in the `for of` loop.
212+
213+
Now what if we need the current index of each value? It's more of a pain to get the current index in a for of loop, and that is because, the for of loop was built to give us the current element. However you can get both the element and it's index.
214+
215+
Example
216+
217+
```js
218+
for (const bar of newMenu2.entries()) {
219+
console.log(bar);
220+
}
221+
222+
console.log([...newMenu2.entries()]);
223+
```

09-Data-Structure-Modern-Operators-and-Strings/script.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ add(...x);
241241
restaurant.orderPizza("Mushrooms", "Onions", "Olives", "Spinach");
242242
restaurant.orderPizza("Mushrooms");
243243

244-
// Short Circuting (OR ||)
245-
console.log("🔸Short Circuting🔸");
244+
// 🔸Short Circuting (OR ||)🔸
245+
console.log("Short Circuting");
246246
console.log(3 || "Eke");
247247
console.log("" || "Eke");
248248
console.log(true || 0);
@@ -268,7 +268,20 @@ if (restaurant.orderPizza) {
268268

269269
restaurant.orderPizza && restaurant.orderPizza("Mushrooms", "Spinach");
270270

271-
// Nullish Coalescing Operator
271+
// 🔸Nullish Coalescing Operator🔸
272272
restaurant.numGuests = 0;
273273
const guest3 = restaurant.numGuests || 10;
274274
console.log(guest3);
275+
276+
// 🔸Looping Arrays (For-Of Loop)🔸
277+
const newMenu2 = [...restaurant.starterMenu, ...restaurant.mainMenu];
278+
279+
// for (let i = 0; i < newMenu2.length; i++) {
280+
// console.log(`FOR: ${newMenu2[i]} is a Cheese Recipe`);
281+
// }
282+
283+
for (const foo of newMenu2) console.log(foo);
284+
for (const [i, el] of newMenu2.entries()) {
285+
// console.log(item);
286+
console.log(`${i + 1}: ${el}`);
287+
}

index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
</head>
1111
<body>
1212
<h1>Console.log</h1>
13-
<script src="09-Data-Structure-Modern-Operators-and-Strings/challenge.js"></script>
13+
<script src="09-Data-Structure-Modern-Operators-and-Strings/script.js"></script>
1414
</body>
1515
</html>

0 commit comments

Comments
 (0)