diff --git a/myArrayMethods.js b/myArrayMethods.js new file mode 100644 index 0000000..9919072 --- /dev/null +++ b/myArrayMethods.js @@ -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 diff --git a/myEach.js b/myEach.js index e95bc02..fcde7b7 100644 --- a/myEach.js +++ b/myEach.js @@ -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`! @@ -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; diff --git a/myMap.js b/myMap.js index ccb09c3..d044ca5 100644 --- a/myMap.js +++ b/myMap.js @@ -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`! @@ -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; diff --git a/myReduce.js b/myReduce.js index 174fbe3..a7afdbd 100644 --- a/myReduce.js +++ b/myReduce.js @@ -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. @@ -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; diff --git a/mySome.js b/mySome.js new file mode 100644 index 0000000..e88feae --- /dev/null +++ b/mySome.js @@ -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; diff --git a/spec/myEachSpec.js b/spec/myEachSpec.js index 33d8e32..6755c50 100644 --- a/spec/myEachSpec.js +++ b/spec/myEachSpec.js @@ -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 @@ -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); }); @@ -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 @@ -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 @@ -71,7 +71,7 @@ describe('myEach', function() { }); it("returns undefined", function() { - var results = myEach(testArr, function(){}); + var results = testArr.myEach(function(){}); expect(results).to.be.a("undefined"); }); @@ -79,7 +79,7 @@ describe('myEach', function() { // 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 @@ -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 @@ -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 diff --git a/spec/myMapSpec.js b/spec/myMapSpec.js index 4eac2e2..ccab2f4 100644 --- a/spec/myMapSpec.js +++ b/spec/myMapSpec.js @@ -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 @@ -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 @@ -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 @@ -60,7 +60,7 @@ describe('myMap', function() { }); it("returns an array", function() { - var results = myMap(testArr, function() { + var results = testArr.myMap(function() { // no-op }); console.log(' results: ', results); @@ -68,13 +68,13 @@ describe('myMap', function() { }); 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); @@ -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 @@ -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 @@ -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 diff --git a/spec/myReduceSpec.js b/spec/myReduceSpec.js index 85257fa..ae38c8c 100644 --- a/spec/myReduceSpec.js +++ b/spec/myReduceSpec.js @@ -23,12 +23,12 @@ describe('myReduce', function() { it("takes a function as the second argument and calls that function (callback)", function testCallback() { function spyOnMe() {} var spy = chai.spy(spyOnMe); - myReduce(testArr, spy); + testArr.myReduce(spy); expect(spy).to.have.been.called(); }); it("has a return value that is equal to the last return value of the callback", function() { - var results = myReduce(testArr, function(){ + var results = testArr.myReduce(function(){ return 1000100; // on every pass }); console.log(' results: ', results); @@ -37,7 +37,7 @@ describe('myReduce', function() { it("returns a single value, not an array", function() { - var results = myReduce(testArr, function() { + var results = testArr.myReduce(function() { return 'mercury'; }, 'foo'); console.log(' results: ', results); @@ -50,7 +50,7 @@ describe('myReduce', function() { "callback on each successive pass", function() { var results = []; - myReduce(testArr, function(previousValue) { + testArr.myReduce(function(previousValue) { results.push(previousValue); return 'blue'; }); @@ -63,7 +63,7 @@ describe('myReduce', function() { it("passes the entire array to the callback as argument 4", function testArrayPassing() { var resultingArray = []; - myReduce(testArr, function(_prev, _curr, index, arr) { + testArr.myReduce(function(_prev, _curr, 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 @@ -76,7 +76,7 @@ describe('myReduce', function() { it("it uses element 0 as the starting point (1st arg)", function() { var stachedValues = []; - myReduce(['a', 0, 0, 0], function(prev, _curr) { + ['a', 0, 0, 0].myReduce(function(prev, _curr) { stachedValues.push(prev); return 'x'; }); @@ -86,7 +86,7 @@ describe('myReduce', function() { it("it uses element 1 as the first currentValue (2nd argument)", function() { var stachedValues = []; - myReduce([0, 'b', 0], function(_prev, curr) { + [0, 'b', 0].myReduce(function(_prev, curr) { stachedValues.push(curr); return 'x'; }); @@ -96,7 +96,7 @@ describe('myReduce', function() { it("passes each element as the second argument to the callback", function testEachElem() { var resultingArray = []; - myReduce(testArr, function(_prev, curr) { + testArr.myReduce(function(_prev, curr) { resultingArray.push(curr); }); // compare elements in the result to the expected array @@ -107,7 +107,7 @@ describe('myReduce', function() { it("passes each index in the array to the callback as the 3rd arg", function testEachIndex() { var resultingArray = []; - myReduce(testArr, function(_prev, _curr, index) { + testArr.myReduce(function(_prev, _curr, index) { resultingArray.push(index); }); // compare elements in the result to expected array @@ -118,7 +118,7 @@ describe('myReduce', function() { it("the first index is 1", function() { var results = []; - myReduce(testArr, function(_prev, _next, index) { + testArr.myReduce(function(_prev, _next, index) { results.push(index); }); expect(results[0]).to.equal(1); @@ -130,7 +130,7 @@ describe('myReduce', function() { it("passes the initialValue in as the first argument to the callback on the first pass", function() { var result = []; - myReduce(testArr, function(previousValue) { + testArr.myReduce(function(previousValue) { result.push(previousValue); }, 192); expect(result[0]).to.equal(192); @@ -138,7 +138,7 @@ describe('myReduce', function() { it("the first index is 0", function() { var results = []; - myReduce(testArr, function(_prev, _next, index) { + testArr.myReduce(function(_prev, _next, index) { results.push(index); }, 'asdf'); expect(results[0]).to.equal(0); @@ -146,7 +146,7 @@ describe('myReduce', function() { it("works with arrays of length 0", function testArrayL0() { var resultingArray = []; - var result = myReduce([], function(item) { + var result = [].myReduce(function(item) { return '44'; }, 99); // compare elements in the result to expected array @@ -158,7 +158,7 @@ describe('myReduce', function() { function spyOnMe() {} var spy = chai.spy(spyOnMe); - myReduce([], spy, 11); + [].myReduce(spy, 11); expect(spy).to.not.have.been.called(); }); diff --git a/spec/mySomeSpec.js b/spec/mySomeSpec.js new file mode 100644 index 0000000..bcb0470 --- /dev/null +++ b/spec/mySomeSpec.js @@ -0,0 +1,88 @@ +/* This is the test file for mySome function + * PLEASE DO NOT EDIT THIS FILE + * To run these tests do `mocha spec/test-mySome.js` +*/ + +var mocha = require('mocha'); +var chai = require('chai'); +var spies = require('chai-spies'); +chai.use(spies); + +var expect = chai.expect; +chai.config.includeStack = false; // turn off stack trace +chai.config.showDiff = true; // turn on reporter diff display + +var mySome = require('../mySome'); + +describe('mySome', function() { + // sample data + beforeEach(function() { + testArr = [true, false, true, false]; + }); + + it("takes a function as the second argument and calls that function (callback)", function testCallback() { + function spyOnMe() {} + var spy = chai.spy(spyOnMe); + testArr.mySome(spy); + expect(spy).to.have.been.called(); + }); + + it("has a return value that is equal to the last return value of the callback", function() { + var results = testArr.mySome(function(){ + return 1000100; // on every pass + }); + console.log(' results: ', results); + expect(results).to.equal(true); + }); + + + it("returns a boolean, not an array", function() { + var results = testArr.mySome(function() { + return 'mercury'; + }, 'foo'); + console.log(' results: ', results); + expect(results).to.not.be.an('array'); + expect(results).to.not.be.an("undefined"); + }); + + it("returns true when the last item is true", function() { + testArr.push(true); + var result = testArr.mySome(function() { + return 'mercury'; + }, 'foo'); + console.log(' result: ', result); + expect(result).to.equal(true); + }); + + it("returns true when all items are true", function() { + newTestArr = [true,true,true,true]; + var result = newTestArr.mySome(function(value) { + return value; + }); + console.log(' result: ', result); + expect(result).to.equal(true); + }); + + it("returns false when all items are false", function() { + newTestArr = [false, false, false, false]; + var result = newTestArr.mySome(function(value) { + return value; + }); + console.log(' result: ', result); + expect(result).to.equal(false); + }); + + it("passes the entire array to the callback as argument 3", function testArrayPassing() { + var resultingArray = []; + testArr.mySome(function(_value, 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 + expect(arr).to.have.members([true, false, true, false]); + }); + }); + + + + +});