-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path022-nested-loop.js
More file actions
79 lines (71 loc) · 1.63 KB
/
022-nested-loop.js
File metadata and controls
79 lines (71 loc) · 1.63 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
// 1: WHAT ARE NESTED LOOPS?
// for (let i = 1; i <= 3; i++) {
// for (let j = 1; j <= 3; j++) {
// console.log(`i = ${i}, j = ${j}`);
// }
// }
// i =1; j=1... i=1, j=2... i=1, j=3
// 2: PRACTICAL EXAMPLE - MULTIPLICATION TABLE
// for (let i = 1; i <= 5; i++) {
// let row = '';
// for (let j = 1; j <= 5; j++) {
// row += (i * j) + '\t';
// }
// console.log(row);
// }
// 3: PATTERN PRINTING
// Stars Pattern
// for (let i = 1; i <= 5; i++) {
// let pattern = '';
// for (let j = 1; j <= i; j++) {
// pattern += '* ';
// }
// console.log(pattern);
// }
// Square pattern
// for (let i = 1; i <= 4; i++) {
// let pattern = '';
// for (let j = 1; j <= 4; j++) {
// pattern += '* ';
// }
// console.log(pattern);
// }
// 4: NESTED LOOPS WITH ARRAYS
// let matrix = [
// [1, 2, 3],
// [4, 5, 6],
// [7, 8, 9]
// ];
// for (let i = 0; i < matrix.length; i++) {
// for (let j = 0; j < matrix[i].length; j++) {
// console.log(`Element at [${i}][${j}]: ${matrix[i][j]}`);
// }
// }
// 5: PERFORMANCE CONSIDERATIONS
// This runs 10,000 times!
// for (let i = 0; i < 100; i++) {
// for (let j = 0; j < 100; j++) {
// console.log(i + j);
// }
// }
// 6: MIXING LOOP TYPES
// let i = 1;
// while (i <= 3) {
// for (let j = 1; j <= 3; j++) {
// console.log(`i = ${i}, j = ${j}`);
// }
// i++;
// }
// PRACTICE EXERCISE
// *
// * *
// * * *
// * * * *
// * * * * *
for (let i = 5; i >= 1; i--) {
let pattern = '';
for (let j = 1; j <= i; j++) {
pattern += '* ';
}
console.log(pattern);
}