Repository navigation
Expand file tree
/
Copy pathfunctional.js
More file actions
133 lines (102 loc) · 3.01 KB
/
Copy pathfunctional.js
File metadata and controls
133 lines (102 loc) · 3.01 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
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
/**
* Example One
* example of non pure / impure function
* and its side effects
*/
let users = [
{ name: "James", score: 30, tries: 1 },
{ name: "Mary", score: 110, tries: 4 },
{ name: "Henry", score: 80, tries: 3 }
]
let currentUser = 2;
let updateScore = newAmt => users[currentUser].score += newAmt;
let returnUsers = () => users;
let updateTries = () => users[currentUser].tries++;
let updateUser = newUser => currentUser = newUser;
console.log(returnUsers())
console.log('Current user has been updated: ', updateUser(0))
console.log(updateScore(10));
/**
* Example Two
* function with side effect
*/
let cnt = 0;
let incrementExampleOne = function() {
cnt++;
return cnt;
}
/**
* function without side effect
* this is called a pure function
*/
let incrementExampleTwo = function(num) {
return ++num
}
console.log('Increment Example Two: ', incrementExampleTwo(1))
/**
* an other example of pure function
*/
let average = scores => {
let total = 0;
for (let i = 0; i < scores.length; i++) {
total += scores[i];
}
console.clear()
console.log('Total of given numbers: ', total);
console.log('Total numbers are: ', scores.length);
return total / scores.length;
};
console.log(average([10,20]));
/**
* copy of object with new memory reference
*/
const clonedUsersList = JSON.parse(JSON.stringify(users));
console.log('Original users list: ', users);
clonedUsersList[0].name = "Jhon Doe";
clonedUsersList[0].score += 10;
console.log('Cloned users list: ', clonedUsersList);
/**
* Exercise
* 1) - any scores that are below 10 needs to be multiplied by 10 and
* the new value included.
* 2) - remove any scores that are over 100.
* 3) - remove nay scores that are 0 or below.
* 4) - provide a count for the number of scores still remaining.
*/
/** scores */
const scores = [50, 6, 100, 5, 10, 55, 8, 60, 90, 80, 0, 30, 110, -3];
/** Solution 01 */
const newScores = scores.map((score, index, array) => {
return (score < 10 && score > 0) ? score * 10 : score;
})
console.log('----- Solution 01 -----');
console.log('New scores: ', newScores);
/** Solution 02 */
const removeOverHundred = scores.filter((score) => {
return score > 100;
})
console.log('----- Solution 02 -----');
console.log('List of scores > 100: ', removeOverHundred);
/** Solution 03 */
const removeZeroAndBelow = scores.filter((score) => {
return score > 0;
})
console.log('----- Solution 03 -----');
console.log('List of scores > 0: ', removeZeroAndBelow);
/** Solution 04 - a
* sum of scores
*/
const sumOfScores = scores.reduce((accumulator, score, index, array) => {
return accumulator +=score;
})
console.log('----- Solution 04 - a -----');
console.log('Sum of scores: ', sumOfScores);
/** Solution 04 - b
* provide a count for the number of scores.
*/
const scoresCount = scores.reduce((accumulator, score, index, array) => {
// return array.length;
return accumulator + 1;
}, 0)
console.log('----- Solution 04 - b -----');
console.log('Scores count: ', scoresCount);