-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_javascript.js
More file actions
268 lines (185 loc) · 5.29 KB
/
basic_javascript.js
File metadata and controls
268 lines (185 loc) · 5.29 KB
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
// Object Example
const bookDetails = [{
name: "Machine learning",
price: 200
}, {
name: "Deep Learning",
price: 300,
discount: {
firstNumber: 10,
secondNumber: 20,
thridNumber: 30
}
}, {
name: "SSL Learning",
price: 20
}
];
console.log(bookDetails["name"]);
// Example of indexOf & lastIndexOf
function findIndex(str, target){
console.log("Orginal Stringh:", str);
console.log("Index:", str.lastIndexOf(target)); //User can implement indexOf/lastIndexOf
}
findIndex("Hello world world", "world");
// Example of CallBack function
function calculation(a,b, total){
const ans = total(a,b);
return ans;
}
function sum(a, b){
return a + b;
}
console.log(calculation(1,2,sum));
// Example of call back
function sum(a,b,finToCall){
const ans = a + b;
finToCall(ans);
}
function displayResult(data){
console.log("Sum of a and b:", data);
}
console.log(sum(1,2,displayResult));
// Example of setTimeOut & setInterval
function greet(){
console.log("hello world");
}
setTimeout(greet, 3*1000);
setInterval(greet, 2000);
// Example of call back function
function calculation(a, b, totalFunction) {
return totalFunction(a, b);
}
function sum(a, b) {
return a + b;
}
function setTimeOut(totalFunction, duration) {
setTimeout(totalFunction, duration);
}
console.log(calculation(1, 2, sum));
//Example of Slice
function getSlice(str, start, end){
console.log("Orginal String:", str);
console.log("After Slice", str.slice(start, end));
}
getSlice("hello world", 1, 5);
//Other way to implementing the slice
function cutIt(str, startIndex, endIndex){
let newStr = "";
for(let i = 0; i < str.length; i++){
if(i >= startIndex && i < endIndex){
newStr = newStr + str[i];
}
}
return newStr;
}
console.log(cutIt("hello world", 1, 5));
// Example of replace
const str = "hello world";
console.log(str.replace("world", "javascript"));
// Example of split function
const value = "hello world javascript";
console.log(value.split(" "));
// Example of trim (It helps removing space of benning and end)
const value1 = " hello world ";
console.log(value1.trim());
//Example of concat
const arr1 = [1,2,3];
const arr2 = [4,5,6];
console.log(arr1.concat(arr2));
// Example for forEach with callBack function
const arrTest = [1,2,3];
function logThing(str){
console.log(str);
}
arrTest.forEach(logThing);
//////////////////////////////////////////////////// Ops In Javascript /////////////////////////////////////////////////
// Example of class and object
class animle { // Creating class
constructor(name, legCount, speaks){
this.name = name;
this.legCount = legCount;
this.speaks = speaks;
}
speak(){
console.log("hi animel speak:", this.speaks);
}
// Static method: It is used to call the class itself
static myType(){
console.log("Animle");
}
}
console.log(animle.myType()); // call the function mytype by using class name
let dog = new animle("dog", 4, "bhow bhow"); // Creating object
let cat = new animle("cat", 4, "meow");
cat.speak(); // It return this -> hi animel speak: meow (Call function on object)
console.log(cat) // Its return this result -> animle { name: 'cat', legCount: 4, speaks: 'meow' }
/////////////////////////////////////////////////////// Date & Time //////////////////////////////////////////////////////
//Example of getting date and time
const currentDate = new Date();
console.log(currentDate.getDate());
console.log(currentDate.getFullYear());
// "Creating a function to measure the program execution time."
function calculation (){
let a = 0;
for(let i = 0; i < 100000000; i++){
a = a + i;
}
return a;
}
const beforeDate = new Date();
const beforeTimeInMs = beforeDate.getTime();
calculation();
const afterDate = new Date();
const afterTimeInMs = afterDate.getTime();
console.log(afterTimeInMs - beforeTimeInMs);
//////////////////////////////////////////////////////Async Function//////////////////////////////////////////////////
// Example of Async function
const fs = require('fs');
fs.readFile('testing.txt', 'utf-8', (err, data) => console.log(data));
console.log('Testing the system');
let a = 0;
for(let i = 0; i<=10000; i++){
a++;
}
console.log('testing is underway');
// Example of own Asynchronous function
const fs = require('fs');
function readText(cb){
fs.readFile('testing.txt', 'utf-8', (err, data) => cb(data));
}
function oneDone(data){
console.log(data);
}
readText(oneDone);
//Example of Promise
const fs = require('fs');
function readText(){
return new Promise((resolve) => fs.readFile('testing.txt', 'utf-8', (err, data) => resolve(data)))
}
function oneDone(data){
console.log(data);
}
readText().then(oneDone);
/////////////////////////////////////////////////////// map, filters, and arrows function ////////////////////////////////////////////////////////
// Arroow function
const sum = (a,b) =>{
return a+b;
}
// Map function
const input = [1,2,3,4,5];
const ans = input.map((i) =>{
return i * 2;
});
console.log(ans);
// Filters function
const arr = [1,2,3,4,5];
const result = arr.filter((i) =>{
if(i % 2 == 0){
return true;
}
else{
return false;
}
});
console.log(result);