Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jon Thibeault #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
62 changes: 56 additions & 6 deletions src/services/array-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
//add the returned value from fnc to the new array
//return the new array
export function map(theArray, fnc){

var myArray = [];
var theArrayLength = theArray.length;
for (var i = 0; i < theArrayLength; i++) {
myArray[i]=fnc(theArray[i]);
}
return myArray;
}

//create a new array
Expand All @@ -14,7 +19,14 @@ 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 myArray = [];
var theArrayLength = theArray.length;
for (var i = 0; i < theArrayLength; i++) {
if (fnc(theArray[i])){
myArray.push(theArray[i]);
}
}
return myArray;
}


Expand All @@ -23,33 +35,52 @@ export function filter(theArray, fnc){
//fnc will return true or false, if true return the item
//return null
export function find(theArray, fnc){

var theArrayLength = theArray.length;
for (var i = 0; i < theArrayLength; i++) {
if (fnc(theArray[i])){
return theArray[i];
}
}
return null;
}


//return the last item in theArray
export function findLast(theArray){
var length = theArray.length;
return theArray[length-1];

}

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

var myArray = [];
var theArrayLength = theArray.length;
for (var i = theArrayLength-1; i >= 0; i--) {
myArray.push(theArray[i]);
}
return myArray;
}

//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){
var myArray = [];
var theArrayLength = theArray.length;
for (var i = 1; i < theArrayLength; i++) {
myArray.push(theArray[i]);
}
return myArray;

}

Expand All @@ -64,5 +95,24 @@ export function tail(theArray){
//after each for loop check the variable, if true, continue the while loop
//if false return theArray
export function sort(theArray){

function swap(a, b){
var temp = theArray[a];
theArray[a] = theArray[b];
theArray[b] = temp;
}
var flag;
var theArrayLength = theArray.length;
while(true){
flag = false;
for (var i = 0; i < theArrayLength; i++) {
if (theArray[i] > theArray[i+1]){
swap(i, i+1);
flag = true;
}
}
if (!flag){
break;
}
}
return theArray;
}
56 changes: 52 additions & 4 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 All @@ -18,7 +18,7 @@ describe("head", () => {
});
});


//map should call the function for each array element and add that functioned element to the array
describe("map", () => {
it("should prepend Hello to each name", () => {
expect(map(names,addHello)).toEqual([
Expand All @@ -34,24 +34,72 @@ describe("map", () => {
});
});

//sort should order the list of numbers ascending
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"]
describe("filter", () => {
it("should return names in array with length of 3", () => {
expect(filter(names,findThree)).toEqual([
"Jon",
"Bob",
"Ted",
"Axe"
]);
});
});

//find should find one name of "Barney"
describe("find", () => {
it("should call find Barney function and return the name 'Barney'", () => {
expect(find(names, findBarney)).toEqual("Barney");
});
});

//findLast should find the last name of "Axe"
describe("findLast", () => {
it("should return the last element of an array 'Axe'", () => {
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 the elements in the array in 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 of the array except the first", () => {
expect(tail(names)).toEqual([
"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, 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("multiple", () => {
it("should multiply 2 and 3 and return 6", () => {
expect(multiple(2, 3)).toBe(6);
});
});

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