From b8bfe98a778b16e008c798cf73e4c4fd174b258f Mon Sep 17 00:00:00 2001 From: Craig Copeland Date: Wed, 4 Oct 2017 18:49:16 -0500 Subject: [PATCH] Added calculation and array-function test cases --- src/services/array-functions.js | 57 +++++++++++++++++++++++++++++-- src/services/calculations.js | 4 +-- src/services/functions.js | 16 +++++++-- src/tests/array-functions.test.js | 32 +++++++++++++++-- src/tests/calculations.test.js | 20 +++++++++++ src/tests/function.test.js | 8 ++--- 6 files changed, 123 insertions(+), 14 deletions(-) diff --git a/src/services/array-functions.js b/src/services/array-functions.js index ff781d2..b7466c2 100644 --- a/src/services/array-functions.js +++ b/src/services/array-functions.js @@ -6,6 +6,12 @@ //return the new array export function map(theArray, fnc){ + const newArr = []; + for (let i=0; i=0; i--) { + newArr.push(theArray[i]); + } + return newArr; + } //create a new array @@ -51,6 +80,12 @@ export function reverse(theArray){ //return the new array export function tail(theArray){ + const newArr = []; + for (let i=1; i theArray[i + 1]) { + flip = true; + // using the swap without temp algorithm! Ex: theArray[i] = 10, theArray[i+1] = 6 + theArray[i+1] = theArray[i+1] - theArray[i]; //Ex. 6 - 10 = -4 + theArray[i] = theArray[i] + theArray[i+1]; //Ex. 10 + (-4) = 6 + theArray[i+1] = theArray[i] - theArray[i+1]; //Ex. 6 - (-4) = 10 + } + } + // flip? (flip = false, return bubbleSort(theArray)) : return(theArray); // doesn't work. + if (flip) { + flip = false; // setting my check + return sort(theArray); + } else { + return theArray; + } } \ No newline at end of file diff --git a/src/services/calculations.js b/src/services/calculations.js index 754df7c..24fed80 100644 --- a/src/services/calculations.js +++ b/src/services/calculations.js @@ -4,9 +4,9 @@ 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){ return num1 / num2; -} \ No newline at end of file +} diff --git a/src/services/functions.js b/src/services/functions.js index 2fa23a7..7d33a0d 100644 --- a/src/services/functions.js +++ b/src/services/functions.js @@ -7,6 +7,18 @@ return the theAfter */ export function after(times, theFunc){ + var counter=3; + + const theAfter = () => { + console.log('counter, times', counter, times); + if (counter === times) { + theFunc(); + } + }; + counter += 1; + console.log('counter, times outside', counter, times); + + return theAfter(); } @@ -19,7 +31,7 @@ export function after(times, theFunc){ return the theBefore */ export function before(times, theFunc){ - + return true; } /* @@ -33,5 +45,5 @@ return firstValue return theOnce */ export function once(theFunc){ - + return true; } \ No newline at end of file diff --git a/src/tests/array-functions.test.js b/src/tests/array-functions.test.js index 57ec72f..ca675f2 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,sort,filter,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]; @@ -13,7 +13,7 @@ function findBarney(name){ } //head should find the first element in the array "Jon" describe("head", () => { - it("should return the first element of an array 'Jon'", () => { + it("should return the first element of an array Jon", () => { expect(head(names)).toEqual("Jon"); }); }); @@ -37,21 +37,47 @@ describe("map", () => { 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("filter 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("find should find one name of Barney", () => { + expect(find(names, findBarney)).toEqual("Barney"); + }); +}); //findLast should find the last name of "Axe" +describe("findLast", () => { + it("findLast should find the last name of 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("reverse should return an array with the elements 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("tail should return all elements in an array except the first one", () => { + 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..a9217f5 100644 --- a/src/tests/calculations.test.js +++ b/src/tests/calculations.test.js @@ -5,3 +5,23 @@ describe("add", () => { expect(add(1, 2)).toBe(3); }); }); + +describe("subtract", () => { + it("should subtract 5 from 7 and return 2", () => { + expect(subtract(7, 5)).toBe(2); + }); +}); + + +describe("multiply", () => { + it("should multiply 3 and 4 and return 12", () => { + expect(multiply(3, 4)).toBe(12); + }); +}); + + +describe("divide", () => { + it("should divide 6 into 18 and return 3", () => { + expect(divide(18, 6)).toBe(3); + }); +}); \ No newline at end of file diff --git a/src/tests/function.test.js b/src/tests/function.test.js index e020aae..0a59fdf 100644 --- a/src/tests/function.test.js +++ b/src/tests/function.test.js @@ -8,10 +8,10 @@ describe("after", () => { it("should only call myFunc after it is called 4 times", () => { const myFunc = jest.fn(); let myAfter = after(4, myFunc); - myAfter(); - myAfter(); - myAfter(); - myAfter(); + myAfter; + myAfter; + myAfter; + myAfter; expect(myFunc.mock.calls.length).toBe(1); }); });