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
69 changes: 52 additions & 17 deletions src/services/array-functions.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,82 @@

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

export function map (theArray, fnc) {
let finalArr = [];
for (let i = 0; i < theArray.length; i++) {
finalArr.push(fnc(theArray[i]));
}
return finalArr;
}

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

export function filter (theArray, fnc) {
let finalArr = [];
for (let i = 0; i < theArray.length; i++) {
if(fnc(theArray[i])) {
finalArr.push(theArray[i]);
}
}
return finalArr;
}


//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 finalArr = [];
for (let i = 0; i < theArray.length; i++) {
if(fnc(theArray[i])) {
finalArr.push(theArray[i]);
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 this. the instructions do not tell you to add anything to an array
fnc will return true or false, if true return the item

}
}
return finalArr;
}


//return the last item in theArray
export function findLast(theArray){

let newArr = [];
newArr.push(theArray[theArray.length - 1]);
return newArr;
}

//return the first element of the array
export function head(theArray){

return theArray[0];
}

//create a new array
//loop theArray in reverse order
//add the item from each loop to the new array
//return the new array
export function reverse(theArray){

let finalArr = [];
for (let i = theArray.length - 1; i >= 0; i--) {
finalArr.push(theArray[i]);
}
return finalArr;
}

//create a new array
//loop theArray
//add the item from each loop to the new array except the first item
//return the new array
export function tail(theArray){

export function tail(theArray) {
let finalArr = [];
for (let i = 1; i < theArray.length; i++) {
finalArr.push(theArray[i]);
}
return finalArr;
}

//implement the most basic sorting algorithm there is
Expand All @@ -63,6 +89,15 @@ export function tail(theArray){
//if a swap is done set it to true
//after each for loop check the variable, if true, continue the while loop
//if false return theArray
export function sort(theArray){

}
export function sort (theArray) {
let finalArr = theArray;
for (let i = 0; i < finalArr.length - 1; i++) {
if (finalArr[i] > finalArr[i + 1]) {
let temp = finalArr[i];
finalArr[i] = finalArr[i + 1];
finalArr[i + 1] = temp;
i = -1;
}
}
return finalArr;
}
65 changes: 58 additions & 7 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,reverse,tail} 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 @@ -34,24 +34,75 @@ describe("map", () => {
});
});

//filter should return an array with names of length 3
//["Jon","Bob","Ted","Axe"]
describe("filter", () => {
it("should filter the array for names equal to 3 letters", () => {
expect(filter(names,findThree)).toEqual([
"Jon",
"Bob",
"Ted",
"Axe"
]);
});
});

//find should find one name of "Barney"
describe("find", () => {
it("should find the name Barney", () => {
expect(filter(names,findBarney)).toEqual([
"Barney"
]);
});
});

describe("sort", () => {
it("should return an array with numbers in order", () => {
expect(sort(myNumbers)).toEqual([
1,2,3,4,5,7,22,55,99,1913
1,2,3,4,4,5,7,22,55,99,1913
]);
});
});

//filter should return an array with names of length 3
//["Jon","Bob","Ted","Axe"]

//find should find one name of "Barney"

//findLast should find the last name of "Axe"
describe("findLast", () => {
it("should find the last name in the array", () => {
expect(findLast(names)).toEqual(["Axe"]);
});
});


//reverse should return an array with the elements in the opposite order
//["Axe","Saul","Robin","Lilly","Barney","Ted","Bob","Jon"]
describe("reverse", () => {
it("should return an array with elements in the opposite order", () => {
expect(reverse(names)).toEqual([
"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"];


describe("tail", () => {
it("should return all elements in an array except the first one", () => {
expect(tail(names)).toEqual([
"Bob",
"Ted",
"Barney",
"Lilly",
"Robin",
"Saul",
"Axe"
]);
});
});
26 changes: 25 additions & 1 deletion src/tests/calculations.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
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 and 1 and return 1", () => {
expect(subtract(2, 1)).toBe(1);
});
});

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

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

describe("extra", () => {
it("this is an extra test", () => {
expect(true).toBe(true);
});
});