-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
243 lines (194 loc) · 4.62 KB
/
script.js
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
alert("thisnweb site not complet" )
let value=prompt("enter the age")
console.log(value);
//Arithmetic Operators
// let name = prompt("What is your name?");
// alert("Hello, " + name + "!");
let a=100;
let b=50,c=3;
console.log(a+a);
console.log(a-b);
console.log((a*b)+b);
console.log((a/b)*b);
console.log(((a+b)/b)%c);
console.log(a**c);
//Logical Operators
//&& , || ,!
// let a=true
// let b=false
// console.log((a&&b),(a||b),(!a));
// var name = "gowthaman";
// let age = 23;
// const state = "Tamilnadu";
// console.log(name);
// console.log(age);
// console.log(state);
// functions
let name="gowthaman";
let age=23;
function myDetails(){
console.log("My Details");
console.log(name);
console.log(age);
}
myDetails()
//Arrow Function
const multiply = (x, y) => x * y;
let result = multiply(4, 5);
console.log(result);
//Higher-Order Function
function applyFunction(func, value) {
return func(value);
}
// let result = applyFunction(x => x ** 2, 10);
// console.log(result);
// IIFE (Immediately Invoked Function Expression)
(function() {
console.log("This is an IIFE!");
})();
function outerFunction(str) {
function innerFunction() {
console.log(str);
}
return innerFunction;
}
let greet = outerFunction("Hello Welcome!");
greet();
function display() {
return 10;
}
let retVal=display()
console.log(retVal);
function outerFunction() {
console.log("outerFunction");
}
function innerFunction() {
console.log("InnerFuntions");
return outerFunction();
}
const retRes=innerFunction();
retRes();
// If-Else Statement
function checkNumber(num) {
if (num > 0) {
console.log("The number is positive.");
} else if (num < 0) {
console.log("The number is negative.");
} else {
console.log("The number is zero.");
}
}
checkNumber(10);
checkNumber(-5);
checkNumber(0);
// Nested if-else used Grading System Based on Marks
function getGrade(marks) {
if (marks >= 90) {
console.log("Grade: A");
} else {
if (marks >= 80) {
console.log("Grade: B");
} else {
if (marks >= 70) {
console.log("Grade: C");
} else {
if (marks >= 60) {
console.log("Grade: D");
} else {
console.log("Grade: F");
}
}
}
}
}
getGrade(95);
getGrade(85);
getGrade(75);
getGrade(65);
getGrade(50);
function getGrade(marks) {
if (marks >= 90) {
console.log("Grade: A");
} else if (marks >= 80) {
console.log("Grade: B");
} else if (marks >= 70) {
console.log("Grade: C");
} else if (marks >= 60) {
console.log("Grade: D");
} else {
console.log("Grade: F");
}
}
getGrade(93)
getGrade(84)
getGrade(62)
getGrade(40)
getGrade(71)
// Ternary Operator (Shorthand If-Else)
function checkEvenOrOdd(num) {
let result = (num % 2 === 0) ? "Even" : "Odd";
console.log(`The number is ${result}.`);
}
checkEvenOrOdd(20);
checkEvenOrOdd(123);
// Switch Statement
function getDayOfWeek(dayNumber) {
let dayName;
switch (dayNumber) {
case 1:
dayName = "Sunday";
break;
case 2:
dayName = "Monday";
break;
case 3:
dayName = "Tuesday";
break;
case 4:
dayName = "Wednesday";
break;
case 5:
dayName = "Thursday";
break;
case 6:
dayName = "Friday";
break;
case 7:
dayName = "Saturday";
break;
default:
dayName = "Invalid day number";
}
console.log(`Day of the week: ${dayName}`);
}
// Test the function with different values
getDayOfWeek(1); // Output: Day of the week: Sunday
getDayOfWeek(4); // Output: Day of the week: Wednesday
getDayOfWeek(7); // Output: Day of the week: Saturday
getDayOfWeek(8); // Output: Day of the week: Invalid day number
// While Loop
let count = 0;
while (count < 5) {
console.log("Count is: " + count);
count++;
}
// Do-While Loop
// let count = 0;
// do {
// console.log("Count is: " + count);
// count++;
// } while (count < 5);
// for loop
for (let i = 1; i <= 5; i++) {
console.log("Number: " + i);
}
const fruits = ["Apple", "Banana", "Cherry", "Date", "Elderberry"];
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
// Nested for Loop
for (let i = 1; i <= 3; i++) {
for (let j = 1; j <= 3; j++) {
console.log(`i = ${i}, j = ${j}`);
}
}