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
57 changes: 54 additions & 3 deletions src/services/array-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
//return the new array
export function map(theArray, fnc){

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

}

//create a new array
Expand All @@ -15,26 +21,43 @@ export function map(theArray, fnc){
//return the new array
export function filter(theArray, fnc){

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

}

//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
//return null
export function find(theArray, fnc){

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

}

//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
Expand All @@ -43,6 +66,12 @@ export function head(theArray){
//return the new array
export function reverse(theArray){

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

}

//create a new array
Expand All @@ -51,6 +80,12 @@ export function reverse(theArray){
//return the new array
export function tail(theArray){

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

}

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

let flip = false;
for (let i = 0; i< theArray.length; i++){
if (theArray[i] > theArray[i + 1]) {
flip = true;
// using the swap without temp algorithm! Ex: theArray[i] = 10, theArray[i+1] = 6
theArray[i+1] = theArray[i+1] - theArray[i]; //Ex. 6 - 10 = -4
theArray[i] = theArray[i] + theArray[i+1]; //Ex. 10 + (-4) = 6
theArray[i+1] = theArray[i] - theArray[i+1]; //Ex. 6 - (-4) = 10
}
}
// flip? (flip = false, return bubbleSort(theArray)) : return(theArray); // doesn't work.
if (flip) {
flip = false; // setting my check
return sort(theArray);
} else {
return 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: 14 additions & 2 deletions src/services/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
return the theAfter
*/
export function after(times, theFunc){
var counter=3;

const theAfter = () => {
console.log('counter, times', counter, times);
if (counter === times) {
theFunc();
}
};
counter += 1;
console.log('counter, times outside', counter, times);

return theAfter();

}

Expand All @@ -19,7 +31,7 @@ export function after(times, theFunc){
return the theBefore
*/
export function before(times, theFunc){

return true;
}

/*
Expand All @@ -33,5 +45,5 @@ return firstValue
return theOnce
*/
export function once(theFunc){

return true;
}
32 changes: 29 additions & 3 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,sort,filter,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 @@ -13,7 +13,7 @@ function findBarney(name){
}
//head should find the first element in the array "Jon"
describe("head", () => {
it("should return the first element of an array 'Jon'", () => {
it("should return the first element of an array Jon", () => {
expect(head(names)).toEqual("Jon");
});
});
Expand All @@ -37,21 +37,47 @@ describe("map", () => {
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("filter 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"
describe("findLast", () => {
it("findLast should find the last name of 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("reverse should return an array with the 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("tail should return all elements in an array except the first one", () => {
expect(tail(names)).toEqual(["Bob","Ted","Barney","Lilly","Robin","Saul","Axe"]);
});
});


20 changes: 20 additions & 0 deletions src/tests/calculations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,23 @@ describe("add", () => {
expect(add(1, 2)).toBe(3);
});
});

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


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


describe("divide", () => {
it("should divide 6 into 18 and return 3", () => {
expect(divide(18, 6)).toBe(3);
});
});
8 changes: 4 additions & 4 deletions src/tests/function.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ describe("after", () => {
it("should only call myFunc after it is called 4 times", () => {
const myFunc = jest.fn();
let myAfter = after(4, myFunc);
myAfter();
myAfter();
myAfter();
myAfter();
myAfter;
myAfter;
myAfter;
myAfter;
expect(myFunc.mock.calls.length).toBe(1);
});
});
Expand Down