diff --git a/src/services/array-functions.js b/src/services/array-functions.js index ff781d2..cf7d019 100644 --- a/src/services/array-functions.js +++ b/src/services/array-functions.js @@ -5,7 +5,11 @@ //add the returned value from fnc to the new array //return the new array export function map(theArray, fnc){ - + var result = []; + for (var i=0, j=theArray.length; i < j; i++) { + result.push(fnc(theArray[i])); + } + return result; } //create a new array @@ -14,7 +18,13 @@ 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 result = []; + for (var i=0, j=theArray.length; i < j; i++) { + if (fnc(theArray[i])) { + result.push(theArray[i]); + } + } + return result; } @@ -23,18 +33,23 @@ export function filter(theArray, fnc){ //fnc will return true or false, if true return the item //return null export function find(theArray, fnc){ - + for (var i=0, j=theArray.length; i < j; i++) { + if (fnc(theArray[i])) { + return theArray[i]; + } + } + return false; } //return the last item in theArray export function findLast(theArray){ - + return theArray[theArray.length - 1]; } //return the first element of the array export function head(theArray){ - + return theArray[0]; } //create a new array @@ -42,7 +57,11 @@ export function head(theArray){ //add the item from each loop to the new array //return the new array export function reverse(theArray){ - + var result = []; + for (var i = theArray.length - 1; i >= 0; i--) { + result.push(theArray[i]); + } + return result; } //create a new array @@ -50,7 +69,7 @@ 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){ - + return theArray.slice(1); } //implement the most basic sorting algorithm there is @@ -64,5 +83,26 @@ 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){ - + var sorted = false; + + while (sorted === false) { + + var swapRequired = false; + + for (var i=0, j=theArray.length; i theArray[i+1]) { + var num = theArray[i]; + theArray.splice(i, 1); + theArray.splice(i+1, 0, num); + swapRequired = true; + } + } + + if (swapRequired === false) { + sorted = true; + } + + } + + return theArray; } \ No newline at end of file diff --git a/src/services/calculations.js b/src/services/calculations.js index 754df7c..afcf333 100644 --- a/src/services/calculations.js +++ b/src/services/calculations.js @@ -4,7 +4,7 @@ export function add(num1, num2){ export function subtract(num1, num2){ return num1 - num2; } -export function multiple(num1, num2){ +export function multiply(num1, num2){ return num1 * num2; } export function divide(num1, num2){ diff --git a/src/tests/array-functions.test.js b/src/tests/array-functions.test.js index 171fffe..868749d 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 {head,map,filter,sort,find,findLast,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]; @@ -11,6 +11,7 @@ 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'", () => { @@ -18,7 +19,7 @@ describe("head", () => { }); }); - +// map should perform a function on each individual element in an array describe("map", () => { it("should prepend Hello to each name", () => { expect(map(names,addHello)).toEqual([ @@ -34,6 +35,19 @@ describe("map", () => { }); }); +//filter should return an array with names of length 3 +//["Jon","Bob","Ted","Axe"] +describe("filter", () => { + it("should return an array of only the names that are 3 letters long", () => { + expect(filter(names,findThree)).toEqual([ + "Jon", + "Bob", + "Ted", + "Axe" + ]); + }); +}); + describe("sort", () => { it("should return an array with numbers in order", () => { expect(sort(myNumbers)).toEqual([ @@ -42,16 +56,32 @@ describe("sort", () => { }); }); -//filter should return an array with names of length 3 -//["Jon","Bob","Ted","Axe"] - //find should find one name of "Barney" +describe("find", () => { + it("should find one person whose name matches Barney", () => { + expect(find(names,findBarney)).toEqual("Barney"); + }); +}); //findLast should find the last name of "Axe" +describe("findLast", () => { + it("should find the last name in the list, which is 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"] -//tail should return all elements in an array except the first one -//[Bob","Ted","Barney","Lilly","Robin","Saul","Axe"]; - +describe("reverse", () => { + it("should return the array in reverse 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 the elements in the array except the first one", () => { + expect(tail(names)).toEqual(["Bob","Ted","Barney","Lilly","Robin","Saul","Axe"]); + }); +}); \ No newline at end of file diff --git a/src/tests/calculations.test.js b/src/tests/calculations.test.js index 409e055..a5ac068 100644 --- a/src/tests/calculations.test.js +++ b/src/tests/calculations.test.js @@ -5,3 +5,21 @@ describe("add", () => { expect(add(1, 2)).toBe(3); }); }); + +describe("subtract", () => { + it("should subtract 4 from 5 and return 1", () => { + expect(subtract(5, 4)).toBe(1); + }); +}); + +describe("multiply", () => { + it ("should multiply 4 times 5 and return 20", () => { + expect(multiply(4,5)).toBe(20); + }); +}); + +describe("divide", () => { + it ("should divide 100 by 4 and return 25", () => { + expect(divide(100,4)).toBe(25); + }); +}); \ No newline at end of file