Code for Java Script Cube / Box Volume Calculator
By Stephen Bucaro
Note: Use the same measuring system units for all entries.
In other words don't mix inches and feet or centimeters and meters. The output units
will be cubic units of the same measuring system.
In this article I'll show you how to use to Java Script to create a Cube / Box
Volume calculator. With this calculator, the user inputs the side dimension of
a cube or the height, width and length of a box, and the calculator will return
the volume. I'll explain everything in detail so that you can learn Java Script
and modify the code for your own purposes.
I have programmed many Java Script calculators and discovered that, the process
of coding any calculator can be broken down into a step-by-step procedure. Shown
below are the steps to build a calculator.
1. Acquire the formula
2. Determine the inputs and outputs
3. Design the html form
4. Write Java Script to process the form
5. Test and Final Touches
Step 1: Cube and Box Formulas
The formula for the volume of a cube is side3
The formula for the volume of a box is height x width x length
Step 2. The inputs and outputs
For the cube volume calculator, since by definition all sides of a cube have
the same length, the user needs only to enter the length of a side. However, a box
can have different lengths for height, width and length, so the user will need
to enter all three dimensions.
The actual units of measurement are irrelevant in a volume calculator because sides
are always in length units while the volume is in cubic units of the same measuring
system. One problem might be if the user enters different units in the box volume
calculator. For example, the user can't enter the height in feet and the width in inches.
We could design a calculator where the user indicates the units entered for each
input value and the desired output units and then have the code convert all the
input lengths to the desired output units. In that case, this example would consist
of 99 percent code involved in units conversion. So to keep it simple, instead we
can just add a note instructing the user to use the same units for all values.
|