-
Notifications
You must be signed in to change notification settings - Fork 0
/
005-functions.js
98 lines (77 loc) · 2.45 KB
/
005-functions.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
// Function Declaration
function greet(name) {
// This function greets the user with the provided name
console.log('Hello, ' + name + '!');
}
greet('fitse'); // Output: Hello, fitse!
// Function with Parameters and Return Value
function add(a, b) {
// This function adds two numbers and returns the result
return a + b;
}
let result = add(3, 5); // Calling the add function with arguments 3 and 5
console.log('Result of addition:', result); // Output: Result of addition: 8
// Function with Default Parameters
function greetUser(name = 'Guest') {
// This function greets the user with a default name of "Guest"
console.log('Hello, ' + name + '!');
}
greetUser(); // Output: Hello, Guest!
greetUser('fitse'); // Output: Hello, Alice!
// Function Expression (Anonymous Function)
let sayHello = function () {
// This is an anonymous function that says hello
console.log('Hello!');
};
sayHello(); // Output: Hello!
// Arrow Function
let square = (num) => {
// This arrow function returns the square of a number
return num * num;
};
console.log('Square of 5:', square(5)); // Output: Square of 5: 25
// Higher-Order Functions
function double(x) {
// This function doubles a number
return x * 2;
}
function applyOperation(num, operation) {
// This function applies the specified operation to the given number
return operation(num);
}
let resultDouble = applyOperation(3, double); // Passing double function as an argument
console.log('Result of applying double:', resultDouble); // Output: Result of applying double: 6
// Nested Functions
function outerFunction() {
// This is the outer function
console.log('Outer function');
function innerFunction() {
// This is the inner function
console.log('Inner function');
}
innerFunction(); // Calling the inner function
}
outerFunction(); // Output: Outer function, Inner function
// Closure
function createCounter() {
// This function creates a counter using closure
let count = 0;
return function () {
// This inner function increments the counter
count++;
console.log('Count:', count);
};
}
let counter = createCounter(); // Creating a counter function
counter(); // Output: Count: 1
counter(); // Output: Count: 2
// Recursion
function factorial(n) {
// This function calculates the factorial of a number recursively
if (n === 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
console.log('Factorial of 5:', factorial(5)); // Output: Factorial of 5: 120