You are on page 1of 3

ASSIGNMENT 5: Doing Math Computations with Variables Purpose.

The purpose of this assignment is to show the difference between two types of data text and numeric, and how math computations can be performed with numbers. All input is text by default. Even if the text involves digits and is intended as a number, it is still just text. That is, the computer does not distinguish between letters and numbers on the keyboard its all just series of keystrokes. Remember adding the first and last names in the previous assignment? Adding 1 and 1 in the same way would result in 11. So in order to use input as if it was actually a number to be used to perform a calculation, the programmer has to write script that specifies it as being numeric. Thats what you will learn to do in this assignment. Requirements. Write a program that converts a temperature from degrees Centigrade to degrees Fahrenheit. (If you can do this one, then you will be able to write programs to convert just about anything from one measurement system to another.) To do this one, start with GreetingsTo.html and save it as c2fconvert.html. Adjust the title to be for assignment 5, and remove everything between the curly braces in the scripts calculate function, and everything between <body> and </body>. That will give you a clean slate for starting this assignment. Save and open the file see if its what you expected. Now design and add HTML between <body> and </body> so that it shows something like the following when you save and open the file. Use your own words for the instructions. Check out the referenced URL at csgnetwork.com. Its similar to what we are writing in this assignment. There are 2 boxes. Use the following for their ids: cdegreesbox and fdegreesbox, from top to bottom very short IDs, indeed! Remember to add the go button. Save and open the file it will look something like the display to the left. Remember to have comments in each section to tell what each section is to accomplish. Now add script (between the curly braces of function calculate ()). First, enter the comment //declare two variables, then create two variables named c and f. c, for celcius will be entered by a viewer of your page and f, for Fahrenheit, will be calculaged with a formula to be entered later. Next, write script to copy the contents of the first box into the first variable, but because we want it to be a number (instead of text), do it like this: c = getInputAsNumber(cdegreesbox)

Do you see that what we wrote in previous assignments said asText, and it now says asNumber? Go back and look at previous assignments if needed. This is what tells the program to interpret the box contents as a number instead of text. If it seems that the computer is smart enough to figure that out for itself, its not. A computer is just a tool and needs a human to make it a valuable tool. Now do the calcuation, like this: This works somewhat like a calculator, except that the asterisk is used to denote multiplication instead of an x. Its like entering the value 1.8, and multiplying it by the number stored in the variable named c. Then add 32. Thats how you would convert from degrees C to degrees F. Heres how the above script reads: multiply 1.8 times whatevers stored in c, then add 32, and store the result in f. Finally, copy the contents of the variable f into the box whose id is fdegreesbox. The script for doing that is left up to you refer to what you did in the previous assignment. So remember that when you create a variable, its going to be used for either text or for a number. You will have these two choices to make for var x (for example, because x could be anything else): x = getInputAsText("xbox") as text or x = getInputAsNumber("xbox") (The bolding is for emphasis). Submit the c2fconvert.html file per your instructors directions for credit. Program I/O. Input: a temperature in degrees C and click go. Output: the equivalent temperature in degrees F. The browser window will look something like: as a number

Be sure to check your work by comparing these easy reference points: -40C is -40F (the only point where C and F are the same), 0C is 32F (the freezing point of water), and 100C is 212F (the boiling point of water). You can check other conversions using the csgnetwork.com URL listed in your instructions.

You might also like