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
48 lines (27 loc) · 1.58 KB
/
Copy pathwhile-Loop-Exercises.js
File metadata and controls
48 lines (27 loc) · 1.58 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
//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 fuelLevel = '';
let numAstronauts = '';
let shuttleAltitude = '';
/*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 input = require('readline-sync');
while (fuelLevel <= 5000 || fuelLevel > 30000 || isNaN(fuelLevel)) {
fuelLevel = input.question("Enter the starting fuel level: ");
}
//b. Use a second loop to query the user for the number of astronauts (up to a maximum of 7). Validate the entry.
while (numAstronauts < 1 || numAstronauts >= 7) {
numAstronauts = input.question("Enter the number of astronauts. ");
}
//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.
while (fuelLevel - 100 * numAstronauts >= 0) {
((shuttleAltitude += 50) && (fuelLevel -= 100 * numAstronauts));
break;
}
if (shuttleAltitude >= 2000) {
console.log('Orbit achieved!')
} else {
console.log('Failed to reach orbit.')
}
console.log(`The shuttle gained an altitude of ${shuttleAltitude}km.`)
/*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.”*/