-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay5.js
More file actions
147 lines (89 loc) · 3.02 KB
/
Day5.js
File metadata and controls
147 lines (89 loc) · 3.02 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
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
147
//! Write a function to check if a number is even or odd and log the result to the console.
function EvenOdd(num) {
if (num % 2 === 0) {
console.log(`The ${num} is even`)
}
else {
console.log(`The ${num} is odd`)
}
}
EvenOdd(10)
//! Write a function to calculate the square of a number and return the result.
function squareNumber(num) {
let res = num * num
console.log(`The square of ${num} is ${res}`)
}
squareNumber(5)
// Write a function expression to find the maximum of two numbers and log the result to the console.
function maxTwoNumbers(a, b) {
let res = a > b ? a : b
console.log(`The maximum of ${a} and ${b} is ${res}`)
}
maxTwoNumbers(10, 11)
// Write a function expression to concatenate two strings and return the result.
function concatenateTwoString(str1, str2) {
console.log(`${str1} ${str2}`)
}
concatenateTwoString("Faisal", "Khan")
// Write an arrow function to calculate the sum of two numbers and return the result.
const calTwoNumbers = (a, b) => {
console.log(`The sum of ${a} ${b} is ${a + b}`)
}
calTwoNumbers(10, 10)
// Write an arrow function to check if a string contains a specific character and return a boolean value.
function containCharacter(char, str) {
return str.includes(char)
}
const string1 = "i am doing JS challenge"
const charact = "a"
const result = containCharacter(charact, string1)
console.log(`Does ${string1} has this ${charact}? ${result}`)
// Write a function that takes two parameters and returns their product. Provide a default value for the second parameter.
function myFunction(a, b = 1) {
console.log(a * b)
}
myFunction(10, 10)
myFunction(10)
// Write a function that takes a person's name and age and returns a greeting message. Provide a default value for the age.
function person(name, age = 19) {
console.log(`Hello ${name} age is ${age}`)
}
person("Ali", 20)
// Write a higher-order function that takes a function and a number, and calls the function that many times.
function hof(func, time) {
for (let i = 1; i <= time; i++) {
func();
}
}
function sayHello() {
console.log("Hello buddy")
}
hof(sayHello, 5)
// 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 hoff(fun1, fun2 val) {
// const res = fun1(val);
// fun2(res)
// }
// function firstFun(val) {
// console.log(val * val)
// }
// function secondFun(val) {
// console.log(
// `1st function result is ${res}, add 1 to it. ${res + 1}`
// )
// }
// hoff(firstFun, secondFun, 6)
const applyFunctions = (func1, func2, value) => {
const firstResult = func1(value);
const finalResult = func2(firstResult);
return finalResult;
};
const addTwo = (num) => {
return num + 2;
};
const multiplyByThree = (num) => {
return num * 3;
};
const initialValue = 5;
const res = applyFunctions(addTwo, multiplyByThree, initialValue);
console.log(`The final res is ${res}`);