Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions src/services/array-functions.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double check you code here.
how do you compare things to true or false in an if statement

shortNamesArray.push(returnedItem);
}
}
// console.log(shortNameArray);
return shortNameArray;
}


Expand All @@ -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 {
Copy link
Contributor

@jw56578 jw56578 Oct 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in general, you don't ever want to have a else statement return in a loop

return null;
}
}
// console.log(returnedItem);
}


Expand Down
2 changes: 1 addition & 1 deletion src/services/calculations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down
12 changes: 12 additions & 0 deletions src/tests/array-functions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
24 changes: 23 additions & 1 deletion src/tests/calculations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});