forked from faisal2410/js_basic_ostad_b3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpattern.js
328 lines (257 loc) · 8.59 KB
/
pattern.js
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
Learning pattern using loops in JavaScript can offer several benefits, including:
Improved problem-solving skills: Learning to create patterns using loops requires breaking down a problem into smaller parts and figuring out how to use loops to solve each part. This can help improve your problem-solving skills and teach you how to approach complex problems in a structured way.
Increased efficiency: Using loops to create patterns can be more efficient than manually typing out each character or number. Once you have written a loop to create a pattern, you can reuse that code to generate similar patterns with different characters or conditions.
Enhanced understanding of loops: Creating patterns with loops can help you better understand how loops work and how to use them effectively. You can learn about loop syntax, conditions, and how to nest loops to create more complex patterns.
Improved coding skills: Learning how to create patterns with loops can help you become a better programmer by improving your understanding of fundamental programming concepts. You can also learn how to write more efficient, readable, and maintainable code by practicing writing patterns with loops.
Practical applications: Understanding how to create patterns with loops can be useful for a variety of practical applications, such as generating charts, graphs, or visual displays. You can also use patterns to create animations or interactive designs on websites or mobile apps.
Overall, learning pattern using loops in JavaScript can help you become a better problem solver, programmer, and provide you with practical skills that can be useful in a variety of applications.
*/
// In JavaScript, you can use loops to create patterns using a combination of characters, numbers, or symbols.A common way to do this is by using nested loops to iterate over rows and columns of a pattern and using string concatenation to build the final pattern.
// Here's an example of how to create a simple pattern of asterisks using nested loops:
for (let i = 1; i <= 5; i++) {
let row = '';
for (let j = 1; j <= i; j++) {
row += '*';
}
console.log(row);
}
// In this code, the outer loop iterates over the rows of the pattern, starting from 1 and ending at 5. For each row, a new empty string is created to store the characters of the row.The inner loop iterates over the columns of the pattern for each row, starting from 1 and ending at the current row number.Within the inner loop, an asterisk is added to the row string for each column.Finally, the row string is printed to the console, creating the final pattern:
// Output
/*
👀
*
**
***
****
*****
👀
*/
// You can modify this code to create different patterns by changing the characters used and the conditions for the outer and inner loops.For example, you could create a pattern of numbers or letters by replacing the asterisks with the desired characters.You could also create a diagonal pattern by adding an if statement to the inner loop that only adds the character to the row string if the column number equals the row number.
/*
👀 Some pattern question solving tricks using for loops in JavaScript:
Identify the pattern: The first step in solving any pattern question is to identify the pattern. Look for any repetition or sequences in the given pattern.
Plan your approach: Once you have identified the pattern, plan your approach for generating it using for loops in JavaScript. Think about how many loops you will need and how to use them to create the pattern.
Use nested loops: Most pattern questions require nested loops to generate the pattern. The outer loop is used for generating the rows and the inner loop is used for generating the characters within each row.
Use conditional statements: Sometimes, you may need to use conditional statements within your loops to generate the pattern correctly. For example, you may need to check the row number or the column number to determine what character to print.
Experiment with variables: Try experimenting with different variables and values to generate the pattern. You may need to adjust the values of variables such as row number, column number, and character length to get the desired pattern.
Test your code: Always test your code by running it and checking the output. Make sure that the pattern generated by your code matches the pattern given in the problem statement.
Refactor your code: If your code is not generating the correct pattern, go back and refactor it. Check that your loop conditions and variable values are correct. You may need to make small tweaks to your code until it generates the correct pattern.
By following these tricks, you can quickly and effectively solve pattern questions using for loops in JavaScript. Remember to practice and experiment with different patterns to develop your problem-solving skills.
*/
// Pattern 1 (triangle)
for (let i = 1; i <= 5; i++) {
let row = '';
for (let j = 1; j <= i; j++) {
row += '*';
}
console.log(row);
}
// Pattern 2 :Square :
for (let i = 1; i <= 5; i++) {
let row = '';
for (let j = 1; j <= 5; j++) {
row += '*';
}
console.log(row);
}
// This code generates a square pattern made up of asterisks:
/*
*****
*****
*****
*****
*****
*/
// Pattern 3: Reverse triangle
for (let i = 5; i >= 1; i--) {
let row = '';
for (let j = 1; j <= i; j++) {
row += '*';
}
console.log(row);
}
// This code generates a reverse triangle pattern made up of asterisks:
/*
*****
****
***
**
*
*/
//Pattern 4 Diamond pattern:
for (let i = 1; i <= 5; i++) {
let row = '';
for (let j = 1; j <= 5 - i; j++) {
row += ' ';
}
for (let k = 1; k <= 2 * i - 1; k++) {
row += '*';
}
console.log(row);
}
for (let i = 4; i >= 1; i--) {
let row = '';
for (let j = 1; j <= 5 - i; j++) {
row += ' ';
}
for (let k = 1; k <= 2 * i - 1; k++) {
row += '*';
}
console.log(row);
}
// This code generates a diamond pattern made up of asterisks and spaces:
/*
*
***
*****
*******
*********
*******
*****
***
*
*/
// Pattern 5 Hourglass pattern:
for (let i = 5; i >= 1; i--) {
let row = '';
for (let j = 1; j <= 5 - i; j++) {
row += ' ';
}
for (let k = 1; k <= 2 * i - 1; k++) {
row += '*';
}
console.log(row);
}
for (let i = 2; i <= 5; i++) {
let row = '';
for (let j = 1; j <= 5 - i; j++) {
row += ' ';
}
for (let k = 1; k <= 2 * i - 1; k++) {
row += '*';
}
console.log(row);
}
// This code generates an hourglass pattern made up of asterisks and spaces:
/*
*********
*******
*****
***
*
***
*****
*******
*********
*/
// Pattern 6 Number pattern:
for (let i = 1; i <= 5; i++) {
let row = '';
for (let j = 1; j <= i; j++) {
row += j + ' ';
}
console.log(row);
}
// This code generates a number pattern:
/*
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
*/
//6. Hollow square pattern:
for (let i = 1; i <= 5; i++) {
let row = '';
for (let j = 1; j <= 5; j++) {
if (i == 1 || i == 5 || j == 1 || j == 5) {
row += '*';
} else {
row += ' ';
}
}
console.log(row);
}
// This code generates a hollow square pattern made up of asterisks and spaces:
/*
*****
* *
* *
* *
*****
*/
// 7. Right triangle pattern:
for (let i = 1; i <= 5; i++) {
let row = '';
for (let j = 1; j <= i; j++) {
row += '*';
}
console.log(row);
}
// This code generates a right triangle pattern made up of asterisks:
/*
*
**
***
****
*****
*/
// 8. Left triangle pattern:
for (let i = 1; i <= 5; i++) {
let row = '';
for (let j = 1; j <= 5 - i; j++) {
row += ' ';
}
for (let k = 1; k <= i; k++) {
row += '*';
}
console.log(row);
}
// This code generates a left triangle pattern made up of asterisks and spaces:
/*
*
**
***
****
*****
*/
//9. Pyramid pattern:
for (let i = 1; i <= 5; i++) {
let row = '';
for (let j = 1; j <= 5 - i; j++) {
row += ' ';
}
for (let k = 1; k <= 2 * i - 1; k++) {
row += '*';
}
console.log(row);
}
// This code generates a pyramid pattern made up of asterisks and spaces:
/*
*
***
*****
*******
*********
*/
//10. Alternating number pattern:
for (let i = 1; i <= 5; i++) {
let row = '';
for (let j = 1; j <= i; j++) {
if (j % 2 == 0) {
row += '0 ';
} else {
row += '1 ';
}
}
console.log(row);
}
// This code generates an alternating number pattern:
/*
1
1 0
1 0 1
1 0 1 0
1 0 1 0 1
*/