Skip to content

Commit 1bbd7a8

Browse files
refactor: iteration
1 parent 587fc55 commit 1bbd7a8

File tree

7 files changed

+30
-26
lines changed

7 files changed

+30
-26
lines changed
+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Break Keyword
22

3-
for (let variable = 0; variable < 10; variable++) {
4-
if (variable === 5) {
3+
for (let index = 0; index < 10; index++) {
4+
if (index === 5) {
55
break;
66
}
7-
console.log(variable);
7+
console.log(index);
88
}
+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Continue Keyword
22

3-
for (let num = 0; num < 10; num++) {
4-
if (num === 5) {
3+
for (let index = 0; index < 10; index++) {
4+
if (index === 5) {
55
continue;
66
}
7-
console.log(num);
7+
console.log(index);
88
}

19_Iterations/1_for_loop.js

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ for (let index = 0; index <= 10; index++) {
3333
console.log(index);
3434
}
3535

36+
// Nested for loop
3637
for (let i = 1; i <= 10; i++) {
3738
// console.log(`Outer loop: ${i}`);
3839
for (let j = 1; j <= 10; j++) {

19_Iterations/5_for_of_loop.js

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
// Note
1717
// - The order of iteration is guaranteed.
1818
// - Not recommended to use on object.
19-
// - Unlike traditional for loops, for...of doesn't give you access to the current index directly. If you need the index, you can use a combination of for...of with a traditional for loop (nested or with an index counter).
2019
// - The for...of loop doesn't modify the original iterable object. It iterates over a copy of the values.
2120

2221
// Syntax

19_Iterations/7_for_each.js

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ array1.forEach((element) => {
5656

5757
// Note
5858
// - The forEach() method accepts a callback function as its argument, which is invoked for each element in the array. The callback function takes three parameters: the current element being processed, the index of the current element, and the array or array-like object being iterated.
59+
60+
5961
// - The forEach() method in JavaScript doesn't return a value itself. Here's a breakdown of why:
6062

6163
// Purpose of forEach()

19_Iterations/9_map.js

+16-18
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,21 @@ const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
2424
const newNumbers = array.map((number) => number < 10);
2525
console.log(newNumbers);
2626

27-
// EXAMPLE
28-
const array1 = [
29-
{
30-
key1: "value1",
31-
key2: "value2",
32-
key3: "value3",
33-
sameKey: "same value",
34-
},
35-
{
36-
key4: "value4",
37-
key5: "value5",
38-
key6: "value6",
39-
sameKey: "same value",
40-
},
27+
const books = [
28+
{ title: "Book One", genre: "Fiction", publish: 1981, edition: 2004 },
29+
{ title: "Book Two", genre: "Non-Fiction", publish: 1992, edition: 2008 },
30+
{ title: "Book Three", genre: "History", publish: 1999, edition: 2007 },
31+
{ title: "Book Four", genre: "Non-Fiction", publish: 1989, edition: 2010 },
32+
{ title: "Book Five", genre: "Science", publish: 2009, edition: 2014 },
33+
{ title: "Book Six", genre: "Fiction", publish: 1987, edition: 2010 },
34+
{ title: "Book Seven", genre: "History", publish: 1986, edition: 1996 },
35+
{ title: "Book Eight", genre: "Science", publish: 2011, edition: 2016 },
36+
{ title: "Book Nine", genre: "Non-Fiction", publish: 1981, edition: 1989 },
4137
];
4238

43-
const newArray3 = array1.map((object) => {
44-
return object.sameKey;
45-
});
46-
console.log(newArray3);
39+
const fictionBook = books
40+
.filter((book) => book.genre === "Fiction")
41+
.filter((book) => book.edition > 2000)
42+
.map((book) => book.edition + 1000);
43+
44+
console.log(fictionBook);

playground.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ const myMap = new Map([
44
["key3", "value3"],
55
]);
66

7-
console.log(myMap);
7+
// console.log(myMap);
8+
9+
for (const key of myMap) {
10+
console.log(myMap[key]);
11+
}

0 commit comments

Comments
 (0)