W3101: Scripting Languages: Javascript -- HW2
Due: Friday, Feb 21 (anytime)
Please name the file as "yourname"-HW4.html andsubmit the HTML file with your Javascript code
in Courseworks.
In this homework, you will write a simple calculator as follows.
Create a form that will hold the entire calculator with the following fields.
- An input text that is used to hold the value.
- Ten buttons, the first one for '0', the second for '1',
the third for '2' and so on, with the last being '9'.
When any of these buttions is pressed, the value in the input text should be updated.
- Four more buttons '+', '-', '*' and '/' for addition, subtraction, multiplication and division.
These should be used for the addtion, subtraction, multiplication and division operations, respectively.
- A button "." that is used to create a decimal value.
Note: The user should be able to use a button only once for any number.
That is, something "3.4.5" is an invalid number and should not be allowed.
- A button "+/-" that is supposed to toggle the value of the current number from positive to negative and negative to positive.
- A button '=' that should show the updated value (result) in the input text.
Note: You can use eval function on the input text value to find the result
- A button called 'C' that is used to clear the value in the input text.
Note:
- You can add two numbers (e.g., 25.34 + 30.12) at a time or multiple numbers (25.34 + 30.12 + 11.9 + 36 + ..) at a time.
- If you are adding multiple numbers at a time, you need to show all the numbers in the input box (25.34 + 30.12 + 11.9 + 36 + ..) and only the result after the "=" is pressed.
- If you are adding two numbers at a time, you can show both the numbers (e.g. 25.34 + 20.12) on the screen or
one at a time (e.g., show 25.34 first, then show only 30.12 after the "+" symbols is pressed). Show the result after the "=" is showed.
- You can store the numbers in a local variable or evaluate them from the text input field using "eval" function.