|
Abstract This is a good first-time programming project. You'll learn how to use JavaScript to create a basic calculator program that you can run in your Web browser.Objective The objective of this project is to learn how to write a simple calculator with the JavaScript programming language. You will learn how to write a simple JavaScript calculator that can add, subtract, multiply or divide two numbers. You will be able to run your program in a Web browser. Introduction This is an example of a first-time programming project. You will be writing a calculator that can add, subtract, multiply or divide two numbers. The project uses JavaScript, an interpreted programming language supported by most Web browsers. You will learn how to write an HTML file containing your program, and how to load and run the file in your browser. In this project, you will learn the basics of how JavaScript functions can be written to work with HTML FORM elements. Specifically, you will be working with HTML INPUT elements, and using JavaScript's built-in parseValue() and basic math functions. Preliminaries These are things you need to know before you get started.
Getting Started with JavaScript Now that you've succeeded with writing an HTML file and opening it with your browser, you're ready to delve into JavaScript. The following link has a step-by-step tutorial that will give you a great introduction to JavaScript programming: http://www.webteacher.com/javascript/index.html Writing a JavaScript Calculator To help get you started, here is an example of a JavaScript calculator that adds two numbers. This is just one of many ways to accomplish this task. You can use this example as a basis for your program, or you can start from scratch. If you build from this example, you will need to add functions and buttons for subtraction, multiplication, and division. Simple Adder Here is the JavaScript source code for the simple adder. After you have gone through the JavaScript tutorial (above), you should be able to read through this code and understand what it is doing. Note that the two methods for denoting JavaScript comments are different than the method for HTML comments: <!-- 2005/11/08 Science Buddies: JavaScript calculator, adds two numbers --><HTML> <HEAD> <TITLE>Simple Adder</TITLE> <!-- saved from url=(0030)http://www.sciencebuddies.org/ --> <!-- When this code is saved as a local file, the preceding line tells Internet Explorer to treat this file according to the security rules for the Internet zone (plus any security rules specific for the Science Buddies website). --> <SCRIPT LANGUAGE="JavaScript"> <!-- old browsers can't handle JavaScript functions so comment them out // This is a single-line JavaScript comment. // Below is a multi-line JavaScript comment. /* CalculateSum: this function has 3 arguments: Atext, Btext and form. It converts Atext and Btext to numbers using the built-in JavaScript "parseFloat" method. It then uses the form argument to output the sum of the numbers to the form's Answer field. Notice that the function does *not* need to know the the names of the form's input fields. Those values are passed as arguments. It does need to know that the form has a field named "Answer" so that it can put the result there. Here is how to end a multi-line JavaScript comment: */ function CalculateSum(Atext, Btext, form) { var A = parseFloat(Atext); var B = parseFloat(Btext); form.Answer.value = A + B; } /* ClearForm: this function has 1 argument: form. It clears the input and answer fields on the form. It needs to know the names of the INPUT elements in order to do this. */ function ClearForm(form) { form.input_A.value = ""; form.input_B.value = ""; form.Answer.value = ""; } // end of JavaScript functions --> </SCRIPT> </HEAD> <BODY> <P><FONT SIZE="+2">Simple Adder</FONT></P> <FORM NAME="Calculator" METHOD="post"> <P>Enter a number: <INPUT TYPE=TEXT NAME="input_A" SIZE=10></P> <P>Enter a number: <INPUT TYPE=TEXT NAME="input_B" SIZE=10></P> <P><INPUT TYPE="button" VALUE="Add Numbers" name="AddButton" onClick="CalculateSum(this.form.input_A.value, this.form.input_B.value, this.form)"></P> <P><INPUT TYPE="button" VALUE="Clear Fields" name="ClearButton" onClick="ClearForm(this.form)"></P> <P>Answer = <INPUT TYPE=TEXT NAME="Answer" SIZE=12></P> </FORM> </BODY> </HTML>
Terms, Concepts, and Questions to Start Background Research To write a simple program in JavaScript, you should do research that enables you to understand the following terms and concepts:
Questions:
Bibliography
Materials and Equipment
Experimental Procedure Using what you have learned about programming in Javascript, create a program that can add, subtract, multiply or divide two numbers supplied by the user. Here are some generally applicable programming tips to keep in mind as you get started with this project.
Variations
Credits Ken Hess and Andrew Olson, Ph.D., Science Buddies
|
If you like this project, you might enjoy exploring related careers.
![]() |
Computer Programmer Computers are essential tools in the modern world, handling everything from traffic control, car welding, movie animation, shipping, aircraft design, and social networking to book publishing, business management, music mixing, health care, agriculture, and online shopping. Computer programmers are the people who write the instructions that tell computers what to do. |
![]() |
Computer Software Engineer Are you interested in developing cool video game software for computers? Would you like to learn how to make software run faster and more reliably on different kinds of computers and operating systems? Do you like to apply your computer science skills to solve problems? If so, then you might be interested in the career of a computer software engineer. | |
![]() |
Software Quality Assurance Engineer & Tester Software quality assurance engineers and testers oversee the quality of a piece of software's development over its entire life cycle. Their goal is to see to it that the final product meets the customer's requirements and expectations in both performance and value. During the software life cycle, they verify (officially state) that it is possible for the software to accomplish certain tasks. They detect problems that exist in the process of developing the software, or in the product itself. They try and make things not work (try to "break" the software) by creating errors or combinations of errors that a user might make. For example, if a user enters a period or a pound sign for a password, will that break the software? They seek to anticipate potential issues with the software before they become visible. At the end of the life cycle, they reflect upon how problems or bugs arose, and figure out ways to make the software development process better in the future. | |||
|
Join Science Buddies
Become a Science Buddies member! It's free! As a member you will be the first to receive our new and innovative project ideas, news about upcoming science competitions, science fair tips, and information on other science related initiatives. |