-
Notifications
You must be signed in to change notification settings - Fork 0
/
p3.html
157 lines (124 loc) · 6.37 KB
/
p3.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JS Operators</title>
</head>
<body>
<h1>JavaScript Operators</h1>
<p>Operators are used to assign values, compare values, perform arithmetic operations, and more</p>
<ol>
<li>Arithmetic Operators</li>
<li>Assignment Operators</li>
<li>Comparison Operator</li>
<li>Logical Operator</li>
<li>Type and Bitwise Operator</li>
</ol>
<script>
// 1. Use logical operators to find wether the age of a person lies between 10 and 20.
let age = 20;
age = Number.parseInt(age);
if(age >= 10 && age <=20){
console.log("The age Of the person is: " + age + " The person is lies between 10 to 20.")
}
else{
console.log("The person is not lies between 10 to 20.")
}
// 2. Write a js program to find whether a number is divisible by 2 and 3.
const number = 12;
if(number % 2 ==0 && number % 3 ==0){
console.log("The Number : " + number + " is Divisible by 2 and 3.")
}
else{
console.log("The Number is not Divisible by 2 and 3.")
}
// 3. Write a js program to find whether a number is divisible by 2 or 3
const numb = 20;
if(numb % 2 == 0 || numb % 3 == 0){
console.log("The number "+ numb + " is devisile by 2 or 3")
}else{
console.log("The number is not devisible by 2 or 3.")
}
// 4. Print "you can drive" or "u cannot drive" bsed on age being greater then 18 usig turnary operators.
let old = 10;
let drive = (old >= 18)?"you can drive" : "No you cannot drive";
console.log(drive);
// 1. Arithmetic Operators:
// a) Write a JavaScript function to calculate the area of a rectangle given its width and height.
function areaOfRect(length, breadth){
return length*breadth;
}
console.log("The area of rectangle is: " + areaOfRect(6,7));
// b) Create a function that takes two numbers as input and returns their sum, difference, product, and quotient as an object.
function performOperations(num1,num2){
let obj = {};
obj.sum = num1 + num2;
obj.difference = num1 - num2;
obj.product = num1 * num2;
obj.quotient = num1 / num2;
return obj;
}
let result = performOperations(10,4);
console.log(result);
// 2. Comparison Operators:
// a) Write a JavaScript function to check if a given number is positive, negative, or zero.
function number1(numb){
if(numb > 0){
console.log("The Number " +numb+ " is Positive")
}
else if (numb < 0) {
console.log("The Number " +numb+ " is Negative")
} else {
console.log("The Number " +numb+ " is Zero")
}
}
number1(0);
// b) Create a function that takes two strings as input and compares them lexicographically. Return -1 if the first string comes before the second, 1 if it comes after, or 0 if they are equal.
function stringComparison(str1,str2){
return str1.localeCompare(str2);
}
let op = stringComparison("hello","hello");
if(op == -1){
console.log("The Value is : "+op + " Means The first string comes before the second")
}else if (op == 1) {
console.log("The Value is : "+op + " Means The first string comes After the second")
} else {
console.log("The Value is : "+op + " Means Strings are Equal")
}
// 3. Logical Operators:
// a) Write a JavaScript function that takes a number as input and checks if it falls within the range of 10 to 20 (inclusive).
function range(num){
return num >= 10 && num <= 20;
}
console.log(range(11));
console.log(range(9));
console.log(range(21));
// b) Create a function that takes two boolean values as input and returns true if either of them is true, but not both.
function boolean(bool1,bool2){
return (bool1 || bool2) && !(bool1 && bool2);
}
console.log(boolean(true,false))
// 4. Assignment Operators:
// a) What is the result of the following expression: var x = 10; x = x + x -= 5;
var x = 10; x = x + x; x -= 5;
console.log(x)
// b) Explain the behavior of the expression x = y = z = 5; in JavaScript.
let x1 = y = z = 5;
console.log(z);
console.log(y);
// 5. Ternary Operator:
// a) Write a JavaScript function that takes a number as input and returns "Even" if it's even and "Odd" if it's odd, using the ternary operator.
function turnaryReturn(n){
return (n%2==0)? "even" : "odd";
}
let res = turnaryReturn(5);
console.log(res)
// b) Create a function that takes a boolean value as input and returns "Yes" if it's true and "No" if it's false, using the ternary operator.
function boolTake(n){
return (n==true)? "Yes":"No";
}
console.log(boolTake(true));
</script>
</body>
</html>