Skip to content

Commit ef986ea

Browse files
committed
Code Refactor : Remove anonymous functions
1 parent f7156d0 commit ef986ea

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

libs/array_lib.js

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,26 @@ const addNumbers = function(numbers){
2424
return numbers.reduce(sum,0);
2525
}
2626

27+
const unshift = function(list, elementToUnshift){
28+
list.unshift(elementToUnshift);
29+
return list;
30+
}
31+
2732
// Reverse the order of an array
2833
const reverse = function(source){
29-
let reversed = source.reduce(function(result, element){
30-
result.unshift(element);
31-
return result;
32-
}, []);
34+
let reversed = source.reduce(unshift, []);
3335
return reversed;
3436
}
3537

3638
// Extract every second element from an array
3739
const extractAlternatingElements = function(elements){
38-
return elements.filter(function(e, index){
39-
return index % 2 == 0;
40-
});
40+
let result = [];
41+
for(let index = 0; index < elements.length; index++){
42+
if(index % 2 == 0){
43+
result.push(elements[index]);
44+
}
45+
}
46+
return result;
4147
}
4248

4349
// Create fibonacci series upto given limit
@@ -157,47 +163,53 @@ const isInAscendingOrder = function(list){
157163
return isInSpecificOrder(list, isGreater);
158164
}
159165

166+
const convertToNumber = function(string){
167+
return +string;
168+
}
169+
160170
// Extract digits of a number into an array
161171
const extractDigits = function(number){
162172
let digits = number.toString().split("");
163-
return digits.map(function(element){ return +element;});
173+
return digits.map(convertToNumber);
164174
}
165175

166176
// Get all unique elements from an array
167177

168178
const unique = function(list){
169179
let result = [];
170-
list.forEach(function(element){
180+
for(let element of list){
171181
let isAlreadyPresent = result.includes(element);
172182
if(!isAlreadyPresent){
173183
result.push(element);
174184
}
175-
});
185+
}
176186
return result;
177187
}
178188

179189
// get Union set for two given sets
180190
const union = function(firstSet, secondSet){
181191
let unionSet = firstSet.slice();
182-
secondSet.forEach(function(element){
192+
for(let element of secondSet){
183193
unionSet.push(element);
184-
});
194+
};
185195
return unique(unionSet);
186196
}
187197

188198
//get Intersection set of two sets
189199
const getIntersection = function(firstSet, secondSet){
190-
let intersectionSet = firstSet.filter(function(element){
200+
const isAlreadyPresent = function(element){
191201
return secondSet.includes(element);
192-
});
202+
}
203+
let intersectionSet = firstSet.filter(isAlreadyPresent);
193204
return unique(intersectionSet);
194205
}
195206

196207
//get Difference of two sets
197208
const getDifference = function(firstSet, secondSet){
198-
let differenceSet = firstSet.filter(function(element){
209+
const isNotAlreadyPresent = function(element){
199210
return !secondSet.includes(element);
200-
});
211+
}
212+
let differenceSet = firstSet.filter(isNotAlreadyPresent);
201213
return unique(differenceSet);
202214
}
203215

0 commit comments

Comments
 (0)