-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.js
More file actions
223 lines (195 loc) · 4.46 KB
/
functions.js
File metadata and controls
223 lines (195 loc) · 4.46 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// functions in an array
function hello() {
console.log("Hello");
}
hello();
function print1to5() {
for (let i = 1; i <= 5; i++) {
console.log(i);
}
}
print1to5();
function isAdult() {
let age = 18;
let personAge = 25;
if (personAge >= age) {
console.log("You are an adult");
}
else {
console.log("You are not an adult");
}
}
isAdult();
// Practice Question
// Question 1
// Create a function that prints a poem
function printPoem() {
console.log("The sun shines bright in the sky,");
console.log("The birds sing their sweet melody,");
console.log("The flowers bloom with vibrant hue,");
console.log("A beautiful world, for me and you.");
}
printPoem();
// Question 2
// Create a function to roll a dice and always display the value of the dice (1 to 6)
function rollDice() {
let diceValue = Math.floor(Math.random() * 6) + 1;
console.log("You rolled a " + diceValue);
}
rollDice();
// Functions with Arguments
function printInfo(name, age) {
console.log(`${name}'s age is ${age}`);
}
printInfo("Inam", 19);
printInfo("Inam"); // order wise argruments access
function sum(a, b) {
console.log(a + b);
}
sum(5, 6);
sum(4567876, 456787);
// Practice Question
// Question 1
// Create a function that gives us the average of 3 numbers
function average(a, b, c) {
let avg = (a + b + c) / 3;
console.log(avg);
}
average(5, 6, 7);
average(5, 6); // error because of missing argument
average(5); // error because of missing argument
// Question 2
// Create a function that prints the multiplication table of a number.
function printTable(num) {
for (let i = 1; i <= 10; i++) {
console.log(`${num} * ${i} = ${num * i}`);
}
}
printTable(5);
printTable(10);
// recursion
function sum2(a, b) {
return a + b; // nothing will execute after return
}
console.log(sum2(sum2(1, 2), 3));
// Question 3
// get sum from 1 to 10
function getSum(n) {
let sum = 0;
for (let i = 1; i <= n; i++) {
sum += i;
}
return sum;
}
console.log(getSum(10));
// Question 4
// function that returns the concatenation of all strings in an array
let str = ["hi", "hello", "bye", "!"];
function concatStrings(arr) {
let result = "";
for (let i = 0; i < arr.length; i++) {
result += arr[i];
}
return result;
}
console.log(concatStrings(str));
// scope
// function scope
function calSum(a, b) {
// function scope
let sum = a + b;
console.log(sum);
}
calSum(5, 6); // sum will not access by outside the function
// block scope
{
let a = 25;
// it will not accessable out of the block
}
// lexical scope
function outer() {
let x = 10;
function inner() {
let a = 10;
console.log(x);
}
inner();
// 'a' variable is not access able here
}
outer(); // it will print 10
// Practice Questions
// Question 1
let greet = "Hello";
function changedGreet() {
// global greet variable is not available in the function
let greet = "Salam";
console.log(greet);
function innerGreet() {
// greet is not accessable here because its a block scope variable
console.log(greet);
}
}
console.log(greet);
changedGreet();
// Function Expressions
const sum3 = function (a, b) {
return a + b;
}
console.log(sum3(5, 16)); // it will print 21
// Higher order functions
function mulipleGreet(func, count) {
for (let i = 1; i <= count; i++) {
func();
}
}
let greet_ = function () {
console.log("Hello");
}
mulipleGreet(greet_, 100);
function oddEvenTester(request) {
if (request % 2 == 0) {
return function (n) {
console.log(!(n % 2 == 0));
}
} else if (request == "even") {
return function (n) {
console.log(n % 2 == 0);
}
} else {
return console.log("wrong descion");
}
}
let request = "odd";
// Methods
const calculator = {
num: 55,
add: function (a, b) {
return a + b;
},
sub: function (a, b) {
return a - b;
},
mul: function (a, b) {
return a * b;
},
div: function (a, b) {
return a / b;
},
square: function (a) {
return a * a;
},
cube: function (a) {
return a * a * a;
},
power: function (a, b) {
return Math.pow(a, b);
},
sqrt: function (a) {
return Math.sqrt(a);
},
abs: function (a) {
return Math.abs(a);
}
};
console.log(calculator.add(9, 5));
console.log(calculator.power(9, 5));