diff --git a/src/services/array-functions.js b/src/services/array-functions.js index ff781d2..66458b3 100644 --- a/src/services/array-functions.js +++ b/src/services/array-functions.js @@ -1,11 +1,18 @@ //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 ( 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 +//return the new array (1.square brackets, 2.for loop, add item=push in js syntax, all these functions use 'loop') export function map(theArray, fnc){ - + var newArray = []; + for(var i = 0; i < theArray.length; i ++){ + var currentItem = theArray[i]; + fnc(currentItem); //pass in the 'thing' + var returnedItem = fnc(currentItem); + newArray.push(returnedItem); //newArray[i] = (returnedItem) + } + return newArray; //outside of the loop so that it only happens once. } //create a new array @@ -14,7 +21,17 @@ export function map(theArray, fnc){ //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){ - + var shortNamesArray = []; + for(var i = 0; i < theArray.length; i ++){ + var currentItem = theArray[i]; + fnc(currentItem); + var returnedItem = fnc(currentItem); + if (returnedItem = true) { + shortNamesArray.push(returnedItem); + } + } + // console.log(shortNameArray); + return shortNameArray; } @@ -23,7 +40,17 @@ export function filter(theArray, fnc){ //fnc will return true or false, if true return the item //return null export function find(theArray, fnc){ - + for(var i = 0; i < theArray.length; i ++){ + var currentItem = theArray[i]; + fnc(currentItem); + var returnedItem = fnc(currentItem); + if (returnedItem) { + return returnedItem; + } else { + return null; + } + } + // console.log(returnedItem); } diff --git a/src/services/calculations.js b/src/services/calculations.js index 754df7c..afcf333 100644 --- a/src/services/calculations.js +++ b/src/services/calculations.js @@ -4,7 +4,7 @@ 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){ diff --git a/src/tests/array-functions.test.js b/src/tests/array-functions.test.js index 57ec72f..c3733da 100644 --- a/src/tests/array-functions.test.js +++ b/src/tests/array-functions.test.js @@ -34,6 +34,18 @@ describe("map", () => { }); }); +describe("filter", () => { + it("should filter names with 3 letters", () => { + expect(find(names,findThree)).toEqual(["Jon","Bob","Ted","Axe"]); + }); +}); + +describe("find", () => { + it("should find Barney", () => { + expect(find(names,findBarney)).toEqual(["Barney"]); + }); +}); + describe("sort", () => { it("should return an array with numbers in order", () => { expect(sort(myNumbers)).toEqual([ diff --git a/src/tests/calculations.test.js b/src/tests/calculations.test.js index 409e055..e05e75f 100644 --- a/src/tests/calculations.test.js +++ b/src/tests/calculations.test.js @@ -2,6 +2,28 @@ 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); + var result = add(1, 2); + expect(result).toBe(3); }); }); + +describe("subtract", () => { + it("should subtract 3 and 2 and return 1", () => { + var result = subtract(3, 2); + expect(result).toBe(1); + }); +}); + +describe("multiply", () => { + it("should multiply 3 and 2 and return 6", () => { + var result = multiply(3, 2); + expect(result).toBe(6); + }); +}); + +describe("divide", () => { + it("should divide 6 and 3 and return 2", () => { + var result = divide(6, 3); + expect(result).toBe(2); + }); +}); \ No newline at end of file