JavaScript: Simple Math

 

prompt

A method of the 'window' object [like 'alert()'] is 'prompt().' prompt() asks the viewer for input in response to a question. prompt() can store the viewer's response in a variable. Let's use prompt() to ask the viewer for her name, and then print her name to the screen with a welcome message. Here's the code:

userName = prompt("What is your first name?"," ")

var res = "<p>Nice to meet you, " + userName + "."

document.write(res.fontsize("4"))

Inside the first set of double-quotes in prompt() is the question. Inside the second set of double-quotes is a default answer that shows up in the answer space. If you want the answer space to be blank, leave the second set of double-quotes empty.

The viewer's response gets stored in the variable 'userName.' res.fontsize("4") determines the font size of the output text. (A fontsize should be between 1 and 7.)

to see this how this code works.

 

Simple Math

Let's use prompt() to ask the viewer for her first name and 2 numbers. We'll then add the numbers and print the result in a document.write() statement. Here's the code:

<BODY>

<script type="text/javascript">

userName = prompt("What is your first name?","")

num1 = prompt("Please give me a number:","")

num2 = prompt("Please give me another number:","")

sum = num1 + num2

var res = "<p>" + userName + ", the sum of " + num1 + " and " + num2 + " is " + sum + "."

document.write(res.fontsize("4"));

var res2 = "<p>Do you see the problem? The result is probably wrong. Back to the drawing board..."

document.write(res2.fontsize("4"));

</script>

to see this how this code works.

 

eval()

The prompt() method produces a 'string,' which is a series of characters, like your name. What you want in this script are numbers. You can use the eval() command to convert the strings into numbers. Here's the revised code with this change:

<BODY>

<script type="text/javascript">

num1 = prompt("Please give me a number:","")

num2 = prompt("Please give me another number:","")

sum = eval(num1) + eval(num2)

var res = "<p>The sum of " + num1 + " and " + num2 + " is " + sum + "."

document.write(res.fontsize("4"))

var res2 = "<p>Now that's more like it."

document.write(res2.fontsize("4"))

</script>

to see this how this code works.

 

Powers

Let's do an example in which we ask the viewer for 2 numbers and use the first number as the base and the second number as the exponent. We use the Math.pow() function do to this. (In this case, we do not have to use eval() to convert num1 and num2 to numbers before using them in Math.pow(). Math.pow() converts num1 and num2 to numbers.) Here's the code:


<BODY>

<script type="text/javascript">

num1 = prompt("Please give me a base:","")

num2 = prompt("Please give me an exponent:","")

result = Math.pow(num1,num2)

var res = "<p>" + num1 + " to the power " + num2 + " is " + result + "."

document.write(res.fontsize("4"))

</script>

 

to see this how this code looks in a new window.

 

Other Resources

Try these links on simple math and variables:


Page Link
Prompts and Variables www.htmlgoodies.com/primers/jsp/article.php/3478471
Math Variables www.htmlgoodies.com/primers/jsp/article.php/3478261

or check out one of our reference books, or look for other help on the Web.

 

Exercise

Work on either 1) your own web page, or 2) the eEverything.com site.

If you work on eEverything.com, use 'prompt()'s, variables and some math to create a simple sales-tax calculator. Using 2 'prompt()'s, ask the user for the name and price of an item. Using a third 'prompt()', ask the user what the sales tax rate is. (Assume that the sales tax rate is 6.625% and have 0.06625 show up as the default in the prompt). An example:

var taxRate = prompt("What is the tax rate, as a decimal?", "0.06625")

Then use 'document.write()' to report the results. Output will be something like: "Sales tax on the $1000 leather sofa is $66.25. The total cost is $1066.25."

If you work on your own page, implement something similar.