|
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 | +const input = require('readline-sync'); |
2 | 3 |
|
| 4 | +let fuelLevel = 0; |
| 5 | +let numAstronauts = 0; |
| 6 | +let altitude = 0; |
| 7 | +let output = `The shuttle gained an altitude of ${altitude} km` ; |
3 | 8 |
|
4 | 9 |
|
5 | 10 |
|
6 | 11 |
|
7 | 12 | /*Exercise #4: Construct while loops to do the following: |
8 | 13 | 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 | 14 |
|
| 15 | + |
10 | 16 |
|
| 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 | + |
11 | 27 |
|
12 | 28 |
|
13 | 29 |
|
14 | 30 | //b. Use a second loop to query the user for the number of astronauts (up to a maximum of 7). Validate the entry. |
15 | 31 |
|
| 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 | +} |
16 | 38 |
|
17 | | - |
18 | | - |
| 39 | + |
| 40 | + |
19 | 41 | //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. |
20 | 42 |
|
| 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 | + |
21 | 49 |
|
22 | 50 |
|
23 | 51 | /*Exercise #5: Output the result with the phrase, “The shuttle gained an altitude of ___ km.” |
24 | 52 |
|
25 | 53 | 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