File tree Expand file tree Collapse file tree 2 files changed +31
-4
lines changed Expand file tree Collapse file tree 2 files changed +31
-4
lines changed Original file line number Diff line number Diff line change 22
33Write 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+
Original file line number Diff line number Diff line change 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 ) ;
You can’t perform that action at this time.
0 commit comments