Skip to content

Instantly share code, notes, and snippets.

@airportyh
Created July 2, 2018 18:47
Show Gist options
  • Select an option

  • Save airportyh/5f5aa8c3cc775043119f29766c4f2942 to your computer and use it in GitHub Desktop.

Select an option

Save airportyh/5f5aa8c3cc775043119f29766c4f2942 to your computer and use it in GitHub Desktop.
const doctorWhoEpisodes = [
{"episode": "eleventh hour", "season": 5, "uk_ratings": 10.9, "doctor": 11, "writer": "moffat"},
{"episode": "blink", "season": 3, "uk_ratings": 6.62, "doctor": 10, "writer": "moffat"},
{"episode": "human nature", "season": 3, "uk_ratings": 7.74, "doctor": 10, "writer": "cornell"},
{"episode": "family of blood", "season": 3, "uk_ratings": 7.21, "doctor": 10, "writer": "cornell"},
{"episode": "silence in the library", "season": 4, "uk_ratings": 6.27, "doctor": 10, "writer": "moffat"},
{"episode": "forest of the dead", "season": 4, "uk_ratings": 7.84, "doctor": 10, "writer": "moffat"},
{"episode": "midnight", "season": 4, "uk_ratings": 8.05, "doctor": 10, "writer": "davies"},
{"episode": "a good man goes to war", "season": 6, "uk_ratings": 7.51, "doctor": 11, "writer": "moffat"},
{"episode": "let's kill hitler", "season": 6, "uk_ratings": 8.10, "doctor": 11, "writer": "moffat"},
{"episode": "Husbands of River Song", "season": 9, "uk_ratings": 7.69, "doctor": 12, "writer": "moffat"}
];

FP Exercises 2 (map, filter, and reduce)

You will solve the following problems using map, filter, and reduce or and combination of those 3. No loops of any kind are allowed.

Some String Manipulation

Write a function to do each of the following. Each function should take a string as input and return a string as output.

  1. Replace A's with 4 and E's with 3 in a string.
  2. Remove all vowels from a string.
  3. Reverse a string.
  4. Reverse all words within a sentence.

Doctor Who!!!

Start with these Doctor Who Episodes:

const doctorWhoEpisodes = [
    {"episode": "eleventh hour", "season": 5, "uk_ratings": 10.9, "doctor": 11, "writer": "moffat"},
    {"episode": "blink", "season": 3, "uk_ratings": 6.62, "doctor": 10, "writer": "moffat"},
    {"episode": "human nature", "season": 3, "uk_ratings": 7.74, "doctor": 10, "writer": "cornell"},
    {"episode": "family of blood", "season": 3, "uk_ratings": 7.21, "doctor": 10, "writer": "cornell"},
    {"episode": "silence in the library", "season": 4, "uk_ratings": 6.27, "doctor": 10, "writer": "moffat"},
    {"episode": "forest of the dead", "season": 4, "uk_ratings": 7.84, "doctor": 10, "writer": "moffat"},
    {"episode": "midnight", "season": 4, "uk_ratings": 8.05, "doctor": 10, "writer": "davies"},
    {"episode": "a good man goes to war", "season": 6, "uk_ratings": 7.51, "doctor": 11, "writer": "moffat"},
    {"episode": "let's kill hitler", "season": 6, "uk_ratings": 8.10, "doctor": 11, "writer": "moffat"},
    {"episode": "Husbands of River Song", "season": 9, "uk_ratings": 7.69, "doctor": 12, "writer": "moffat"}
];
  1. Write a episodesForSeason(episodes, seasonNo) function that returns all episodes of a season given an array of all episode objects and the season number, and returns an array of episode objects which are of the matching season.
  2. Write an averageRating(episodes) function that returns the average rating of all episodes that are passed in.
  3. Write a highestRatingEpisode(episodes) function that returns the episode object that has the highest rating.
  4. Write a averageRatingForDoctor(episodes, doctorNo) function that returns the average rating for a particular doctor.
  5. Write a standardDeviation(episodes) function that returns the standard deviation of the ratings across the episodes.

Super Duper Challenge: Implement myfilter

You've seen how the array map method can be reimplemented as a simple function. Your challenge --- if you choose to accept it --- is to reimplement the array filter method. If you are not familiar with the filter method, that's okay.

The myFilter function takes 2 parameters:

  1. an array
  2. a function which takes one parameter that is one of the element of the array. It is expected to return either a truthy or falsey value.

myFilter will return a new array of elements, each of which must be in the original array. Each element within the returned array would cause the passed in function to yield a truthy value.

Example:

const names = ["Jill", "Kay", "Carlos", "Ilyana", "Solveig"];
const shortNames = myFilter(names, (name) => name.length < 5);
console.log(shortNames.join(", "));

should output:

Jill, Kay
let string = "to be or not to be";
// we want to:
// * uppercase all vowels
// * remove all the spaces
let letters = string.split("");
const result = letters
.filter(letter => letter !== " ")
.map(letter => {
// if ("aeiou".indexOf(letter) !== -1) { // it is a vowel
if (letter.match(/[aeiou]/)) {
return letter.toUpperCase();
} else {
return letter;
}
})
.join("");
console.log("result", result);
function range(start, end) {
let arr = [];
for (let i = start; i < end; i++) {
arr.push(i);
}
return arr;
}
function solution(number) {
const numbers = range(1, number);
const multiplesOf3 = numbers.filter(item => item % 3 === 0 && item % 5 !== 0);
const multiplesOf5 = numbers.filter(item => item % 5 === 0 && item % 3 !== 0);
const multiplesOf15 = numbers.filter(item => item % 15 === 0);
// console.log("multiplesOf3", multiplesOf3);
// console.log("multiplesOf5", multiplesOf5);
// console.log("multiplesOf15", multiplesOf15);
const result = [multiplesOf3.length, multiplesOf5.length, multiplesOf15.length];
// console.log("result", result);
return result;
}
console.log(solution(20));
// Reduce *is* the accumulator pattern. Anything you can do
// using the accumulator pattern, you can do with reduce.
const teams = [
{ country: 'Uruguay', mp: 3, w: 3, l: 0, d: 0, gf: 5, ga: 0 },
{ country: 'Russia', mp: 3, w: 2, l: 1, d: 0, gf: 8, ga: 4 },
{ country: 'Saudi Arabia', mp: 3, w: 1, l: 2, d: 0, gf: 2, ga: 7 },
{ country: 'Egypt', mp: 3, w: 0, l: 3, d: 0, gf: 2, ga: 6 }
];
const allGoals = teams.map(team => team.gf)
.reduce((sum, gf) => sum + gf, 0);
console.log("all goals", allGoals);
const allGoals = teams
.reduce((sum, team) => sum + team.gf, 0);
console.log("all goals", allGoals);
// equivalnt to:
let sum = 0;
for (let team of teams) {
sum = sum + team.gf;
}
console.log("all goals", sum);
// Max problem: the mean that scored most goals
let teamWMostGoals = null;
for (let team of teams) {
if (teamWMostGoals === null) {
teamWMostGoals = team;
} else if (team.gf > teamWMostGoals.gf) {
teamWMostGoals = team;
}
}
// equivalent version using reduce
const accFn = (teamWMostGoals, team) => {
if (teamWMostGoals === null) {
return team;
} else if (team.gf > teamWMostGoals.gf) {
return team;
} else {
return teamWMostGoals;
}
};
const teamWMostGoals = teams.reduce(accFn, null);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment