From 0f803d80f847901340d3d6a09208dba4fb609fa4 Mon Sep 17 00:00:00 2001 From: Andrew Sommere Date: Wed, 24 Apr 2019 12:44:18 -0500 Subject: [PATCH] Aca-Dash --- index.js | 64 ++++++++++++++++++++++++++++++++++++++++------------ package.json | 19 ++++++++++++++++ 2 files changed, 69 insertions(+), 14 deletions(-) create mode 100644 package.json diff --git a/index.js b/index.js index a32d484..23132a3 100644 --- a/index.js +++ b/index.js @@ -5,16 +5,24 @@ //`iteratee` must be a function that takes one parameter and returns a bool //The point of the javascript map function is to take an array and return different array with the exact same number of items in it //The items will be whatever the function `iteratee` creates -//in the map function create a new empty array and store in a variable named whatever you want (myNewArray) -//loop array and extract the single item from the array per loop and store it in a variable -//on each loop call iteratee() passing in the item from the current loop into the call to iteratee() +//-in the map function create a new empty array and store in a variable named whatever you want (myNewArray) +//-loop array and extract the single item from the array per loop and store it in a variable +//-on each loop call iteratee() passing in the item from the current loop into the call to iteratee() //iteratee is a function that must return something, capture whatever it returns in a variable //add the returned value from iteratee tp myNewArray //after looping, return myNewArray function map(array, iteratee){ - + myNewArray = []; + for(let i=0; i < array.lenght; i++){ + let arrayElement = array[i]; + let returnedValue = iteratee(arrayElement); + myNewArray.push(returnedValue); + } + return myNewArray; } + + //https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter //create a function called `filter`, it should take 2 parameters `array` and `iteratee` //`array` must be an array @@ -25,7 +33,13 @@ function map(array, iteratee){ //iteratee will return true or false, if true add the item to myNewArray else do not //after looping, return myNewArray function filter(array, iteratee){ - + let myNewArray = []; + for(let i = 0; i < array.length; i++){ + let arrayElement = array[i]; + if(iteratee(arrayElement) === true){ + myNewArray.push(arrayElement) + }; + } return myNewArray; } //https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find @@ -35,34 +49,45 @@ function filter(array, iteratee){ //fnc will return true or false, if true return the item //after looping, return null function find(theArray, fnc){ - + for (let i = 0; i < array.lenght; i++){ + if (fnc(theArray[i]) === true){ + return theArray[i]; + } + } return null; } //return the last item in theArray function findLast(theArray){ - + return theArray[theArray.length-1] } //return the first element of the array function head(theArray){ - -} + return theArray[0] +}; //create a new array //loop theArray in reverse order //add the item from each loop to the new array //return the new array function reverse(theArray){ - -} + let reversedArray = []; + for(let i= theArray.length-1; i > -1; i--){ + reversedArray.unshift(theArray[i]) + } return reversedArray +}; //create a new array //loop theArray //add the item from each loop to the new array except the first item //return the new array function tail(theArray){ - + let newArray = []; + for(let i =1; i < theArray.length;i++){ + newArray.push(theArray[i]); + } return newArray + } //implement the most basic sorting algorithm there is @@ -76,8 +101,19 @@ function tail(theArray){ //after each for loop check the variable, if true, continue the while loop //if false return theArray function sort(theArray){ - -} + //sorting function to sort the numbers from least to greatest; + function sortArray(a,b){ + if(a >b){ + return 1; + } else if(b>a){ + return -1; + } else { + return 0; + } + } return theArray.sort(sortArray); + + +}; exports.map = map; exports.filter = filter; diff --git a/package.json b/package.json new file mode 100644 index 0000000..9e40873 --- /dev/null +++ b/package.json @@ -0,0 +1,19 @@ +{ + "name": "andrew-dash", + "version": "1.0.0", + "description": "useful functions", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/AndrewSommer89/aca-dash.git" + }, + "author": "Andrew Sommer", + "license": "ISC", + "bugs": { + "url": "https://github.com/AndrewSommer89/aca-dash/issues" + }, + "homepage": "https://github.com/AndrewSommer89/aca-dash#readme" +}