forked from LaunchCodeEducation/javascript-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhile-Loop-Exercises.js
More file actions
59 lines (47 loc) · 2.02 KB
/
Copy pathwhile-Loop-Exercises.js
File metadata and controls
59 lines (47 loc) · 2.02 KB
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
//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.
let startingFuelLevel = 100;
let numberOfAstronauts = 5;
let altitudeReached = 0;
/*Exercise #4: Construct while loops to do the following:
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. */
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
while (
startingFuelLevel <= 0 ||
startingFuelLevel < 5000 ||
startingFuelLevel >= 30000 ||
!Number.isInteger(startingFuelLevel)
) {
startingFuelLevel = parseInt(
prompt("Enter the starting fuel level (between 5000 and 30000, inclusive):")
);
}
//b. Use a second loop to query the user for the number of astronauts (up to a maximum of 7). Validate the entry.
while (
numberOfAstronauts <= 0 ||
numberOfAstronauts > 7 ||
!Number.isInteger(numberOfAstronauts)
) {
numberOfAstronauts = parseInt(
prompt("Enter the number of astronauts (up to 7):")
);
}
//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.
let fuelLevel = startingFuelLevel;
let currentAltitude = altitudeReached;
while (fuelLevel > 0 && currentAltitude < 300) {
fuelLevel -= numberOfAstronauts * 100;
currentAltitude += 50;
}
/*Exercise #5: Output the result with the phrase, “The shuttle gained an altitude of ___ km.”
If the altitude is 2000 km or higher, add “Orbit achieved!” Otherwise add, “Failed to reach orbit.”*/
if (currentAltitude >= 2000) {
console.log(`The shuttle gained an altitude of ${currentAltitude} km.`);
console.log("Orbit achieved!");
} else {
console.log(`The shuttle gained an altitude of ${currentAltitude} km.`);
console.log("Failed to reach orbit.");
}