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: 29 additions & 8 deletions src/services/array-functions.js
Original file line number Diff line number Diff line change
@@ -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){

const newArr = [];
for (let i=0; i<theArray.length; i++) {
const curr = theArray[i];
const returned = fnc(curr);
newArr.push(returned);
}
return newArr;
}

//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){

const newArr = [];
for (let i = 0; i < theArray.length; i++) {
const curr = theArray[i];
const returned = fnc(curr);
if(returned){
newArr.push(theArray[i]);
}
}
return newArr;
}


//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){

for (let i = 0; i < theArray.length; i++) {
const curr = theArray[i];
const returned = fnc(curr);
if(returned){
return theArray[i];
}
}
return null;
}


Expand Down Expand Up @@ -65,4 +86,4 @@ export function tail(theArray){
//if false return theArray
export function sort(theArray){

}
}
18 changes: 15 additions & 3 deletions src/tests/array-functions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,26 @@ 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("should find one name of Barney", () => {
expect(find(names,findBarney)).toEqual(
"Barney"
);
});
});
//findLast should find the last name of "Axe"

//reverse should return an array with the elements in the opposite order
//["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"];


21 changes: 20 additions & 1 deletion src/tests/calculations.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
import {add, subtract, multiply,divide} from "../services/calculations";
import {add, subtract, multiple,divide} from "../services/calculations";

describe("add", () => {
it("should add 1 and 2 and return 3", () => {
expect(add(1, 2)).toBe(3);
});
});


describe("subtract", () => {
it("should subtract 2 from 3 and return 1", () => {
expect(subtract(3,2)).toBe(1);
});
});

describe("multiply", () => {
it("should multiply 2 and 2 and return 4", () => {
expect(multiple(2,2)).toBe(4);
});
});

describe("divide", () => {
it("should divide 2 into 4 and return 2", () => {
expect(divide(4,2)).toBe(2);
});
});