Skip to content

Commit b434bc2

Browse files
committed
while loop exercises
1 parent 1b99d7b commit b434bc2

File tree

1 file changed

+44
-10
lines changed

1 file changed

+44
-10
lines changed
Lines changed: 44 additions & 10 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-
3-
4-
2+
const input = require('readline-sync');
3+
let fuel = 0, astronautsNum = 0, altitude = 0;
54

65

76
/*Exercise #4: Construct while loops to do the following:
87
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. */
98

9+
while (fuel <= 5000 || fuel > 30000 || isNaN(fuel)) {
10+
fuel = input.question("Enter the starting fuel level: ");
11+
12+
if (fuel <= 5000 || fuel > 30000 || isNaN(fuel)){
13+
console.log("That is an invalid fuel level, please make the correct adjustments."); } else {
14+
break;
15+
}
16+
}
17+
if (fuel > 5000 && fuel <= 30000 && !isNaN(fuel)) {
18+
console.log("Achieved valid fuel levels.");
19+
} else {
20+
console.log("Invalid fuel levels.")
21+
}
1022

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

25+
while (astronautsNum <= 0 || astronautsNum > 7 || isNaN(astronautsNum)) {
26+
astronautsNum = input.question("How many astronauts are aboard the ship?: ");
1227

28+
if (astronautsNum <= 0 || astronautsNum > 7 || isNaN(astronautsNum)) {
29+
console.log("Invalid number of astronauts. Please enter a valid number between 1 and 7.");
30+
} else {
31+
break;
32+
}
33+
}
1334

14-
//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.
35+
if (astronautsNum > 0 && astronautsNum <= 7 && !isNaN(astronautsNum)) {
36+
console.log("Number of astronauts is acceptable.");
37+
} else {
38+
console.log("Invalid number of astronauts. Please stand by.");
39+
}
2040

2141

42+
//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.
43+
while (fuel-100*astronautsNum >= 0) {
44+
altitude += 50;
45+
fuel -= 100*astronautsNum;
46+
}
47+
console.log(`Fuels status: @ ${fuel} gallons`);
48+
console.log(`Altitude status: @ ${altitude} km`);
2249

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

0 commit comments

Comments
 (0)