-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctional-programming.js
More file actions
108 lines (83 loc) Β· 2.5 KB
/
functional-programming.js
File metadata and controls
108 lines (83 loc) Β· 2.5 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
// Functional Programming
// map
// filter,
// reduce,
// some,
// every,
// find,
// findIndex,
const numbers = [1, 2, 3, 4, 5]
const modifyArray = (arr) => {
const newArr = []
for (const num of arr) {
newArr.push(num * num)
}
return newArr
}
console.log(modifyArray(numbers))
// Higher order function
// A higher order function is a function which takes another function as a parameter or return a function
const names = ['Hazrat', 'Sohan', 'Sakib', 'Sumon', 'Morshed', 'Mitu', 'Sabia']
const changeToUpperCase = (arr) => {
const newArr = []
for (const name of arr) {
newArr.push(name.toUpperCase())
}
return newArr
}
console.log(changeToUpperCase(names))
// Map it modify and map the element
// [1, 2, 3, 4, 5] => [1, 4, 9, 16, 25]
//['Hazrat', 'Sohan', 'Sakib', 'Sumon', 'Morshed', 'Mitu', 'Sabia']
// syntax
const changedNames = names.map((name) => name.toUpperCase())
console.log(changedNames)
const squaredNumbers = numbers.map((num) => num ** 2)
console.log(squaredNumbers)
// [1, 2, 3, 4, 5,6, 7]=> [[1,1],[ 2,4],[ 3,9],[ 4,16],[ 5,25], [6, 36], [7, 49]]
// const countries = ['Finalnd', 'Estonia', 'Sweden'] => [['Filand','FINLAND',7],['Estonia','ESTONIA',7],['Sweden',SWEDEN', 6]]
const arrayOfArrays = numbers.map((num) => [num, num ** 2])
console.log(arrayOfArrays)
const arrayOfCountries = countries.map((country) => [
country,
country.toUpperCase(),
country.length,
])
console.log(arrayOfCountries)
// Filter
const evens = numbers.filter((num) => {
return num % 2 === 0
})
console.log(evens)
const odds = numbers.filter((num) => num % 2 !== 0)
console.log(odds)
console.log(countries)
const countriesWithLand = countries.filter((country) =>
country.includes('stan')
)
console.log(countriesWithLand)
// Reduce
// ['papaya','Guava','Lemen'] => a fruit juice
// [1, 2, 3, 4, 5] =>
// arr.reduce((acc, curr)=> acc + curr, initial)
// const findTotal = (arr) => {
// let sum = 0
// for (const num of arr) {
// sum += num
// }
// return sum
// }
// console.log(findTotal(numbers))
const total = numbers.reduce((acc, cur) => acc + cur, 0)
console.log(total)
// [1, 2, 3, 4, 5]
const someArrEvens = numbers.some((num) => num % 2 == 0)
console.log(someArrEvens)
const someAreOdds = numbers.some((num) => num % 2 !== 0)
console.log(someAreOdds)
const everyItemIsEven = numbers.every((num) => num % 2 == 0)
console.log(everyItemIsEven)
const three = numbers.find((num) => num == 3)
console.log(three)
const threeIndex = numbers.findIndex((num) => num == 3)
console.log(threeIndex)