0% found this document useful (0 votes)
11 views17 pages

L3. Operators in JS, CSE 202, BN11

JavaScript

Uploaded by

Saurav Barua
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views17 pages

L3. Operators in JS, CSE 202, BN11

JavaScript

Uploaded by

Saurav Barua
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Course Code: CSE 202

Course Title: Computer Programming Lab


Lecture 3: Operators in JS
Course Teacher: Saurav Barua (SB)
Assistant Professor, Dept. of CE, DIU
Phone: +88-01715334075
Email: [email protected]
Contents

Types of
operators Arithmetic Assignment
operator operator
.

Comparison Logical Ternary operator


operator operators

2
Types of operators
There are different types of JavaScript operators:
1. Arithmetic Operators
2. Assignment Operators
3. Comparison Operators
4. String Operators
5. Logical Operators
6. Bitwise Operators
7. Ternary Operators
8. Type Operators
3
Arithmetic operator
Operator Description
+ Addition
- Subtraction
* Multiplication
** Exponentiation (ES2016)
/ Division
% Modulus (Division Remainder)
++ Increment
-- Decrement

4
Arithmetic operator

 i++ stands for i= i+1


 % stands for reminder. Example: 6%4 = 2.

5
Assignment operator
Operator Example Same As
= x=y x=y
+= x += y x=x+y
-= x -= y x=x-y
*= x *= y x=x*y
/= x /= y x=x/y
%= x %= y x=x%y
**= x **= y x = x ** y

6
Comparison operator
Operator Description

== equal to
=== equal value and equal type
!= not equal
!== not equal value or not equal type
> greater than
< less than
>= greater than or equal to
<= less than or equal to
? ternary operator

7
Comparison operator

 20==“20”. //output: True. == only check value.


 20===“20”. //output: False. === check value and data type.
Syntax for ternary operator:
condition ? exprIfTrue : exprIfFalse
Example:
const age = 26;
const beverage = age >= 5 ? “Coke" : "Juice";
console.log(beverage); // output: Coke
Syntax:
variablename = (condition) ? value1:value2

Example: let voteable = (age < 18) ? "Too young":"Old enough";


8
Logical operator

Operator Description Example


&& and (x < 10 && y > 1) is true
|| or (x == 5 || y == 5) is false
! not !(x == y) is true

9
logical operator

Logical operator only return true (1) or false (0).


 Evaluate: (2<3)&&(4>2||3<2)
 Evaluate: (!1&&2>=3)||(3<4)

10
Worked out example
Example 3: Program to take input from prompt and calculate through JS and
display in the browser. (Calculate area of a triangle, take length and height as an
input through prompt and display in the browser.)

Google drive link:


https://drive.google.com/drive/folders/1DNqm-
2rFkuRJXg5Cb6PMa9fgd82mE-pD?usp=drive_link

Git-hub link: https://github.com/sauravbarua02/triangleAreaCalculation

11
Interface

12
html codes
<!DOCTYPE html> <body>
<html lang="en"> <div class="container"
<head> id="container">
<meta charset="UTF-8"> <div id="result"></div>
<meta name="viewport"
content="width=device-width, initial- <script src="app.js"></script>
scale=1.0"> </div>
<title>LogicControl</title> </body>
<link rel="stylesheet" href="style.css"> </html>
</head>

13
css codes
margin: 20px auto;
body{
margin: 0px; padding: 10px;
background-color: rgba(90,90,230,0.6); display: flex;
} flex-direction: column;
justify-content: center;
.container{ align-items: center;
background-color: rgba(90,90,230,0.3); border-radius: 10px;
width: 300px; box-shadow: 0 0 3px 1px rgba(0,0,0,0.5);
height: 300px; font-size: large;

14
JS codes

const resultEl =
document.getElementById("result"); function areaCal(){
const length = prompt("Enter length in meter const area = 0.5*length*height;
= "); resultEl.innerText = "Area of the
const height = prompt("Enter height in meter triangle: " + area + " sq. meter";
= "); }

areaCal();

15
Class tasks
Task 3.1: Program to calculate average penetration in mm from three bitumen
samples in the bitumen penetration test. Take penetration values as input and
display average value in the browser. (hints: average = (val1 + val2 +val3)/3)
Task 3.2: Program to calculate area of a rectangular industrial plot. Take width and
breadth as input and display area (sq. ft) in the browser. (hints: areaRectangle = width x
breadth)
Task 3.3: Program to calculate discharge of an irrigation canal. Take cross-section and
water velocity as input and display discharge (cum/sec) in the browser. (hints:
discharge= crossSection x velocity)
Task 3.4: Program to calculate delay in a signalized intersection. Take travel time and
free flow travel time as input and display delay (sec) in the browser.(hints: delay =
travelTime – freeFlowTravelTime)
Task 3.5: Program to calculate maximum bending moment of a simply supported
beam under UDL . Take UDL and span length as input and display moment (kip-ft) in
the browser.(hints: maxMoment = 1/8 x UDL x span x span)
16
End of the Lecture

17

You might also like