-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasicObjectExercises.js
More file actions
594 lines (547 loc) · 14.9 KB
/
basicObjectExercises.js
File metadata and controls
594 lines (547 loc) · 14.9 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
'use strict';
/* ------------------------------------------------------ */
/* JavaScript Object - Exercises, Practice, Solution */
/* ------------------------------------------------------ */
/* -------------------------- 1 ------------------------- */
//1. Write a JavaScript program to list the properties of a JavaScript object.
//Solution 1:
const student = {
name : "David Rayy",
sclass : "VI",
rollno : 12 };
function listOfProperties (obj) {
const output = Object.keys(obj).join(', ')
return output
}
console.log(listOfProperties(student))
//Solution 2:
function _keys(obj)
{
if (!isObject(obj)) return [];
if (Object.keys) return Object.keys(obj);
var keys = [];
for (var key in obj) if (_.has(obj, key)) keys.push(key);
return keys;
}
function isObject(obj)
{
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
}
console.log(_keys({red: "#FF0000", green: "#00FF00", white: "#FFFFFF"}));
/* -------------------------- 2 ------------------------- */
//2. Write a JavaScript program to delete the rollno property from the following object. Also print the object before or after deleting the property.
//Solution 1:
const student = {
name: 'David Rayy',
sclass: 'VI',
rollno: 12,
};
console.log(student)
delete student.rollno
console.log(student)
/* -------------------------- 3 ------------------------- */
//3. Write a JavaScript program to get the length of a JavaScript object.
//Solution 1:
console.log(Object.values(student).length); ////or keys
//Solution 2:
Object.objsize = function (myObj) {
let oSize = 0,
key;
for(key in myObj) {
if(myObj.hasOwnProperty(key))
oSize++
}
return oSize
};
let objsize = Object.objsize(student)
console.log('Size of the current object: ' + objsize)
/* -------------------------- 4 ------------------------- */
//4. Write a JavaScript program to display the reading status (i.e. display book name, author name and reading status) of the following books.
//Solution 1:
var library = [
{
author: 'Bill Gates',
title: 'The Road Ahead',
readingStatus: true,
},
{
author: 'Steve Jobs',
title: 'Walter Isaacson',
readingStatus: true,
},
{
author: 'Suzanne Collins',
title: 'Mockingjay: The Final Book of The Hunger Games',
readingStatus: false,
},
];
library.map(element => {
if(element.readingStatus) {
console.log(`Already read ${element.title}`)
}else {
console.log((`You still need to read ${element.title}`))
}
})
//Solution 2:
for (var i = 0; i < library.length; i++) {
var book = "'" + library[i].title + "'" + ' by ' + library[i].author + '.';
if (library[i].readingStatus) {
console.log('Already read ' + book);
} else {
console.log('You still need to read ' + book);
}
}
/* -------------------------- 5 ------------------------- */
//5. Write a JavaScript program to get the volume of a cylindrical with four decimal places using object classes.
//Solution 1:
function Cylinder(height, diameter) {
this.height = height;
this.diameter = diameter;
}
Cylinder.prototype.Volume = function () {
let radius = this.diameter / 2;
return this.height * Math.PI * radius * radius;
};
var cyl = new Cylinder(7,4)
console.log(`volume = ${cyl.Volume().toFixed(4)}`)
//Solution 2:
function CylinderVolume(radius, height) {
this.radius = radius;
this.height = height;
this.volume = function() {
var vol = Math.PI * this.radius * this.radius * this.height;
return vol.toFixed(4);
};
}
var aCylinder = new CylinderVolume(5, 10);
console.log(aCylinder.volume() + " cubic centimeters.");
/* -------------------------- 6 ------------------------- */
//6. Write a bubble sort algorithm in JavaScript. Note : Bubble sort is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted,
//Solution 1:
function sortArray(arr) {
let temp = 0;
let x = 0;
let flag = false;
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length - i - 1; j++) {
if (arr[j + 1] < arr[j]) {
let temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return arr
}
console.log(sortArray([6, 4, 0, 3, -2, 1]));
//Solution 2:
function ex(arr){
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr.length-1; j++) {
if(arr[j] > arr[j+1]){
var temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
return arr;
}
console.log(ex([6,4,0, 3,-2,1]))
//Solution 3:
function bubble(arr){
let chek = true;
while(chek){
chek = false;
for(let i = 0; i < arr.length; i++) {
if (arr[i] > arr[i+1]){
chek = true;
[arr[i],arr[i+1]] = [arr[i+1],arr[i]];
}
}
}
return arr;
}
console.log( bubble([6,4,0, 3,-2,1]) ); // [ -2, 0, 1, 3, 4, 6 ]
//Solution 4:
Array.prototype.bubbleSort = function () {
let isSorted = false;
while (!isSorted) {
isSorted = true;
for (let i = 0; i < this.length - 1; i++) {
if (this[i] > this[i + 1]) {
let temp = this[i + 1];
this[n + 1] = this[n];
this[n] = temp;
isSorted = false;
}
}
}
return this
};
//Solution 5:
Array.prototype.bubbleSort_algo = function()
{
var is_sorted = false;
while (!is_sorted)
{
is_sorted = true;
for (var n = 0; n < this.length - 1; n++)
{
if (this[n] > this[n+1]){
var x = this[n+1];
this[n+1] = this[n];
this[n] = x;
is_sorted = false;
}
}
}
return this;
};
console.log([6,4,0, 3,-2,1].bubbleSort_algo());
/* -------------------------- 7 ------------------------- */
//7. Write a JavaScript program that returns a subset of a string.
//Solution 1:
function subString(str) {
const subStrings = [];
for (let i = 0; i < str.length; i++) {
for (let j = i + 1; j < str.length+1; j++) {
subStrings.push(str.slice(i, j));
}
}
return subStrings;
}
console.log(subString('dog'));
//Solution 2:
String.prototype.sub_String = function () {
var subset = [];
for (var m = 0; m < this.length; m++) {
for (var n = m + 1; n < this.length + 1; n++) {
subset.push(this.slice(m, n));
}
}
return subset;
};
console.log('dog'.sub_String());
/* -------------------------- 8 ------------------------- */
8. Write a JavaScript program to create a clock. Note: The output will come every second.
//Solution 1:
function my_Clock() {
this.cur_date = new Date();
this.hours = this.cur_date.getHours();
this.minutes = this.cur_date.getMinutes();
this.seconds = this.cur_date.getSeconds();
}
my_Clock.prototype.run = function () {
setInterval(this.update.bind(this), 1000);
};
my_Clock.prototype.update = function () {
this.updateTime(1);
console.log(this.hours + ':' + this.minutes + ':' + this.seconds);
};
my_Clock.prototype.updateTime = function (secs) {
this.seconds += secs;
if (this.seconds >= 60) {
this.minutes++;
this.seconds = 0;
}
if (this.minutes >= 60) {
this.hours++;
this.minutes = 0;
}
if (this.hours >= 24) {
this.hours = 0;
}
};
var clock = new my_Clock();
clock.run();
//Solution 2:
/* -------------------------- 9 ------------------------- */
//9. Write a JavaScript program to calculate circle area and perimeter. Note : Create two methods to calculate the area and perimeter. The radius of the circle will be supplied by the user.
//Solution 1:
function Cylinder(height, diameter) {
this.height = height;
this.diameter = diameter;
}
Cylinder.prototype.area = function () {
let radius = this.diameter / 2;
return 2 * Math.PI * radius;
};
Cylinder.prototype.perimeter = function () {
let radius =this.diameter /2
return Math.PI * radius * radius
}
var cyl = new Cylinder(7,4)
console.log(`area = ${cyl.area().toFixed(4)}`)
console.log(`perimeter = ${cyl.perimeter().toFixed(4)}`)
//Solution 2:
function cylinderAreaPerimeter(radius) {
this.radius = radius;
this.area = function() {
return (Math.PI * this.radius * this.radius).toFixed(4);
};
this.perimeter = function(){
return (2 * Math.PI * this.radius).toFixed(4);
}
}
var aCylinder = new cylinderAreaPerimeter(5, 10);
console.log(aCylinder)
console.log(aCylinder.perimeter())
console.log(aCylinder.area())
/* -------------------------- 10 ------------------------- */
///10. Write a JavaScript program to sort an array of JavaScript objects.
//Solution 1:
const library = [
{
title: 'The Road Ahead',
author: 'Bill Gates',
libraryID: 1254,
},
{
title: 'Walter Isaacson',
author: 'Steve Jobs',
libraryID: 4264,
},
{
title: 'Mockingjay: The Final Book of The Hunger Games',
author: 'Suzanne Collins',
libraryID: 3245,
},
];
function sortObject(array, propertyName) {
return array.sort((obj1, obj2) => {
return obj1[propertyName].localeCompare(obj2[propertyName]);
});
}
console.log(sortObject(library, 'title'));
//Solution 2:
var sort_by = function (field_name, reverse, initial) {
var key = initial
? function (x) {
return initial(x[field_name]);
}
: function (x) {
return x[field_name];
};
reverse = !reverse ? 1 : -1;
return function (x, y) {
return (x = key(x)), (y = key(y)), reverse * ((x > y) - (y > x));
};
};
var newobj = library.sort(sort_by('libraryID', true, parseInt));
console.log(newobj);
/* -------------------------- 11 ------------------------- */
//11. Write a JavaScript function to print all the methods in a JavaScript object.
//Solution 1:
function findAllMethods(obj) {
return Object.getOwnPropertyNames(obj).filter(function (property) {
return typeof obj[property] == "function";
});
}
console.log(findAllMethods(Math));
console.log(findAllMethods(Array));
/* -------------------------- 12 ------------------------- */
//12. Write a JavaScript function to parse an URL.
//Solution 1:
function parse_URL(url) {
var a = document.createElement("a");
a.href = url;
return {
source: url,
protocol: a.protocol.replace(":", ""),
host: a.hostname,
port: a.port,
query: a.search,
params: (function () {
var ret = {},
seg = a.search.replace(/^\?/, "").split("&"),
len = seg.length,
i = 0,
s;
for (; i < len; i++) {
if (!seg[i]) {
continue;
}
s = seg[i].split("=");
ret[s[0]] = s[1];
}
return ret;
})(),
file: (a.pathname.match(/\/([^\/?#]+)$/i) || [, ""])[1],
hash: a.hash.replace("#", ""),
path: a.pathname.replace(/^([^\/])/, "/$1"),
relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [, ""])[1],
segments: a.pathname.replace(/^\//, "").split("/"),
};
}
console.log(
parse_URL("https://github.com/pubnub/python/search?utf8=%E2%9C%93&q=python")
);
/* -------------------------- 13 ------------------------- */
//13. Write a JavaScript function to retrieve all the names of an object's own and inherited properties.
//Solution 1:
function isObject(obj) {
const type = typeof obj;
return type === "function" || (type === "object" && !!obj);
}
//Solution 2:
function allKeys(obj) {
if (!isObject(obj)) return [];
const keys = [];
for (let key in obj) keys.push(key);
return keys;
}
function isObject(obj) {
let type = typeof obj;
return type === "function" || (type === "object" && !!obj);
}
function Student(name) {
this.name = name;
}
Student.prototype.rollno = true;
console.log(allKeys(new Student("Sara")));
/* -------------------------- 14 ------------------------- */
//14. Write a JavaScript function to retrieve all the values of an object's properties.
//Solution 1:
const person = { name: "John", age: 30, city: "New York" };
function retrieveValues(obj) {
return Object.values(obj).map((value) => value);
}
console.log(retrieveValues(person));
//Solution 2:
function all_values(obj) {
var keys = _keys(obj);
var length = keys.length;
var values = Array(length);
for (var i = 0; i < length; i++) {
values[i] = obj[keys[i]];
}
return values;
}
function _keys(obj)
{
if (!isObject(obj)) return [];
if (Object.keys) return Object.keys(obj);
var keys = [];
for (var key in obj) if (_.has(obj, key)) keys.push(key);
return keys;
}
function isObject(obj)
{
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
}
console.log(all_values({red: "#FF0000", green: "#00FF00", white: "#FFFFFF"}));
/* -------------------------- 15 ------------------------- */
//15. Write a JavaScript function to convert an object into a list of `[key, value]` pairs.
//Solution 1:
const person = { name: "John", age: 30, city: "New York" };
function keyValuePairs(obj) {
return Object.entries(obj).map(([key, value]) => {
return [key, value];
});
}
console.log(keyValuePairs(person));
//Solution 2:
function keyValuePair(obj) {
let key = Object.getOwnPropertyNames(obj);
let value = Object.values(obj);
let temp = [];
for (let i = 0; i < Object.values(obj).length; i++) {
temp[i] = [key[i],value[i]];
}
console.log(temp);
}
var obj = {
name : 'rahul',
roll : 11,
age : 30
};
keyValuePair(obj);
/* -------------------------- 16 ------------------------- */
//16. Write a JavaScript function to get a copy of the object where the keys become the values and the values are the keys.
//Solution 1:
const person = { name: "John", age: 30, city: "New York" };
function keyValuePairs(obj) {
let temp = {};
Object.keys(obj).map((key) => {
temp[obj[key]] = key;
});
return temp;
}
console.log(keyValuePairs(person));
//Solution 2:
const invert_key_value = (obj) =>
Object.entries(obj).reduce((acc, cur) => {
acc[cur[1]] = cur[0];
return acc;
}, {});
console.log(keyValuePairs(person));
//Solution 3:
var test = {
a: 10,
b: 11,
c: 12,
d: 13,
e: 14,
f: -1,
};
var numbers = {
zero: 0,
one: 1,
two: 2,
three: 3,
four: 4,
};
function reverseKeyValue(obj) {
let temp = {};
for (let key in obj) {
if (isObject(obj)) temp[obj[key]] = key;
}
return temp;
}
function isObject(obj) {
let type = typeof obj;
return type === "function" || (type === "object" && !!obj);
}
console.log(reverseKeyValue(test));
console.log(reverseKeyValue(numbers));
/* -------------------------- 17 ------------------------- */
//17. Write a JavaScript function to check whether an object contains a given property.
//Solution 1:
var test = {
a: 10,
b: 11,
c: 12,
d: 13,
e: 14,
f: -1,
};
function checkPropertyName(obj, propertyName) {
for (const key in obj) {
if (key === propertyName) {
return true;
}
}
return false;
}
console.log(checkPropertyName(test, "a"));
//Solution 2:
function checkPropertyName(obj, propertyName) {
let found = false;
Object.keys(obj).forEach((key) => {
if (key === propertyName) {
found = true;
}
});
return found;
}
console.log(checkPropertyName(test, "w"));
/* -------------------------- 18 ------------------------- */
//18. Write a JavaScript function to check whether a given value is a DOM element.
//Solution 1:
function isDOMElement(obj) {
return !!(obj && obj.nodeType === 1);
}
console.log(isDOMElement(jQuery('body')[0]));