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
56 changes: 48 additions & 8 deletions src/services/array-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
//add the returned value from fnc to the new array
//return the new array
export function map(theArray, fnc){

var result = [];
for (var i=0, j=theArray.length; i < j; i++) {
result.push(fnc(theArray[i]));
}
return result;
}

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


Expand All @@ -23,34 +33,43 @@ 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, j=theArray.length; i < j; i++) {
if (fnc(theArray[i])) {
return theArray[i];
}
}
return false;
}


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

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

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

return theArray.slice(1);
}

//implement the most basic sorting algorithm there is
Expand All @@ -64,5 +83,26 @@ 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){

var sorted = false;

while (sorted === false) {

var swapRequired = false;

for (var i=0, j=theArray.length; i<j; i++) {
if (theArray[i] > theArray[i+1]) {
var num = theArray[i];
theArray.splice(i, 1);
theArray.splice(i+1, 0, num);
swapRequired = true;
}
}

if (swapRequired === false) {
sorted = true;
}

}

return theArray;
}
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
46 changes: 38 additions & 8 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 {head,map,filter,sort,find,findLast,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 @@ -11,14 +11,15 @@ function findThree(name){
function findBarney(name){
return name === "Barney";
}

//head should find the first element in the array "Jon"
describe("head", () => {
it("should return the first element of an array 'Jon'", () => {
expect(head(names)).toEqual("Jon");
});
});


// map should perform a function on each individual element in an array
describe("map", () => {
it("should prepend Hello to each name", () => {
expect(map(names,addHello)).toEqual([
Expand All @@ -34,6 +35,19 @@ describe("map", () => {
});
});

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

describe("sort", () => {
it("should return an array with numbers in order", () => {
expect(sort(myNumbers)).toEqual([
Expand All @@ -42,16 +56,32 @@ describe("sort", () => {
});
});

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

//find should find one name of "Barney"
describe("find", () => {
it("should find one person whose name matches Barney", () => {
expect(find(names,findBarney)).toEqual("Barney");
});
});

//findLast should find the last name of "Axe"
describe("findLast", () => {
it("should find the last name in the list, which is 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"]
//tail should return all elements in an array except the first one
//[Bob","Ted","Barney","Lilly","Robin","Saul","Axe"];

describe("reverse", () => {
it("should return the array in reverse 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 the elements in the array except the first one", () => {
expect(tail(names)).toEqual(["Bob","Ted","Barney","Lilly","Robin","Saul","Axe"]);
});
});
18 changes: 18 additions & 0 deletions src/tests/calculations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,21 @@ describe("add", () => {
expect(add(1, 2)).toBe(3);
});
});

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

describe("multiply", () => {
it ("should multiply 4 times 5 and return 20", () => {
expect(multiply(4,5)).toBe(20);
});
});

describe("divide", () => {
it ("should divide 100 by 4 and return 25", () => {
expect(divide(100,4)).toBe(25);
});
});