You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

while-loop.js 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. let input = require('readline-sync');
  2. let fuelLevel = 7000;
  3. let astronauts = 1;
  4. let altitude_km = 50;
  5. // while (fuelLevel < 5000 || fuelLevel > 30_000) {
  6. // console.log("Enter starting fuel level:");
  7. // }
  8. // while (astronauts < 1 || astronauts > 7) {
  9. // console.log("Enter the number of astronauts");
  10. // }
  11. while (true) {
  12. fuelLevel = input.question('Enter starting fuel level:');
  13. fuelLevel = Number(fuelLevel);
  14. // anders wordt deze input als string geïnterpreteerd
  15. if (fuelLevel > 5_000 && fuelLevel < 30_000) {
  16. break;
  17. }
  18. }
  19. while (true) {
  20. astronauts = input.question('Enter number of astronauts:');
  21. astronauts = Number(astronauts);
  22. // anders wordt deze input als string geïnterpreteerd
  23. if (!Number.isInteger(astronauts)) {
  24. continu;
  25. }
  26. // als het nummer geen integer is (bv. hallo, kommagetal,...), wordt de loop verder uitgevoerd
  27. if (astronauts >= 1 && astronauts <= 7) {
  28. break;
  29. }
  30. }
  31. // while (fuelLevel > 100) {
  32. // fuelLevel -= 100;
  33. // altitude_km += 50;
  34. // if (fuelLevel < 100) {
  35. // break;
  36. // }
  37. // }
  38. while (fuelLevel - 100 * astronauts >= 0) {
  39. altitude_km += 50;
  40. fuelLevel -= 100 * astronauts;
  41. }
  42. console.log(`Fuel level: ${fuelLevel}`);
  43. console.log(`Altitude: ${altitude_km}`);
  44. if (fuelLevel === 100) {
  45. console.log("Fuel level is 100 units, impossible to increase altitude !")
  46. }
  47. if (fuelLevel > 5000 && fuelLevel > 30_000 && astronauts > 0 && astronauts < 7 && fuelLevel === 100) {
  48. console.log(`The shuttle gained an altitude of ${altitude_km} km.`);
  49. }
  50. if (altitude_km >= 2000) {
  51. console.log("Orbit achieved!");
  52. } else {
  53. console.log("Failed to reach orbit.");
  54. }