In this project you will write a simple calculator program. You may
find it useful to refer back to your MadLibs page to see examples of
a button, a function definition, and calls to the alert
and
prompt
functions.
If your code isn't working as expected, try opening up Firefox's error console to check for (potentially) helpful error messages. If you get stuck, don't hesitate to ask the instructor or a teaching assistant for help.
In this project you will create a simple calculator in a new page. Your calculator will prompt the user for two numbers and then display the sum, the product, the difference, and the quotient of those two numbers. A sample session might look like the following:
[prompt] Please enter the first number. [user enters] 3 [prompt] Please enter the second number. [user enters] 4 [Output] 3 + 4 = 7 3 - 4 = -1 3 * 4 = 12 3 / 4 = .75
.html
.
Tip: Here are some handy keyboard shortcuts:
Select-All: Command-A (Mac), Control-A (Windows)
Copy: Command-C (Mac), Control-C (Windows)
Paste: Command-V (Mac), Control-V (Windows)
Note: The values returned by the
prompt
function are strings, not numbers. What is the result if you try to use the+
operator on those values?Tip: Before you can perform arithmetic on string values they must be converted to numbers using the built-in
parseFloat
function. For example, if the variablefirstNum
contains the string "3", the following command will convert it to the number 3:firstNum = parseFloat(firstNum);
Tip: There are several possible ways to complete this exercise. One possibility would be to create four variables to store the the sum, the product, the difference, and the quotient. You can then use the "+" operator to build the output string. For example, the first output statement might look something like the following:[Click on thealert(firstNum + " + " + secondNum + " = " + sum);alert
call above to see an explanation. Click on it again to make the explanation disappear.]
Edit the main COMP 102 web page you created in a previous
activity and add a link to your new Calculator page.
In the link, refer to your new page with a relative pathname
which is just the name of the file (e.g.,
<a href="Calculator.html">
).
This tells the browser that the file to look for is in the same
directory or folder as the current file (your main COMP 102 web page,
in this case).
You do not want to give
a full or absolute pathname, like
<a href="file:///Desktop/Calculator.html">
,
because the location of your file on the
student.cs.kzoo.edu
server will not be the same as on
your own laptop or classroom computer.
If you worked in a team, each member of your group should do this, so each of you has a link to your calculator page. After the link, list your teammates in parentheses; for example, "Calculator Project (Harry, Ron, and Hermione)".
student.cs.kzoo.edu
server.
Test that the link to the calculator project works on the server by clicking
on it from your home page there.
(Make sure that your browser is looking at the page on
student.cs.kzoo.edu
, not your local version.)
Note: After you have learned more about JavaScript Objects, you will be updating your Calculator to write your output directly to the page, rather than communicating via alert boxes. You can find the instructions for Part II of the Calculator programming project on the Detailed Schedule page.