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
60 changes: 41 additions & 19 deletions src/services/array-functions.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,70 @@

//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,
// passing in the item from the current loop into the call to fnc
//in the function map, create a new array '[]'and store in a variable
//loop 'for' theArray and call 'fnc()' the fnc for each thing in the array,
// passing 'push' 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){
export function map(names, fnc){
var mapped = [];
for (var i = 0; i < names.length; i++){
mapped.push("Hello " + names[i]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double check this code.
you edited the comment to demonstrate what to code, but you didn't actually code it fnc()
its similar to filter but there is no if statement


}
return mapped;
}


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

export function filter(names, fnc){
const filter = [];
for (var i = 0; i < names.length; i++){
var functionThing = fnc(names[i]);
if (functionThing === true ){
filter.push(names[i]);
}

};
return filter;
}


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

export function find(names, fnc){
for (var i = 0; i < names.length; i++){
var functionThing = fnc(names[i]);
if (functionThing === true ){
return names[i];
}
return null;
};
}


//return the last item in theArray
export function findLast(theArray){

export function findLast(names){
const lastItem = [names].pop();
}

//return the first element of the array
export function head(theArray){

export function head(names){
const firstItem
}

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

export function reverse(names){
const reverseArray = [];
for (var i = arr.length - 1; i >= 0; --i) {

}
}

//create a new array
Expand All @@ -65,4 +87,4 @@ export function tail(theArray){
//if false return theArray
export function sort(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;
}
}
13 changes: 11 additions & 2 deletions src/tests/array-functions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ describe("map", () => {
});
});

describe("filter", () => {
it("should filter each name that has three letters", () => {
expect(filter(names,findThree)).toEqual([
"Jon",
"Bob",
"Ted",
"Axe"
]);
});
});

describe("sort", () => {
it("should return an array with numbers in order", () => {
expect(sort(myNumbers)).toEqual([
Expand All @@ -53,5 +64,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"];


30 changes: 28 additions & 2 deletions src/tests/calculations.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
import {add, subtract, multiply,divide} from "../services/calculations";
import {add, subtract, multiply, divide} from "../services/calculations";

// ADD
describe("add", () => {
it("should add 1 and 2 and return 3", () => {
expect(add(1, 2)).toBe(3);
var result = add(1, 2);
expect(result).toBe(3);
});
});

// Subtract
describe("subtract", () => {
it("should subtract 3 against 2 and return 1", () => {
var result = subtract(3, 2);
expect(result).toBe(1);
});
});

// Multiply
describe("multiply", () => {
it("should multiply 2 and 3 and return 6", () => {
var result = multiply(2, 3);
expect(result).toBe(6);
});
});

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