Skip to content

Commit ac64578

Browse files
committed
pattern ques update
1 parent 5350359 commit ac64578

File tree

2 files changed

+225
-15
lines changed

2 files changed

+225
-15
lines changed

js_notes/cohert1-v1.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ console.log(sum);
3131

3232
//Question2
3333
//Print fibonacci series - it is a series of number where every number is the sum of the preceeding 2 numbers
34-
let t1 = 0,t2=1;
35-
let terms = 10;
36-
let nextterm;
37-
console.log(t1);
34+
let t1 = 0,t2=1; //t1 and t2 are always same
35+
let terms = 10; //no.of fibonacci terms required
36+
let nextterm; //declaring the nextterm variable
37+
console.log(t1); //logging t1 and t2 in console
3838
console.log(t2);
39-
for(let i=3;i<=terms;++i){
39+
for(let i=3;i<=terms;++i){ //iterating form term 3
4040
nextterm = t1+t2
41-
console.log(nextterm);
41+
console.log(nextterm); //once printed we change t1 to t2 and t2 to nextterm.
4242
t1 = t2;
4343
t2 = nextterm;
44-
nextterm = t1+t2;
44+
nextterm = t1+t2; //now add the new 2 preceding terms
4545
}
4646

js_notes/patternQ.js

Lines changed: 218 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,223 @@
1-
//Square star pattern in js -(5 x 5)
2-
/*
3-
*****
4-
*****
5-
*****
1+
//loops in javascript
2+
//Q1 = sum of first 10 numbers
3+
let sum=0;
4+
for(let i=0;i<=10;i++){
5+
sum += i;
6+
}
7+
console.log(sum);
8+
9+
10+
//Q2 = print all even numbers from 1 to 10
11+
//using for loop
12+
for(let j=0;j<=10;j++){
13+
j%2==0?console.log(j):""
14+
//in terenary operator syntax is [condition ? execute_if_true:else statements]
15+
//if you dont have any else block you still have to complete the syntax using "" as above
16+
}
17+
//using while loop
18+
let num = 0;
19+
while(num<=10){
20+
num%2==0 ? console.log(num) : ""
21+
num++;
22+
}
23+
24+
//Q3 = find factorial of the number upto 10
25+
//do in in a for loop
26+
let factorial = 1;
27+
for(let i=1;i<=5;i++){
28+
factorial *= i;
29+
}
30+
console.log(factorial);
31+
32+
//make it as a function named calcfactorial
33+
function calcfactorial (terms){
34+
let ans = 1;
35+
for(let i=1;i<=terms;i++){
36+
ans *= i;
37+
}
38+
console.log(ans);
39+
}
40+
console.log(calcfactorial(5));
41+
42+
43+
//Understanding nested loops
44+
/* nested loops are loops that are embedded into one another . Each time the outer loop runs and comes to the inner loop , the inner loop completes complete iteration and then goes back to outer loop. This process takes place until the outer loop is fully iterated */
45+
46+
//Example to understand this phenomena
47+
for(let i=1;i<=2;i++){
48+
for(let j=1;j<=3;j++){
49+
console.log(`outerloop iteration no.${i} : innierloop iteration no.${j}`);
50+
}
51+
}
52+
53+
54+
55+
//Print a pattern if starts using nested loops
56+
/* require output pattern
57+
58+
*
59+
**
60+
***
61+
****
62+
63+
*/
64+
for(let i=0;i<=4;i++){
65+
let pattern = "" //creates an empty line after each iteration
66+
for(let j=0;j<=i;j++){ //flls the no.of stars on the empty line based on the row.no (i)
67+
pattern += "*";
68+
}
69+
console.log(pattern);
70+
}
71+
72+
console.log('\n'); //escape sequence
73+
74+
75+
//Print a pattern of starts using nested loops
76+
/* require output pattern
77+
78+
******
79+
******
80+
******
81+
******
82+
******
83+
84+
*/
85+
for(let i=0;i<=4;i++){
86+
let pattern = ""; //starts each line as a empty string
87+
for(let j=0;j<=5;j++){ //prints 6 stars on each line
88+
pattern += "*";
89+
}
90+
console.log(pattern);
91+
}
92+
93+
console.log('\n'); //escape sequence
94+
95+
//Print a pattern of starts using nested loops
96+
/* require output pattern
97+
98+
******
99+
*****
100+
****
101+
***
102+
**
103+
*
104+
105+
*/
106+
for(let i=5;i>=0;i--){ //since we require a inverted triangle we are initiasing i from and continuing the iteration till i reaches 0 by decrementing it as i--
107+
let pattern = "";
108+
for(let j=0;j<=i;j++){
109+
pattern += "*";
110+
111+
}
112+
console.log(pattern);
113+
}
114+
115+
116+
//Print a pattern of starts using nested loops
117+
/* require output pattern
118+
119+
*
120+
**
121+
***
122+
****
123+
*****
124+
125+
To create a right triangle pattern in javascript you will have to deal with 3 loops, 1 of which is external and 2 are internal. The external loop will execute internal loops for 'n' number of times and the internal loop will design a pattern for each row.
126+
127+
From the above pattern, you can see each row has a series of stars and spaces. The number of stars in a row starts from 1 preceding with 'n-1' spaces and ends with 'n' star and 0 spaces.
128+
129+
Create 2 internal loops, 1st print n - i spaces and 2nd print i stars, where i is the number of times the external loop is executed.
130+
131+
*/
132+
let row=5;
133+
for(let i=0;i<row;i++){
134+
let pattern="";
135+
for(let j=0;j<row-i;j++){
136+
pattern += " " ;
137+
}
138+
for(let k=0;k<=i;k++){
139+
pattern += "*";
140+
}
141+
console.log(pattern);
142+
}
143+
144+
//Print a pattern of starts using nested loops
145+
/* require output pattern
146+
*
147+
***
6148
*****
149+
*/
150+
let x=3;
151+
152+
//initalising the outer loop
153+
for(let i=1;i<=x;i++){
154+
let pattern = "";
155+
//loop for spacing viz x-i
156+
for(let j=1;j<=x-i;j++){
157+
pattern += " ";
158+
}
159+
//loop for creating stars viz odd no. per row (2*i-1)
160+
for(let k=1;k<=2*i-1;k++){
161+
pattern += "*";
162+
}
163+
console.log(pattern);
164+
}
165+
166+
//Print a pattern of starts using nested loops
167+
/* require output pattern
168+
*
169+
***
7170
*****
171+
*******
172+
*********
8173
*/
9-
for(let i=0;i<5;i++){
10-
for(let j=0;j<=i;j++){
11-
console.log("*");
174+
175+
let n = 5;
176+
let string = "";
177+
//External loop
178+
for (let i = 1; i <= n; i++) {
179+
// printing spaces
180+
for (let j = 1; j <= n - i; j++) {
181+
string += " ";
182+
}
183+
// printing star
184+
for (let k = 0; k < 2 * i - 1; k++) {
185+
string += "*";
186+
}
187+
string += "\n";
188+
}
189+
console.log(string);
190+
191+
192+
//Print a pattern of starts using nested loops
193+
/* require output pattern
194+
* * * * *
195+
* *
196+
* *
197+
* *
198+
* * * * *
199+
200+
201+
Steps to create a hollow square star pattern are:
202+
203+
Create a variable to store the string and assign it with an empty string
204+
Create a for loop to run for 'n' number of times, where 'n' is number of rows/columns in the square, i.e for(let i = 0; i < n; i++)
205+
Inside the loop, create a for loop that prints a star (*) at the beginning and end of the line and space in between
206+
Also, keep in mind that the first and last row should have only stars
207+
Add a new line after each row
208+
209+
*/
210+
let rows = 5;
211+
for(let i=1;i<=rows;i++){
212+
let pattern = "";
213+
for(let j=1;j<=rows;j++){
214+
if(i==1||i==rows||j==1||j==rows){
215+
pattern += "*";
216+
}
217+
else{
218+
pattern += " ";
219+
}
12220
}
221+
222+
console.log(pattern);
13223
}

0 commit comments

Comments
 (0)