From 85e20ce915faf25a4d51e43dab50b8bb96d57a56 Mon Sep 17 00:00:00 2001 From: wilymon Date: Wed, 17 Jan 2018 20:36:32 -0600 Subject: [PATCH] changes to the tests --- src/services/array-functions.js | 37 ++++++++++++++++++++++++------- src/services/calculations.js | 4 ++-- src/tests/array-functions.test.js | 16 ++++++++----- src/tests/calculations.test.js | 20 ++++++++++++++++- 4 files changed, 61 insertions(+), 16 deletions(-) diff --git a/src/services/array-functions.js b/src/services/array-functions.js index ff781d2..3e378e8 100644 --- a/src/services/array-functions.js +++ b/src/services/array-functions.js @@ -1,29 +1,50 @@ //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 convertedArray = []; + for (let i=0; i < theArray.length; i++){ + let element = theArray[i]; + let converted = fnc(element); + convertedArray.push(converted); + } + return convertedArray; } //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 filteredArray = []; + for (let i=0; i < theArray.length; i++){ + let element = theArray[i]; + if (fnc(element) === true){ + filteredArray.push(element); + } + } + return filteredArray; } -//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){ - + let filteredArray = ""; + for (let i=0; i < theArray.length; i++){ + let element = theArray[i]; + if (fnc(element) === true){ + filteredArray = element; + } + } + return filteredArray; } @@ -65,4 +86,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 171fffe..7275ad0 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]; @@ -42,10 +42,18 @@ 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("find should find one name of 'Barney'", () => { + expect(find(names, findBarney)).toEqual("Barney"); + }); +}); //findLast should find the last name of "Axe" @@ -53,5 +61,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..ac85e04 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, multiply, divide} from "../services/calculations"; describe("add", () => { it("should add 1 and 2 and return 3", () => { expect(add(1, 2)).toBe(3); }); }); + +describe("sub", () => { + it("should subtract 5 from 12 and return 7", () => { + expect(subtract(12, 5)).toBe(7); + }); +}); + +describe("multiply", () => { + it("should multiply 6 and 6, then return 36", () => { + expect(multiply(6, 6)).toBe(36); + }); +}); + +describe("divide", () => { + it("should 36 by 6 and return 6", () => { + expect(divide(36, 6)).toBe(6); + }); +});