Skip to content

Commit e54dd2d

Browse files
added new file here
1 parent 737649e commit e54dd2d

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

D. Loop/Break/century.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,24 @@
22
33
Write a loop 1 to 200. Use break to exit the loop once you find 100.
44
*/
5-
// Solution-2 by for loop
6-
for(let j=1; j<=200; j++){
7-
if(j<=100){
5+
6+
// Solution-1 by while loop
7+
let i = 0;
8+
while (i <= 200) {
9+
i++;
10+
console.log(i);
11+
if (i >= 100) {
812
break;
913
}
14+
}
15+
16+
17+
// Solution-2 by for loop
18+
for (let j = 1; j <= 200; j++) {
1019
console.log(j);
11-
}
20+
if (j >= 100) {
21+
break;
22+
}
23+
}
24+
25+

D. Loop/Break/sum.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* Problem-2
2+
Write a while loop that adds numbers starting from 1, but stops (using break) as soon as the sum reaches or exceeds 100
3+
*/
4+
let i = 1;
5+
let sum = 0;
6+
while (i <= 100) {
7+
i++;
8+
sum = sum + i;
9+
if (sum = 100) {
10+
break;
11+
}
12+
}
13+
console.log(sum);

0 commit comments

Comments
 (0)