Skip to content

Commit 967b355

Browse files
committed
day 5 js Funtion and Methods
1 parent 66364f8 commit 967b355

3 files changed

Lines changed: 180 additions & 0 deletions

File tree

Chapter 5 Functions & Methods.docx

579 KB
Binary file not shown.

Functions & Methods/index.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Document</title>
7+
</head>
8+
<body>
9+
10+
</body>
11+
<script src="scripts.js"></script>
12+
</html>

Functions & Methods/scripts.js

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
// Funtion
2+
/*
3+
Block Of code that performss a specific task,can be invoked whenever needed
4+
*/
5+
//1
6+
function myfun(msg){ // one parameter
7+
console.log(msg);
8+
}
9+
myfun("I Love Js");
10+
//2
11+
function Hifun(){ //zero param
12+
console.log("Hii JS");
13+
}
14+
Hifun();
15+
16+
//Funtion -> add two numbers
17+
18+
function sum(x,y){
19+
s=x+y;
20+
return s;
21+
}
22+
let va=sum(4,7);
23+
console.log("sum of two numbers =",va);
24+
25+
/*Arrow funtion
26+
Compact way of writing a funtion
27+
*/
28+
const arrowmul=(a,b)=>{
29+
return a*b;
30+
};
31+
let mul=arrowmul(5,4);
32+
console.log("Multiplication of Number=",mul);
33+
34+
35+
//vowel count
36+
37+
function vowelcount(str){
38+
let count=0;
39+
for(const char of str){
40+
if(
41+
char=="a"||
42+
char=="e"||
43+
char=="i"||
44+
char=="o"||
45+
char=="u"
46+
47+
){
48+
count++
49+
}
50+
}
51+
return count;
52+
}
53+
let re=vowelcount("vowel");
54+
console.log("Number of vowel=",re);
55+
56+
//vowel count using arrow funtion
57+
const countvow=(str)=>{
58+
let count=0;
59+
for(const char of str){
60+
if(
61+
char=="a"||
62+
char=="e"||
63+
char=="i"||
64+
char=="o"||
65+
char=="u"
66+
67+
){
68+
count++
69+
}
70+
}
71+
return count;
72+
};
73+
let r=countvow("aeiou");
74+
console.log("Number of vowel=",r);
75+
76+
77+
//for each loop in array
78+
//it is also called higher order funtion(funtion as a parametre or funtion return)
79+
let arr=["delhi","Mumbai","Kolkata"];
80+
arr.forEach((val,idx,arr)=>{
81+
console.log(val.toUpperCase(),idx,arr);
82+
});
83+
84+
//PQ
85+
let nums=[2,5,6,8];
86+
console.log("values=",nums);
87+
let calSquere=(nums)=>{
88+
console.log("Squere of each value",nums*nums);
89+
};
90+
nums.forEach(calSquere);
91+
92+
93+
//Map methods in array
94+
let num=[5,6,2,9];
95+
console.log("Numbers are",num);
96+
let newarr=num.map((val)=>{
97+
return val*2;
98+
});
99+
console.log("Each numbers multiplied by 2= ",newarr);
100+
101+
102+
//filter method in array
103+
/*
104+
Print the even & odd values in given array
105+
*/
106+
let v=[1,2,3,4,5,6,7,8];
107+
console.log("Values are",v);
108+
let evenarr=v.filter((vt)=>{
109+
return vt%2==0;
110+
});
111+
let oddarr=v.filter((vt)=>{
112+
return vt%2!==0;
113+
});
114+
console.log("Even values are",evenarr);
115+
console.log("Odd values are",oddarr);
116+
117+
118+
/*
119+
Reduce Method in array
120+
Sum of each value in given array
121+
Find the biggest value in array
122+
Find the smallest value in array
123+
*/
124+
let barr=[1,3,2,5];
125+
console.log("Values are",barr);
126+
127+
const output=barr.reduce((prev,curr)=>{
128+
return prev+curr;
129+
});
130+
const big=barr.reduce((prev,curr)=>{
131+
return prev>curr? prev:curr;
132+
});
133+
134+
const small=barr.reduce((prev,curr)=>{
135+
return prev<curr? prev:curr;
136+
});
137+
console.log("Sum Of array values",output);
138+
console.log("Biggest value is=",big);
139+
console.log("Smallest value is=",small);
140+
141+
142+
//PQ
143+
let marks=[97,64,32,49,99,96,86];
144+
console.log("Marks =",marks)
145+
let topper=marks.filter((val)=>{
146+
return val>90;
147+
});
148+
console.log("Students Got 90+ marks",topper);
149+
150+
151+
152+
//PQ
153+
let n=prompt("Enter a number");
154+
let no=[];
155+
for(let i=1;i<=n;i++){
156+
no[i-1]=i;
157+
}
158+
console.log("Array elements are=",no);
159+
160+
const sumarr=no.reduce((res,curr)=>{
161+
return res+curr;
162+
});
163+
164+
const product=no.reduce((res,curr)=>{
165+
return res*curr;
166+
});
167+
console.log("Sum of all numbers in array",sumarr);
168+
console.log("Product of all numbers in array(Factorial)",product);

0 commit comments

Comments
 (0)