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

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;
}


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

}
}
4 changes: 2 additions & 2 deletions src/services/calculations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
16 changes: 11 additions & 5 deletions src/tests/array-functions.test.js
Original file line number Diff line number Diff line change
@@ -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];

Expand Down Expand Up @@ -42,16 +42,22 @@ 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"

//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"];


20 changes: 19 additions & 1 deletion src/tests/calculations.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});