You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
//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
+
letnum=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
+
letfactorial=1;
27
+
for(leti=1;i<=5;i++){
28
+
factorial*=i;
29
+
}
30
+
console.log(factorial);
31
+
32
+
//make it as a function named calcfactorial
33
+
functioncalcfactorial(terms){
34
+
letans=1;
35
+
for(leti=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 */
letpattern=""//creates an empty line after each iteration
66
+
for(letj=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(leti=0;i<=4;i++){
86
+
letpattern="";//starts each line as a empty string
87
+
for(letj=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(leti=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
+
letpattern="";
108
+
for(letj=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
+
letrow=5;
133
+
for(leti=0;i<row;i++){
134
+
letpattern="";
135
+
for(letj=0;j<row-i;j++){
136
+
pattern+=" ";
137
+
}
138
+
for(letk=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
+
***
6
148
*****
149
+
*/
150
+
letx=3;
151
+
152
+
//initalising the outer loop
153
+
for(leti=1;i<=x;i++){
154
+
letpattern="";
155
+
//loop for spacing viz x-i
156
+
for(letj=1;j<=x-i;j++){
157
+
pattern+=" ";
158
+
}
159
+
//loop for creating stars viz odd no. per row (2*i-1)
160
+
for(letk=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
+
***
7
170
*****
171
+
*******
172
+
*********
8
173
*/
9
-
for(leti=0;i<5;i++){
10
-
for(letj=0;j<=i;j++){
11
-
console.log("*");
174
+
175
+
letn=5;
176
+
letstring="";
177
+
//External loop
178
+
for(leti=1;i<=n;i++){
179
+
// printing spaces
180
+
for(letj=1;j<=n-i;j++){
181
+
string+=" ";
182
+
}
183
+
// printing star
184
+
for(letk=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
0 commit comments