-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path30. Print number triangle pattern .js
More file actions
119 lines (98 loc) · 2.86 KB
/
30. Print number triangle pattern .js
File metadata and controls
119 lines (98 loc) · 2.86 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
// pint number triangle pattern :
// --------------------------------------------------------------------------------------------------------------::
let rows = 5;
for (let i = 1; i <= rows; i++) {
let line = "";
for (let j = 1; j <= i; j++) {
line += j + " ";
}
console.log(line);
}
/*
PATTERN ANALYSIS:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
RULE: Row i contains numbers from 1 to i
EXPLANATION FOR ABOVE CODE:
let rows = 5; // Total number of rows
for (let i = 1; i <= rows; i++) { // Row counter from 1 to 5
let line = ""; // Build string for current row
for (let j = 1; j <= i; j++) { // Number counter from 1 to current row number
line += j + " "; // Add number and space
}
console.log(line); // Print completed row
}
EXECUTION BREAKDOWN:
i=1: j runs 1 to 1 → "1 "
i=2: j runs 1 to 2 → "1 2 "
i=3: j runs 1 to 3 → "1 2 3 "
i=4: j runs 1 to 4 → "1 2 3 4 "
i=5: j runs 1 to 5 → "1 2 3 4 5 "
*/
// ---------------------------------------------------------------
function printNumberTriangle(size) {
for (let i = 1; i <= size; i++) {
let pattern = "";
for (let j = 1; j <= i; j++) {
pattern += j;
if (j < i) pattern += " ";
}
console.log(pattern);
}
}
printNumberTriangle(5);
/*
EXPLANATION FOR ABOVE FUNCTION:
function printNumberTriangle(size) {
for (let i = 1; i <= size; i++) { // Row counter
let pattern = ""; // Build pattern string
for (let j = 1; j <= i; j++) { // Number counter from 1 to i
pattern += j; // Add current number
if (j < i) pattern += " "; // Add space between numbers (not after last)
}
console.log(pattern); // Print row
}
}
EXECUTION for printNumberTriangle(4):
i=1: j=1 → "1"
i=2: j=1,2 → "1 2"
i=3: j=1,2,3 → "1 2 3"
i=4: j=1,2,3,4 → "1 2 3 4"
SPACING LOGIC: Add space only between numbers, not after last number
*/
// ---------------------------------------------------------------
let num = 1;
for (let i = 1; i <= 5; i++) {
let line = "";
for (let j = 1; j <= i; j++) {
line += num + " ";
num++;
}
console.log(line);
}
/*
EXPLANATION FOR CONTINUOUS NUMBER PATTERN:
let num = 1; // Global counter for continuous numbering
for (let i = 1; i <= 4; i++) { // Row counter
let line = ""; // Build row string
for (let j = 1; j <= i; j++) { // Position counter in current row
line += num + " "; // Add current number
num++; // Increment for next position
}
console.log(line); // Print row
}
EXECUTION for continuous pattern:
Row 1: num=1 → "1 " (num becomes 2)
Row 2: num=2,3 → "2 3 " (num becomes 4)
Row 3: num=4,5,6 → "4 5 6 " (num becomes 7)
Row 4: num=7,8,9,10 → "7 8 9 10 "
OUTPUT:
1
2 3
4 5 6
7 8 9 10
PATTERN VARIATION: Numbers continue sequentially instead of restarting each row
*/