-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
85 lines (60 loc) · 1.83 KB
/
Copy pathapp.js
File metadata and controls
85 lines (60 loc) · 1.83 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
// Function #1: Array Slice
// creating new array modifiedFood and returning
const modifiedFood =["pizza", "burger", "fingerChips", "donuts", "springRoll"];
const ans = modifiedFood.slice(1,4)
console.log(ans);
// Function #2: Array Splice
const modifiedFood =["pizza", "burger", "fingerChips", "donuts", "springRoll"];
modifiedFood.splice(1,0,"Subway");
// inserts at index 1
console.log(modifiedFood);
// expected output: Array ["Jan", "Feb", "March", "April", "June"]
modifiedFood.splice(4, 1, "Toberlone");
// replaces 1 element at index 4
console.log(modifiedFood);
// expected output: Array ["Jan", "Feb", "March", "April", "May"]
// Function #3: Filter
const numberArray = [12,324,213,4,2,3,45,4234,23,17];
const even = numberArray.filter(isEven = (x) => {
return x%2==0;
})
console.log(even);
// Prime function using filter
const prime = numberArray.filter(isPrime = (x) =>{
let i,flag;
flag=1;
for(i=2;i<=x/2;i++){
if(x%i==0)
flag=0;
}
if(flag==1)
return x;
})
console.log(prime);
// Function #4: Reject
const numberArray = [12,324,213,4,2,3,45,4234,23,17];
const notprime = numberArray.filter(nonPrime = (x) =>{
let i,flag;
flag=1;
for(i=2;i*i<=x;i++){
if(x%i==0)
flag=0;
}
if(flag==0)
return x;
})
console.log(notprime);
// Function #5: Lambda
const numberArray = [12,324,213,4,2,3,45,4234,23,17];
const even = numberArray.filter( (x) => {
return x%2==0;
})
console.log(even)
// Function #6: Map
const myArray = [11, 34, 20, 5, 53, 16];
const newArray = functionmyArray.map(Math.sqrt);
console.log(newArray);
// Function #7: Reduce
const myArray = [2,3,5,10];
const multiply = (currentVal) => currentVal*3;
console.log(myArray.reduce(multiply));