-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
383 lines (297 loc) · 7.49 KB
/
index.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// Basic Code of Javascript, It's usefull when you learn JS.
// Four Data Types In JavaScript
// Numbers
var number1 = 10;
var number2 = 20;
console.log(number1 + number2);
// String
var str1 = "This is string";
var str2 = "This is string";
console.log(str1)
console.log(str2)
// Objects
var marks = {
krish: 100,
max: 92,
jhon: 93,
}
console.log(marks)
// Booleans
var a = true;
var b = false;
console.log(a)
console.log(b)
// Undefined
var und;
console.log(und);
// Null
var null1 = null;
console.log(null);
/* At a very high level,
there are two types of datatypes in JavaScript.
1. Primitive data types: undefinde, null, number, string, boolen, symbol
2. Refernece data types: Arrays and Objects
*/
// var arr = [1,2,"Krish",4,5]
// Operators in javascript
// 1. Airithmetic Operators
var a = 5;
var b = 5;
console.log("The value of a + b is ", a + b);
console.log("The value of a - b is ", a - b);
console.log("The value of a * b is ", a * b);
console.log("The value of a / b is ", a / b);
// 2. Assingnment Operators
var c = b;
c += 2; // 7
c -= 2; // 3
c *= 2; // 10
c /= 2; // 2.5
console.log(c)
// 3.Comparison Operators
var x = 10;
var y = 20;
console.log(x == y);
console.log(x >= y);
console.log(x <= y);
console.log(x != y);
// 4. Logical Operators
// AND
console.log(true && true);
console.log(true && false);
console.log(false && false);
// OR
console.log(true || true);
console.log(true || false);
console.log(false || false);
// NOT
console.log(!false);
console.log(!true);
// Function in JavaScript
// DRY = Do not repeat yourself
function maths(a, b){
return a + b
}
console.log(maths(2, 4))
// Conditional in JS
var userAge = prompt("Enter Your age: ");
// if statement
if(userNumber >= 18){
alert("You can drive a vehcal.")
}
// if - else Statement
if(userNumber >= 18){
alert("You can drive a vehcal.")
} else {
alert("You can't drive vehical.")
}
// if-else Ladder
if (userAge > 32){
alert("You are under 32")
}
else if (userAge > 28){
alert("You are under 28")
}
else if (userAge > 22){
alert("You are under 22")
}
else if (userAge > 18){
alert("You are under 18")
}
alert("End Of Game")
// Loops in JS
var arr = [1,2,3,4,5,6,7];
console.log(arr)
for (var i = 0; i < arr.length; i++){
console.log(arr[i])
}
// Switch Statement
let a = 2 + 2;
switch(a){
case 3:
alert('Too small');
case 4:
alert("Exactly");
case 5:
alert('Too big');
}
function myName(firstName, lastName){
alert('Hello, '+ firstName +" "+ lastName);
}
myName("Aagja","Krish")
function checkAge(Age){
if (Age > 18){
return alert("You can drive a vehical.")
}
else{
return alert("You can't drive vehical.")
}
}
checkAge(19)
let sum = (a,b) => {
let result = a + b;
return result;
}
alert(sum(1,2));
// Objects
let collegTopStudents = {
Surname: "Aagja",
name: "Krish",
SPI : 9.12,
Enrollment: 226170316001,
}
alert("GPA In I.T. Engineering is top student name is "+collegTopStudents.Surname +" "+ collegTopStudents.name + " "+ collegTopStudents.SPI +" "+collegTopStudents.Enrollment+ " .")
const user = {
name: "Jhon"
};
user.name = "Krish";
alert(user.name);
let user = {
name: "Krish",
sizes: {
age: 17,
height: 1.84,
width: 65,
}
}
let clone = Object.assign({}, user);
alert(user.sizes === clone.sizes);
user.sizes = 64;
alert(clone.sizes.width);
let user = {
name: "Jhon",
age: 30
};
user.sayHi = function(){
alert("Hello! ");
};
user.sayHi();
function User(name){
this.name = name;
this.isAdmin = false;
}
let user = new User("Jack");
alert(user.name);
alert(user.isAdmin);
// Dates in JS
let myDate = new Date();
console.log(myDate);
console.log(myDate.getTime());
console.log(myDate.getFullYear());
console.log(myDate.getDay());
console.log(myDate.getHours());
// Document Object Module
let elem = document.getElementById('click');
console.log(elem);
let elemclass = document.getElementsByClassName("container");
console.log(elemclass);
elemclass[0].classList.add("bg-primary")
elemclass[0].classList.add("text-success")
elemclass[0].classList.remove("text-success")
console.log(elem.innerHTML);
console.log(elem.innerText);
console.log(elemclass[0].innerHTML);
console.log(elem.innerText);
tn = document.getElementsByTagName('div')
console.log(tn)
createElement = document.createElement('p');
createElement.innerText = "This is a created para."
tn[0].appendChild(createElement);
createElement2 = document.createElement('b');
createElement2.innerText = "This is a created bold."
tn[0].replaceChild(createElement2, createElement);
removeChild(Element)
// Selecting using Query
sel = document.querySelector('.container');
console.log(sel)
sel = document.querySelectorALL('.container')
console.log(sel)
// Events in JavaScript
function clicked(){
console.log('The button was clicked')
}
window.onload = function(){
console.log('The document was loaded.')
}
firstcontainer.addEventListener('click', function(){
document.querySelectorAll('.container')[1].innerHTML = "We have click."
console.log("Click Hua")
})
firstcontainer.addEventListener('mouseover', function(){
console.log("Mouse on container")
})
firstcontainer.addEventListener('mouseout', function(){
console.log("Mouse out container")
})
let preveHTML = document.querySelectorAll('.container')[1].innerHTML;
firstcontainer.addEventListener('mouseup', function(){
document.querySelectorAll('.container')[1].innerHTML = preveHTML;
console.log("Mouse up container")
})
firstcontainer.addEventListener('mousedown', function(){
document.querySelectorAll('.container')[1].innerHTML = "We have click."
console.log("Mouse down container")
})
// Normal Function
function sum(a,b){
return a + b;
}
// Arrow Function
sum = (a,b) => {
return a + b ;
}
// SetTimeout and setinterval
logKaro = () => {
document.querySelectorAll('.container')[1].innerHTML = "Set TimeOut Fired"
console.log("I am your log")
}
clr = setTimeout(logKaro, 5000);
clr = setInterval(logKaro, 2000);
// Backticks
a = 34;
console.log(`this is my ${a}`)
// JavaScript LocalStorage
localStorage.setItem('name','krish');
localStorage.getItem('name')
localStorage.removeItem('name')
localStorage.clear();
// JSON
obj = {name: "Krish", length: 1,a: {this: `tht"s`}}
jso = JSON.stringify(obj);
console.log(typeof(obj))
console.log(typeof(jso))
console.log(obj)
console.log(jso)
parsed = JSON.parse(`{"name" : "Krish", "length": 1, "a": {"this":"that"}}`)
console.log(parssed);
// Class
class User{
Using Method constructor
constructor(name){
this.name = name;
}
sayHi(){
alert(this.name);
}
}
let user = new User("Jhon");
user.sayHi();
let userClass = class Myclass{
sayhii(){
alert(Myclass);
}
};
new userClass.sayhii();
alert(Myclass);
class car{
constructor(name, year);{
this.name = name;
this.age = age;
}
age(){
const daTe = new Date();
return daTe.getFullYear() - this.year;
}
}
const myCar = new car('Ford',2023);
document.getElementById("demo").innerHTML = "My car is " + myCar.age() + "years old."