From 6f51918e6a2dbee6bb04f3dc6f054559c4839d14 Mon Sep 17 00:00:00 2001 From: jmwhitman7 Date: Mon, 9 Oct 2017 20:35:22 -0500 Subject: [PATCH] functions projects --- src/services/array-functions.js | 60 +++++++++++++++++++++---------- src/services/calculations.js | 4 +-- src/tests/array-functions.test.js | 13 +++++-- src/tests/calculations.test.js | 30 ++++++++++++++-- 4 files changed, 82 insertions(+), 25 deletions(-) diff --git a/src/services/array-functions.js b/src/services/array-functions.js index ff781d2..0e1b38c 100644 --- a/src/services/array-functions.js +++ b/src/services/array-functions.js @@ -1,48 +1,70 @@ -//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, -// passing in the item from the current loop into the call to fnc +//in the function map, create a new array '[]'and store in a variable +//loop 'for' theArray and call 'fnc()' the fnc for each thing in the array, +// passing 'push' 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){ +export function map(names, fnc){ + var mapped = []; + for (var i = 0; i < names.length; i++){ + mapped.push("Hello " + names[i]); + } + return mapped; } + //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){ - +export function filter(names, fnc){ +const filter = []; + for (var i = 0; i < names.length; i++){ + var functionThing = fnc(names[i]); + if (functionThing === true ){ + filter.push(names[i]); + } + + }; + return filter; } - -//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){ - +export function find(names, fnc){ + for (var i = 0; i < names.length; i++){ + var functionThing = fnc(names[i]); + if (functionThing === true ){ + return names[i]; + } + return null; + }; } //return the last item in theArray -export function findLast(theArray){ - +export function findLast(names){ + const lastItem = [names].pop(); } //return the first element of the array -export function head(theArray){ - +export function head(names){ + const firstItem } //create a new array //loop theArray in reverse order //add the item from each loop to the new array //return the new array -export function reverse(theArray){ - +export function reverse(names){ + const reverseArray = []; + for (var i = arr.length - 1; i >= 0; --i) { + + } } //create a new array @@ -65,4 +87,4 @@ export function tail(theArray){ //if false return theArray export function sort(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/tests/array-functions.test.js b/src/tests/array-functions.test.js index 57ec72f..ca44065 100644 --- a/src/tests/array-functions.test.js +++ b/src/tests/array-functions.test.js @@ -34,6 +34,17 @@ describe("map", () => { }); }); +describe("filter", () => { + it("should filter each name that has three letters", () => { + expect(filter(names,findThree)).toEqual([ + "Jon", + "Bob", + "Ted", + "Axe" + ]); + }); +}); + describe("sort", () => { it("should return an array with numbers in order", () => { expect(sort(myNumbers)).toEqual([ @@ -53,5 +64,3 @@ describe("sort", () => { //["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"]; - - diff --git a/src/tests/calculations.test.js b/src/tests/calculations.test.js index 409e055..7aff3ac 100644 --- a/src/tests/calculations.test.js +++ b/src/tests/calculations.test.js @@ -1,7 +1,33 @@ -import {add, subtract, multiply,divide} from "../services/calculations"; +import {add, subtract, multiply, divide} from "../services/calculations"; +// ADD describe("add", () => { it("should add 1 and 2 and return 3", () => { - expect(add(1, 2)).toBe(3); + var result = add(1, 2); + expect(result).toBe(3); + }); +}); + +// Subtract +describe("subtract", () => { + it("should subtract 3 against 2 and return 1", () => { + var result = subtract(3, 2); + expect(result).toBe(1); + }); +}); + +// Multiply +describe("multiply", () => { + it("should multiply 2 and 3 and return 6", () => { + var result = multiply(2, 3); + expect(result).toBe(6); + }); +}); + +// Divide +describe("divide", () => { + it("should divide 6 by 2 and return 3", () => { + var result = divide(6, 2); + expect(result).toBe(3); }); });