-
Notifications
You must be signed in to change notification settings - Fork 1
/
6.js
254 lines (215 loc) · 5.03 KB
/
6.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//01 Functions in JS
// Function Defining
// function funcName(){
// //do something
// }
// Function Calling
// funcName();
function hello(){
console.log("Hello")
}
hello();
function print1to5(){
for(i=1;i<=5;i++){
console.log(i)
}
}
print1to5();
function isAdult(){
let age =15
if(age>=18){console.log("Adult")}
else{console.log("Not adult")}
}
isAdult();
//02 Practice q1
function printPoem(){
console.log("Twinkle twinkle little star")
console.log("How I wonder what you are")
}
printPoem();
//03 Practice q2
function rollDice(){
console.log(Math.floor(Math.random()*6)+1)
}
rollDice();
//04 Functions with arguement
//Values we pass to the function
// function funcName(arg1,arg2..){
// //do something
// }
function printName(name){
console.log(name);
}
printName("Kuldeep");
printName("Rajat")
function printInfo(name,age){
console.log(`${name}'s age is${age}.`)
}
printInfo("Kuldeep",17);
printInfo("kutta",5)
function sum(a,b){
console.log(a+b)
}
sum(9,8);
sum(1,4);
//05 Practice q3
//Create a function that gives us teh average of 3 numbers.
function avg(a,b,c){
console.log((a+b+c)/3)
}
avg(12,67,99);
//06 Practice q4
//Table of any number
function table(n){
for(i=n;i<=n*10;i=i+n){
console.log(i)
}
}
table(7);
//07 return Keyword
//return keywor dis used to return some value from the function.
// function funcName(are1,arg2){
// //do something
// return val;
// }
//return ke badd ke statement execute nhi hote function mein
function sum(a,b){
return a+b;
}
console.log(sum(1,4))
function adult(age){
if(age>=18){
return "adult";
}
else{return"not adult"}
}
console.log(adult(13))
//08 Practice q5
//Create a function that returns the sum of numbers from 1 to n
function allSum(n){
return n*(n+1)/2;
}
console.log(allSum(8));
//OR
function getSum(n){
let sum =0;
for(i=0;i<=n;i++){
sum=sum+i //sum+=i
}
return sum;
}
console.log(getSum(8))
//09 Practice q6
//Create a function that returns the concatenation of all the strings in an array.
let str = ["hi","hello","bye"];
function concat(str){
let result ="";
for(i=0;i<str.length;i++){
result +=str[i];
}
return result; //yaha return hi kiya hai to bahar console krna padha kyuki function ke andar console nhi tha
}
console.log(concat(str));
//10 Scope
//Scope determines the accssiblity of variables, objects, and functions from different parts of the code
//Three types
//Function Scope
//Block Scope
//Lexical Scope
//Function Scope = Variables defined inside function are not accessible(visible)from outside the function
let summ = 54; //Global Scope
function calSum(a,b){
let summ = a+b; //Function Scope //function ke inside hi raaj hai iska
console.log(summ)
}
calSum(1,2)
console.log(summ)
//11 Block Scope
//Variables declared inside a {} block cannot be accessed from outside the block.
//12 Lexical Scope
//a variable defined outside a function can be accessible inside another function defined after the variable declaration
//The opposite is NOT true.
function outerFunc(){
let x=5;
let y=7;
function ineerFunc(){
console.log(x)
console.log(y)
}
}
//13 Practice q7
let greet = "hello"; //global scope
function changeGreet(){
let greet = "namastee"; //function scope
console.log(greet);
function innerGreet(){
console.log(greet); //lexical scope
}
innerGreet();
}
console.log(greet);
changeGreet();
//14 Function Expressions
// let variable = function(){
// //do or return something
// }
let suum = function(a,b){ //yaha function ka koi name nhi hai function is a variable , variable ke naam se hi function ko call krenge.
return a+b;
}
console.log(suum(8,2));
//15 Higher Order Functions
//A functin that does one or both:
// -takes one or multiple functions as arguements
// -returns a function
function greeetFunc(func,count){
for(i=1;i<=count;i++){
func();
}
}
let greeet = function(){
console.log("hello");
}
greeetFunc(greeet,9);
greeetFunc(function(){console.log("namaste")},200);
//16 Higher Order Functions (return)
//returns a function
function oddEvenTest(request){
if(request == "even"){
return function(n){
console.log(n%2==0)
}
}
else if(request ="odd"){
return function(n){
console.log(!(n%2==0))
}
}
else{
console.log("wrong request.")
}
}
console.log(oddEvenTest("odd"))
console.log(oddEvenTest("even")(8))
//17 Methods
//ACtions that can be performed on an object.
let calculator = {
add : function (a,b){
return a+b;
},
sub: function(a,b){
return a-b;
},
mul : function(a,b){
return a*b;
}
}
console.log(calculator.add(5,5));
// Methods(Shorthand) // Yaha function likhne ki need nhi direct likh diya
const calculatorr={
add(a,b){
return a+b ;
},
sub(a,b){
return a-b;
}
}