From 0b8a519161f1226d9ef0f7b32fcee96aa8878cc3 Mon Sep 17 00:00:00 2001 From: CruzMendez Date: Wed, 4 Oct 2017 18:40:43 -0500 Subject: [PATCH 1/2] first question --- src/services/array-functions.js | 13 ++++++++++--- src/services/calculations.js | 2 +- src/tests/array-functions.test.js | 12 ++++++++++++ src/tests/calculations.test.js | 24 +++++++++++++++++++++++- 4 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/services/array-functions.js b/src/services/array-functions.js index ff781d2..529d467 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); //fnc means functoin and this---> calls it then pass in the 'thing' + var returnedItem = fnc(currentItem); + newArray.push(returnedItem); //newArray[i] = (returnedItem) <---works too + } + return newArray; //outside of the loop so that it only happens once. } //create a new array 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..af3290e 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([3]); + }); +}); + +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 From 69886bb9c1d1de2c9b8625a45dafdf85da90b268 Mon Sep 17 00:00:00 2001 From: CruzMendez Date: Sat, 7 Oct 2017 14:40:37 -0500 Subject: [PATCH 2/2] Updated Find and Filter still having issues with terminal. missing link or bad syntax? --- src/services/array-functions.js | 30 +++++++++++++++++++++++++----- src/tests/array-functions.test.js | 2 +- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/services/array-functions.js b/src/services/array-functions.js index 529d467..66458b3 100644 --- a/src/services/array-functions.js +++ b/src/services/array-functions.js @@ -5,12 +5,12 @@ //add the returned value from fnc to 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 = [] + var newArray = []; for(var i = 0; i < theArray.length; i ++){ var currentItem = theArray[i]; - fnc(currentItem); //fnc means functoin and this---> calls it then pass in the 'thing' + fnc(currentItem); //pass in the 'thing' var returnedItem = fnc(currentItem); - newArray.push(returnedItem); //newArray[i] = (returnedItem) <---works too + newArray.push(returnedItem); //newArray[i] = (returnedItem) } return newArray; //outside of the loop so that it only happens once. } @@ -21,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; } @@ -30,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/tests/array-functions.test.js b/src/tests/array-functions.test.js index af3290e..c3733da 100644 --- a/src/tests/array-functions.test.js +++ b/src/tests/array-functions.test.js @@ -36,7 +36,7 @@ describe("map", () => { describe("filter", () => { it("should filter names with 3 letters", () => { - expect(find(names,findThree)).toEqual([3]); + expect(find(names,findThree)).toEqual(["Jon","Bob","Ted","Axe"]); }); });