You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: loops/exercises/while-Loop-Exercises.js
+35-14Lines changed: 35 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -1,24 +1,45 @@
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
-
3
-
4
-
1
+
constINPUT=require('readline-sync');
2
+
constMIN_FUEL_LEVEL=5001;
3
+
constMAX_FUEL_LEVEL=29999;
4
+
constMAX_NUMBER_ASTRONAUTS=7;
5
+
constFUEL_USED_PER_ASTRONAUT=100;
6
+
constALTITUDE_UPDATES_KM=50;
7
+
constORBIT_ALTITUDE_KM=2000;
5
8
9
+
//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.
10
+
letstartingFuelLevel=0;
11
+
letnumberOfAstronauts=-1;
12
+
letaltitudeShuttleReachesKM=0;
13
+
letuserInput='';
6
14
7
15
/*Exercise #4: Construct while loops to do the following:
8
16
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. */
userInput=INPUT.question(`What is the starting fuel level (positive interger greater than ${MIN_FUEL_LEVEL} but less than ${MAX_FUEL_LEVEL}): `);
19
+
startingFuelLevel=Number(userInput);
20
+
}
21
+
console.log(`Starting fuel level is ${startingFuelLevel}.`)
13
22
14
23
//b. Use a second loop to query the user for the number of astronauts (up to a maximum of 7). Validate the entry.
15
-
16
-
17
-
18
-
19
-
//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.
userInput=INPUT.question(`What is the number of astronauts on board (up to ${MAX_NUMBER_ASTRONAUTS}): `);
26
+
numberOfAstronauts=Number(userInput);
27
+
}
28
+
console.log(`Number of astronauts is ${numberOfAstronauts}`);
21
29
30
+
//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.
0 commit comments