We are going to move away from input via "prompt" and output via "alert".
Those are fine for smaller interactive programs, or for occasional use. But for many web pages input comes from text fields on the page. Often output is reported similarly, in text fields on the page. (Another way is to write a completely new web page, but we will save that for later).
In this program we will type input values into text fields on the web page.
In addition to a new form of I/O, in this program we will practice writing and calling functions. We will also get more practice in writing sequences of conditional statements, and in input data validation.
It might make more sense to read the description of the program functionality while viewing the HTML template I have prepared for you to use on this program.
The user will type into a text window the number that the selected function will convert. When the "convert" button is clicked, your program will excute and compute the selected function on the given input value. The value returned by your program will be installed in the last text field (this installation happens automatically by the HTML template).
Your program will have some things to do right at the beginning, before the selected function is computed:
Once the inputs are validated, your program will then decide (based on the conversion type value) which of the 4 functions to invoke; it will call that function, pass the input value as argument, and will then return the value that function computes. Depending on how much validation (if any) you leave to be done in the functions themselves, the returned "result" might be an error message.
Here are the details for each of the 4 functions:
Make sure your program properly detects and rejects all invalid inputs... like passing a non-number, or passing a negative number in when you ask for the feet to inches conversion, or passing a non-integer to the summing function.
The following test cases are all given as ordered pairs, where (i,o) means that with input i the function will produce output o.
Feet to Inches: (1, 12) (10, 120) (0.5, 6) (0.01, 0.12) (22.7, 272.4) (131.3, 1575.6000000000001)
Fahrenheit to Celsius:
(32,0) (212,100) (0,-17.77777777777778) (-100,-73.33333333333333)
Pounds to Kilograms:
(2.20462,1) (1,0.45359290943563974) (10,45.35929094356398)
(177177717777,80366556493.6361) (2.20462,1) (22.0462,10)
(1,1) (8,19) (11,38) (14,64) (23,176) (24,200) (213,18353)
Growing your program means adding a little piece of new code at a time. Do not type in 100 lines of code and then hope it works. It won't... and then you won't be able to easily find the line(s) that are causing the problem(s). Don't add 50 lines at a time; don't add 30, or 15. Add a few lines... 4, 5, maybe 10. Compile, test. Then add more.
You might also have the stub return a "fake value" of the correct type, range, and format so that the rest of the program that is calling it can use the stub. This means that if the function supposed to compute and return some non-negative number, then just put in "return 1" so that it at least appears to be working.
Once it is all connected up correctly and running when called, you can go back and make it produce the correct results and replace the "fake return" with the correct return value.