Skip to content

Commit 8cd28db

Browse files
authored
Create polyfills.js
1 parent cf010cb commit 8cd28db

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

Coding Interview Prep/polyfills.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// map
2+
3+
Array.prototype.ourMap = function(callback) {
4+
var arr = [] // since, we need to return an array
5+
for (var i = 0; i < this.length; i++) {
6+
arr.push(callback(this[i], i, this)) // pushing currentValue, index, array
7+
}
8+
return arr // finally returning the array
9+
}
10+
11+
12+
// filter
13+
14+
Array.prototype.filterAlbums = function(callback, context) {
15+
arr = []
16+
for (var i = 0; i < this.length; i++) {
17+
if (callback.call(context, this[i], i, this)) {
18+
arr.push(this[i])
19+
}
20+
}
21+
return arr
22+
}
23+
24+
// reduce
25+
26+
Array.prototype.reduceAlbums = function(callback, initialValue) {
27+
var accumulator = initialValue === undefined ? undefined : initialValue
28+
29+
for (var i = 0; i < this.length; i++) {
30+
if (accumulator !== undefined) {
31+
accumulator = callback.call(undefined, accumulator, this[i], i, this)
32+
} else {
33+
accumulator = this[i]
34+
}
35+
}
36+
return accumulator
37+
} // our polyfill for reduce

0 commit comments

Comments
 (0)