Repository navigation
Expand file tree
/
Copy pathJavaScript_Fundamentals_Objects.js
More file actions
72 lines (60 loc) · 1.78 KB
/
Copy pathJavaScript_Fundamentals_Objects.js
File metadata and controls
72 lines (60 loc) · 1.78 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
//Task 1 Hello, Object
// Write the code, one line for each action:
// Create an empty object user.
let user = {};
// Add the property name with the value John.
user.name = "John";
// Add the property surname with the value Smith.
user.surname = "Smith";
// Change the value of the name to Pete.
user.name = "Pete";
// Remove the property name from the object.
delete user.name;
//Task 2 Check For Emptiness
//Write the function isEmpty(obj) which returns true if the object has no properties, false otherwise.
function isEmpty(obj) {
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
return false;
}
}
return true;
}
//Task 3 Sum Object Properties
//We have an object storing salaries of our team:
let salaries = {
John: 100,
Ann: 160,
Pete: 130
};
//Write the code to sum all salaries and store in the variable sum. Should be 390 in the example above.
// If salaries is empty, then the result must be 0.
let sum = 0;
for (let key in salaries) {
sum += salaries[key];
}
alert(sum);
//Task 4 Multiply Numeric Property Values By 2
// Create a function multiplyNumeric(obj) that multiplies all numeric property values of obj by 2.
//beforre the call
let menu = {
width: 200,
height: 300,
title: "My menu"
};
multiplyNumeric(menu);
//after the call
menu = {
width: 400,
height: 600,
title: "My menu"
};
//Please note that multiplyNumeric does not need to return anything. It should modify the object in-place.
// P.S. Use typeof to check for a number here.
function multiplyNumeric(obj) {
for (let key in obj) {
if (typeof obj[key] === 'number') {
obj[key] *= 2;
}
}
}