Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Passing functions as arguments created
  • Loading branch information
mike-diff committed Nov 26, 2018
commit 2932c7ba52a20438dc18549e3dece54b33413c46
98 changes: 67 additions & 31 deletions 5-advanced-JS/starter/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,38 +43,74 @@
// job: { value: 'Designer'}
// });

//Primitives vs Objects

//Primitives
var a = 23;
var b = a;
a = 46;
console.log(a);
console.log(b);

//Objects
var obj1 = {
name: 'John',
age: 26
};

var obj2 = obj1;
obj1.age = 30;
console.log(obj1.age);
console.log(obj2.age);

//Functions
var age = 27;
var obj = {
name: "Jonas",
city: 'Lisbon'
// //Primitives vs Objects

// //Primitives
// var a = 23;
// var b = a;
// a = 46;
// console.log(a);
// console.log(b);

// //Objects
// var obj1 = {
// name: 'John',
// age: 26
// };

// var obj2 = obj1;
// obj1.age = 30;
// console.log(obj1.age);
// console.log(obj2.age);

// //Functions
// var age = 27;
// var obj = {
// name: "Jonas",
// city: 'Lisbon'
// }

// function change(a, b){
// a = 30;
// b.city = 'San Fancisco';
// }

// change(age, obj);
// console.log(age);
// console.log(obj.city);

//Passing functions as arguments
var years = [1990, 1965, 1937, 2005, 1998];

function arrayCalc(arr, fn){
var arrRes = [];
for(var i in years){
arrRes.push(fn(arr[i]));
}
return arrRes;
}

function calculateAge(el){
return 2016 - el;
}

function change(a, b){
a = 30;
b.city = 'San Fancisco';
function isFullAge(el){
return el >= 18;
}

change(age, obj);
console.log(age);
console.log(obj.city);
function maxHeartRate(el){
if (el >= 18 && el <= 81) {
return Math.round(206.9 - (0.67 * el));
} else {
return -1;
}
}

var ages = arrayCalc(years, calculateAge);
console.log(ages);

var fullAges = arrayCalc(ages, isFullAge);
console.log(fullAges);

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