Skip to content

Commit 3658a6b

Browse files
committed
for +while
1 parent 5e16b92 commit 3658a6b

File tree

3 files changed

+123
-22
lines changed

3 files changed

+123
-22
lines changed

loops/exercises/for-Loop-Exercises.js

Lines changed: 84 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,89 @@
11
/*Exercise #1: Construct for loops that accomplish the following tasks:
22
a. Print the numbers 0 - 20, one number per line.
3+
34
b. Print only the ODD values from 3 - 29, one number per line.
45
c. Print the EVEN numbers 12 to -14 in descending order, one number per line.
56
d. Challenge - Print the numbers 50 - 20 in descending order, but only if the numbers are multiples of 3. (Your code should work even if you replace 50 or 20 with other numbers). */
6-
7-
8-
9-
10-
/*Exercise #2:
11-
Initialize two variables to hold the string “LaunchCode” and the array [1, 5, ‘LC101’, ‘blue’, 42].
12-
13-
14-
Construct ``for`` loops to accomplish the following tasks:
15-
a. Print each element of the array to a new line.
16-
b. Print each character of the string - in reverse order - to a new line. */
17-
18-
19-
20-
21-
22-
/*Exercise #3:Construct a for loop that sorts the array [2, 3, 13, 18, -5, 38, -10, 11, 0, 104] into two new arrays:
23-
a. One array contains the even numbers, and the other holds the odds.
24-
b. Print the arrays to confirm the results. */
7+
for (let a=0 ; a<=20 ;a++){
8+
console.log(a,"\n");
9+
}
10+
11+
for (let b= 3; b< 29 ;b = b + 2 ) {
12+
13+
console.log( b,"\n")
14+
}
15+
16+
for (let c=12;c >-14; c=c-2){
17+
console.log(c,"\n");
18+
}
19+
20+
for (d=50; d >20 ; d--){
21+
22+
if ( d % 3 === 0 ) {
23+
console.log(d,"\n");
24+
25+
}
26+
}
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
39+
40+
41+
/*Exercise #2:
42+
Initialize two variables to hold the string “LaunchCode” and the array [1, 5, ‘LC101’, ‘blue’, 42].
43+
44+
45+
Construct ``for`` loops to accomplish the following tasks:
46+
a. Print each element of the array to a new line.
47+
b. Print each character of the string - in reverse order - to a new line. */
48+
let str = ('LaunchCode');
49+
let arr= [1, 5, 'LC101', 'blue', 42];
50+
51+
for (let i = 0; i<arr.length;i++){
52+
53+
console.log(arr[i] );
54+
55+
}
56+
let rev= []
57+
rev= str.split('').reverse().join('');
58+
for (let i = 0; i<str.length;i++){
59+
60+
console.log(rev[i]);
61+
62+
63+
64+
65+
66+
}
67+
68+
69+
70+
71+
/*Exercise #3:Construct a for loop that sorts the array [2, 3, 13, 18, -5, 38, -10, 11, 0, 104] into two new arrays:
72+
a. One array contains the even numbers, and the other holds the odds.
73+
b. Print the arrays to confirm the results. */
74+
75+
let set = [2, 3, 13, 18, -5, 38, -10, 11, 0, 104] ;
76+
let odd= [];
77+
let even= [];
78+
let i = 0 ;
79+
for ( i=0 ;i<set.length ;i++){
80+
if ( set[i]%2===0 ){
81+
82+
even.push(set[i]);
83+
}else{
84+
odd.push(set[i]);
85+
}
86+
87+
}
88+
console.log(even);
89+
console.log(odd);
Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,55 @@
1-
//Define three variables for the LaunchCode shuttle - one for the starting fuel level, another for the number of astronauts aboard, and the third for the altitude the shuttle reaches.
2-
1+
const input = require('readline-sync');
32

43

4+
//Define three variables for the LaunchCode shuttle - one for the starting fuel level, another for the number of astronauts aboard, and the third for the altitude the shuttle reaches.
55

66

77
/*Exercise #4: Construct while loops to do the following:
88
a. Query the user for the starting fuel level. Validate that the user enters a positive, integer value greater than 5000 but less than 30000. */
99

1010

11+
12+
let fuel = input.question("Fuel Level ?");
13+
fuel = Number(fuel);
14+
while (5000.1>fuel || fuel >30000){
15+
fuel = input.question("Invalid Fuel Level !");
16+
fuel= Number(fuel);
17+
}
1118

1219

1320

1421
//b. Use a second loop to query the user for the number of astronauts (up to a maximum of 7). Validate the entry.
22+
let crew = input.question("Number of crew ?");
23+
crew= Number(crew);
1524

16-
25+
while (0>crew || crew>7){
26+
crew = input.question("Invalid crew total");
27+
crew= Number(crew);
28+
}
1729

1830

1931
//c. Use a final loop to monitor the fuel status and the altitude of the shuttle. Each iteration, decrease the fuel level by 100 units for each astronaut aboard. Also, increase the altitude by 50 kilometers.
2032

33+
let alt =0 ;
2134

2235

36+
while (fuel > 0 ){
37+
38+
fuel= fuel - (crew * 100 );
39+
alt = alt + 50 ;
40+
41+
42+
43+
}
44+
while (alt >= 2000){
45+
console.log("The shuttle gained an altitude of", alt,"km.\n orbit acheived !" );
46+
break
47+
}
48+
while (alt<2000){
49+
console.log("The shuttle gained an altitude of" ,alt,"Failed to reach orbit." ) ;
50+
break
51+
}
52+
2353
/*Exercise #5: Output the result with the phrase, “The shuttle gained an altitude of ___ km.”
2454
2555
If the altitude is 2000 km or higher, add “Orbit achieved!” Otherwise add, “Failed to reach orbit.”*/

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)