Mars Adventure: Further Study#

Optional further study for the Mars Adventure lab.

More lenient output#

At the beginning of the adventure game, we ask the user to choose Y or N. While this is short and straighforward, it’s possible the user might type the full word yes or no. One way to implement this is with the string method, startsWith().

Click here to research how to use startsWith(). Be sure to try it out in the JavaScript console to understand how it works.

Then, use it to make your program a bit more lenient on the user’s varying input.

Stripping whitespace#

When we ask the user for input, it’s possible that they would type a space either before or after their answer. A string like ' y ' will not evaluate to be equal to the string 'y'. Go ahead and confirm that this breaks in your current program by answering the yes/no question with spaces around your answer.

JavaScript has a string method called trim() that removes leading and trailing whitespace from a string. Use this method each time you prompt the user for input to allow the user to put spaces around their answer. You can learn more about trim() here.

More features#

Using the skills you’ve practiced so far in this journey, add any of the following features:

Easier#

  • Make the countdown more dramatic (bigger, more dramatic liftoff statement)

  • Ask the user for a second companion animal, and incorporate them into the story at some point.

  • Add more spaceship decor options. (You’ll need to add more conditional logic as well, in case the user selects the new option.)

  • Add the user’s name to print statements throughout the program to keep them engaged.

Harder#

  • Ask the user which planet they’d like to go to. If they say they’d like to go to Saturn, tell them the spaceship for Saturn left last week. If they’d like to go to Venus, tell them it’s not worth the trip. If they’d like to go to Mercury, assure them that it’s the wrong season for Mercury. If they type anything else, just assure them that Mars is far more interesting.