-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4-tasks-js-objects.js
More file actions
66 lines (52 loc) · 1.3 KB
/
Copy path4-tasks-js-objects.js
File metadata and controls
66 lines (52 loc) · 1.3 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
/*Hello, object
importance: 5
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 = {
name: 'John',
surname: 'Smith',
}
user.name = 'Pete';
delete user.name;
/*Write the function isEmpty(obj) which returns true if the object has no properties, false otherwise. */
function isEmpty(obj) {
for(let key in obj) {
return false
}
return true
}
/*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.*/
function sumSalaries(obj) {
let sum = 0
for(let key in obj) {
sum = sum + salaries[key]
}
alert(sum)
}
sumSalaries(salaries)
/* Create a function multiplyNumeric(obj) that multiplies all numeric property values of obj by 2 */
let menu = {
width: 200,
height: 300,
title: "My menu"
};
function multiplyNumeric(obj) {
for(let key in obj) {
if(typeof obj[key] == 'number') {
obj[key] = obj[key] * 2
}
}
}