SlideShare a Scribd company logo
Course Code: CSE 202
Course Title: Computer Programming Lab
Lecture 2: Function in JS
Course Teacher: Saurav Barua (SB)
Assistant Professor, Dept. of CE, DIU
Phone: +88-01715334075
Email: saurav.ce@diu.edu.bd
Contents
2
Function
Basic HTML
tags
Basic CSS
selectors and
properties
DOM
manipulation
through JS
DOM
.
Parameter,
argument.
3
Accessing element by ID
4
Syntax: document.getElementById(“ID_name");
❑ The getElementById() method of the Document interface
returns an Element object representing the element whose id
property matches the specified string.
❑ Since element IDs are required to be unique if specified,
they're a useful way to get access to a specific element
quickly.
Function definition
5
Syntax: function function_name(){
//code
}
function_name(); //function invoking
A function in JavaScript is similar to a procedure—a set of statements that performs a task or
calculates a value, but for a procedure to qualify as a function, it should take some input and
return an output. A function definition (also called a function declaration, or function
statement) consists of the function keyword, followed by:
✓ The name of the function.
✓ A list of parameters to the function, enclosed in parentheses and separated by commas.
✓ The JavaScript statements that define the function, enclosed in curly braces, { /* … */ }.
Return
6
Syntax: function function_name(){
//code
return val;
}
✓ The return statement ends function execution and specifies
a value to be returned to the function caller.
✓ Return can be zero, value or expression.
✓ Return must be stored in a variable.
Parameters and arguments
7
Syntax: function function_name(para1,
para2){
//code
}
function_name(arg1, arg2);
❑ Function parameters are the names listed in the function
definition.
❑ Function arguments are the real values passed to (and received
by) the function.
Changing text in html
8
Syntax: element.innerText = text;
The innerText property sets or returns the text content of an
element.
Worked out example
9
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.)
Google drive link:
https://drive.google.com/drive/folders/15iLzTYrZmplDGngEk1e1CnhMIBoR
X1OF?usp=drive_link
Git-hub link: https://github.com/sauravbarua02/MasonCostEstimate
Interface
10
html codes
11
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-
scale=1.0">
<title>Function Calculation</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container" id="container">
</div>
<script src="app.js"></script>
</body>
</html>
css codes
12
body{
margin: 0px;
background-color: rgba(176, 129, 219,
0.6);
}
.container{
background-color: rgba(176, 129, 219,
0.2);
height: 300px;
width: 300px;
margin: 20px auto;
padding: 10px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
border-radius: 10px;
box-shadow: 0 0 3px 1px rgba(0,0,0,0.5);
}
JS codes
13
const containerEl =
document.getElementById("container");
function costEstimate(masonNo, perDayWage){
const totalCost = masonNo * perDayWage;
return totalCost;
}
costValue = costEstimate(5,500);
containerEl.innerText = "Total masonry labor
cost: " + costValue + " BDT";
Class tasks
14
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)
End of the Lecture
15

More Related Content

PPTX
Lab #2: Introduction to Javascript
PPTX
PPTX
Intro to Javascript
PDF
Lecture 03 - JQuery.pdf
PPT
25-functions.ppt
PPTX
11. session 11 functions and objects
PPSX
Javascript variables and datatypes
PPTX
Lecture 4- Javascript Function presentation
Lab #2: Introduction to Javascript
Intro to Javascript
Lecture 03 - JQuery.pdf
25-functions.ppt
11. session 11 functions and objects
Javascript variables and datatypes
Lecture 4- Javascript Function presentation

Similar to L2. Function in JS, CSE 202, BN11.p1df documents (20)

PDF
JavaScript_introduction_upload.pdf
PPTX
ppt- u 2.pptx
PPTX
BITM3730 10-17.pptx
PPTX
Javascript
PPTX
BITM3730Week6.pptx
PPTX
Java script
DOC
14922 java script built (1)
PPT
Java Script Programming on Web Application
PPTX
Javascript note for engineering notes.pptx
PPT
PPTX
Wt unit 5
PPT
17javascript.ppt17javascript.ppt17javascript.ppt
PPTX
gdscWorkShopJavascriptintroductions.pptx
PDF
Introduction to web programming with JavaScript
PPTX
BITM3730 10-4.pptx
PPTX
BITM3730 10-3.pptx
PPTX
An introduction to javascript
PDF
Java script tutorial
PPTX
Javascript for web Programming creating and embedding with html
PPTX
Javascripting.pptx
JavaScript_introduction_upload.pdf
ppt- u 2.pptx
BITM3730 10-17.pptx
Javascript
BITM3730Week6.pptx
Java script
14922 java script built (1)
Java Script Programming on Web Application
Javascript note for engineering notes.pptx
Wt unit 5
17javascript.ppt17javascript.ppt17javascript.ppt
gdscWorkShopJavascriptintroductions.pptx
Introduction to web programming with JavaScript
BITM3730 10-4.pptx
BITM3730 10-3.pptx
An introduction to javascript
Java script tutorial
Javascript for web Programming creating and embedding with html
Javascripting.pptx
Ad

More from SauravBarua11 (20)

PDF
L11, Project Survey , Spring 24, lecture notes, SB.pdf
PDF
L5, Computation of Area, Spring 24, SB.pdf
PDF
L4, Contour, Spring 24, lecture notes, SB.pdf
PDF
L3, Traverse Survey, Spring 24, lecture notes, SB.pdf
PDF
L8, Tacheometry survey, Spring 24, SB.pdf
PDF
L10, Astronomical surveying, Spring 24, SB.pdf
PDF
L6, Computation of Volume, Spring 24, SB.pdf
PDF
L9, photogrammetric survey, Spring 24, SB.pdf
PDF
L2, Level surveying, Spring 24,class notes, SB.pdf
PDF
Confusion matrix in Transportation Engineering.pdf
PDF
Ordinary least square (OLS) and MLE in Transportation Engineering.pdf
PDF
L5, Loop and iteration, CSE 202, BN11.pdf
PDF
L6, Array in JS, CSE 202, BN11.pdf JavaScript
PDF
L4, Conditional statement, CSE 202 JavaScript
PDF
L3. Operators in JS, CSE 202, BN11.pdf JavaScript
PDF
L1. Introduction, CSE 202, BN11.pdf JavaScript
PDF
L7. Object in JS, CSE 202, BN11.pdf JavaScript
PDF
L9. Math object in JS, CSE 202, BN11.pdf
PDF
L10. Math.random method in JS, CSE 202, BN11.pdf
PDF
L8. Constructor and method in JS, CSE 202, BN11.pdf
L11, Project Survey , Spring 24, lecture notes, SB.pdf
L5, Computation of Area, Spring 24, SB.pdf
L4, Contour, Spring 24, lecture notes, SB.pdf
L3, Traverse Survey, Spring 24, lecture notes, SB.pdf
L8, Tacheometry survey, Spring 24, SB.pdf
L10, Astronomical surveying, Spring 24, SB.pdf
L6, Computation of Volume, Spring 24, SB.pdf
L9, photogrammetric survey, Spring 24, SB.pdf
L2, Level surveying, Spring 24,class notes, SB.pdf
Confusion matrix in Transportation Engineering.pdf
Ordinary least square (OLS) and MLE in Transportation Engineering.pdf
L5, Loop and iteration, CSE 202, BN11.pdf
L6, Array in JS, CSE 202, BN11.pdf JavaScript
L4, Conditional statement, CSE 202 JavaScript
L3. Operators in JS, CSE 202, BN11.pdf JavaScript
L1. Introduction, CSE 202, BN11.pdf JavaScript
L7. Object in JS, CSE 202, BN11.pdf JavaScript
L9. Math object in JS, CSE 202, BN11.pdf
L10. Math.random method in JS, CSE 202, BN11.pdf
L8. Constructor and method in JS, CSE 202, BN11.pdf
Ad

Recently uploaded (20)

DOCX
573137875-Attendance-Management-System-original
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Geodesy 1.pptx...............................................
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
Digital Logic Computer Design lecture notes
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPT
Project quality management in manufacturing
573137875-Attendance-Management-System-original
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
CH1 Production IntroductoryConcepts.pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
UNIT 4 Total Quality Management .pptx
CYBER-CRIMES AND SECURITY A guide to understanding
Operating System & Kernel Study Guide-1 - converted.pdf
OOP with Java - Java Introduction (Basics)
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Geodesy 1.pptx...............................................
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Digital Logic Computer Design lecture notes
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
R24 SURVEYING LAB MANUAL for civil enggi
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Project quality management in manufacturing

L2. Function in JS, CSE 202, BN11.p1df documents

  • 1. Course Code: CSE 202 Course Title: Computer Programming Lab Lecture 2: Function in JS Course Teacher: Saurav Barua (SB) Assistant Professor, Dept. of CE, DIU Phone: +88-01715334075 Email: saurav.ce@diu.edu.bd
  • 2. Contents 2 Function Basic HTML tags Basic CSS selectors and properties DOM manipulation through JS DOM . Parameter, argument.
  • 3. 3
  • 4. Accessing element by ID 4 Syntax: document.getElementById(“ID_name"); ❑ The getElementById() method of the Document interface returns an Element object representing the element whose id property matches the specified string. ❑ Since element IDs are required to be unique if specified, they're a useful way to get access to a specific element quickly.
  • 5. Function definition 5 Syntax: function function_name(){ //code } function_name(); //function invoking A function in JavaScript is similar to a procedure—a set of statements that performs a task or calculates a value, but for a procedure to qualify as a function, it should take some input and return an output. A function definition (also called a function declaration, or function statement) consists of the function keyword, followed by: ✓ The name of the function. ✓ A list of parameters to the function, enclosed in parentheses and separated by commas. ✓ The JavaScript statements that define the function, enclosed in curly braces, { /* … */ }.
  • 6. Return 6 Syntax: function function_name(){ //code return val; } ✓ The return statement ends function execution and specifies a value to be returned to the function caller. ✓ Return can be zero, value or expression. ✓ Return must be stored in a variable.
  • 7. Parameters and arguments 7 Syntax: function function_name(para1, para2){ //code } function_name(arg1, arg2); ❑ Function parameters are the names listed in the function definition. ❑ Function arguments are the real values passed to (and received by) the function.
  • 8. Changing text in html 8 Syntax: element.innerText = text; The innerText property sets or returns the text content of an element.
  • 9. Worked out example 9 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.) Google drive link: https://drive.google.com/drive/folders/15iLzTYrZmplDGngEk1e1CnhMIBoR X1OF?usp=drive_link Git-hub link: https://github.com/sauravbarua02/MasonCostEstimate
  • 11. html codes 11 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial- scale=1.0"> <title>Function Calculation</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="container" id="container"> </div> <script src="app.js"></script> </body> </html>
  • 12. css codes 12 body{ margin: 0px; background-color: rgba(176, 129, 219, 0.6); } .container{ background-color: rgba(176, 129, 219, 0.2); height: 300px; width: 300px; margin: 20px auto; padding: 10px; display: flex; flex-direction: column; justify-content: center; align-items: center; border-radius: 10px; box-shadow: 0 0 3px 1px rgba(0,0,0,0.5); }
  • 13. JS codes 13 const containerEl = document.getElementById("container"); function costEstimate(masonNo, perDayWage){ const totalCost = masonNo * perDayWage; return totalCost; } costValue = costEstimate(5,500); containerEl.innerText = "Total masonry labor cost: " + costValue + " BDT";
  • 14. Class tasks 14 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)
  • 15. End of the Lecture 15