-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavascript Fundamentals: Objects
84 lines (67 loc) · 1.83 KB
/
Javascript Fundamentals: Objects
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
//Exercise 1
//Write the code, one line for each action:
//Create an empty object user.
//Add the property name with the value John.
//Add the property surname with the value Smith.
//Change the value of the name to Pete.
//Remove the property name from the object.
let user = {};
user.name = "John";
user.surname = "Smith";
user.name = "Pete"
delete user.name;
//Exercise 2
//Write the function isEmpty(obj) which returns true if the object has no properties, false otherwise.
//Should work like that:
function isEmpty(obj){
for (let keys in obj){
return false;
} return true;
}
let schedule = {};
//console.log(isEmpty(schedule)); // true
//console.log(isEmpty({"get up": 8})); // false
//console.log(isEmpty({})); // true
//Exercise 3: Sum object properties
//We have an object storing salaries of our team:
let salaries = {
John: 100,
Ann: 160,
Pete: 130
}
function salariesSum (obj){
let variableSum = 0;
for (let keys in obj){
variableSum = variableSum + obj[keys]
}
return variableSum;
}
//Write the code to sum all salaries and store in the variable sum. Should be 390 in the example above.
//let sum = salariesSum(salaries);
//console.log(sum); // 390
//let emptySalaries = {};
//console.log(salariesSum(emptySalaries)); // 0
//If salaries is empty, then the result must be 0.
//Exercise 4: Multiply numeric property values by 2
//Create a function multiplyNumeric(obj) that multiplies all numeric property values of obj by 2.
//For instance:
// before the call
let menu = {
width: 200,
height: 300,
title: "My menu"
};
function multiplyNumeric(obj){
for (let keys in obj){
if (typeof obj[keys] === 'number'){
obj[keys] = obj[keys] * 2;
}
} console.log(menu)
}
//console.log(multiplyNumeric(menu));
// after the call
/*menu = {
width: 400,
height: 600,
title: "My menu"
};*/