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
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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”



61 changes: 48 additions & 13 deletions src/services/array-functions.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,78 @@

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

let newArray = [];
theArray.forEach((index) => {
newArray.push(fnc(index));
})
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){

let newArray = [];
theArray.forEach((item)=> {
if(fnc(item)) {
newArray.push(item);
}
})
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(let i = 0; i < theArray.length; i++) {
if (fnc(theArray[i])){
return theArray[i];
}
}
}


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

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

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

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

//implement the most basic sorting algorithm there is
Expand All @@ -64,5 +86,18 @@ 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 newArray = theArray;
let swapped;
do {
swapped = false;
for (var i = 0; i < newArray.length; i++) {
if (newArray[i] > newArray[i + 1]) {
let temp = newArray[i];
newArray[i] = newArray[i + 1];
newArray[i + 1] = temp;
swapped = true;
}
}
} while (swapped === true)
return newArray;
}
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;
}
}
55 changes: 51 additions & 4 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,tail,reverse} 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 Down Expand Up @@ -34,24 +34,71 @@ 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("should return names with three letters", () => {
expect(filter(names,findThree)).toEqual([
"Jon",
"Bob",
"Ted",
"Axe"
]);
});
});

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

//findLast should find the last name of "Axe"
describe("findLast", () => {
it("should return the last element of an array '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("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("should return all elements in an 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("multiply", () => {
it("should multiply 2 and 3 and return 6", () => {
expect(multiply(2,3)).toBe(6);
});
})

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

describe("divide", () => {
it("should divide 6 by 3 and return 2", () => {
expect(divide(6,3)).toBe(2);
});
})