L2. Function in JS, CSE 202, BN11
L2. Function in JS, CSE 202, BN11
DOM
manipulation Function Parameter,
through JS argument.
2
3
Accessing element by ID
Syntax: document.getElementById(“ID_name");
4
Function definition
Syntax: function function_name(){
//code
}
function_name(); //function invoking
5
Return
Syntax: function function_name(){
//code
return val;
}
6
Parameters and arguments
Syntax: function function_name(para1,
para2){
//code
}
function_name(arg1, arg2);
7
Changing text in html
Syntax: element.innerText = text;
8
Worked out example
Example 2: Program to calculate multiplication of two numbers in JS and display
result in the browser. (Calculate total masonry labor cost using per day wage and
nos. of mason worked in a construction project and display total cost in the
browser.)
9
Interface
10
html codes
<body>
<!DOCTYPE html>
<div class="container" id="container">
<html lang="en">
<head>
</div>
<meta charset="UTF-8">
<meta name="viewport"
<script src="app.js"></script>
content="width=device-width, initial-
</body>
scale=1.0">
</html>
<title>Function Calculation</title>
<link rel="stylesheet" href="style.css">
</head>
11
css codes
body{
margin: 0px; padding: 10px;
background-color: rgba(176, 129, 219, display: flex;
0.6); flex-direction: column;
}
.container{ justify-content: center;
background-color: rgba(176, 129, 219, align-items: center;
0.2); border-radius: 10px;
height: 300px; box-shadow: 0 0 3px 1px rgba(0,0,0,0.5);
width: 300px; }
margin: 20px auto;
12
JS codes
const containerEl =
document.getElementById("container");
13
Class tasks
Task 2.1: Program to calculate total cost of a building, where material cost,
labor cost, equipment cost and overhead cost are given. Display total cost in
the browser. (hints: totalCost = materialCost + laborCost + equipmentCost + overheadCost;)
Task 2.2: Program to calculate number of masons required to erect a N sq.ft
brick wall (5”thick) per day, where one mason can work p sq.ft wall per day.
(hints: NoOfMasonRequired = N/p;)
Task 2.3: Program to calculate gallons of plastic paint required to cover N sq.
ft interior wall. (hints: gallonOfPaintRequired = N/400)
Task 2.4: Program to calculate volume of a rectangular shaped timber log,
where width (inch), breadth (inch) and length (ft) are given. (hints:
timberVolume = width x breadth x length/144;)
Task 2.5: Program to calculate bacterial growth rate of pathogen in a water
sample, where growth rate = 2n. (hints: a**b returns a power b value)
14
End of the Lecture
15