-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday4.js
More file actions
142 lines (117 loc) · 1.96 KB
/
day4.js
File metadata and controls
142 lines (117 loc) · 1.96 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Activity 1: For Loop
// Task 1: WAP to print numbers from 1 to 10 using a for loop.
for (let i = 1; i <= 10; i++)
console.log(i);
//output:
// 1
// 2
// 3
// 4
// 5
// 6
// 7
// 8
// 9
// 10
// Task 2: WAP to print the multiplication table of 5 using a for loop.
for (let i = 1; i <= 10; i++)
console.log(`5*${i}=${5 * i}`);
// Output:
// 5*1=5
// 5*2=10
// 5*3=15
// 5*4=20
// 5*5=25
// 5*6=30
// 5*7=35
// 5*8=40
// 5*9=45
// 5*10=50
// Activity 2: While Loop
// Task 3: WAP to calculate the sum of numbers from 1 to 10 using a while loop.
let sum = 0;
for (let i = 1; i <= 10; i++)
sum += i;
console.log(sum);
// output:55
// Task 4: WAP to print numbers from 10 to 1 using a while loop.
let i = 10;
while (i > 0) {
console.log(i--);
}
//output:
// 10
// 9
// 8
// 7
// 6
// 5
// 4
// 3
// 2
// 1
// Activity 3: Do While Loop
// Task 5: WAP to print numbers from 1 to 5 using a do...while loop.
let num = 1;
do {
console.log(num++);
} while (num <= 5);
// output:
// 1
// 2
// 3
// 4
// 5
// Task 6: WAP to calculate the factorial of a number using a do...while loop.
let fac = 1;
num = 7;
do {
fac *= num--;
} while (num != 0)
console.log(fac);
// output:5040
// Activity 4: Nested Loops
// Task 7: WAP to print a pattern using nested for loops:
for (let i = 1; i <= 5; i++) {
let row = '';
for (let j = 1; j <= i; j++) {
row += '*';
}
console.log(row);
}
// Pattern
// *
// * *
// * * *
// * * * *
// * * * * *
// Activity 5: Loop Control Statements
// Task 8: WAP to print numbers from 1 to 10, but skip the number 5 using the continue statement.
for (let i = 1; i <= 10; i++) {
if (i == 5)
continue;
console.log(i);
}
// output
// 1
// 2
// 3
// 4
// 6
// 7
// 8
// 9
// 10
// Task 9: WAP to print numbers from 1 to 10, but stop the loop when the number is 7 using the break statement.
for (let i = 1; i <= 10; i++) {
if (i == 7)
break;
console.log(i);
}
// output
// 1
// 2
// 3
// 4
// 5
// 6