diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..68ebc15 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "editor.tabSize": 4 +} \ No newline at end of file diff --git a/src/containers/UserButtonsContainer.js b/src/containers/UserButtonsContainer/index.js similarity index 78% rename from src/containers/UserButtonsContainer.js rename to src/containers/UserButtonsContainer/index.js index 80124d6..50ea087 100755 --- a/src/containers/UserButtonsContainer.js +++ b/src/containers/UserButtonsContainer/index.js @@ -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{ diff --git a/src/services/array-functions.js b/src/services/array-functions.js index c6d987f..175a731 100644 --- a/src/services/array-functions.js +++ b/src/services/array-functions.js @@ -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 @@ -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; } @@ -26,18 +40,26 @@ 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 @@ -45,7 +67,13 @@ export function head(theArray){ //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 @@ -53,9 +81,17 @@ export function reverse(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 @@ -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; } diff --git a/src/services/functions.js b/src/services/functions.js index 2fa23a7..9ad44e3 100644 --- a/src/services/functions.js +++ b/src/services/functions.js @@ -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; } /* @@ -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; } \ No newline at end of file diff --git a/src/tests/calculations.test.js b/src/tests/calculations.test.js index 409e055..39f03b7 100644 --- a/src/tests/calculations.test.js +++ b/src/tests/calculations.test.js @@ -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); + }); +});