L9. Math Object in JS, CSE 202, BN11
L9. Math Object in JS, CSE 202, BN11
Trigonometric
and Template Practice problems
logarithmic literals on math object
operations
2
Math Object
✓ The JavaScript Math object allows you to perform
mathematical tasks on numbers.
3
Math methods (basic operations)
Methods Description
Math.PI returns PI
Math.pow(x, y) returns the value of x to the power of y
Math.sqrt(x) returns the square root of x
Math.abs(x) returns the absolute (positive) value of x
Math.min() can be used to find the lowest value in a list
of arguments
Math.max() can be used to find the highest value in a list
of arguments
4
Math methods (trigonometric and logarithmic operations)
Methods Description
Math.log(x) returns the natural logarithm of x
Math.log2(x) returns the base 2 logarithm of x.
Math.sin(x) returns the sine (a value between -1 and 1) of the angle x (given
in radians). Angle in radians = Angle in degrees x PI / 180.
Math.cos(x) returns the cosine (a value between -1 and 1) of the angle x
(given in radians).
Math.tan(x) Returns the tangent of an angle in radians
5
Template literals
6
Worked out project
9.0 Project name: Program to calculate using Math methods and
display in browser. (Calculate height of a building using distance and
angle value and display in the browser.)
Github link:
https://github.com/sauravbarua02/buildingHeightStatic
7
Interface
8
html codes
<!DOCTYPE html>
<html lang="en">
<body>
<head>
<div class="container">
<meta charset="UTF-8">
<div id="result"></div>
<meta name="viewport"
</div>
content="width=device-width, initial-
scale=1.0">
<script src="app.js"></script>
<title>Document</title>
</body>
<link rel="stylesheet"
</html>
href="style.css">
</head>
9
css codes
body{
margin: 0px;
background-color: rgba(45, 158, 161, padding: 20px;
0.4); display: flex;
} flex-direction: column;
.container{ justify-content: center;
background-color: rgba(45, 158, 161, align-items: center;
0.5); border-radius: 5px;
height: 300px; box-shadow: 0 0 1px 2px rgba(0,0,0,0.4);
width: 300px;
margin: 20px auto; }
10
JS codes
//Math object
const resultEl =
document.getElementById("result"); height = calculation(20,45);
11
Class tasks
Task 9.1: Program to calculate minimum among four given N-values 23, 18, 21 and 35
obtained from the Standard Penetration Test (SPT) of a site and display in the browser. (hints:
Math.min())
Task 9.2: Program to calculate the length of a side in a closed triangular traverse survey whose
interior angle is 600. The measured length of one side 20 m and corresponding interior angle
300 and display the result in the browser. (hints: a/Sin A = b/Sin B, law of Sines)
Task 9.3: Program to calculate width of a river channel from a missing survey data where, B
and C points are in the same bank side, A point is in the other side and ∠ BAC = 300 , AC = 40
meter. Display result in the browser. (hints: AB = AC x cos ∠ BAC )
Task 9.4: Program to predict future AADT volume in a township, for a relationship: Future
AADT = (present AADT)growthfactor . Given, present traffic 1100 AADT, and growth factor 1.08.
Display result in the browser. (hints: use pow(x,y) method)
Task 9.5: Program to calculate the size of a square footing, whose allowable footing area is 50
sq.ft. Display result in the browser. (hints: Math.sqrt()).
12
End of the Lecture
13