Close Klik 2x!

Tuesday, July 14, 2015

How to Create a Calculator With Javascript


Howtojetro.blogspot.com | How to Create a Calculator With Javascript - JavaScript is a scripting language used for developing web applications. Using JavaScript, you can create a simple calculator to put on your website or on your desktop by opening the local file in your JavaScript-enabled web browser. Creating a JavaScript calculator is a good exercise for beginners to practice JavaScript and improve coding skills.

Things You'll Need
  • Text editor
  • Javascript-enabled web browser
Instructions
1. Open your text editor, which must be set to "plain text" mode. Examples of appropriate text editors include Notepad for Microsoft Windows and TextEdit for Mac OS X.

2. Create your HTML document. Include a table that has three sections: one for numerical input buttons; another for operation buttons; and a section for displaying input and output. Create input buttons for each number (0 through 9), a decimal point and buttons for each operation (addition, subtraction, multiplication, division, exponents, etc) that you want to be able to perform with your calculator.

3. Define all global variables and constants. These variables should include a maximum length constant, a memory variable for storing the previously entered value, a current variable for storing the entered value and a variable for storing the operation.

4. Define functions for each operation (i.e. addition, subtraction, etc), clearing the input, putting a decimal point in the input and a function for performing the desired calculation.

5. Include a function that is called in the division function to ensure that the user does not attempt to divide by zero. If this is attempted, include a warning message of some type.

Tips & Warnings
  • Three source code examples for calculators written in JavaScript are listed in the References section. Anaesthetist provides thorough explanations with code examples for a simple calculator similar to the design discussed in this article. JS Made Easy and The JavaScript Source provide source code for more advanced calculators that are able to calculate trigonometric functions and logarithms.
  • This project should take less than an hour for an advanced JavaScript programmer. If you are a beginner, expect to spend two to four hours completing this project; this time frame estimate includes time spent reviewing code examples and other materials.

No comments:

Post a Comment