-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
374 lines (288 loc) · 10.3 KB
/
script.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
//Function constructor
/* var john = {
name: 'John',
yearOfBirth: 1990,
job: 'teacher'
};
var Person = function (name, yearOfBirth, job){
this.name = name;
this.yearOfBirth = yearOfBirth;
this.job = job;
/*this.calculateAge = function(){
console.log(2018-
this.yearOfBirth);
}
}
//Usando o prototype inherit eu posso retirar o this.calculateAge da var Person
Person.prototype.calculateAge = function (){
console.log(2018-
this.yearOfBirth);
};
//function constructors are always with the capital letter at begining (Person for exemple)
//new operator a brand new object is created - objeto novo eh criado com o new
//e depois chama a função Person para preencher tal objeto - com as variaveis apontando para o new
var john = new Person('John', 1990, 'teacher');
var jane = new Person('Jane', 1969, 'designer');
var mark = new Person('Mark', 1948, 'retired');
john.calculateAge();
jane.calculateAge();
mark.calculateAge();
*/
//Object.create
//First define object that will act as the prototype
//then we create a new object based on that prototype
//no capital letter because is not function constructor
/* var personProto = {
calculaAge: function(){
console.log(2018 - this.yearOfBirth)
}
};
var john = Object.create(personProto);
john.name = 'John';
john.yearOfBirth = 1990;
var jane = Object.create(personProto, {
name: {value: 'Jane'},
yearOfBirth: {value: 1969},
}); */
//Primitives vs objects
//variaveis contendo primitives seguram o conteudo dentro da variavel
//variaveis associadas a objetos contem referencia ao conteudo de onde o objeto eh salvo
//Primitives
/* var a = 23;
var b = a;
a = 46;
console.log(a); //a = 46
console.log(b); //b = 23 ter mudado a para 46 não afetou o valor da variavel b
//Objects
var obj1 = {
name: 'John',
age: 26
};
var obj2 = obj1;
obj1.age = 30;
console.log(obj1.age); //obj1 age = 30
console.log(obj2.age); //obj2 age = 30 */
/*
Functions - A function is an intance of the object type;
- A function behaves like any other object;
- We can store functions in a variable;
- We can pass a function as an argument to another function;
- We can return a function from a function.
*/
/*
var age = 27;
var obj = {
name: 'Jonas',
city: 'Lisbon'
};
function change (a, b){
a = 30;
b.city = 'San Francisco';
}
change(age, obj);
console.log(age); //age = 27 didint changed because its primitive pois contem um dado e cria uma copia do dado
console.log(obj.city); //obj.city = Sao Francisco changed because its object e contem uma referencia e nao um dado
*/
//Fazer uma função que determine a idade a partir dos anos de nascimento. Segue abaixo.
//Retornar o valor e dizer se é fullAge ou não
//Dizer o max heart rate de acordo com a idade
//First Class Functions Passing Functions as Arguments
/*
var years = [1990, 1988, 1976, 2918, 2005, 1911];
function arrayCalc (arr, fn) {
var arrRes = [];
for (var i=0; i < arr.length; i++){
arrRes.push(fn(arr[i]));
}
return arrRes;
}
function calculateAge (el) {
return 2018 - el;
}
function isFullAge (el){
return el>= 18;
}
function maxHeartRate (el){
if (el >= 18 && el <= 81){
return Math.round(206.9 - (0.67*el));
} else{
return -1;
}
}
var ages = arrayCalc(years, calculateAge); //store that function into variable
var fullAges = arrayCalc(ages, isFullAge);
var rates = arrayCalc(ages, maxHeartRate);
console.log(ages); // [28, 39, 42, -900, 13, 107]
console.log(fullAges);
console.log(rates);
*/
//Functions returning functions - diferent interview question for each dif. job
/*
function interviewQuestion(job){
if (job === 'teacher'){
return function(name){
console.log('What do you teach, '+name+'?');
}
} else if (job === 'lawyer'){
return function(name){
console.log('Can you describe the importance of the constitution for our country, '+name+'?');
}
} else {
return function(name){
console.log('What do you do, '+name+'?');
}
}
}
var teacherQuestion = interviewQuestion('teacher');
var lawyerQuestion = interviewQuestion('lawyer');
var homelessQuestion = interviewQuestion('homeless');
teacherQuestion('Mariana');
lawyerQuestion('Rick');
homelessQuestion('Vini');
interviewQuestion('teacher')('Mark'); // vai retornar a mesma coisa do que a var teacherQuestion
*/
//IIFE
/*
Serve para criar data privacy e impede que um dado dentro da function
esteja protegido do 'mundo externo'.
Um jogo no qual ganha quem tirar o numero maior ou igual a 5. O primeiro modo
eh sem a privacidade dos dados (da pra ver quanto o player tirou);
ja o segundo eh com a privacidade nos dados (nao da pra ver o numero que o player tirou).*/
/*
function game () {
var score = Math.random()*10;
console.log(score >=5);
}
game();//os dados nao estao protegidos
(function (){
var score = Math.random()*10;
console.log(score >=5);
})(); //coloca a função entre parentesis e executa com o () no final. dados protegidos
(function (goodLuck){
var score = Math.random()*10;
console.log(score >=5 - goodLuck);
})(5); //nesse caso eu crio um cheat que faz com que a pessoa sempre ganhe
*/
//Closures - possibilita que a inner function sempre tenha acesso ás variaveis e parametros da outer function, mesmo que a outer function tenha retornado (return).
/*
function retirement(retirementAge){
var a = ' a years left until retirement.';
return function(yearOfBirth) {
var age = 2018 - yearOfBirth;
console.log((retirementAge - age) + a);
}
} // nesse caso a function yearOfBirth continuou tendo acesso à var a e ao retirementAge
var retirementUS = retirement(66);
retirementUS(1990);
function interviewQuestion(job){
return function (name){
if (job === 'teacher'){
console.log('What do you teach, '+name+'?');
} else if (job === 'lawyer'){
console.log('Can you describe the importance of the constitution for our country, '+name+'?');
} else {
console.log('What do you do, '+name+'?');
}
}
};
interviewQuestion('teacher')('Kyal');
*/
//Bind, Call and Apply
/*Bind - copia uma function e deixa armazenar em uma variavel
Call - usa uma function que ja existe
Apply - usa uma function que ja existe mas tem q por array []*/
/*
var rick = {
name: 'Rick',
age: 29,
job: 'lawyer',
presentation: function(style, timeOfDay){
if (style === 'formal'){
console.log('Good '+ timeOfDay + ', Ladies and Gentlemen! I\'m a '+
this.job+' and my name is '
+this.name+ '.')
} else if (style === 'friendly'){
console.log('Hey, what\'s up? I\'m a '+
this.job+' and my name is '
+this.name+ '. '+ timeOfDay+'.')
}
}
}
var winnie = {
name: 'Winnie',
age: 31,
job: 'acupunturist',
};
rick.presentation.call(winnie, 'friendly', 'Morning'); //pega emprestado do rick e usa na winnie
rick.presentation.apply(winnie, ['formal', 'afternoon']); //msma coisa que anterior mas usa array
var rickFriendly =
rick.presentation.bind(rick, 'friendly');
rickFriendly('morning');
var winnieFriendly =
rick.presentation.bind(winnie, 'friendly');
winnieFriendly('morning');
var years = [1990, 1988, 1976, 2017, 2005, 1911];
function arrayCalc (arr, fn) {
var arrRes = [];
for (var i=0; i < arr.length; i++){
arrRes.push(fn(arr[i]));
}
return arrRes;
}
function calculateAge (el) {
return 2018 - el;
}
function isFullAge (limit, el){
return el>= limit;
}
var ages = arrayCalc(years, calculateAge);
var fullJapan = arrayCalc(ages,
isFullAge.bind(this, 20));
console.log(ages);
console.log(fullJapan);
*/
/////////////////////////////
// CODING CHALLENGE
/*
--- Let's build a fun quiz game in the console! ---
1. Build a function constructor called Question to describe a question. A question should include:
a) question itself
b) the answers from which the player can choose the correct one (choose an adequate data structure here, array, object, etc.)
c) correct answer (I would use a number for this)
2. Create a couple of questions using the constructor
3. Store them all inside an array
4. Select one random question and log it on the console, together with the possible answers (each question should have a number) (Hint: write a method for the Question objects for this task).
5. Use the 'prompt' function to ask the user for the correct answer. The user should input the number of the correct answer such as you displayed it on Task 4.
6. Check if the answer is correct and print to the console whether the answer is correct ot nor (Hint: write another method for this).
7. Suppose this code would be a plugin for other programmers to use in their code. So make sure that all your code is private and doesn't interfere with the other programmers code (Hint: we learned a special technique to do exactly that).
*/
(function (){
var Question = function (question, options, answer){
this.question = question;
this.options = options;
this.answer = answer;
}
var comida = new Question ('Qual a melhor comida do mundo?', [' 0 - Jiló', ' 1 - melão', ' 2 - brigadeiro'], 2)
var casa = new Question ('Onde gostaria de morar?', [' 0 - Africa do Sul', ' 1 - Canadá', ' 2 - Brasil'], 1)
var peso = new Question ('De acordo com o nutricionista, qual o seu peso ideal?', [' 0 - 83 Kg', ' 1 - 100 Kg', ' 2 - 70 Kg'], 0)
var qAll = [comida, casa, peso];
var n = Math.floor(Math.random() * qAll.length);
Question.prototype.displayQuestion = function (){
console.log(this.question);
for (var i = 0; i < this.options.length; i++){
console.log(this.options[i]);
}
}
Question.prototype.checkAnswer = function (ans){
if (janelaResposta === this.answer){
console.log("CORRECT!")
} else {
console.log("wrong")
}
}
qAll[n].displayQuestion();
var janelaResposta = parseInt (prompt
("What is the correct answer?")
); //esse parseInt transforma uma string em número
qAll[n].checkAnswer(janelaResposta);
})();