-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathsolutions.js
136 lines (103 loc) · 3.03 KB
/
solutions.js
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
// A for-in loop is used to iterate through the properties (key - value pairs) of an object.
// Here is the syntax:
/*for(var in object){
//your logic
}*/
//Here is example of using a for-in loop to iterate through properties of the following object:
var prepClass = {
month: "February",
year: 2018,
students: 8,
roster: ["Cyrus", "Tyler", "May", "Yao", "Ben", "Reese", "Jace", "Sean"],
level: "advance",
areTheyCool: true
};
for(var prop in prepClass){ //loop through all the properties in the object
console.log(prop); //prints the keys in the object
console.log(prepClass[prop]); //prints the values in the object
}
/*1. Create a function that will take in an object and loop through the properties of that object.
Console.log the keys of the object.*/
/*@param {Object}
@return {Object}*/
var donutBox = {
store: "Safeway",
location: "Manoa",
quantity: 12,
price: 5,
types: ["old fashion", "glazed", "chocolate", "jelly-filled", "sprinkles", "red velvet"],
taste: "fabulous"
};
function getDonutsKeys(obj){
for(var prop in obj){
console.log(prop);
}
return obj;
}
getDonutsKeys(donutBox);
/*2. Create a function that will take in an object and loop through the properties of that object.
Console.log the values of the object. Use the donutBox object from the previous exercise above*/
/*@param {Object}
@return {Object}*/
function getDonutValues(obj){
for(var prop in obj){
console.log(obj[prop]);
}
return obj;
}
getDonutValues(donutBox);
/*3. Create a function that will take in an object and will delete the taste property from the donutBox object above.*/
/*@param {Object}
@return {Object}*/
function removeProp(obj){
delete obj.taste;
return obj;
}
console.log(removeProp(donutBox));
/*4. Create a function that will take in an object and will return all the values of the object in an array.*/
/*@param {Object}
@return {Array}*/
var legend = {
firstName: "Bruce",
lastName: "Lee",
birthPlace: "San Francisco, CA",
occupation: "Bad Ass",
hobbies: ["martial arts", "fitness", "dancing", "knitting", "coding"]
};
function convertToArray(obj){
var arr = [];
for(var prop in obj){
arr.push(obj[prop]);
}
return arr;
}
console.log(convertToArray(legend));
/*5. Create a function that will take in an object and will return the number of properties (key -value pairs) in the object. Use the legend object above.
Hint: You'll need to use hasOwnProperty method
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
*/
/*@param {Object}
@return {Number}*/
function keySize(obj){
var numKeys = 0;
for(var key in obj){
if(obj.hasOwnProperty(key)){
numKeys++;
}
}
return numKeys;
}
console.log(keySize(legend));
/*6. Create a function that will take in an object and check to see if the legend object has a property of 'occupation'.*/
/*@param {Object}
@return boolean*/
function checkProp(obj){
for(var prop in obj){
if(obj.hasOwnProperty('occupation')){
return true;
}else{
return false;
}
}
}
console.log(checkProp(legend));