-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01-functions.js
More file actions
83 lines (70 loc) · 2.07 KB
/
01-functions.js
File metadata and controls
83 lines (70 loc) · 2.07 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
function logger() {
console.log('My name is Debu');
}
// Calling / running / invoking function
logger();
logger();
logger();
function fruitProcessor(apples, oranges) {
const juice = `Juice with ${apples} apples and ${oranges} oranges`;
return juice;
}
const appleJuice = fruitProcessor(5, 0);
console.log(appleJuice);
const appleOrangeJuice = fruitProcessor(2, 4);
console.log(appleOrangeJuice);
const num = Number('23');
// FUNCTION DECLARATION VS. EXPRESSIONS :
// Function declaration -
function calcAge1(birthYear) {
return 2037 - birthYear;
}
const age1 = calcAge1(1991);
// Function expression
const calcAge2 = function (birthYear) {
return 2037 - birthYear;
}
const age2 = calcAge2(1991);
console.log(age1, age2);
// ARROW FUNCTIONS :
/*
const calcAge3 = birthYear => 2037 - birthYear;
const age3 = calcAge3 (1991);
console.log (age3);
const yearsUntilRetirement = (birthYear, firstName) => {
const age = 2037 - birthYear;
const retirement = 65 -age;
//return retirement ;
return `${firstName} retires in ${retirement} years`;
}
console.log (yearsUntilRetirement(1991, 'Jonas'));
console.log (yearsUntilRetirement(1980, 'Bob'));
*/
// FUNCTION CALLING OTHER FUNCTIONS :
function cutFruitPieces(fruit) {
return fruit * 4;
}
function fruitProcessor(apples, oranges) {
const applePieces = cutFruitPieces(apples);
const orangePieces = cutFruitPieces(oranges);
const juice = `Juice with ${applePieces} pieces of apple and ${orangePieces} pieces of orange`;
return juice;
}
console.log(fruitProcessor(2, 3));
// REVIEWING FUNCTION :
const calcAge = function (birthYear) {
return 2037 - birthYear;
}
const yearsUntilRetirement = function (birthYear, firstName) {
const age = calcAge(birthYear);
const retirement = 65 - age;
if (retirement > 0) {
console.log(`${firstName} retires in ${retirement} years`);
return retirement;
} else {
console.log(`${firstName} has already retired`);
return -1;
}
}
console.log(yearsUntilRetirement(1991, 'Jonas'));
console.log(yearsUntilRetirement(1950, 'Maik'));