How to Use JavaScript to Get User Entry and Use the Entry in a Calculation


The use of variables are key to all programming languages. You may remember the x's and y's from your high school algebra class. In programming, variables have even more meaning than just some vague idea of ​​an unknown value. In programming, a variable is used as the name of a particular information storage area. You can then use that variable name to call whatever you happened to store there. If you throw away the old stuff and put something new in the storage are, the variable calls the new stuff.

Some programming languages ​​have to know ahead of time what you plan to store. They like to plan ahead of time what size storage area you are renting. Telling these language what you plan to store means that you have to declare a data type. These types of programming languages ​​are called strongly typed languages. Other languages, such as JavaScript, could care less what kind of stuff you are storing. They decide what size space you need when you send it to the storage area. That is one reason JavaScript is less complicated to learn than some programming languages.

However, there are still some things you will need to learn about data types because there are some side effects to not telling what type of information you want to store.

In any case, the information still has a variable name. Here is an exercise that

1) gathers information from a prompt box,
2) puts it into a variable,
3) does a calculation, and
4) sends the answer back in an alert.

Add this code to the of a new web page

var SquareThis = prompt ( "Enter a number you would like to square.");
var Answer;

Answer = SquareThis * SquareThis;

alert (Answer);

Basic Programming Skills in this Bit of Code

1. When you do any kind of programming, you have to think of what pieces of information the computer will need to identify and hold on to. Then you make variables to hold the information. In this example, we need to hold two pieces of information, 1) the number that will be squared, 2) the answer.

2. You tell JavaScript that you want it to store information by naming var, a space and a variable name.

3. The first line of this script does three jobs, 1) it tells JavaScript to set aside memory space for the variable SquareThis, 2) it uses a pre-written function to prompt the user for a number, 3) it takes whatever the user types in the prompt box and puts it in the memory space for SquareThis.

4. Notice that the function prompt requires an argument just like the alert () did. Notice too that the argument is in "", which means that the argument is a String data type.

5. Now the script needs a variable to hold the answer. This is done with "var Answer;"

6. Everything is in place now to do the math: "Answer = SquareThis * SquareThis;" The part that tells the computer what kind of math to do is called an operator. The main operators are +, -, *, / (add, subtract, multiply, divide). There are many other operators, as well. A square is any number multiplied by itself; so this formula multiplies SquareThis by SquareThis.

7. The program has an answer, but it needs to tell the user. Again, we will use an alert box.

8.Now change the formula to do something else. Try adding another variable and another alert () to ask for another number (or several.) Here are some operators for you to experiment with:


JavaScript Math Functions

+ Adds two numbers

– Subtracts two numbers

/ Divides two numbers

++ Adds one to a number (only needs one variable)

– Subracts one from a number (only needs one variable)


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.