-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathexercises.js
221 lines (158 loc) · 9.67 KB
/
exercises.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
Objects in Javascript can be compared to objects in real life. Objects has a collection of properties associated with it (known as key-value pairs) and are basically the same as ordinary variables.
*/
//A literal object.
console.log('test')
/*
1. Declare a variable named `books` and create the following properties (key-value pairs to the object:
- add a key named `title` and a string value of "The Obstacle is the Way"
- add a key named `author` and a string value of "Ryan Holiday"
- add a key named `category` and string value of "Self Help"
- add a key named `pages` and number value of 200
Console.log the object.
*/
/*
2. Declare a variable named `dog` and create the following properties (key-value pairs) to the object:
- add a key named `name` and set it to a string value of your choice
- add a key named `age` and set it to a number value of your choice
- add a key named `vegeterian` and set it to a boolean value of your choice
- add a key named `color` and set it to an array with two string elements in it
- add a key named `speak` which will be a method that return a string "bark!";
Console.log the following phrase by accessing the appropriate object properties:
"My dog `name` is `age` year old and likes to `speak` at strangers."
*/
//An empty object
/*
3. Declare a variable named `kicks` and assign it to an EMPTY object.
Next you will add the following properties to this object. Each property declaration expression will be on its own line and
ending with semicolons (`;`).
- add a property named `brand` and set it to your favorite sneaker brand
- add a property named `color` and set it the value "cornflower blue"
- add a property named `size` and set it to your shoe size
- add a property named `price` and set it to a number value between 50 to 100
- add a property named `buy` and set it to a method that will return a string "Now I is a cool kid!"
Console.log the object.
*/
/*
4. Declare a variable named `plainBox` and set its value to be an EMPTY object.
Next, you will add three properties to this object.
- add a property named `color` and set it the name of any color.
- add a property named `size` and set it to a number value between `0` and `20`.
- add a property named `contents` and set it to be an empty array.
console.log the object
*/
/*
5. Declare a variable named `stockCar` and create the following properties (key-value pairs) to the object:
- add a key named `model` and set its value to the name of an American car manufacturer.
- add a key named `year` and set it to be a value above 2000 but below 2016.
- add a key named `automaticTransmission` and give it a Boolean value of your choice.
- add a key named `driver` and set it to be `null`.
- add a key named `passengers` and set it to be an empty array.
console.log the object
*/
/*
6. Add new property inside a function
Declare a variable named `plainPerson`. Set its value to be an EMPTY object with no properties.
Next, declare a function named `buildPerson` and then define three parameters. The first parameter will be named `person`
and the second will be `nameString` and the third will be `age`. Within this function you will modify the `person` object
by:
- adding a property called `name` and set its value to be the second parameter.
- adding a property called `age` and set its value to be the third parameter.
The function will return the first parameter.
Finally, invoke your new function while passing in the `plainPerson` object and additional agrument values, then store
the return value in a variable named `completePerson`. Use `console.log` three times to print the entire object, just
the value at `name`, and just the value at `age`.
*/
/*
7. Display values of objects that are inside an array
Declare a new variable named `arrayOfObjects` and set it to be [this array of objects
(https://gist.github.com/sgnl/958adf99007329d2e4ff).
Declare a new function named `printOrders`and set one parameter called `orders`. Within this function you
will return a string that prints the order details to the page in the following format.
Example output:
=====
id: 0
purchase date: Monday Jan 25 2015 2:01 PM
purchase total: 279.38
=====
id: 1
purchase date: Monday Jan 27 2015 11:31 AM
purchase total: 79.80
=====
id: 2
purchase date: Monday Feb 1 2015 7:56 AM
purchase total: 15.62
=====
...
*/
/*
8. Addition with an object
Declare a new variable named sumObj and set it to be a new object with the properties `a`, `b`, and `result`.
Properties 'a' and 'b' will be set to a number and result will be 'undefined.'
Declare a new function named objectAddition. Declare the first parameter, which will be an object. Within this
function, you will access the values stored `a` and `b` of the object being passed in. Add the these values and store
the sum to this object's `result` property. This function will return the object.
Invoke your function and pass in your object, store the result to a variable named sumObjResult and use `console.log`
to inspect your results.
*/
/*
9. Print sum function and add as new key-value
Declare a new function named printObj and a single parameter which will be the object from the challenge just above.
Within this function you are to print to the screen a message of the operation performed. For Example:
// if this object was passed into your function:
{ a: 10, b:67, result: 77 }
// it should print a message saying
'10 + 67 = 77'
Before returning this object, add a new property to it named `output` and set it to be the message that was printed
out.
Invoke this function and pass in your object. Further test by changing the values of the object being passed in or
**create more** objects and invoke your function multiple times.
*/
/*
10. Putting stuff in `plainBox`
Declare a function named putInPlainBox and a single parameter which will be an object. Within this function, write a
FOR loop that adds **10** random number values to the array referenced at the `contents` property of the object being
passed in. This function will return the object.
Invoke your function and pass in your object (which should be `plainBox`), store the result to a variable named
plainBoxResult and use `console.log` to inspect your results.
*/
/*
11. Detecting transmission
Declare a function named detectingTransmission and a single parameter which will be an object. Within this function
you will check to see if the car has an automatic or manual transmission and print the results on screen.
If `automaticTransmission` is true then print a message saying so. Also, provide an appropriate message for when the
its false.
Invoke your function and pass in your stockCar object, store the result to a variable named isAutomaticTransmission and use `console.log` to inspect your results.
*/
/*
12. Who's driving this thing?!
As you may have noticed that the `stockCar` doesn't have a driver!
Declare a function named addDriver with two parameters. The first parameter will be an object with represents a
**car**, the other will be a **person**. Within this function, set the `driver` value of the **stockCar** to the
second parameter being passed into your function.
Invoke your function and pass in your objects, store the result to a variable named stockCarWithDriver, and inspect
your results. Consider using `plainPerson` as your driver.
*/
/*
#Final Boss
The Dev League instructors want to ride your whip!
Declare a variable named `passengerList` and set it to be `['Jon', 'Jason', 'Tony', 'Joe', 'Jesse', 'Nigel', 'Kelli', 'Marifel', 'Victor']`
Declare a variable named `passengerAges` and set it to be `[19, 12, 21, 22, 16, 9, 19, 20, 15]`
Declare a function named `addPassengers` with three parameters. The first will be a **car** and the second will be an array of **names** and the third will be an array of **ages**. The names and ages are in sequence, e.g. "Jon" is "19", "Jason" is "12".
In the end you will return the **car** but within the function...
You will have to populate the `passengers` array on the **car** object with proper objects that represent a person. Currently you have two arrays, one which contains names and another which contains ages.
You should iterate through the names and ages, pass the values to your `buildPerson` function to build person objects (remember that this function returns a new object). Don't forget that this function actually takes **three** arguments, how will you handle that? (you should not have to change your function).
Example of a loaded Car:
# Display passengers
Delcare a function named `displayPassengers` and set one parameter which will be a **car**. This function should print out each passenger's name and age one line at a time.
example output:
'Jon, age 19, is riding dirty!'
'Jason, age 12, is riding dirty!'
'Tony, age 21, is riding dirty!'
'Joe, age 22, is riding dirty!'
'Jesse, age 16, is riding dirty!'
'Nigel, age 9, is riding dirty!'
'Kelli, age 19, is riding dirty!'
'Marifel, age 19, is riding dirty!'
'Victor, age 19, is riding dirty!'
*/