-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay_5.js
130 lines (82 loc) · 3.15 KB
/
Day_5.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Day 5: Functions
// Tasks/Activities:
// Task 1: Write a function to check if a number is even or odd and log the result to the console.
function checkEvenOrOdd(num) {
if (num % 2 == 0) {
console.log(`${num} is even`)
}
else{
console.log(`${num} is odd`)
}
}
checkEvenOrOdd(10) // 10 is even
checkEvenOrOdd(3) // 3 is even
// Task 2: Write a function to calculate the square of a number and return the result.
function calculateSquare(num) {
return num * num
}
console.log(calculateSquare(2)) // 4
console.log(calculateSquare(10)) // 100
// Activity 2: Function Expression
// Task 3: Write a function expression to find the maximum of two numbers and log the result to the console.
const findMax = function (num1, num2) {
if (num1>num2) {
console.log(`${num1} is greater than ${num2}`)
}
else{
console.log(`${num2} is greater then ${num1}`)
}
}
findMax(2,3) // 3 is greater than 2
findMax(10,7) // 10 is greater than 7
// Task 4: Write a function expression to concatenate two strings and return the result.
const concateTwoStrings = function (str1, str2) {
return str1 + str2
}
console.log(concateTwoStrings("hello ", "world")) // hello world
console.log(concateTwoStrings("good ", "morning")) // good morning
// Activity 3: Arrow Functions
// Task 5: Write an arrow function to calculate the sum of two numbers and return the result.
const calSum = (num1, num2) =>{
return num1 + num2
}
console.log(calSum(2,2)); // 4
console.log(calSum(10,39)); // 49
// Task 6: Write an arrow function to check if a string contains a specific character and return a boolean value.
const charCheck = (str, charcheck) =>{
return str.includes(charcheck)
}
console.log(charCheck("Farooq","q")) // true
console.log(charCheck("Umar","f")) // false
// Activity 4: Function Parameters and Default Values
// Task 7: Write a function that takes two parameters and returns their product. Provide a default value for the second parameter.
function maxChecker(num1, num2 = 2) {
if (num1>num2) {
console.log(`${num1} is greater than ${num2}`)
}
else{
console.log(`${num2} is greater then ${num1}`)
}
}
maxChecker(20)
// Task 8: Write a function that takes a person’s name and age and returns a greeting message. Provide a default value for the age.
function greet(person, age = 21) {
return `${person} age is ${age}`
}
console.log(greet("farooq"))
// Activity 5: Higher-Order Function
// Task 9: Write a higher-order function that takes a function and a number, and calls the function that many times.
function repeatFunction(fn, times) {
for (let i = 0; i < times; i++) {
fn();
}
}
repeatFunction(() => console.log("Hello!"), 3);
// Task 10: Write a higher-order function that takes two functions and a value, applies the first function to the value, and then applies the second function to the result.
function composeFunctions(func1, func2, value) {
return func2(func1(value));
}
const addOne = x => x + 1;
const square = x => x * x;
const result = composeFunctions(addOne, square, 2); // ((2 + 1) ^ 2) = 9
console.log(result); // Outputs: 9