-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_4.js
149 lines (96 loc) · 1.91 KB
/
day_4.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
// Activity 1: For loop
// Task 1
function print1to10() {
for(let i = 1; i <= 10; i++) {
// console.log() prints every digit in newline
// console.log(i);
// while using process.stdout we can print the digits in same line
process.stdout.write(`${i} `);
}
console.log();
}
print1to10();
// Task 2
function multiplicationTable(number) {
for(let i = 1; i <= 10; i++) {
console.log(`${number} X ${i} = ${number * i}`);
}
}
multiplicationTable(5);
// Activity 2: While loop
// Task 3
function sum1to10() {
let limit = 10;
let sum = 0;
while(limit) {
sum += limit;
limit--;
}
console.log(sum);
}
sum1to10();
// Task 4
function print10to1() {
let limit = 10;
while(limit) {
process.stdout.write(`${limit} `);
limit--;
}
console.log();
}
print10to1();
// Activity 3: DO...While loop
// Task 5
function print1to5() {
let limit = 1;
do {
process.stdout.write(`${limit} `);
limit++;
} while(limit <= 5);
console.log();
}
print1to5();
// Task 6
function factorial(num) {
let fact = 1;
do {
fact *= num;
num--;
} while(num);
console.log(fact);
}
factorial(10);
// Activity 4: Nested loops
// Task 7
function printPattern(height) {
for(let i = 1; i <= height; i++) {
for(let j = 1; j <= i; j++) {
process.stdout.write(`* `);
}
console.log();
}
}
printPattern(5);
// Activity 5: Loop control statements
// Task 8
function skip5() {
for(let i = 1; i<=10; i++) {
if(i == 5) {
continue;
}
process.stdout.write(`${i} `);
}
console.log();
}
skip5();
// Task 9
function breakAt7() {
for(let i = 1; i<=10; i++) {
if(i == 7) {
break;
}
process.stdout.write(`${i} `);
}
console.log();
}
breakAt7();