From 25a10b988c2238955b6afc0f17d8f4bba0fed87b Mon Sep 17 00:00:00 2001 From: Patrick Creamer Date: Mon, 22 Jan 2018 20:05:34 -0600 Subject: [PATCH] HW 1 --- src/services/array-functions.js | 105 +++++++++++++++++++++++++++--- src/tests/array-functions.test.js | 4 +- 2 files changed, 97 insertions(+), 12 deletions(-) diff --git a/src/services/array-functions.js b/src/services/array-functions.js index c6d987f..d680838 100644 --- a/src/services/array-functions.js +++ b/src/services/array-functions.js @@ -7,53 +7,80 @@ //fnc is a function that must return something, capture whatever it returns in a variable //add the returned value from fnc to the new array //after looping, return the new array -export function map(theArray, fnc){ +export function map(theArray, fnc){ + let output=[]; + for(i=0;i-1;i--){ + output.push(fnc(theArray[i])); + } + return output; } //create a new array //loop theArray //add the item from each loop to the new array except the first item //return the new array -export function tail(theArray){ +export function tail(theArray){ + theArray.splice(0,1); + return theArray; } //implement the most basic sorting algorithm there is @@ -66,6 +93,64 @@ export function tail(theArray){ //if a swap is done set it to true //after each for loop check the variable, if true, continue the while loop //if false return theArray + export function sort(theArray){ + let stillWorking = true; + + while (stillWorking==true){ + let swaps = 0; + for (i=0;i<(theArray.length-1);i++){ + if(theArray[i]>theArray[i+1]){ + let tempVal=theArray[i]; + theArray[i]=theArray[i+1]; + theArray[i+1]=tempVal; + swaps++; + } + + if (swaps>0){ + stillWorking=true; + }else { + stillWorking=false; + } + + } + } + return theArray; +} +const names = ["Jon","Bob","Ted","Barney","Lilly","Robin","Saul","Axe"]; +const myNumbers = [4,3,55,22,99,1913,7,5,4,2,1]; + +function addHello(name){ + return "Hello " + name; +} +function findThree(name){ + return name.length === 3; } +function findBarney(name){ + return name === "Barney"; +} + + + +console.log("should return the last element of an array 'Axe': "+findLast(names)); + +console.log("should return the first element of an array 'Jon': "+head(names)); + +console.log("should return the 7 names (everything after 'Jon'): "+tail(names)); + +console.log("Map (should append hello): "+map(names,addHello)); + +console.log("Numbers Raw: "+myNumbers+", Numbers sorted: "+sort(myNumbers)); + +//filter should return an array with names of length 3 +//["Jon","Bob","Ted","Axe"] + +//find should find one name of "Barney" + +//findLast should find the last name of "Axe" + +//reverse should return an array with the elements in the opposite order +//["Axe","Saul","Robin","Lilly","Barney","Ted","Bob","Jon"] +//tail should return all elements in an array except the first one +//[Bob","Ted","Barney","Lilly","Robin","Saul","Axe"]; diff --git a/src/tests/array-functions.test.js b/src/tests/array-functions.test.js index 171fffe..060c49e 100644 --- a/src/tests/array-functions.test.js +++ b/src/tests/array-functions.test.js @@ -11,6 +11,8 @@ function findThree(name){ function findBarney(name){ return name === "Barney"; } + + //head should find the first element in the array "Jon" describe("head", () => { it("should return the first element of an array 'Jon'", () => { @@ -53,5 +55,3 @@ describe("sort", () => { //["Axe","Saul","Robin","Lilly","Barney","Ted","Bob","Jon"] //tail should return all elements in an array except the first one //[Bob","Ted","Barney","Lilly","Robin","Saul","Axe"]; - -