-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpractice.js
135 lines (88 loc) · 1.81 KB
/
practice.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
// 1 - Strings
let myStr = "This is my string";
let myStr2 = 'This is my second string';
console.log(myStr);
console.log(myStr2);
let both = `${myStr} + ${myStr2} + \"both concatenated\"
this will be printed in new line beacause of backticks`;
console.log(both);
let str = "hello";
let strLen = str.length;
console.log(strLen);
// String methods
console.log("Slice - ",str.slice(1,3));
console.log("Replace - ",str.replace('lo','p'));
console.log("Search",str.search('e'));
str = " hello";
console.log("Trim - ",str.trim());
// 2 - Arrays
let a = [1,2,3,4,5];
a.push(45);
console.log(a);
a.pop();
console.log(a);
a.reverse();
console.log(a);
a.sort();
console.log(a);
console.log(a.indexOf(2));
console.log(a.length);
a = a.map((a)=>a+1);
console.log(a);
a = a.filter((a)=>a%2==0);
console.log(a);
a = [1,2,3]
a = a.reduce((a,b)=>a+b);
console.log(a);
a = [1,2,3,4,5]
a.shift();
console.log(a);
a.unshift(1);
console.log(a);
// Objects
let obj1 = {
cars : ['Ferrari','Audi','Lamborghini','Porsche'],
str : "This is cars object",
num : 1
};
let obj2 = {
fruits : ["Apple","Mango","Banana","Pear"],
str : "This is fruits object",
num : 2
}
obj1.new = "new property";
delete obj1.new;
let obj = {...obj1, ...obj2};
let {cars:myCars, str:myNewStr} = obj1;
console.log(myCars);
console.log(myNewStr);
let {cars:one,str:two,num:three} = obj1;
console.log(one);
console.log(two);
console.log(three)
console.log(obj1)
console.log(obj2)
console.log(obj)
console.log(Object.keys(obj1));
console.log(Object.values(obj2));
// Loops
// for
for(let i=0;i<5;i++){
console.log(i);
}
// while
i=0;
while(i<5){
console.log(i);
i++;
}
// for-of
let nums = [1,2,3,4,5];
for(let i of nums){
console.log(i)
}
// for-in
for(let i in nums){
console.log(i)
console.log(nums[i])
}