0

please excuse my ignorance; I am learning JavaScript from scratch with no background in coding.

Here is the question: 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.

Construct while loops to do the following: Prompt the user to enter the starting fuel level. The loop should continue until the user enters a positive value greater than 5000 but less than 30000. I am very confused by the question, can you please explain it to me?

Use a second loop to query the user for the number of astronauts (up to a maximum of 7). Validate the entry by having the loop continue until the user enters an integer from 1 - 7.

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. (Hint: The loop should end when there is not enough fuel to boost the crew another 50 km, so the fuel level might not reach 0).

After the loops complete, output the result with the phrase, The shuttle gained an altitude of ___ km.

If the altitude is 2000 km or higher, add "Orbit achieved!" Otherwise add, "Failed to reach orbit."

And here is my code for the question to answer first part:

  const input = require("readline-sync")

let startingFuelLevel=0;
let numberAstronautsAboard =0;
let altitudeshuttleReaches =0;

//a:
while (startingFuelLevel <= 5000 || startingFuelLevel >30000){
     startingFuelLevel = input.question("Enter the starting fuel level: ")
};

//b:
while(numberAstronautsAboard < 1 || numberAstronautsAboard > 7){
  numberAstronautsAboard = input.question("Enter the number of astronauts: ")
}
6
  • 2
    You are supposed to keep on asking the user for the fuel level, in a loop, until the value they have entered fulfills the conditions (i.e., is > 5000 and < 30000). From your code, it looks like you did that already. So what is the actual question here now? Commented Mar 23, 2021 at 13:58
  • 2
    If you are struggling with the second part, then your question should focus on that. Don’t put so much emphasis on the previous part that you already got then, that just adds unnecessary noise to the question. Can you show what you tried, for that second part? Describe what exactly the problem is? Commented Mar 23, 2021 at 14:01
  • 1
    also, your labels are switched, the number of astronauts is being used for fuel level Commented Mar 23, 2021 at 14:07
  • Thank you for the feedback, I wrote the first part because I was not sure if what I have done is right!! Commented Mar 23, 2021 at 14:10
  • for part b: Use a second loop to query the user for the number of astronauts (up to a maximum of 7). Validate the entry by having the loop continue until the user enters an integer from 1 - 7. Here is my answer which I am not sure if it is right too!! while(numberAstronautsAboard < 1 || numberAstronautsAboard > 7){ numberAstronautsAboard = input.question("Enter the starting fuel level: ") } Commented Mar 23, 2021 at 14:11

1 Answer 1

1

JSFiddle (pure js): https://jsfiddle.net/DariusM/14parbue/18/

Apart from the messages, a condition and a check for isNaN, it seems that you got the first two parts somewhat right - the user input.

As for the third loop, you can use setInterval() to run a function repeatedly, each time doing the following:

  1. decrease fuel level
  2. increase altitude
  3. check if there is enough fuel for another iteration.

Depending on the check at (3) above, you can then show a certain message to the user depending on the current altitude.

Then, after the fuel is finished and the final message is displayed to the user, the interval needs to be cleared using clearInterval()

Good luck with your endeavor and happy coding!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.