Skip to content

Commit bb68636

Browse files
feat: add iterations
1 parent c055701 commit bb68636

27 files changed

+334
-253
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
// Topic: For loop
1+
// `for` loop
22

3-
// Q. What is for loop?
3+
// What
44
// - A for loop in JavaScript is a programming construct that allows you to repeat a set of instructions a certain number of times. It's great for automating tasks that need to be done repeatedly.
55

6-
// Q. Why use for loop?
6+
// Why
77
// - Imagine you have a task you need to do over and over again. Instead of writing the same code multiple times, a for loop helps you perform that task a specific number of times, making your code more efficient and easier to manage.
88

9-
// Q. How for loop work?
9+
// How
1010
// - Initialization: You start by defining a variable and giving it an initial value. This variable will keep track of the loop's progress.
1111
// - Condition: You specify a condition that, as long as it's true, the loop will continue to run. This condition is checked before every loop iteration.
1212
// - Iteration: After each iteration of the loop, the loop variable is updated according to a certain pattern.
@@ -22,9 +22,48 @@
2222
// }
2323

2424
// Example
25-
for (let i = 1; i <= 5; i++) {
26-
console.log(i);
25+
for (let index = 0; index <= 10; index++) {
26+
console.log(index);
2727
}
2828

29-
// Summary
30-
// - A for loop in JavaScript helps you repeat a set of instructions a specific number of times. You provide an initial value, a condition to check before each iteration, and an iteration pattern. The loop runs as long as the condition is true. It's a useful tool for automating repetitive tasks and making your code more efficient. Just ensure your loop's condition eventually becomes false to avoid infinite loops.
29+
for (let index = 0; index <= 10; index++) {
30+
if (index === 5) {
31+
console.log("This is number 5");
32+
}
33+
console.log(index);
34+
}
35+
36+
for (let i = 1; i <= 10; i++) {
37+
// console.log(`Outer loop: ${i}`);
38+
for (let j = 1; j <= 10; j++) {
39+
// console.log(`Inner loop: ${j}`);
40+
console.log(`${i} * ${j} = ${i * j}`);
41+
}
42+
}
43+
44+
// For loop for array
45+
const myArray = [0, 1, 2, 3, 4, 5];
46+
console.log(myArray);
47+
48+
for (let index = 0; index < myArray.length; index++) {
49+
const element = myArray[index];
50+
console.log(element);
51+
}
52+
53+
// Break
54+
for (let index = 0; index <= 10; index++) {
55+
if (index === 5) {
56+
console.log("Number 5 detected");
57+
break;
58+
}
59+
console.log(index);
60+
}
61+
62+
// Continue
63+
for (let index = 0; index <= 10; index++) {
64+
if (index === 5) {
65+
console.log("Number 5 detected");
66+
continue;
67+
}
68+
console.log(index);
69+
}

19_Iterations/3_while_loop.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// While Loop
2+
3+
// Syntax
4+
// Initialization
5+
// while (condition){
6+
// Code...
7+
// Increment/ Decrement
8+
// }
9+
10+
let index = 0;
11+
while (index <= 10) {
12+
console.log(index);
13+
index++;
14+
}
15+
16+
let myArray = ["red", "green", "blue"];
17+
let i = 0;
18+
while (i < myArray.length) {
19+
console.log(myArray[i]);
20+
i++;
21+
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// Do While Loop
22

3-
// SYNTAX
3+
// Syntax
44
// Initialization
55
// do {
66
// Code...
77
// Increment/ Decrement
88
// } while (condition)
99

10-
let variable = 0;
10+
let index = 11;
1111
do {
12-
console.log(variable);
13-
variable++;
14-
} while (variable <= 10);
12+
console.log(index);
13+
index++;
14+
} while (index <= 10);

19_Iterations/5_for_of_loop.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// For of Loop
2+
3+
// What
4+
// - for...of is a loop specifically designed to iterate over iterable objects in JavaScript.
5+
// - Iterable objects are those that can provide a sequence of values, such as arrays, strings, maps, sets, and some custom objects.
6+
7+
// Why
8+
// - It offers a concise and readable way to iterate over the values of an iterable object, often used in modern JavaScript code.
9+
// - Compared to traditional for loops (with indices), it focuses on the values themselves, potentially improving code clarity.
10+
11+
// - How
12+
// - It works by creating an iterator for the iterable object.
13+
// - An iterator is an object that provides a next() method, which returns the next element in the sequence along with a done property indicating whether the iteration is complete.
14+
// - The loop automatically iterates until there are no more values left (the done property becomes true).
15+
16+
// Note
17+
// - The order of iteration is guaranteed.
18+
// - 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).
20+
// - The for...of loop doesn't modify the original iterable object. It iterates over a copy of the values.
21+
22+
// Syntax
23+
// for (const element of iterable_object) {
24+
// // Code to be executed for each element
25+
// }
26+
27+
// Example
28+
29+
// For of loop on string
30+
const myString = "String";
31+
for (const character of myString) {
32+
console.log(character);
33+
}
34+
35+
// For of loop on array
36+
const array = [0, 1, 2, 3, 4, 5];
37+
38+
for (const element of array) {
39+
console.log(element);
40+
}
41+
42+
// For of loop on Map()
43+
const myMap = new Map([
44+
["key1", "value1"],
45+
["key2", "value2"],
46+
["key3", "value3"],
47+
]);
48+
49+
// for of -> Map -> key
50+
for (const key of myMap) {
51+
console.log(key);
52+
}
53+
// for of -> Map -> [key, value] -> key | value
54+
for (const [key, value] of myMap) {
55+
console.log(key);
56+
console.log(value);
57+
}
58+
59+
// For of loop on object (NOT RECOMMENDED)
60+
// Object.keys(objectName) -> key -> objectName[key] -> value
61+
const object = {
62+
key1: "value1",
63+
key2: "value2",
64+
};
65+
for (const key of Object.keys(object)) {
66+
console.log(object[key]);
67+
}

19_Iterations/6_for_in_loop.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// `for in` Loop
2+
3+
// What
4+
// - for...in is a loop specifically designed to iterate over the enumerable properties of an object in JavaScript.
5+
// - It provides a way to access all properties (including inherited ones) of an object, regardless of their type (strings, numbers, functions, etc.).
6+
// - It is use to iterate over the object property.
7+
8+
// Why
9+
// - It can be useful for generic object property iteration, especially when you need to access property names and values for various purposes.
10+
// - It can simplify looping through object properties without requiring manual property access by name.
11+
// How
12+
// - It works by cycling through the enumerable properties of an object, one by one.
13+
// - The loop body executes for each property, providing you with the property name (prop) in each iteration.
14+
// - You can then access the property value using the property name: object[key].
15+
16+
// Note
17+
// - The order of iteration is not guaranteed.
18+
// - Not recommended to use on array.
19+
20+
// Syntax
21+
// for (key in object){
22+
// Code...
23+
// }
24+
25+
// For in loop on object
26+
const object = {
27+
key1: "value1",
28+
key2: "value2",
29+
key3: "value3",
30+
};
31+
32+
for (let key in object) {
33+
console.log(key);
34+
console.log(object[key]);
35+
}
36+
37+
// For in loop on array (NOT RECOMMENDED)
38+
const array = [1, 2, 3, 4, 5];
39+
for (const index in array) {
40+
console.log(index);
41+
console.log(array[index]);
42+
}

19_Iterations/7_for_each_loop.js

Whitespace-only changes.

20_map/1_map.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Map
2+
3+
// What
4+
// - It is iterable.
5+
// - It store data in `[key, value]` form.
6+
// - Element insertion order is guaranteed.
7+
// - It can store duplicate value with different key. If you add same key in map it will take the last added key and update the value.
8+
// - It is a built-in object in JavaScript that stores key-value pairs. Unlike objects (which use strings or symbols as keys), Map allows you to use any data type (including objects) as keys.
9+
10+
// Why
11+
// - It provides a more flexible and ordered way to store data compared to traditional objects.
12+
// - Map objects are particularly useful when you need to use non-string keys or when you want to maintain the insertion order of key-value pairs.
13+
14+
// How
15+
// - You can create a Map object using the new Map() constructor or by providing an iterable of key-value pairs.
16+
// - You can add key-value pairs using the set() method and access values using the get() method.
17+
// - You can also check if a key exists using the has() method and remove a key-value pair using the delete() method.
18+
19+
// SYNTAX
20+
// const map = new Map([
21+
// ["key1", "value1"],
22+
// ["key2", "value2"],
23+
// ["key3", "value3"],
24+
// ]);
25+
26+
// Using the constructor
27+
const myMap = new Map();
28+
29+
// Using an iterable (array of key-value pairs)
30+
const anotherMap = new Map([
31+
["name", "Alice"],
32+
[1, "one"],
33+
[true, "boolean value"],
34+
]);
35+
36+
// Adding key-value pair in Map()
37+
myMap.set("age", 30);
38+
myMap.set({}, "object key"); // Can use objects as keys
39+
40+
// Accessing value
41+
const value = myMap.get("age"); // value will be 30
42+
43+
// Checking for keys
44+
if (myMap.has("name")) {
45+
console.log("Name exists in the map");
46+
}
47+
48+
// Removing key-value pair
49+
myMap.delete("age");
File renamed without changes.
File renamed without changes.

20_map/3_map_Iterations.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Map Iterate
2+
3+
const myMap = new Map(
4+
["key1", "value1"],
5+
["key2", "value2"],
6+
["key3", "value3"]
7+
);
8+
9+
myMap.forEach(function (value, key, map) {
10+
console.log(key + ": " + value);
11+
});
12+
13+
// Iterable
14+
15+
// Return values
16+
for (let key of myMap) {
17+
console.log(key);
18+
}
19+
20+
// Return keys and values
21+
// It's return key and value in array form.
22+
for (let [key, value] of myMap) {
23+
console.log([key, value]);
24+
}
25+
26+
// Using object as key
27+
const teachersDetail = {
28+
id: "101",
29+
name: "Math Teacher",
30+
};
31+
const extraTeachersDetail = new Map();
32+
extraTeachersDetail.set(teachersDetail, {
33+
subject: "Math",
34+
department: "Math Department",
35+
});
36+
console.log(teachersDetail.id);
37+
console.log(teachersDetail.name);
38+
console.log(extraTeachersDetail.get(teachersDetail).department);

Set/1Set.js 21_set/1Set.js

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Map/1Map.js

-48
This file was deleted.

Map/3MapIterate.js

-13
This file was deleted.

0 commit comments

Comments
 (0)