-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasy.js
146 lines (93 loc) · 2.91 KB
/
easy.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
//*******************************************************************************************************
function abc() {
console.log(a)
var a = 10
}
// Sol - undefined
//*******************************************************************************************************
// Polyfill for map
Array.prototype.MyMap = function(cb){
const res = []
for(let i = 0; i < this.length ; i++){
res.push(cb(this[i]))
}
return res
}
// Ployfill for filter
Array.prototype.MyFilter = function(cb) {
const res = []
for(let i = 0; i < this.length ; i++) {
if(cb(this[i])){
res.push(this[i])
}
}
return res
}
// Polyfill for reduce
Arrar.prototype.MyReduce = function(cb) {
}
//*******************************************************************************************************
Q) What is difference between forEach or Map ?
Map returns array whereas forEach doesnt
Q) let students= [
{ name : "Piyush" , roll : 31 , marks : 80},
{ name : "Rahul" , roll : 32 , marks : 80},
{ name : "Jane" , roll : 33, marks : 80},
{ name : "John " , roll : 34 , marks : 80},
];
function solve(students) {
let ans = []
for(let i = 0 ;i < students.length ; i++) {
let new_name = students[i].name.toUpperCase()
ans.push(new_name)
}
return res
}
-OR-
const names = students.map((stu) => stu.name.toUpperCase())
const stud = students.filter((stu) => stu.marks > 60 )
// Chaining in it
// Map filter and reduce can be chained
const names = students.filter((stu) => stu.marks > 60 ).map((stu) => stu.name)
//*******************************************************************************************************
// Hoisting , scope , callback , arrow functions
Q) What is function declaration ?
function (num) {
return num * num
}
Q) What is function expression ?
const sqaure = function (num) {
return num * num
}
Q) what are First class functions?
A programming language is said to have First-class functions if functions in that language are treated like other variables
Q) What is IIFE ?
(() => console.log("This is an IIFE"))()
Q) (function(x){
return (function (y) {
console.log(x) //1
}(2)
}(1)
//*******************************************************************************************************
console.log("a");
setTimeout(() => console.log("b"), 0);
Promise.resolve("c").then((value) => console.log(value));
console.log("d");
//*******************************************************************************************************
function fn() {
for (var count = 0; count < 3; count++) {
setTimeout(function () {
console.log(count);
}, 1000);
}
}
//*******************************************************************************************************
function fn() {
for (var count = 0; count < 3; count++) {
(function (c) {
setTimeout(function () {
console.log(c);
}, 1000);
})(count);
}
}