Skip to content

Instantly share code, notes, and snippets.

@DavidGoussev
Last active July 27, 2016 07:09
Show Gist options
  • Select an option

  • Save DavidGoussev/514e302d8cf63eb50433b0714be113f6 to your computer and use it in GitHub Desktop.

Select an option

Save DavidGoussev/514e302d8cf63eb50433b0714be113f6 to your computer and use it in GitHub Desktop.
Functional JS
/*
Filter!
Test 1
Name your function filterOutOdds
Write a function that takes a list and filters out all the odd numbers
Takes one parameter, a list of numbers
Returns a list with only the even numbers remaining
*/
const filterOutOdds = nums => nums.filter(num => num % 2 === 0);
/*
Test 2
Name your function filterState
Takes two parameters
- a list of people objects that have a name and state (as in state where they're from)
- a string of the state which you want to filter for
Returns a list of people objects (in the same order) from the state specified
*/
const filterState = (list, string) => list.filter(person => person.state === string);
/*
Test 3
Name your function showOutOfCADevs
You will need to use map, filter, and reduce (you could skip map by try to use it)
Takes one parameter, a list of people objects (same from test 3)
Takes that list, filters out people from CA, pulls out the name strings and throws
away the rest of the object, uppercases the name of the person, and reduces the
list down to one string, the names separated by a comma and a space (", "). Use
reduce, not join.
Returns a string of uppercase names, separated by a comma and a space.
*/
const showOutOfCADevs = list => {
return list
.filter(person => person.state !== 'CA')
.map(person => person.name.toUpperCase())
.reduce((acc, name) => acc+", "+name);
}
/*
Test 4
Name your function myFilter
myFilter implements filter
Takes two parameters:
- A list that will be filtered
- A function that returns true if the item stays in the list, or false if it removed
Returns a list that has been filtered
*/
const myFilter = (list, fn) => {
const answer = [];
for (let i = 0; i < list.length; i++) {
if(fn(list[i])){
answer.push(list[i]);
}
}
return answer;
}
// unit tests
// do not modify the below code
describe('filter', function() {
const people = [
{name: 'Brian Holt', state: 'CA'},
{name: 'Ryan Florence', state: 'WA'},
{name: 'Kent Dodds', state: 'UT'},
{name: 'Kyle Simpson', state: 'TX'},
{name: 'Pete Hunt', state: 'CA'},
{name: 'Jafar Husain', state: 'CA'},
{name: 'Yehuda Katz', state: 'OR'},
{name: 'Matt Zabriskie', state: 'UT'},
{name: 'Marshall Upshur', state: 'CA'}
];
it('filterOutOdds', () => {
const list = [1,5,7,2,6,3,5,4,10,50,51];
expect(filterOutOdds(list)).toEqual([2,6,4,10,50]);
});
it('filterState', () => {
expect(filterState(people, 'CA')).toEqual([
{name: 'Brian Holt', state: 'CA'},
{name: 'Pete Hunt', state: 'CA'},
{name: 'Jafar Husain', state: 'CA'},
{name: 'Marshall Upshur', state: 'CA'}
]);
expect(filterState(people, 'UT')).toEqual([
{name: 'Kent Dodds', state: 'UT'},
{name: 'Matt Zabriskie', state: 'UT'}
]);
});
it('showOutOfCADevs', () => {
expect(showOutOfCADevs(people)).toEqual('RYAN FLORENCE, KENT DODDS, KYLE SIMPSON, YEHUDA KATZ, MATT ZABRISKIE');
});
it('myFilter', () => {
expect(myFilter([1,2,3,4,5,6,7,8,9,10,11,12], (num) => num % 3 === 0)).toEqual([3,6,9,12]);
});
});
/*
Map
Map is a method on the array prototype in JavaScript. It takes one (required)
parameter: the function you want called on each element in the array. While you
can make these functions, I'd recommend making them named and thus resuseable.
There are four tests to pass here:
Test 1
Make a function named doubleEach. doubleEach takes in an array and returns an
array where every element in the array is doubled. Do not use a loop.
*/
const double = num => num*2;
const doubleEach = input => input.map( double );
/*
Test 2
Make a function named squareEach. squareEach takes in an array and returns an
array where every element in the array is squared. Do not use a loop.
*/
const square = num => num*num;
const squareEach = input => input.map( square );
/*
Test 3
Make a function named doubleAndSquareEach. If you made your other functions
composeable, you can reuse them here. Return an array where each element
is doubled first and then squared. Do not use a loop.
*/
const doubleAndSquareEach = input => input.map(double).map(square);
/*
Test 4
Make a function named myMap. myMap is going to simulate the behavior of the
map method on the Array prototype. myMap takes two parameters: the array being
mapped over, and the function being called on each element. You must use a loop
in myMap. myMap returns the resulting array of calling the inputted function on
each value in the array.
*/
const myMap = (array, fn) => {
const results = [];
for (let i = 0; i < array.length; i++) {
results.push(fn(array[i]));
}
return results;
}
// unit tests
// do not modify the below code
describe('map tests', function() {
it('doubleEach', () => {
const testList = [5,50,500,5000,10,5,3];
expect(doubleEach(testList)).toEqual([10,100,1000,10000,20,10,6]);
});
it('squareEach', () => {
const testList = [10,1,9,2,8,3,8,4,7,5,6,50];
expect(squareEach(testList)).toEqual([100,1,81,4,64,9,64,16,49,25,36,2500]);
});
it('doubleAndSquareEach', () => {
const testList = [5,2,4,8,1,7,10];
expect(doubleAndSquareEach(testList)).toEqual([100,16,64,256,4,196,400]);
});
it('myMap', () => {
const testList = [6,2,4,8,10];
const divideByTwo = num => num/2;
expect(myMap(testList, divideByTwo)).toEqual([3,1,2,4,5]);
});
});
/*
Reduce
Good for taking a list and reducing it down to one value in a user defined way.
Test 1
Name your function addTogether
Take in a list and return the result of that list added together
Do not use a loop
*/
const addTogether = list => {
return list.reduce((acc, num) => acc+num, 0);
}
/*
Test 2
Name your function concatenateStringsWithSpaces
Take in a list, return that string with those strings concatenated together with spaces between them
Don't worry about leading or trailing whitespace
Do not use .join or loops
*/
const concatenateStringsWithSpaces = list => {
return list.reduce((acc, string) => acc+string+" ", "");
}
/*
Test 3
Name your function squaresAndSubtracts
Map over your list, square each value, and then subtract them in order (take index 0, subtract index 1,
then index 2, etc.)
Do not use a loop
*/
const squaresAndSubtracts = list => {
return list
.map(num => num*num)
.reduce((acc,num) => acc-num);
}
/*
Test 4
Name your function myReduce
Implement your own reduce
myReduce takes three parameters: the list being operated on, a function to apply the reduction, and
seed value to start the reduce
You will need to use a loop
*/
const myReduce = (list, fn, seed) => {
let answer = seed;
for(var i = 0; i < list.length; i++) {
answer = fn(answer, list[i]);
}
return answer;
}
// unit tests
// do not modify the below code
describe('reduce', function() {
it('addTogether', () => {
const testList = [5,3,0,7,2,5,6,10,9]
expect(addTogether(testList)).toEqual(47);
});
it('concatenateStringsWithSpaces', () => {
const testList = ['this', 'is', 'so', 'fun'];
expect(concatenateStringsWithSpaces(testList).trim()).toEqual('this is so fun');
});
it('squaresAndSubtracts', () => {
const testList = [10, 5, 4, 2, 1];
expect(squaresAndSubtracts(testList)).toEqual(54);
});
it('myReduce', () => {
const testList = [4,2,3,2];
const applyDivisors = (accumulator, divisor) => accumulator/divisor;
expect(myReduce(testList, applyDivisors, 240)).toEqual(5);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment