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
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 -->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.
Variations
Credits
Ken Hess and Andrew Olson, Ph.D., Science Buddies
Last edit date: 2006-04-17 19:04:30
Science Buddies gratefully acknowledges its Presenting Sponsor
Science Fair Project Home
Our Sponsors
About Us
Volunteer
Donate
Contact Us
Online Store
Privacy Policy
Image Credits
Site Map
Science Fair Project Ideas
Science Fair Project Guide
Ask an Expert
Teacher Resources
Science Fair Competitions
Copyright © 2002-2008 Kenneth Lafferty Hess Family Charitable Foundation. All rights reserved.
Reproduction of material from this website without written permission is strictly prohibited.
Use of this site constitutes acceptance of our
Terms and Conditions of Fair Use.