-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_5.js
147 lines (95 loc) · 2.21 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Activity 1: Function Declaration
// Task 1
function evenOrOdd(number) {
if(number % 2 === 0) {
console.log(`${number} is even`);
} else {
console.log(`${number} is odd`);
}
}
evenOrOdd(7);
/* Output: 7 is odd */
// Task 2
function sqOfNumber(number) {
let square = number * number;
return square;
}
console.log(sqOfNumber(5));
/* Output: 25 */
// Activity 2: Function Expression
// Task 3
function maxOfTwoNumbers(num1, num2) {
if(num1 > num2) {
console.log(`${num1} is max`);
} else {
console.log(`${num2} is max`);
}
}
maxOfTwoNumbers(10, 8);
/* Output: 10 is max */
// Task 4
function concat(str1, str2) {
return str1 + " " + str2;
}
console.log(concat("Prince", "Singh"));
/* Output: Prince Singh */
// Activity 3: Arrow Functions
// Task 5
const sum = (num1, num2) => {
return num1 + num2;
}
console.log(sum(4, 8));
/* Output: 12 */
// Task 6
const includeChar = (str, ch) => {
return str.includes(ch);
}
console.log(includeChar("Prince Singh", 'w'));
/* Output: false */
// Activity 4: Function Parameters and Default Values
// Task 7
const product = (num1, num2=9) => {
return num1 * num2;
}
console.log(product(7));
/* Output: 63 */
// Task 8
const greet = (Name, Age=21) => {
console.log(`Hi ${Name}(${Age}), Welcome to 30 Days Javascript Challenge`)
}
greet("Prince");
/* Output: Hi Prince(21), Welcome to 30 Days Javascript Challenge */
// Activity 5
// Task 9
const callBack = (fun, n) => {
for(let i = 1; i<=n; i++) {
fun();
}
}
function fun() {
console.log("callback function called from Task 9");
}
callBack(fun, 3);
/* Output
callback function called from Task 9
callback function called from Task 9
callback function called from Task 9
*/
// Task 10
const validToVote = (calcAge, isValid, birthYear) => {
return isValid(calcAge(birthYear));
}
function calcAge(birthYear) {
let age = 2024 - birthYear;
console.log(`Your age is ${age}`);
return age;
}
function isValid(calcAge) {
return calcAge > 18;
}
console.log(validToVote(calcAge, isValid, 2003));
/* Output
Your age is 21
Valid to vote
undefined // can any body explain me why this undefined gets printed
*/