diff --git a/src/services/array-functions.js b/src/services/array-functions.js index ff781d2..89857cd 100644 --- a/src/services/array-functions.js +++ b/src/services/array-functions.js @@ -5,7 +5,12 @@ //add the returned value from fnc to the new array //return the new array export function map(theArray, fnc){ - + var myArray = []; + var theArrayLength = theArray.length; + for (var i = 0; i < theArrayLength; i++) { + myArray[i]=fnc(theArray[i]); + } + return myArray; } //create a new array @@ -14,7 +19,14 @@ export function map(theArray, fnc){ //fnc will return true or false, if true add the item to the new array else do not //return the new array export function filter(theArray, fnc){ - + var myArray = []; + var theArrayLength = theArray.length; + for (var i = 0; i < theArrayLength; i++) { + if (fnc(theArray[i])){ + myArray.push(theArray[i]); + } + } + return myArray; } @@ -23,18 +35,26 @@ export function filter(theArray, fnc){ //fnc will return true or false, if true return the item //return null export function find(theArray, fnc){ - + var theArrayLength = theArray.length; + for (var i = 0; i < theArrayLength; i++) { + if (fnc(theArray[i])){ + return theArray[i]; + } + } + return null; } //return the last item in theArray export function findLast(theArray){ + var length = theArray.length; + return theArray[length-1]; } //return the first element of the array export function head(theArray){ - + return theArray[0]; } //create a new array @@ -42,7 +62,12 @@ export function head(theArray){ //add the item from each loop to the new array //return the new array export function reverse(theArray){ - + var myArray = []; + var theArrayLength = theArray.length; + for (var i = theArrayLength-1; i >= 0; i--) { + myArray.push(theArray[i]); + } + return myArray; } //create a new array @@ -50,6 +75,12 @@ export function reverse(theArray){ //add the item from each loop to the new array except the first item //return the new array export function tail(theArray){ + var myArray = []; + var theArrayLength = theArray.length; + for (var i = 1; i < theArrayLength; i++) { + myArray.push(theArray[i]); + } + return myArray; } @@ -64,5 +95,24 @@ export function tail(theArray){ //after each for loop check the variable, if true, continue the while loop //if false return theArray export function sort(theArray){ - + function swap(a, b){ + var temp = theArray[a]; + theArray[a] = theArray[b]; + theArray[b] = temp; + } + var flag; + var theArrayLength = theArray.length; + while(true){ + flag = false; + for (var i = 0; i < theArrayLength; i++) { + if (theArray[i] > theArray[i+1]){ + swap(i, i+1); + flag = true; + } + } + if (!flag){ + break; + } + } + return theArray; } \ No newline at end of file diff --git a/src/tests/array-functions.test.js b/src/tests/array-functions.test.js index 57ec72f..a77678c 100644 --- a/src/tests/array-functions.test.js +++ b/src/tests/array-functions.test.js @@ -1,4 +1,4 @@ -import {map,filter,find,findLast} from "../services/array-functions"; +import {map,filter,find,findLast,head,sort,reverse,tail} from "../services/array-functions"; const names = ["Jon","Bob","Ted","Barney","Lilly","Robin","Saul","Axe"]; const myNumbers = [4,3,55,22,99,1913,7,5,4,2,1]; @@ -18,7 +18,7 @@ describe("head", () => { }); }); - +//map should call the function for each array element and add that functioned element to the array describe("map", () => { it("should prepend Hello to each name", () => { expect(map(names,addHello)).toEqual([ @@ -34,24 +34,72 @@ describe("map", () => { }); }); +//sort should order the list of numbers ascending describe("sort", () => { it("should return an array with numbers in order", () => { expect(sort(myNumbers)).toEqual([ - 1,2,3,4,5,7,22,55,99,1913 + 1,2,3,4,4,5,7,22,55,99,1913 ]); }); }); //filter should return an array with names of length 3 //["Jon","Bob","Ted","Axe"] +describe("filter", () => { + it("should return names in array with length of 3", () => { + expect(filter(names,findThree)).toEqual([ + "Jon", + "Bob", + "Ted", + "Axe" + ]); + }); +}); //find should find one name of "Barney" +describe("find", () => { + it("should call find Barney function and return the name 'Barney'", () => { + expect(find(names, findBarney)).toEqual("Barney"); + }); +}); //findLast should find the last name of "Axe" +describe("findLast", () => { + it("should return the last element of an array 'Axe'", () => { + expect(findLast(names)).toEqual("Axe"); + }); +}); //reverse should return an array with the elements in the opposite order //["Axe","Saul","Robin","Lilly","Barney","Ted","Bob","Jon"] +describe("reverse", () => { + it("should return the elements in the array in opposite order", () => { + expect(reverse(names)).toEqual([ + "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"]; - +describe("tail", () => { + it("should return all elements of the array except the first", () => { + expect(tail(names)).toEqual([ + "Bob", + "Ted", + "Barney", + "Lilly", + "Robin", + "Saul", + "Axe" + ]); + }); +}); diff --git a/src/tests/calculations.test.js b/src/tests/calculations.test.js index 409e055..4702a17 100644 --- a/src/tests/calculations.test.js +++ b/src/tests/calculations.test.js @@ -1,7 +1,25 @@ -import {add, subtract, multiply,divide} from "../services/calculations"; +import {add, subtract, multiple,divide} from "../services/calculations"; describe("add", () => { it("should add 1 and 2 and return 3", () => { expect(add(1, 2)).toBe(3); }); }); + +describe("subtract", () => { + it("should subtract 2 from 3 and return 1", () => { + expect(subtract(3, 2)).toBe(1); + }); +}); + +describe("multiple", () => { + it("should multiply 2 and 3 and return 6", () => { + expect(multiple(2, 3)).toBe(6); + }); +}); + +describe("divide", () => { + it("should divide 3 from 6 and return 2", () => { + expect(divide(6, 3)).toBe(2); + }); +});