Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions myArrayMethods.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//'this' will reference the array
//

Array.prototype.myEach = function(callback, thisArg = this) {
for (var i = 0; i < thisArg.length; i++) {
callback(thisArg[i], i, thisArg);
}
}
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

Array.prototype.myMap = function(callback, thisArg = this) {
returnArray = [];
for (var i = 0; i < thisArg.length; i++) {
returnArray.push(callback(thisArg[i], i, thisArg));
}
return returnArray;
}
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

Array.prototype.myReduce = function(callback, returnValue = this[0]) {

for (var i = arguments.length > 1 ? 0 : 1; i < this.length; i++) {
returnValue = callback(returnValue, this[i], i, this);
}
return returnValue;
}
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

Array.prototype.myFilter = function(callback, thisArg = this) {
returnArray = [];
for (var i = 0; i < thisArg.length; i++) {
if (callback(thisArg[i], i, thisArg)) {
returnArray.push(thisArg[i]);
}
}
return returnArray;
}
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

Array.prototype.mySome = function(callback, thisArg = this) {
for (var i = 0; i < thisArg.length; i++) {
if (callback(thisArg[i], i, thisArg)) {
return true;
}
}
return false;
}

//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

Array.prototype.myEvery = function(callback, thisArg = this) {
for (var i = 0; i < thisArg.length; i++) {
if (!callback(thisArg[i], i, thisArg)) {
return false;
}
}
return true;
}
}
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every
13 changes: 6 additions & 7 deletions myEach.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/ForEach

function myEach(arr, callback) {

// CODE INSIDE HERE //

}

Array.prototype.myEach = function(callback, thisArg = this) {
for (var i = 0; i < thisArg.length; i++) {
callback(thisArg[i], i, thisArg);
}
}
/*
Best if you don't code out here.
If you want to check your code, use `index.js`!
Expand All @@ -18,4 +17,4 @@ function myEach(arr, callback) {


// export this function (you can ignore this for now)
module.exports = myEach;
module.exports = Array.prototype.myEach;
15 changes: 8 additions & 7 deletions myMap.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Map

function myMap(arr, callback) {

// CODE INSIDE HERE //

}

Array.prototype.myMap = function(callback, thisArg = this) {
returnArray = [];
for (var i = 0; i < thisArg.length; i++) {
returnArray.push(callback(thisArg[i], i, thisArg));
}
return returnArray;
}
/*
Best if you don't code out here.
If you want to check your code, use `index.js`!
Expand All @@ -18,4 +19,4 @@ function myMap(arr, callback) {


// export this function (you can ignore this for now)
module.exports = myMap;
module.exports = Array.prototype.myMap;
12 changes: 7 additions & 5 deletions myReduce.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce for more details
// Don't worry about initialValue at first. You can always add it in later.

function myReduce(arr, callback) {

// CODE INSIDE HERE //

}
Array.prototype.myReduce = function(callback, returnValue = this[0]) {
for (var i = arguments.length > 1 ? 0 : 1; i < this.length; i++) {
returnValue = callback(returnValue, this[i], i, this);
}
return returnValue;
}

/*
Best if you don't code out here.
Expand All @@ -19,4 +21,4 @@ function myReduce(arr, callback) {


// export this function (you can ignore this for now)
module.exports = myReduce;
module.exports = Array.prototype.myReduce;
11 changes: 11 additions & 0 deletions mySome.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Array.prototype.mySome = function(callback, thisArg = this) {
for (var i = 0; i < thisArg.length; i++) {
if (callback(thisArg[i], i, thisArg)) {
return true;
}
}
return false;
}


module.exports = Array.prototype.mySome;
18 changes: 9 additions & 9 deletions spec/myEachSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ describe('myEach', function() {
it("takes a function as the second argument and calls that function (callback)", function testCallback() {
function spyOnMe() {}
var spy = chai.spy(spyOnMe);
myEach(testArr, spy);
testArr.myEach(spy);
expect(spy).to.have.been.called();
});

it("passes each value in the array to the callback", function testEachItem(){
var resultingArray = [];
myEach(testArr, function(item) {
testArr.myEach(function(item) {
resultingArray.push(item);
});
// compare elements in the result to expected array
Expand All @@ -40,7 +40,7 @@ describe('myEach', function() {
it("passes each value in the array to the callback, even non-integers", function testArrayPassing() {
var resultingArray = [];
var complexTestArr = ['snoopy', 32, {k: 'val'}, [2,3] ];
myEach(complexTestArr, function(item) {
complexTestArr.myEach(function(item) {
resultingArray.push(item);
});

Expand All @@ -51,7 +51,7 @@ describe('myEach', function() {

it("passes each index in the array to the callback as argument 2", function testEachIndex() {
var resultingArray = [];
myEach(testArr, function(_item, index) {
testArr.myEach(function(_item, index) {
resultingArray.push(index);
});
// compare elements in the result to expected array
Expand All @@ -62,7 +62,7 @@ describe('myEach', function() {

it("passes the entire array to the callback as the 3rd argument", function testArrayPassing() {
var resultingArray = [];
myEach(testArr, function(_item, _index, arr) {
testArr.myEach(function(_item, _index, arr) {
console.log(' results: ', arr);
// each time the callback is called verify that the array is as expected
// until the callback is called though, this test will still pass
Expand All @@ -71,15 +71,15 @@ describe('myEach', function() {
});

it("returns undefined", function() {
var results = myEach(testArr, function(){});
var results = testArr.myEach(function(){});
expect(results).to.be.a("undefined");
});


// edge cases
it("doesn't alter the original array", function testAlterations() {
var resultingArray = [];
myEach(testArr, function(_item, _index, _arr) {
testArr.myEach(function(_item, _index, _arr) {
resultingArray.push('nothing');
});
// compare elements in the result to expected array
Expand All @@ -90,7 +90,7 @@ describe('myEach', function() {

it("works with arrays of length 0", function testArrayL0() {
var resultingArray = [];
myEach([], function(item) {
[].myEach(function(item) {
resultingArray.push(item);
});
// compare elements in the result to expected array
Expand All @@ -102,7 +102,7 @@ describe('myEach', function() {

it("works with arrays of length 1", function testArrayL1() {
var resultingArray = [];
myEach([13], function(item) {
[13].myEach(function(item) {
resultingArray.push(item);
});
// compare elements in the result to expected array
Expand Down
20 changes: 10 additions & 10 deletions spec/myMapSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ describe('myMap', function() {
it("takes a function as the second argument and calls that function (callback)", function testCallback() {
function spyOnMe() {}
var spy = chai.spy(spyOnMe);
myMap(testArr, spy);
testArr.myMap(spy);
expect(spy).to.have.been.called();
});

it("passes each value in the array to the callback", function testEachItem(){
var resultingArray = [];
myMap(testArr, function(item) {
testArr.myMap(function(item) {
resultingArray.push(item);
});
// compare elements in the result to expected array
Expand All @@ -40,7 +40,7 @@ describe('myMap', function() {

it("passes each index in the array to the callback as argument 2", function testEachIndex() {
var resultingArray = [];
myMap(testArr, function(_item, index) {
testArr.myMap(function(_item, index) {
resultingArray.push(index);
});
// compare elements in the result to expected array
Expand All @@ -51,7 +51,7 @@ describe('myMap', function() {

it("passes the entire array to the callback as the 3rd argument", function testArrayPassing() {
var resultingArray = [];
myMap(testArr, function(_item, _index, arr) {
testArr.myMap(function(_item, _index, arr) {
console.log(' results: ', arr);
// each time the callback is called verify that the array is as expected
// Note: until the callback is called though, this test will still pass
Expand All @@ -60,21 +60,21 @@ describe('myMap', function() {
});

it("returns an array", function() {
var results = myMap(testArr, function() {
var results = testArr.myMap(function() {
// no-op
});
console.log(' results: ', results);
expect(results).to.be.an('Array');
});

it("returns an array with the same number of elements", function() {
var results = myMap(testArr, function(){});
var results = testArr.myMap(function(){});
console.log(' results: ', results);
expect(results.length).to.equal(testArr.length);
});

it("returns an array constructed from the return values of the callback", function() {
var results = myMap(testArr, function(){
var results = testArr.myMap(function(){
return 999;
});
console.log(' results: ', results);
Expand All @@ -85,7 +85,7 @@ describe('myMap', function() {
// edge cases
it("doesn't alter the original array", function testAlterations() {
var resultingArray = [];
myMap(testArr, function(_item, _index, _arr) {
testArr.myMap(function(_item, _index, _arr) {
resultingArray.push('nothing');
});
// compare elements in the result to expected array
Expand All @@ -96,7 +96,7 @@ describe('myMap', function() {

it("works with arrays of length 0", function testArrayL0() {
var resultingArray = [];
myMap([], function(item) {
[].myMap(function(item) {
resultingArray.push(item);
});
// compare elements in the result to expected array
Expand All @@ -107,7 +107,7 @@ describe('myMap', function() {

it("works with arrays of length 1", function testArrayL1() {
var resultingArray = [];
myMap([13], function(item) {
[13].myMap(function(item) {
resultingArray.push(item);
});
// compare elements in the result to expected array
Expand Down
Loading