COM271, Week 10
Math Objects
Syllabus | Table of Pages | Assignments | References and Useful Links
The math object contains constants and methods. It is a static object, meaning you can't create instances of it, so you access its properties directly. Here is a complete list (from T. Powell and F. Schneider, JavaScript, 2nd edition., McGraw Hill): (see also the W3Schools page for Math Object)
| CONSTANTS | Property | Description |
|---|---|
| Math.E | The base of the natural logarithm (Euler's constant e) |
| Math.LN2 | Natural log of 2 |
| Math.LN10 | Natural log of 10 |
| Math.LOG2E | Log (base 2) of e |
| Math.LOG10E | Log (base 10) of e |
| Math.PI | PI (π) |
| Math.SQRT1_2 | Square root of 0.5 (equivalently, one over the square root of 2) |
| Math.SQRT2 | Square root of 2 |
| METHODS | Method | Returns |
|---|---|
| Math.abs(arg) | Absolute value of (arg) |
| Math.acos(arg) | Arc cosine of (arg) |
| Math.asin(arg) | Arc sine of (arg) |
| Math.atan(arg) | Arc tangent of (arg) |
| Math.atan2(y, x) | Angle between the x axis and the point (x, y), measured counterclockwise (like polar coordinates). Note how y is passes as the first argument rather than the second. |
| Math.ceil2(arg) | Ceiling of arg (smallest integer greater than or equal to arg) |
| Math.cos(arg) | Cosine of 2 |
| Math.exp(arg) | e to arg power |
| Math.floor(arg) | Floor of arg (greatest integer less than or equal to arg) |
| Math.log(arg) | Natural log of arg (log base e of arg) |
| Math.max(arg1, arg2) | The greater of arg1 or arg2 |
| Math.min(arg1, arg2) | The lesser of arg1 or arg2 |
| Math.pow(arg1, arg2) | arg1 to the arg2 power |
| Math.random( ) | A random number in the interval [0,1] |
| Math.round(arg) | The result of rounding arg to the nearest integer. If the decimal portion of arg is greater than or equal to .5, it is rounded up. Otherwise, arg is rounded down. |
| Math.sin(arg) | Sine of arg |
| Math.sqrt(arg) | Square root of arg |
| Math.tan(arg) | Tangent of arg |
Examples:
random roll of a die: myRoll = Math.round(Math.random() * (5) + 1;
Try it—click here to get a die roll here:
<a onclick="getElementById('roll').innerHTML=Math.round(Math.random()*5) + 1;">click here</a> to get a die roll here: <span id="roll"></span>
JavaScript Mortgage Calculator: You can google up any number of scripts for javascript calculators. Here's one for a simple mortgage calculator:
<form action="POST" name="myform">
< script language="JavaScript">
function Morgcal()
{
form = document.myform;
LoanAmount= form.LoanAmount.value;
DownPayment= "0";
AnnualInterestRate=form.InterestRate.value/100;
Years= form.NumberOfYears.value;
MonthRate=AnnualInterestRate/12;
NumPayments=Years*12;
Prin=LoanAmount-DownPayment;
MonthPayment=Math.floor((Prin*MonthRate)/(1-Math.pow((1+MonthRate),(-1*NumPayments)))*100)/100;
form.NumberOfPayments.value=NumPayments;
form.MonthlyPayment.value=MonthPayment;
}
</script>
<h4>Repayment Mortgage Calculator</h4>>
<fieldset><legend>Terms</legend>
<table border="0" style="width:300px;">
<tr><td>Loan Amount $</td><td><input type="text" size="10" name="LoanAmount" value="80000" onblur="Morgcal()" onchange="Morgcal()"></td></tr>
<tr><td>Annual Interest Rate</td><td><input type="text" size="3" name="InterestRate" value="6.0" onblur="Morgcal()" onchange="Morgcal()"> %</td></tr>
<tr><td>Term of Loan</td><td><input type="text" size="3" name="NumberOfYears" value="20" onblur="Morgcal()" onchange="Morgcal()"> Years</td></tr>
<tr><td colspan="2"><input type="button" name="morgcal" value="Calculate" language="JavaScript" onclick="Morgcal()"></td></tr>
</table></fieldset>
<fieldset><legend>Schedule</legend><table border="0" style="width:300px;">
<tr><td>Number of Payments</td><td><input type="text" size="7" name="NumberOfPayments"></td></tr>
<tr><td>Monthly Payment $</td><td><input type="text" size="7" name="MonthlyPayment"></td></tr>
</table></fieldset></form>