From 00d756abe9524663c973043a1bd54d39ceb90a0b Mon Sep 17 00:00:00 2001 From: Mar10God1 Date: Fri, 6 Oct 2017 12:58:42 -0500 Subject: [PATCH] testing practice --- src/services/array-functions.js | 68 +++++++++++++++++++++++++------ src/tests/array-functions.test.js | 34 ++++++++++++---- src/tests/calculations.test.js | 20 ++++++++- 3 files changed, 101 insertions(+), 21 deletions(-) diff --git a/src/services/array-functions.js b/src/services/array-functions.js index ff781d2..ca031da 100644 --- a/src/services/array-functions.js +++ b/src/services/array-functions.js @@ -1,40 +1,56 @@ //in the function map, create a new array and store in a variable -//loop theArray and call the fnc for each thing in the array, +//loop theArray and call the fnc for each thing in the array, // passing in the item from the current loop into the call to fnc //add the returned value from fnc to the new array //return the new array export function map(theArray, fnc){ - + let newArray = []; + for(let i = 0; i < theArray.length; i++) { + newArray.push(fnc(theArray[i])); + } + return newArray; } //create a new array -//loop theArray and call the fnc for each thing in the array, +//loop theArray and call the fnc for each thing in the array, // passing in the item from the current loop //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){ - + let newArray = []; + for(let i = 0; i < theArray.length; i++) { + if(fnc(theArray[i])) { + newArray.push(theArray[i]); + } + } + return newArray; } -//loop theArray and call the fnc for each thing in the array, +//loop theArray and call the fnc for each thing in the array, // passing in the item from the current loop -//fnc will return true or false, if true return the item +//fnc will return true or false, if true return the item //return null export function find(theArray, fnc){ - + for(let i = 0; i < theArray.length; i++) { + if (fnc(theArray[i])) { + return true; + } + } } //return the last item in theArray export function findLast(theArray){ - + let last = theArray[theArray.length -1]; + return last; } //return the first element of the array export function head(theArray){ - + let first = theArray[0]; + return first; } //create a new array @@ -42,16 +58,26 @@ export function head(theArray){ //add the item from each loop to the new array //return the new array export function reverse(theArray){ - + let newArray = []; + for(let i = theArray.length -1; i >= 0; i--) { + newArray.push(theArray[i]); + } + return newArray; } + //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){ + let newArray = []; + for(let i = 1; i < theArray.length; i++) { + newArray.push(theArray[i]); + } + return newArray; + } -} //implement the most basic sorting algorithm there is //assume the array will always have numbers @@ -64,5 +90,21 @@ 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){ - -} \ No newline at end of file + let arr = theArray; + let sorted; + while (!sorted) { + sorted = true; + for (let i = 0; i < arr.length -1; i++) { + if (arr[i] > arr[i +1]) { + sorted = false; + let temp; + temp = arr[i]; + arr[i] = arr[i + 1]; + arr[i + 1] = temp; + } + } + if (sorted) { + return arr; + } + } +} diff --git a/src/tests/array-functions.test.js b/src/tests/array-functions.test.js index 57ec72f..146c750 100644 --- a/src/tests/array-functions.test.js +++ b/src/tests/array-functions.test.js @@ -1,6 +1,6 @@ -import {map,filter,find,findLast} from "../services/array-functions"; +import {map,filter,find,findLast,reverse,tail,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]; +const myNumbers = [3,55,22,99,1913,7,5,4,2,1]; function addHello(name){ return "Hello " + name; @@ -44,14 +44,34 @@ describe("sort", () => { //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(true); + }); +}); //findLast should find the last name of "Axe" - +describe("findLast", () => { + it("should find the name of 'Axe'", () => { + expect(findLast(names, findLast)).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 an array with the elements in the opposite order", () => { + expect(reverse(names, reverse)).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, tail)).toEqual(["Bob","Ted","Barney","Lilly","Robin","Saul","Axe"]); + }); +}); diff --git a/src/tests/calculations.test.js b/src/tests/calculations.test.js index 409e055..1bdbbdf 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 2 and return 1", () => { + expect(subtract(2, 1)).toBe(1); + }); +}); + +describe("multiple", () => { + it("should multiply 3 by 5 and return 15", () => { + expect(multiple(3, 5)).toBe(15); + }); +}); + +describe("divide", () => { + it("should divide 4 by 2 and return 2", () => { + expect(divide(4, 2)).toBe(2); + }); +});