forked from DevMountain/javascript-2-afternoon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
practice.js
155 lines (101 loc) · 4.01 KB
/
practice.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//Once you complete a problem, open up Chrome and check the answer in the console.
//Create an object called me. Give it a key of name with the value being your name, and another key of age with the value being your age. Then alert your name using dot notation.
var me = {
name: 'Stephen',
age: 46
};
//alert(me.name);
//NEXT PROBLEM
//Make a 'favoriteThings' object that contains the following keys: band, food, person, book, movie, holiday. Have the values to those keys be your favorite thing in that category.
var favoriteThings = {
band: 'Radiohead',
food: 'pizza',
person: 'Sarah Hess',
book: 'Crime and Punishment',
movie: '12 Angry Men',
holiday: 'The next one'
};
//console.log(favoriteThings);
//After you've made your object, add another key named 'car' with the value being your favorite car and then another key named 'brand' with the value being your favorite brand.
favoriteThings.car = 'Toyota'
favoriteThings.brand = 'Samsung'
//Now change the value of the food key in your favoriteThings object to be 'Chicken Nuggets' and change the value of the book key in your favoriteThings object to be 'Harry Potter'.
favoriteThings.food = 'Chicken Nuggets'
favoriteThings.book = 'Harry Potter'
//NEXT PROBLEM
/*
Create an empty Object called backPack. Now, create a variable called 'item'
and set it equal to the string 'firstPocket'. Using bracket notation,
add a 'firstPocket' key (or property) to backPack, using 'item'.
Set the value of that key to 'chapstick'.
Using dot notation, add another key (or property) to your backPack object
that is named color, with the value being the color of your backpack.
*/
var backPack = {};
var item = 'firstPocket';
backPack[item] = 'chapstick';
backPack.color = 'red';
//After you do the above, alert your entire backPack object.
// alert(backPack);
/*
You probably noticed that it just alerted [object Object].
Alerting to see the data in your Object doesn't work so well.
Instead, console.log your whole backPack object and then check out the console.
*/
// console.log(backPack);
//NEXT PROBLEM
var user2 = {
name: 'Ty',
age: 24,
pwHash: 'U+Ldlngx2BYQk',
email: 'ty33@gmail.com',
birthday: '05/02/1990',
username: 'tylermcginnis33'
};
//Let's say I, the user, decided to change my name and email address to the following
// name -> 'Tyler S. McGinnis', email -> 'tyler.mcginnis@devmounta.in'. Make that change.
user2.name = 'Tyler S. McGinnis';
user2.email = 'tyler.mcginnis@devmounta.in';
// =============================================
// =============================================
// EXTRA PRACTICE PROBLEMS BELOW
// =============================================
// =============================================
//NEXT PROBLEM
//Create an empty object called methodCollection.
var methodCollection = {};
/*
Now add two methods (functions that are properties on objects) to your methodCollection
object. One called 'alertHello' which alerts 'hello' and another method called logHello
which logs 'hello' to the console.
*/
methodCollection.alertHello = function() {
alert('hello');
}
methodCollection.logHello = function() {
console.log('hello');
}
//Now call your alertHello and logHello methods.
// methodCollection.alertHello();
// methodCollection.logHello();
//NEXT PROBLEM
// Create a function called makePerson which takes in name, birthday, ssn as its
// parameters and returns a new object with all of the information that you passed in.
function makePerson(name, birthday, ssn) {
return {
name: name,
birthday: birthday,
ssn: ssn
}
}
myPerson(Stephen, April, 234-23-2342);
//NEXT PROBLEM
// Create a function called makeCard which takes in cardNumber, expirationDate, and securityCode to make a Credit Card object and returns that object so that whenever you invoke makeCard, you get a brand new credit card.
function makeCard(cardNumber, expirationDate, securityCode) {
return {
cardNumber: cardNumber,
expirationDate: expirationDate,
securityCode: securityCode
}
}
makeCard()