From 9d7733e33f1ad26394a37d80b27ada8beb38d5d5 Mon Sep 17 00:00:00 2001 From: LilyCole Date: Mon, 12 Sep 2016 17:03:32 -0700 Subject: [PATCH 1/2] commits end of class result --- index.js | 46 ++++++++++++++++++++++++++++++++++------------ myEach.js | 6 +++--- myFilter.js | 9 +++++++++ myMap.js | 8 +++++--- myReduce.js | 28 ++++++++++++++++++++++++---- 5 files changed, 75 insertions(+), 22 deletions(-) create mode 100644 myFilter.js diff --git a/index.js b/index.js index 93cd82f..12411ad 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ var myEach = require('./myEach'); -var myMap = require('./myEach'); -var myReduce = require('./myEach'); +var myMap = require('./myMap'); +var myReduce = require('./myReduce'); /* ********************************************************************* You can edit this file It will make use of your code in myEach.js, myMap.js and myReduce.js @@ -13,21 +13,43 @@ var numArray = [0,1,10,100,1000]; /* myEach */ // -// myEach(numArray, function print(element, index, arr) { -// console.log('inside myEach', element, index, arr); -// }); +myEach(numArray, function print(element) { + // console.log('inside myEach', element, index, arr); + console.log(element); +}); /* myMap */ -// var input = ["a","b","c"]; -// var output = myMap(input, function capitalize(v){ -// return v.toUpperCase(); -// }); -// console.log('Testing myMap') -// console.log(output === ["A", "B", "C"]) // assertion - +var input = ["a","b","c"]; +var output = myMap(input, function capitalize(v){ + return v.toUpperCase(); +}); +console.log('Testing myMap, output: ',output); +console.log(output === ["A", "B", "C"]) // assertion + + +/* myReduce */ + +var reduceInput = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; +var stringInput = ["hi","hello","boo"]; +var reduceOutput = myReduce(stringInput, function add(previous, current) { + return previous + current; +}); +console.log('Testing myReduce, output: ',reduceOutput); + +/* myFilter */ + +var vowels = ["A", "E", "I", "O", "U"]; +function vowelFruit(fruit) { + var result = vowels.indexOf(fruit[0]) >= 0; // indexOf returns -1 if not found + console.log("result for " + fruit + " is " + result); + return result; +} +var vowelFruits = myFilter(fruits,vowelFruit); +console.log("vowelFruits: ",vowelFruits); +//=> ["Apple", "Elderberry", "Ice plant"] console.log("the end"); diff --git a/myEach.js b/myEach.js index e95bc02..0cdc5bb 100644 --- a/myEach.js +++ b/myEach.js @@ -1,9 +1,9 @@ // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/ForEach function myEach(arr, callback) { - -// CODE INSIDE HERE // - + for(var i=0;i1){ + for(var i=1;i1){ + for(var i=0;i Date: Mon, 12 Sep 2016 20:54:10 -0700 Subject: [PATCH 2/2] adds myEvery, mySome --- index.js | 45 ++++++++++++++++++++++++++++++++++++++++----- myEvery.js | 14 ++++++++++++++ myFilter.js | 14 ++++++++++---- mySome.js | 14 ++++++++++++++ 4 files changed, 78 insertions(+), 9 deletions(-) create mode 100644 myEvery.js create mode 100644 mySome.js diff --git a/index.js b/index.js index 12411ad..6577ca4 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,10 @@ var myEach = require('./myEach'); var myMap = require('./myMap'); var myReduce = require('./myReduce'); +var myFilter = require('./myFilter'); +var mySome = require('./mySome'); +var myEvery = require('./myEvery'); + /* ********************************************************************* You can edit this file It will make use of your code in myEach.js, myMap.js and myReduce.js @@ -12,6 +16,8 @@ var numArray = [0,1,10,100,1000]; /* myEach */ + +console.log('Testing myEach, output: '); // myEach(numArray, function print(element) { // console.log('inside myEach', element, index, arr); @@ -19,8 +25,6 @@ myEach(numArray, function print(element) { }); - - /* myMap */ var input = ["a","b","c"]; @@ -42,14 +46,45 @@ console.log('Testing myReduce, output: ',reduceOutput); /* myFilter */ +var fruits = ["Apple", "Banana", "Cherry", "Durian", "Elderberry", +"Fig", "Guava", "Huckleberry", "Ice plant", "Jackfruit"]; + var vowels = ["A", "E", "I", "O", "U"]; -function vowelFruit(fruit) { - var result = vowels.indexOf(fruit[0]) >= 0; // indexOf returns -1 if not found - console.log("result for " + fruit + " is " + result); +function vowelFruit(fruitCollection) { + var result = vowels.indexOf(fruitCollection[0]) >= 0; // indexOf returns -1 if not found + //console.log("result for " + fruitCollection + " is " + result); return result; } + var vowelFruits = myFilter(fruits,vowelFruit); console.log("vowelFruits: ",vowelFruits); //=> ["Apple", "Elderberry", "Ice plant"] + +/* mySome */ + +function isBiggerThan10(element, index, array) { + return element > 10; +} + +console.log('Testing mySome:'); +console.log("False: ",mySome([2, 5, 8, 1, 4],isBiggerThan10)); // false +console.log("True: ",mySome([12, 5, 8, 1, 4],isBiggerThan10)); // true +console.log("False: ",mySome([4],isBiggerThan10)); // false +console.log("False: ",mySome([10],isBiggerThan10)); // false +console.log("True: ",mySome([11],isBiggerThan10)); // true + console.log("the end"); + + +/* myEvery */ + +function isBigEnough(element, index, array) { + return element >= 10; +} + +console.log('Testing myEvery:'); +console.log("False: ",myEvery([12, 5, 8, 130, 44],isBigEnough)); // false +console.log("True: ",myEvery([12, 54, 18, 130, 44],isBigEnough)); // true +console.log("False: ",myEvery([5],isBigEnough)); // false +console.log("True: ",myEvery([44],isBigEnough)); // true diff --git a/myEvery.js b/myEvery.js new file mode 100644 index 0000000..3fc314d --- /dev/null +++ b/myEvery.js @@ -0,0 +1,14 @@ + + +function myEvery(arr, callback) { + var result = []; + for (var i = 0; i < arr.length; i++) { + if (!callback(arr[i], result, i, arr)) { + return false; + } + } + return true; +} + + +module.exports = myEvery; \ No newline at end of file diff --git a/myFilter.js b/myFilter.js index 154e438..f76171b 100644 --- a/myFilter.js +++ b/myFilter.js @@ -1,9 +1,15 @@ function myFilter(arr, callback) { - if(){ - array.push(callback(arr[i],i,arr)); - } - + var result = []; + for (var i = 0; i < arr.length; i++) { + var value = arr[i]; + if (callback(arr[i], value, i, arr)) { + result.push(value); + } + } + return result; } + + module.exports = myFilter; \ No newline at end of file diff --git a/mySome.js b/mySome.js new file mode 100644 index 0000000..e274d94 --- /dev/null +++ b/mySome.js @@ -0,0 +1,14 @@ + + +function mySome(arr, callback) { + var result = []; + for (var i = 0; i < arr.length; i++) { + if (callback(arr[i], result, i, arr)) { + return true; + } + } + return false; +} + + +module.exports = mySome; \ No newline at end of file