-
Notifications
You must be signed in to change notification settings - Fork 0
/
Practice.js
231 lines (129 loc) · 3.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
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
const { error } = require('console');
const fs = require('fs');
// console.log("reading the file asynchronously");
// console.log("async reading start");
// fs.readFile('hey.txt','utf-8',(error , data)=>{
// if(error){
// console.log("getting error in reading the file");
// }
// console.log("file content is:",data);
// })
// console.log("async end");
// console.log("reading the file synchronously");
// console.log("sync reading start");
// fs.readFileSync('hey.txt','utf-8',(error,data)=>{
// if(error){
// console.log("getting errror in reading the file")
// }
// console.log("file data is",data)
// })
// console.log("sync reading end")
// writing in the file
// const content ="hello"
// // console.log(fs);
// fs.writeFile('hey.txt',content,'utf8',(error)=>{
// if(error){
// console.log(error)
// }
// console.log("write the file successfullly")
// // console.log(data);
// })
// const cont = "hello buddy!";
// fs.appendFile('hey.txt',cont,'utf8',(error)=>{
// if(error){
// console.log(error);
// return;
// }
// console.log("appending was done successfully");
// })
// making new directory
// fs.mkdir('sunny',(error)=>{
// if(error){
// console.log("directory already exists");
// }
// console.log("directory created successfullly");
// })
// to remove any directory
// fs.rmdir('sunny',(error)=>{
// if(error){
// console.log(error)
// }
// console.log("directory removed successfullly")
// })
// shallow copy and deep copy
// let obj = {
// name: "sunny",
// age: 22,
// city: {
// country: 'india',
// state: 'uttar pradesh',
// }
// }
// console.log("the value of the object is:")
// console.log(obj)
// let obj2 = new Object({ ...obj, test:10 });
// console.log("after the shallow copy:");
// console.log(obj2)
// obj2.city.state = "delhi";
// console.log("the value of the object 2 after changing the value of the state is:")
// console.log(obj.city.state);
// console.log(obj2.city.state);
// deep copy of the object
// let obj = {
// name: "sunny",
// age: 22,
// city: {
// country: 'india',
// state: 'uttar pradesh',
// }
// }
// console.log("the value of the object is:")
// console.log(obj)
// let obj2 = JSON.parse(JSON.stringify(obj));
// console.log("after the shallow copy:");
// console.log(obj2)
// obj2.city.state = "delhi";
// console.log("the value of the object 2 after changing the value of the state is:")
// console.log(obj.city.state);
// console.log(obj2.city.state);
// itrative approach of doing this coz this is not an itrative approach
// let obj = {
// name: "sunny",
// age: 22,
// city: {
// country: 'india',
// state: 'uttar pradesh',
// }
// }
// console.log("obj is");
// console.log(obj)
// function makeDeepCopy(obj){
// // check for the object
// if(typeof obj != 'object' || obj == null){
// return obj;
// }
// // checking for the array
// const copiedVal = Array.isArray(obj) ? [] : {};
// const keys = Object.keys(obj);
// for(var i = 0;i<keys.length;i++){
// copiedVal[keys[i]]=makeDeepCopy(obj[keys[i]]);
// }
// return copiedVal;
// }
// var copy = makeDeepCopy(obj)
// console.log("deep copy of the obj is");
// console.log(copy)
// obj.city.state="west bangal";
// console.log(obj.city.state)
// console.log(copy.city.state)
let arr = [1,2,4,6,[1,23,4]];
console.log("the array is:")
console.log(arr);
console.log("swallow copy of array is");
const shallow = [...arr];
console.log(shallow);
shallow[4][1]=13;
console.log("the value of shallow array after changing its value")
console.log(shallow)
console.log("the value of the array is ")
console.log(arr)