Skip to content

Commit ba93dac

Browse files
committed
functions map filter reduce
1 parent b27f9d5 commit ba93dac

1 file changed

Lines changed: 174 additions & 0 deletions

File tree

Javascript/one.js

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/*-
2+
function a() {
3+
var b=10;
4+
c();
5+
function c() {
6+
console.log(b);
7+
8+
}
9+
10+
}
11+
a();
12+
13+
// Output: 10
14+
*/
15+
/*
16+
2.WAP In JS to print 1 to 5 with 1 sec interval then 2 then 3 so on using setTimeout and closure
17+
18+
function x(){
19+
for(let i=1; i<=5; i++){
20+
setTimeout(function(){
21+
console.log(i);
22+
},i* 1000);
23+
}
24+
}
25+
x();// Output: 1 2 3 4 5
26+
27+
*/
28+
29+
/*
30+
3
31+
function x(){
32+
for(var i=1; i<=5; i++){
33+
function closer(x){
34+
setTimeout(function(){
35+
console.log(x);
36+
},x* 1000);
37+
38+
}
39+
closer(i);
40+
41+
}
42+
}
43+
x();
44+
// Output: 1 2 3 4 5
45+
46+
47+
4. wAP to find area of circle using function and array
48+
49+
const radius =[5,6,7,8,9];
50+
const area = function(radius){
51+
const Output =[];
52+
for(let i=0; i<radius.length; i++){
53+
Output.push(Math.PI*radius[i]*radius[i]);
54+
}
55+
return Output;
56+
}
57+
58+
console.log(area(radius));
59+
60+
What is Higher order function?
61+
ans: A higher-order function is a function that takes
62+
one or more functions as arguments, returns a function as its result,
63+
or both. In other words, it can operate on functions, either by taking them as parameters or by returning them.
64+
65+
5. Higher order function to find area, circumference and diameter of circle using function and array
66+
const radius =[5,6,7,8,9];
67+
68+
const area = function(radius){
69+
70+
return Math.PI*radius*radius;
71+
};
72+
73+
const circumference = function(radius){
74+
75+
return 2*Math.PI*radius;
76+
};
77+
78+
const diameter = function(radius){
79+
80+
return 2*radius;
81+
}
82+
83+
const calculate = function(radius, logic){
84+
const Output =[];
85+
for(let i=0; i<radius.length; i++){
86+
Output.push(logic(radius[i]));
87+
}
88+
return Output;
89+
}
90+
91+
console.log(calculate(radius, area));
92+
console.log(calculate(radius, circumference));
93+
console.log(calculate(radius, diameter));
94+
95+
96+
6. Map
97+
98+
const r=[5,6,7,8,9];
99+
const Double = function(x){
100+
return 2*x;
101+
};
102+
103+
function Square(x){
104+
return x*x;
105+
}
106+
107+
function Cube(x){
108+
return x*x*x;
109+
}
110+
111+
function SquareRoot(x){
112+
return Math.sqrt(x);
113+
}
114+
function Binary(x){
115+
return x.toString(2);
116+
}
117+
118+
const Output=r.map(Double)
119+
const Output1=r.map(Binary)
120+
console.log(Output1);
121+
console.log(Output);
122+
123+
7. Filter
124+
125+
const r=[5,6,7,8,9];
126+
127+
function isOdd(x){
128+
return x%2!==0;
129+
}
130+
function isEven(x){
131+
return x%2===0;
132+
}
133+
134+
function isPrime(x){
135+
if(x<2) return false;
136+
for(let i=2; i<=Math.sqrt(x); i++){
137+
if(x%i===0) return false;
138+
}
139+
return true;
140+
}
141+
function isPerfectSquare(x){
142+
return Number.isInteger(Math.sqrt(x));
143+
}
144+
function isArmstrong(x){
145+
const str = x.toString();
146+
const len = str.length;
147+
let sum = 0;
148+
for(let i=0; i<len; i++){
149+
sum += Math.pow(Number(str[i]), len);
150+
}
151+
return sum === x;
152+
}
153+
154+
const Output=r.filter(isOdd)
155+
const Output1=r.filter(isEven)
156+
console.log(Output1);
157+
console.log(Output);
158+
159+
8. Reduce
160+
161+
const r=[5,5,5,5,5];
162+
163+
function sumofall(x){
164+
let sum = 0;
165+
for(let i=0; i<x.length; i++){
166+
sum += x[i];
167+
}
168+
return sum;
169+
}
170+
171+
const Output=r.reduce((acc, curr) => acc + curr, 0);
172+
173+
console.log(Output);
174+
*/

0 commit comments

Comments
 (0)