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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.tabSize": 4
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

import { connect } from "react-redux";
import {addUser, removeUser} from "../actions"
import UserButtons from "../components/UserButtons"
import { addUser, removeUser } from "../../actions"
import UserButtons from "../../components/UserButtons"

function mapDispatchToProps(dispatch){
return{
Expand Down
56 changes: 52 additions & 4 deletions src/services/array-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
//add the returned value from fnc to the new array
//after looping, return the new array
export function map(theArray, fnc){
arr = [];

theArray.forEach(el => {
arr.push(fnc(el));
});

return arr;
}

//create a new array
Expand All @@ -17,7 +23,15 @@ 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){
arr = [];

theArray.forEach(el => {
if (fnc(el)) {
arr.push(fnc(el));
}
});

return arr;
}


Expand All @@ -26,36 +40,58 @@ export function filter(theArray, fnc){
//fnc will return true or false, if true return the item
//return null
export function find(theArray, fnc){
arr = [];

theArray.forEach(el => {
if (fnc(el)) {
arr.push(fnc(el));
}
});

return arr[0];
}


//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){
arr = [];

theArray.forEach(el => {
arr.unshift(el);
});

return arr;
}

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

arr = [];

theArray.forEach(el => {
arr.push(el);
});

arr.shift();
return arr;
}

// TOO COMPLICATED!:
//implement the most basic sorting algorithm there is
//assume the array will always have numbers
//use a while loop to constantly loop theArray until it is sorted
Expand All @@ -67,5 +103,17 @@ 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){

sortedArr = [];

theArray.forEach( (el, i, arr) => {
if (i == 0) {
sortedArr.push(el);
} else if (el >= arr[i - 1]) {
sortedArr.push(el);
} else {
sortedArr.unshift(el);
}
});

return sortedArr;
}
60 changes: 43 additions & 17 deletions src/services/functions.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,41 @@
/*
creates a function that invokes theFunc once it's called n or more times
in the after function, create a variable to be a counter
create a new function called theAfter in the after function
when theAfter is called increment the counter
if counter === times, call theFunc()
return the theAfter
*/
creates a function that invokes theFunc once it's called n or more times
in the after function, create a variable to be a counter
create a new function called theAfter in the after function
when theAfter is called increment the counter
if counter === times, call theFunc()
return the theAfter
*/
export function after(times, theFunc){
let i = 0;

function theAfter() {
i++;
if (i === times)
theFunc();
}

return theAfter;
}

/*
creates a function that invokes theFunc while it's called n or less times
create a variable to be a counter
create a new function called theBefore in the before function
when theBefore is called increment the counter
if counter <= times, call theFunc()
return the theBefore
*/
creates a function that invokes theFunc while it's called n or less times
create a variable to be a counter
create a new function called theBefore in the before function
when theBefore is called increment the counter
if counter <= times, call theFunc()
return the theBefore
*/
export function before(times, theFunc){
let i = 0;

function theBefore() {
i++;
if (i <= times)
theFunc();
}

return theBefore;
}

/*
Expand All @@ -28,10 +44,20 @@ Repeat calls to the function return the value of the first invocation.
create a variable called firstValue and set it to null
create a new function called theOnce
in theOnce check if firstValue is null,
if so call theFunc and assign the returned value into firstValue
if so call theFunc and assign the returned value into firstValue
return firstValue
return theOnce
*/
*/
export function once(theFunc){

let firstValue;

function theOnce() {
if (firstValue === null) {
firstValue = theFunc();
}

return firstValue;
}

return theOnce;
}
21 changes: 20 additions & 1 deletion src/tests/calculations.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
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("subtract", () => {
it("should subtract 4 from 5 and return 1", () => {
expect(subtract(5, 4)).toBe(1);
});
});

describe("multiply", () => {
it("should multiply 4 and 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);
});
});