diff --git a/README.md b/README.md index cc7118f..1860089 100755 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ ### Add Calculation Tests * subtracts 4 from 5 to equal 1 * multiply 4 by 5 to equal 20 -* device 100 by 4 to equal 25 +* divide 100 by 4 to equal 25 ### Add Function Tests * Implement the tests as described in the comments @@ -48,13 +48,10 @@ ### Hopefully at this point you can see there is a pattern to making the tests ### ChangeTemperatureContainer.js -* Create a test file for this container to ensure that when the text box changes, the state currentTemp changes +* Create a test file for this container to ensure that when the text box changes, the state currentTemp changes ### CityDropDownContainer.js * Create a test file to ensure that when the drop down changes, the state currentCity changes ### CurrentCityContainer.js * Create a test file to ensure that when the state currentCity is change, the text of the div changes to “CurrentCity: whatever city” - - - diff --git a/src/services/array-functions.js b/src/services/array-functions.js index ff781d2..a3f922c 100644 --- a/src/services/array-functions.js +++ b/src/services/array-functions.js @@ -1,40 +1,55 @@ //in the function map, create a new array and store in a variable -//loop theArray and call the fnc for each thing in the array, +//loop theArray and call the fnc for each thing in the array, // passing in the item from the current loop into the call to fnc //add the returned value from fnc to the new array //return the new array export function map(theArray, fnc){ - + const newArray = []; + for (var i = 0; i < theArray.length; i++) { + newArray.push(fnc(theArray[i])); + } + return newArray; } //create a new array -//loop theArray and call the fnc for each thing in the array, +//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 add the item to the new array else do not //return the new array export function filter(theArray, fnc){ - + const newArray = []; + for (var i = 0; i < theArray.length; i++) { + if (fnc(theArray[i])) { + newArray.push(theArray[i]) + } + } + return newArray; } -//loop theArray and call the fnc for each thing in the array, +//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 +//fnc will return true or false, if true return the item //return null export function find(theArray, fnc){ - + for (var i = 0; i < theArray.length; i++) { + if (fnc(theArray[i])) { + return theArray[i]; + } + } + return null; } //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 @@ -42,7 +57,11 @@ export function head(theArray){ //add the item from each loop to the new array //return the new array export function reverse(theArray){ - + const newArray = []; + for (var i = 0; i < theArray.length; i++) { + newArray.unshift(theArray[i]); + } + return newArray; } //create a new array @@ -50,7 +69,13 @@ 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){ - + const newArray = []; + for (var i = 0; i < theArray.length; i++) { + if (i > 0) { + newArray.push(theArray[i]); + } + } + return newArray; } //implement the most basic sorting algorithm there is @@ -64,5 +89,22 @@ 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){ + while (true) { + let swapped = false; + for (var i = 0; i < theArray.length; i++) { + if (theArray[i] > theArray[i+1]) { + let temp = theArray[i]; + + theArray[i] = theArray[i+1]; + + theArray[i+1] = temp; -} \ No newline at end of file + swapped = true; + } + } + if (swapped === false) { + return theArray; + } + } + +} diff --git a/src/services/calculations.js b/src/services/calculations.js index 754df7c..24fed80 100644 --- a/src/services/calculations.js +++ b/src/services/calculations.js @@ -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; -} \ No newline at end of file +} diff --git a/src/tests/array-functions.test.js b/src/tests/array-functions.test.js index 57ec72f..7bd624b 100644 --- a/src/tests/array-functions.test.js +++ b/src/tests/array-functions.test.js @@ -1,4 +1,4 @@ -import {map,filter,find,findLast} from "../services/array-functions"; +import {head,map,filter,find,findLast,reverse,tail,sort} 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]; @@ -37,11 +37,62 @@ 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 ]); }); }); +describe("find", () => { + it("should find the single name of Barney", () => { + expect(find(names,findBarney)).toEqual("Barney"); + }); +}); + +describe("filter", () => { + it("should find all names with 3 letters", () => { + expect(filter(names,findThree)).toEqual([ + "Jon", + "Bob", + "Ted", + "Axe" + ]); + }); +}); + +describe("findLast", () => { + it("should return last element of array", () => { + expect(findLast(names)).toEqual("Axe"); + }); +}); + +describe("tail", () => { + it("should remove the first item", () => { + expect(tail(names)).toEqual([ + "Bob", + "Ted", + "Barney", + "Lilly", + "Robin", + "Saul", + "Axe" + ]); + }); +}); + +describe("reverse", () => { + it("should reverse the array", () => { + expect(reverse(names)).toEqual([ + "Axe", + "Saul", + "Robin", + "Lilly", + "Barney", + "Ted", + "Bob", + "Jon" + ]); + }); +}); //filter should return an array with names of length 3 //["Jon","Bob","Ted","Axe"] @@ -53,5 +104,3 @@ describe("sort", () => { //["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"]; - - diff --git a/src/tests/calculations.test.js b/src/tests/calculations.test.js index 409e055..9745d8f 100644 --- a/src/tests/calculations.test.js +++ b/src/tests/calculations.test.js @@ -2,6 +2,28 @@ 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); + const result = add(1, 2); + expect(result).toBe(3); + }); +}); + +describe("subtract", () => { + it("subtracts 4 from 5 to equal 1", () => { + const result = subtract(5, 4); + expect(result).toBe(1); + }); +}); + +describe("multiply", () => { + it("multiply 4 by 5 to equal 20", () => { + const result = multiply(4, 5); + expect(result).toBe(20); + }); +}); + +describe("divide", () => { + it("divide 100 by 4 to equal 25", () => { + const result = divide(100, 4); + expect(result).toBe(25); }); });