Skip to content

Commit 928b204

Browse files
Quinton KornegayQuinton Kornegay
authored andcommitted
while loops
1 parent 35eec70 commit 928b204

1 file changed

Lines changed: 36 additions & 2 deletions

File tree

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,59 @@
11
//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+
const input = require('readline-sync');
23

4+
let fuelLevel = 0;
5+
let numAstronauts = 0;
6+
let altitude = 0;
7+
let output = `The shuttle gained an altitude of ${altitude} km` ;
38

49

510

611

712
/*Exercise #4: Construct while loops to do the following:
813
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. */
914

15+
1016

17+
while (fuelLevel <= 5000 || fuelLevel > 30000 || isNaN(fuelLevel)) {
18+
fuelLevel = input.question("Enter the starting fuel level: ");
19+
20+
}
21+
if (fuelLevel[input] = fuelLevel <= 1 || fuelLevel >7 || isNaN(fuelLevel)) {
22+
23+
}
24+
25+
26+
1127

1228

1329

1430
//b. Use a second loop to query the user for the number of astronauts (up to a maximum of 7). Validate the entry.
1531

32+
while (numAstronauts <= 1 || numAstronauts > 7 || isNaN(numAstronauts)) {
33+
numAstronauts = input.question("How many astronauts: ");
34+
}
35+
if (numAstronauts[input] = numAstronauts <= 1 || numAstronauts > 7 || isNaN(numAstronauts)) {
36+
37+
}
1638

17-
18-
39+
40+
1941
//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.
2042

43+
while (fuelLevel-100*numAstronauts >= 0) {
44+
altitude = altitude + 50;
45+
fuelLevel = fuelLevel-100*numAstronauts;
46+
} console.log("The shuttle gained and altitude of " + altitude + " km.")
47+
48+
2149

2250

2351
/*Exercise #5: Output the result with the phrase, “The shuttle gained an altitude of ___ km.”
2452
2553
If the altitude is 2000 km or higher, add “Orbit achieved!” Otherwise add, “Failed to reach orbit.”*/
54+
55+
if (altitude >=2000) {
56+
output = console.log("Achieved Orbit!");
57+
} else {
58+
console.log("Failed to reach Orbit!");
59+
}

0 commit comments

Comments
 (0)