-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasics.js
More file actions
206 lines (169 loc) · 4.34 KB
/
basics.js
File metadata and controls
206 lines (169 loc) · 4.34 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
console.log("dynamically typed, no need to mention type");
console.log("var can be redeclared and updated,let cannot be redeclared but updated and none for const");
console.log("heloo");
alert("hiii")
x = null;
console.log(typeof x);
y = undefined
typeof y;
console.log(x);
console.log(typeof y);
let name = "molly"
x = 5;
console.log(x)
var e = 9;
var e = 99;
let n = 99
n = 95;
const age = 44;
typeof age;
let a;
console.log(a);
console.log("not with const");
console.log("var is global while let and const are not");
console.log("7 primitives int. boolean str null bigint undifined symbol");
let t = Symbol("hiii")
let d = BigInt("123")
console.log(typeof t)
console.log("key value pair")
const student = {
namee : "molly",
age :78,
hobby : "singing",
pass: true
};
console.log(student)
student["namee"] = "divi"
console.log(student["namee"])
console.log("const obj key value can be changed")
//this is single line comment
/*multiline*/
console.log("a^b",5**6)
let num1 = 3;
let num2 = "3";
console.log(num1==num2)
//strict version
console.log(num1===num2)
let ages = 56;
if(ages<=18){
console.log("cant vote");
}
else if(ages===22) console.log("can vote");
else console.log("can not vote");
let q = 75
q==55 ? console.log("cant diee"):q>76 ? console.log("live more"):console.log("leave");
for(var i = 1;i<=5;i++){
console.log(i);
}
console.log(i);
//will give error if i was let
console.log(5/4)//float
let str = "molly"
for(let i of str) console.log(i)//in gives index,of gives values
let stu = {
name:"DUSG",
age:423,
cgpa : 7.5
}
for(let keys in stu){
console.log(stu[keys])
}
let string = "SINGLE CODE WILL ALSO WORK";
console.log(string.length)
//string is an object and are immutable
console.log(string)
let item = {
price:10
}
console.log(item.price)
console.log(`the cost of item is ${10}`);
//template literal
//number is highlighted where string is not
//escape symbol like/n is consided in len as 1
// all string methods does not cahnge original str
let g = "apna\ncollege"
console.log(g.length);
let neww = g.toUpperCase()
let namesss = " heelooo my namee. "
console.log(namesss.trim())//remove start and end space
console.log(namesss.slice(6,9)) //last index not inc.
console.log(namesss.concat(g))
g = g+namesss;//can be updated
console.log(g);
console.log(g.replace('h','w'));
console.log(g.slice(2))//till last index
let res = "hello" + 123
console.log(g.charAt(3))
//str[0] is not possible
let arr = [65,"hulk",true,7.9];
console.log(arr)
console.log(arr.length)//property not method
//arrays are mutable
arr.push("erg")
for(let i of arr) console.log(i)
let del =arr.pop();
for(let i of arr) console.log(i)
console.log(del)
console.log(arr.toString())//another array
let num = [1,2,3,4];
let v = num.concat(arr)
arr.unshift("erg");
for(let i of arr) console.log(i)
let val = arr.shift();
console.log(val)
//slice is same as string
let ar = [1,2,3,4,5]
//splice changes original array
ar.splice(2,2,101,102)//del and replacefrom 2idx
ar.splice(2,0,103)//insert at 2
ar.splice(3,2) //del 2 elments from 3 idx
console.log(ar.splice(4)) // acts as slice and dletes it
console.log(ar)
function sum(w){
w = w+5
return w;
}
let w = 6;
console.log(sum(w));
console.log(w);
const diff = (x,y) => {
return x-y
}
console.log(diff(3,4))
const hellooo = (x,y) => {
console.log(x+y);
} // arrow function
hellooo(5,7)
//methods are function that can only be used for certain data structure
//callback fn is passed as an argumnet in other function
//for each methos can be used withh array map set nit with obj and string;
//x is each element of array
ar.forEach((x) => {
console.log(x);
})//returns noting
ar.forEach(function call(val,idx,arr){
console.log(val,idx,arr)
})
/*👉 A Higher Order Function is a function that:
✅ Takes another function as an argument
OR
like foreach
✅ Returns another function*/
//map return a new arr;
let arrayss = ar.map((val) => {
return val*val;
})
console.log(arrayss)
//similary val idx and array is passed
//filter checks the consdition for every val in array and return new array of ele that satisfies
let r = ar.filter((val) => {
return val%2==0;
})
//when values of array operated to single value like sum avg etc->reduce
let output = ar.reduce((prev,curr) => {
return prev+curr;//return is stored in prev
})
console.log(output)
let ou = ar.reduce((prev,res)=>{
return prev>res ? prev:res
})