Question
JAVASCRIPT PROGRAM The goal of this assignment is to use functions. Try to contain as much of your code as possible inside of functions that
JAVASCRIPT PROGRAM
The goal of this assignment is to use functions. Try to contain as much of your code as possible inside of functions that are well named and can be reused. At a minimum your code must use four functions.
You are to create a program that aids an astronaut traveling to the planets (and moons) of our solar system. The planetary bodies in question are
- MERCURY
- VENUS
- EARTH
- MOON
- MARS
- JUPITER
- SATURN
- URANUS
- NEPTUNE
- PLUTO
The relative gravity on these planets (relative to earth) is as follows:
MERCURY | VENUS | EARTH | MOON | MARS | JUPITER | SATURN | URANUS | NEPTUNE | PLUTO |
---|---|---|---|---|---|---|---|---|---|
0.378 | 0.907 | 1 | 0.166 | 0.377 | 2.36 | 0.916 | 0.889 | 1.12 | 0.071 |
This means that if you weight 175 pounds (79.379 Kg) on Earth and you traveled to Jupiter, you would weigh 175 * 2.36 = 413 pounds or 79.379 * 2.36 = 187.33 Kg. Similarly if you went to the moon, you would weigh 175 * 0.166 = 29.05 pounds or 79.379 * 0.166 = 13.177 Kg.
You are to create a program that asks the user to enter a weight (it actually doesn't matter if it is in Kg or pounds) and then asks them which planet they would like to visit. The program will tell the user what they would weigh on that planet (or moon).
In this program you will be using prompts to get the values from the user and you will use an alert to display the final message to the user. The user will not ever need to open the console to use the program.
Requirements
Use the table above as the conversion ratios for the various planets. Build functions that perform the following tasks.
- Ask the user to enter a weight and return the number they entered.
- Ask the user to enter a planet and return the name of the planet they entered.
- Calculate the weight of the user on the other planet and return the new weight.
- Print an output message to the user in an alert.
Program expectations
- Your code must use a minimum of the four functions described above.
- The functions that produce new data must have return statements that correctly return values.
- The functions that gather input do not need parameters as they do not need to accept input.
- The conversion function should accept input parameters and return a value.
- The print function should also take input parameters.
- The final message should display the converted weight rounded to two decimal places.
- For extra credit you can enhance this program by allowing the user to tell you their weight and the planet they are currently on. Then ask them which planet they are visiting and output their weight on that planet. A hint to get you started is that it makes sense to convert everything to a known weight (that of Earth) and then convert to the weight on the other planet. I would probably make use two conversion functions. One function to convert from any planet weight to Earth weight and another to convert from Earth weight to any other planet weight.
Some general coding guidelines
- Add 'use strict' at the top of every js file.
- Parameter names in functions should have a space after each one.
- Don't put a space between the name of the function and the opening parenthesis.
- Do put a space between the closing parenthesis and the opening curly brace.
- The opening curly brace should be on the same line as the function declaration.
- The closing curly brace should be on a line all by itself.
- Code within a function should be indented.
- Function naming conventions are the same as variable naming conventions (camelCase etc.)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started