Browse Source

While-loop: correction in class

master
AzureAD\SigridLievens 2 years ago
parent
commit
95b14f7db1
1 changed files with 42 additions and 11 deletions
  1. 42
    11
      while-loop.js

+ 42
- 11
while-loop.js View File

@@ -1,26 +1,57 @@
let input = require('readline-sync');

let fuelLevel = 7000;
let astronauts = 1;
let altitude_km = 50;

while (fuelLevel < 5000 || fuelLevel > 30_000) {
console.log("Enter starting fuel level:");
}
// while (fuelLevel < 5000 || fuelLevel > 30_000) {
// console.log("Enter starting fuel level:");
// }

while (astronauts < 1 || astronauts > 7) {
console.log("Enter the number of astronauts");
// while (astronauts < 1 || astronauts > 7) {
// console.log("Enter the number of astronauts");
// }

while (true) {
fuelLevel = input.question('Enter starting fuel level:');
fuelLevel = Number(fuelLevel);
// anders wordt deze input als string geïnterpreteerd
if (fuelLevel > 5_000 && fuelLevel < 30_000) {
break;
}
}

while (true) {
astronauts = input.question('Enter number of astronauts:');

while (fuelLevel > 100) {
fuelLevel -= 100;
altitude_km += 50;
if (fuelLevel < 100) {
astronauts = Number(astronauts);
// anders wordt deze input als string geïnterpreteerd

if (!Number.isInteger(astronauts)) {
continu;
}
// als het nummer geen integer is (bv. hallo, kommagetal,...), wordt de loop verder uitgevoerd

if (astronauts >= 1 && astronauts <= 7) {
break;
}
}

console.log(fuelLevel);
console.log(altitude_km);
// while (fuelLevel > 100) {
// fuelLevel -= 100;
// altitude_km += 50;
// if (fuelLevel < 100) {
// break;
// }
// }

while (fuelLevel - 100 * astronauts >= 0) {
altitude_km += 50;
fuelLevel -= 100 * astronauts;
}

console.log(`Fuel level: ${fuelLevel}`);
console.log(`Altitude: ${altitude_km}`);

if (fuelLevel === 100) {
console.log("Fuel level is 100 units, impossible to increase altitude !")

Loading…
Cancel
Save