|
1 | 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 |
| - |
| 2 | +const input = require('readline-sync'); |
| 3 | +let fuel = 0, astronautsNum = 0, altitude = 0; |
5 | 4 |
|
6 | 5 |
|
7 | 6 | /*Exercise #4: Construct while loops to do the following:
|
8 | 7 | 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. */
|
9 | 8 |
|
| 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 | + } |
10 | 22 |
|
| 23 | +//b. Use a second loop to query the user for the number of astronauts (up to a maximum of 7). Validate the entry. |
11 | 24 |
|
| 25 | +while (astronautsNum <= 0 || astronautsNum > 7 || isNaN(astronautsNum)) { |
| 26 | + astronautsNum = input.question("How many astronauts are aboard the ship?: "); |
12 | 27 |
|
| 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 | +} |
13 | 34 |
|
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 | +} |
20 | 40 |
|
21 | 41 |
|
| 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`); |
22 | 49 |
|
23 | 50 | /*Exercise #5: Output the result with the phrase, “The shuttle gained an altitude of ___ km.”
|
24 | 51 |
|
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