From 2746650c3ce152aed41c25d994c36888aed747a2 Mon Sep 17 00:00:00 2001 From: Jordon R Date: Wed, 24 Jan 2018 12:41:36 -0600 Subject: [PATCH 1/4] Calculations test for add, subtract, multiply, and divide complete --- src/tests/calculations.test.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/tests/calculations.test.js b/src/tests/calculations.test.js index 409e055..68a50be 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 1 from 3 and return 2", () => { + expect(subtract(3,1)).toBe(2); + }) +}); + +describe("multiply", () => { + it("should multiply 2 and 3 and return 6", () => { + expect(multiple(2,3)).toBe(6); + }); +}); + +describe("divide", () => { + it("should divide 2 from 6 and return 3", () => { + expect(divide(6,2)).toBe(3); + }); +}); From 94829e1fe629c0552d9f2df2b9eda633073cc394 Mon Sep 17 00:00:00 2001 From: Jordon R Date: Wed, 24 Jan 2018 21:34:26 -0600 Subject: [PATCH 2/4] Finish map/filter/find/findLast --- src/services/array-functions.js | 44 ++++++++++++++++++++++--------- src/tests/array-functions.test.js | 26 ++++++++++++++++-- 2 files changed, 55 insertions(+), 15 deletions(-) diff --git a/src/services/array-functions.js b/src/services/array-functions.js index c6d987f..ab98dc0 100644 --- a/src/services/array-functions.js +++ b/src/services/array-functions.js @@ -8,32 +8,50 @@ //add the returned value from fnc to the new array //after looping, return the new array export function map(theArray, fnc){ - -} + const myNewArray = []; + for (let i=0; i { //filter should return an array with names of length 3 //["Jon","Bob","Ted","Axe"] +describe("filter", () => { + it("should return an array with names of length 3", () => { + expect(filter(names,findThree)).toEqual([ + "Jon","Bob","Ted","Axe" + ]); + }); +}); + //find should find one name of "Barney" +describe("find", () => { + it("should find one name of 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 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"] //tail should return all elements in an array except the first one //[Bob","Ted","Barney","Lilly","Robin","Saul","Axe"]; - - From 1af2b8d73fcfaa2504d4d7765a062d3a28f30be8 Mon Sep 17 00:00:00 2001 From: Jordon R Date: Fri, 26 Jan 2018 12:20:16 -0600 Subject: [PATCH 3/4] Swap up and working --- src/services/array-functions.js | 18 ++++++++++++++++-- src/tests/array-functions.test.js | 2 +- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/services/array-functions.js b/src/services/array-functions.js index ab98dc0..5a2defc 100644 --- a/src/services/array-functions.js +++ b/src/services/array-functions.js @@ -55,7 +55,7 @@ export function findLast(theArray){ //return the first element of the array export function head(theArray){ - + return theArray[0]; } //create a new array @@ -85,5 +85,19 @@ 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){ - + let swap = false; + for(let i=0;i theArray[i+1]) { + let temp = theArray[i]; + theArray[i] = theArray[i+1] + theArray[i+1] = temp; + swap = true; + } + } + if (swap) { + swap = false; + return sort(theArray); + } else { + return theArray; + } } diff --git a/src/tests/array-functions.test.js b/src/tests/array-functions.test.js index 2c24978..b07323f 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} 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]; From 68f88b9ea8be1a4c10a995b8716b8e3c0b0e28fe Mon Sep 17 00:00:00 2001 From: Jordon R Date: Sat, 27 Jan 2018 15:03:00 -0600 Subject: [PATCH 4/4] All array test passing --- src/services/array-functions.js | 12 ++++++++++-- src/tests/array-functions.test.js | 20 ++++++++++++++++---- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/services/array-functions.js b/src/services/array-functions.js index 5a2defc..3734dca 100644 --- a/src/services/array-functions.js +++ b/src/services/array-functions.js @@ -63,7 +63,11 @@ export function head(theArray){ //add the item from each loop to the new array //return the new array export function reverse(theArray){ - + let reverseArray = []; + for (let i = theArray.length-1; i>=0; i--){ + reverseArray.push(theArray[i]); + } + return reverseArray; } //create a new array @@ -71,7 +75,11 @@ 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){ - + let newArray = []; + for (let i=1; i { //filter should return an array with names of length 3 //["Jon","Bob","Ted","Axe"] - describe("filter", () => { it("should return an array with names of length 3", () => { expect(filter(names,findThree)).toEqual([ @@ -54,7 +53,6 @@ describe("filter", () => { }); //find should find one name of "Barney" - describe("find", () => { it("should find one name of Barney", () => { expect(find(names,findBarney)).toEqual( @@ -64,7 +62,6 @@ describe("find", () => { }); //findLast should find the last name of "Axe" - describe("findLast", () => { it("should find the last name in the array - Axe", () => { expect(findLast(names)).toEqual( @@ -75,5 +72,20 @@ describe("findLast", () => { //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 an array with the elemens in the 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 in an array except the first one", () => { + expect(tail(names)).toEqual([ + "Bob","Ted","Barney","Lilly","Robin","Saul","Axe" + ]); + }); +});