forked from wonmaungthein/JS-Core-1-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.js
More file actions
261 lines (213 loc) · 6.51 KB
/
object.js
File metadata and controls
261 lines (213 loc) · 6.51 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
// ---------------------------------------------------------------------------------------------------
// var student = {
// name: "Simon",
// age: "21",
// interests: ["javascript", "react"]
// };
// function printProperties(obj) {
// for (var key in student) {
// console.log(key);
// };
// };
// printProperties(student);
// // Output in this case should be name, age, interests
// ---------------------------------------------------------------------------------------------------
// var student = {
// name: "Simon",
// age: "21",
// interests: ["javascript", "react"]
// };
// function hasProperty(obj, objName) {
// var result = false
// for (var key in student) {
// if (key === objName) {
// result = true;
// } else {
// continue;
// };
// };
// console.log(result)
// };
// hasProperty(student, "interests"); // should return true
// hasProperty(student, "job"); // should return false
// ---------------------------------------------------------------------------------------------------
// var person = {
// name: "Simon",
// surname: "Regont"
// };
// var student = {
// interests: ["javascript", "react"],
// isalive: true
// };
// student.__proto__ = person; // this is setting the prototype of student to be person
// function ownProperty(obj, objName) {
// var result = false;
// if (obj.hasOwnProperty(objName)) {
// result = true;
// };
// console.log(result);
// };
// ownProperty(student, "name"); // should return false
// ownProperty(student, "interests"); // should return true
// ownProperty(student, "surname");
// ownProperty(student, "isalive");
// console.log(student.__proto__);
// console.log(student);
// ---------------------------------------------------------------------------------------------------
// var student = {
// name: "Simon",
// age: "21",
// interests: ["javascript", "react"]
// };
// function printObject(obj) {
// console.log("Name is " + obj.name + ", age is " + obj.age + ", interests are " + obj.interests)
// };
// printObject(student); //output: "name is Simon, age is 21, interests are ["javascript", "react"]
// ---------------------------------------------------------------------------------------------------
// var students = [
// {
// name: "Etza",
// age: "21",
// interests: ["javascript", "css"]
// },
// {
// name: "Mohamed",
// age: 22,
// interests: ["javascript", "c#"]
// }
// ];
// function printArray(arr) {
// for (i = 0; i < arr.length; i++) {
// printObject(arr[i]);
// };
// };
// function printObject(obj) {
// console.log("Name is " + obj.name + ", age is " + obj.age + ", interests are " + obj.interests)
// };
// printArray(students);
// output:
// "name is Simon, age is 21, interests are ["javascript", "react"]"
// "name is Mohamed, age is 22, interests are ["javascript", "c#"]"
// ---------------------------------------------------------------------------------------------------
// var wallet = {
// 5: 3,
// 10: 7,
// 20: 2
// };
// function summWallet(obj) {
// var sum = 0
// for (var key in obj) {
// sum = sum + (key * obj[key]);
// };
// console.log("Total summ in the wallet is £" + sum)
// };
// summWallet(wallet);
// ---------------------------------------------------------------------------------------------------
var walletA = {
// our original wallet
5: 3,
10: 7,
20: 2
};
var walletB = {
5: 6,
10: 0,
20: 1
};
var wallets = [walletA, walletB];
var sum;
// Write a function - sumWallets - that takes the array wallets and returns the total amount of
//money for all of the wallets.
function sumWallets(arr) {
var total = 0
for (i = 0; i < arr.length; i++) {
summWallet(arr[i]);
total = total + sum;
};
console.log("Total summ in wallets is £" + total);
};
function summWallet(obj) {
sum = 0;
for (var key in obj) {
sum = sum + (key * obj[key]);
};
return sum;
};
//sumWallets(wallets);
// 2 Write another function - combineWallets - that takes the array of wallets and combines all of the
// notes in each of them, returning a single wallet with all of the other wallets' notes.
function combineWallets(arr) {
var newWallet = {};
for (i = 0; i < arr.length; i++) {
for (var key in arr[i]) {
if (newWallet[key] == undefined) {
newWallet[key] = arr[i][key];
} else {
newWallet[key] = newWallet[key] + arr[i][key];
};
};
};
console.log(newWallet)
};
//combineWallets(wallets);
// 3 See if you can write a function - sumDynamicWallet - that will sum up and return the total amount
// in a single wallet, but it could have any number of different notes inside it (£7 note or a £13 or
// any other number).
var walletC = {
// our original wallet
5: 3,
10: 7,
7: 20,
13: 10,
20: 2,
25: 5
};
function sumDynamicWallet(obj) {
var sum = 0
for (var key in obj) {
if ((key == 5) || (key == 10) || (key == 20) || (key == 50)) {
sum = sum + (key * obj[key]);
} else { continue };
};
console.log("Total summ in the wallet is £" + sum)
};
//sumDynamicWallet(walletC);
// 4 See if you can write a function that takes in any number of wallets, which could contain any
// denomination/type of notes inside them (each being different). Tip: have a think about if you
// could re-use a function from a previous example...
var myArr = [1, 3, 56, 74, 5, 788, 335, 67]
var sum = 0;
function average(arr) {
arr.forEach(function (item) {
sum = sum + item;
});
return sum / myArr.length;
};
//console.log(average(myArr));
// ---------------------------------------------------------------------------------------------------
// Transform the following array [4,5,3,6,7,8,9,1,2] by 1) sorting it, 2) doubling each number,
// and 3) filtering numbers less than 10 Write named functions that can be used as callbacks.
function compareNum(a, b) {
if (a > b) return 1;
if (a < b) return -1;
};
var myArr = [4,5,3,6,7,8,9,1,2]
.sort(compareNum)
.map(function (item){
return item *2;
})
.filter(function(item){
return item < 10
});
console.log(myArr);
// var test2 = function () { };
// test2.prototype = {
// a: 1,
// b: 2,
// c: 3
// };
// var obj_test2 = new test2();
// var obj_test3 = new test2()
// console.log(test2.a);
// console.log(obj_test2.a);
// console.log(obj_test3.c);