forked from HackYourFuture/JavaScript1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMore JavaScript.js
More file actions
225 lines (160 loc) · 6.43 KB
/
More JavaScript.js
File metadata and controls
225 lines (160 loc) · 6.43 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
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
222
223
224
225
/**************************** More JavaScript ***************************** */
// 1. Create a function that takes 3 arguments and returns the sum of the these arguments.
function sum(a,b,c) {
return a + b + c
};
console.log(sum(1,2,3)); // 6
// 2. Create a function named colorCar that receives a color, and prints out, 'a red car' for example.
function colorCar(color) {
return "a " + color +" car"
}
console.log(colorCar('red')); // a red car
// 3. Create an object and a function that takes the object as a parameter and prints out all of its properties and values.
const bestDriver = {
firstName:"Mika",
lastName:"Hakkinen",
teamName:"McLaren",
age:50};
function printObj(obj) {
for (const key in obj) {
console.log(`His ${key} : ${obj[key]}`)
}
}
printObj(bestDriver);
// 4. Create a function named vehicleType that receives a color, and a code, 1 for car, 2 for motorbike.
// And prints 'a blue motorbike' for example when called as vehicleType("blue", 2)
let vehicleCategory = {
1 : "car",
2 : "motorbike",
};
function vehicleType(color, category) {
if (category <= 2) {
console.log('a ' + color + ' ' + vehicleCategory[category]);
} else {
console.log('Please enter valid');
}
}
vehicleType('blue', 2);
vehicleType('green', 3);
// I could have set the function vehicleType also with array like this.
/* let vehicleCategory = ['car', 'motorbike'];
function vehicleType(color, category) {
if (category <= 2) {
category = vehicleCategory[1];
console.log('a ' + color + ' ' + category);
} else {
console.log('Please enter valid number');
}
}
// 5. Can you write the following without the if statement, but with just as a single line with console.log(...);?
/* Yes, we can use ternary operator to achieve it. */
console.log(3 === 3 ? "yes" : "no");
// 6. Create a function called vehicle, like before, but takes another parameter called age,
//so that vehicle("blue", 1, 5) prints 'a blue used car'
function vehicle(color, category, age) {
if (category > 2) {
console.log('Please enter valid number for category of vehicle');
} else {
if (age >= 1) {
age = 'used';
} else {
age = 'new';
}
console.log('a ' + color + ' ' + age + ' ' + vehicleCategory[category]);
}
}
vehicle('blue', 1, 5);
vehicle('black', 4, 5);
vehicle('blue', 1, 5);
// 7. Make a list of vehicles, you can add "motorbike", "caravan", "bike", or more.
let listOfVehicle = ["motorbike", "caravan", "truck", "bike", "jeep"];
// 8. How do you get the third element from that list?
console.log(listOfVehicle[2]);
// 9. Change the function vehicle to use the list of question 7. So that vehicle("green", 3, 1) prints "a green new bike".
function newVehicle(color, category, age) {
if (category > listofVehicle.length) {
console.log('Please enter valid number for category of vehicle');
} else {
if (age > 1) {
age = 'used';
} else {
age = 'new';
}
console.log('a ' + color + ' ' + age + ' ' + listOfVehicle[category]);
}
}
newVehicle('green', 3, 1);
newVehicle('green', 6, 1);
// 10. Use the list of vehicles to write an advertisement. So that it prints something like: "Amazing Joe's Garage, we service cars, motorbikes, caravans and bikes.". (Hint: use a for loop.)
listOfVehicle = ["car", "motorbike", "caravan", "bike"];
let nameOfVehicle = "";
for (i = 1; i < listOfVehicle.length; i++) {
if (i < listOfVehicle.length - 1) {
nameOfVehicle += ', ' + listOfVehicle[i] + 's';
} else if (i < listOfVehicle.length - 2) {
nameOfVehicle += ', ' + listOfVehicle[i] + 's ';
} else {
nameOfVehicle += ' and ' + listOfVehicle[i] + 's.';
}
}
console.log("Amazing Joe's Garage, we service " + listOfVehicle[0] + 's' + nameOfVehicle);
// 11. What if you add one more vehicle to the list, can you have that added to the advertisement without changing the code for question 10?
listOfVehicle.push('jeep');
nameOfVehicle = '';
for (i = 1; i < listOfVehicle.length; i++) {
if (i < listOfVehicle.length - 1) {
nameOfVehicle += ', ' + listOfVehicle[i] + 's';
} else if (i < listOfVehicle.length - 2) {
nameOfVehicle += ', ' + listOfVehicle[i] + 's ';
} else {
nameOfVehicle += ' and ' + listOfVehicle[i] + 's.';
}
}
console.log("Amazing Joe's Garage, we service " + listOfVehicle[0] + 's' + nameOfVehicle);
// 12. Create an empty object.
let emptyObject = {};
// 13. Create an object that contains the teachers that you have had so far for the different modules.
// 14. Add a property to the object you just created that contains the languages that they have taught you.
const teachers = {
teacher1: { name: 'Philipp' },
teacher2: { name: 'Rob' },
teacher3: { name: 'Unmesh & Bonan' },
teacher4: { name: 'Yash' },
};
teachers.teacher1.language = ['HTML'];
teachers.teacher2.language = ['CSS'];
teachers.teacher3.language = ['Git'];
teachers.teacher4.language = ['JavaScript1'];
console.log(teachers);
// 15. Write some code to test two arrays for equality using == and ===. Test the following:
let x = [1, 2, 3];
let y = [1, 2, 3];
let z = y;
/*as we discussed the issue in the previous hw and lecture, they are objects and to ensure equlatiy,
it needs to reference to the same memory locations */
x == y // false --> because of the different references.
x === y // false --> because of the different references.
z == y // true --> they reference to same memory locations.
z == x // false --> there is no correlation.
// 16.
const o1 = { foo: "bar" };
const o2 = { foo: "bar" };
const o3 = o2;
// Show that changing o2 changes o3 (or not) and changing o1 changes o3(or not).
/* Changing o2 changes o3 since o3 is referenced to value of o2.*/
o2.foo = 'bar1';
console.log(o2); // {foo: "bar1"}
console.log(o3); // {foo: "bar1"}
/*But changing o1 doesn`t change o3 since they are not correlated.*/
o1.foo = 'o1bar';
console.log(o1); // {foo:"o1bar"}
console.log(o3); // {foo:"bar1"}
// Does the order that you assign (o3 = o2 or o2 = o3) matter?
/* It does matter since o2 is a constant and cannot be reassigned. */
// 17. What does the following code return? (And why?)
let bar = 42;
// typeof typeof bar;
/* The result will be a string because typeof bar is a "number"
and typeof "number" is string.*/
console.log(typeof bar); // "number"
console.log(typeof "number"); // string