Skip to content

Commit c0d446e

Browse files
committed
Functions returning another function
1 parent 2932c7b commit c0d446e

File tree

1 file changed

+50
-24
lines changed

1 file changed

+50
-24
lines changed

5-advanced-JS/starter/script.js

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -80,37 +80,63 @@
8080
// console.log(obj.city);
8181

8282
//Passing functions as arguments
83-
var years = [1990, 1965, 1937, 2005, 1998];
83+
// var years = [1990, 1965, 1937, 2005, 1998];
8484

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-
}
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+
// }
9292

93-
function calculateAge(el){
94-
return 2016 - el;
95-
}
93+
// function calculateAge(el){
94+
// return 2016 - el;
95+
// }
9696

97-
function isFullAge(el){
98-
return el >= 18;
99-
}
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+
// }
100108

101-
function maxHeartRate(el){
102-
if (el >= 18 && el <= 81) {
103-
return Math.round(206.9 - (0.67 * el));
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);
117+
118+
//Functions returning another function
119+
function interviewQuestion(job){
120+
if (job === 'designer') {
121+
return function(name) {
122+
console.log(name, ', Can you please explain what UX design is?')
123+
}
124+
} else if(job === 'teacher') {
125+
return function(name){
126+
console.log('what subject do you teach', name);
127+
}
104128
} else {
105-
return -1;
129+
return function(name) {
130+
console.log('Hello', name, 'what do you do?');
131+
}
106132
}
107133
}
108134

109-
var ages = arrayCalc(years, calculateAge);
110-
console.log(ages);
135+
var teacherQuestion = interviewQuestion('teacher');
136+
var designerQuestion = interviewQuestion('designer');
137+
138+
teacherQuestion('John');
139+
designerQuestion('Jane');
111140

112-
var fullAges = arrayCalc(ages, isFullAge);
113-
console.log(fullAges);
141+
interviewQuestion('teacher')('Mark');
114142

115-
var rates = arrayCalc(ages, maxHeartRate);
116-
console.log(rates);

0 commit comments

Comments
 (0)