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 other programming languages it is not unusual for input data to come from files stored on the hard drive. JavaScript can do this too, but many JavaScript programs are intended for use in Web pages, so file use is not nearly as common in JavaScript as in other languages.
In this program we will type input values into text fields on the web page.
Then when the "run" button is clicked, the values we typed will be transmitted to your JavaScript main function as arguments. When the program returns, it will return a string (either containing the result or an error message) and that string will be installed into a text field on the web page so it can be viewed in the browser.
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.
You are to write a JavaScript program that allows the user to select one of four different functions to be computed:
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:
Return value
The main function will be returning a string
(it is being installed into a text field on the web page).
This means that if the inputs are valid and you compute
a conversion function, the number it produces will get converted
to text automatically by JavaScript. It also means that
if something fails (like invalid input values) you can simply
have the main function return a string that is an error
message (something like return "input value is not in range").
The user will see the error message in the text field on
the web page where the result would be.
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)
(73.4,23.000000000000004) (45,7.222222222222222)
Pounds to Kilograms:
(2.20462,1) (1,0.45359290943563974) (10,45.35929094356398)
(177177717777,80366556493.6361) (2.20462,1) (22.0462,10)
Non-primes Sum:
(1,1) (8,19) (11,38) (14,64) (23,176) (24,200) (213,18353)
(1101,513698) (724680,242352142155)
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.