Skip to content

Commit f2a35a9

Browse files
authored
Merge pull request #2 from 3daddict/dev
Dev
2 parents 2a72f2e + 2932c7b commit f2a35a9

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

5-advanced-JS/starter/script.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
2+
// //function constructors
3+
// var Person = function(name,yearOfBirth, job){
4+
// this.name = name;
5+
// this.yearOfBirth = yearOfBirth;
6+
// this.job = job;
7+
// }
8+
// //Using the prototype method to include function in constructor
9+
// Person.prototype.calculateAge = function(){
10+
// console.log(this.name, 'Age: ', 2016 - this.yearOfBirth);
11+
// }
12+
13+
// Person.prototype.lastName = 'Smith';
14+
15+
// // Instanciation
16+
// var john = new Person('John', 1990, 'Teacher');
17+
// var jane = new Person('Jane', 1969, 'designer');
18+
// var mark = new Person('Mark', 1948, 'retired');
19+
20+
// john.calculateAge();
21+
// jane.calculateAge();
22+
// mark.calculateAge();
23+
24+
// console.log(john.lastName);
25+
// console.log(jane.lastName);
26+
// console.log(mark.lastName);
27+
28+
//Object.create
29+
// var personProto = {
30+
// calculateAge: function(){
31+
// console.log(2016 - this.yearOfBirth);
32+
// }
33+
// };
34+
35+
// var john = Object.create(personProto);
36+
// john.name = 'John';
37+
// john.yearOfBirth = 1990;
38+
// john.job = "Teacher";
39+
40+
// var jane = Object.create(personProto, {
41+
// name: { value: 'Jane'},
42+
// yearOfBirth: { value: 1969},
43+
// job: { value: 'Designer'}
44+
// });
45+
46+
// //Primitives vs Objects
47+
48+
// //Primitives
49+
// var a = 23;
50+
// var b = a;
51+
// a = 46;
52+
// console.log(a);
53+
// console.log(b);
54+
55+
// //Objects
56+
// var obj1 = {
57+
// name: 'John',
58+
// age: 26
59+
// };
60+
61+
// var obj2 = obj1;
62+
// obj1.age = 30;
63+
// console.log(obj1.age);
64+
// console.log(obj2.age);
65+
66+
// //Functions
67+
// var age = 27;
68+
// var obj = {
69+
// name: "Jonas",
70+
// city: 'Lisbon'
71+
// }
72+
73+
// function change(a, b){
74+
// a = 30;
75+
// b.city = 'San Fancisco';
76+
// }
77+
78+
// change(age, obj);
79+
// console.log(age);
80+
// console.log(obj.city);
81+
82+
//Passing functions as arguments
83+
var years = [1990, 1965, 1937, 2005, 1998];
84+
85+
function arrayCalc(arr, fn){
86+
var arrRes = [];
87+
for(var i in years){
88+
arrRes.push(fn(arr[i]));
89+
}
90+
return arrRes;
91+
}
92+
93+
function calculateAge(el){
94+
return 2016 - el;
95+
}
96+
97+
function isFullAge(el){
98+
return el >= 18;
99+
}
100+
101+
function maxHeartRate(el){
102+
if (el >= 18 && el <= 81) {
103+
return Math.round(206.9 - (0.67 * el));
104+
} else {
105+
return -1;
106+
}
107+
}
108+
109+
var ages = arrayCalc(years, calculateAge);
110+
console.log(ages);
111+
112+
var fullAges = arrayCalc(ages, isFullAge);
113+
console.log(fullAges);
114+
115+
var rates = arrayCalc(ages, maxHeartRate);
116+
console.log(rates);

0 commit comments

Comments
 (0)