Skip to content

Instantly share code, notes, and snippets.

@Charlie-robin
Last active June 1, 2022 08:49
Show Gist options
  • Save Charlie-robin/a9275dbc3cbc95d8a7c71cf9fe6426ff to your computer and use it in GitHub Desktop.
Save Charlie-robin/a9275dbc3cbc95d8a7c71cf9fe6426ff to your computer and use it in GitHub Desktop.

Iterators Only!

You have some pure functions to write, your functions should take parameters and return a result!

Method Description
.map() creates a new array by performing a callback function on each array element
.filter() creates a new array with array elements that pass a certain condition
.reduce() takes all of the elements in an array and "reduces" them to a single value

Time to practice the different Array iterators!


1. Practice Syntax

Capital names 🏰

Write a function that:

  • Takes an array of names as a parameter.
  • It needs return a new array with each of the first letters of the names capitalized
// EXAMPLE INPUT -> ["charlie", "rob"];
// EXPECTED OUTPUT -> ["Charlie", "Rob"]

Remove vowels πŸ““

Write a function that:

  • Takes an array of letters as a parameter.
  • It needs return a new array with only consonants, the vowels need to be removed.
// EXAMPLE INPUT -> ["c", "r", "a"];
// EXPECTED OUTPUT -> ["c", "r"]

Mean coaches πŸ’­

Write a function that:

  • Takes an array of numbers as a parameter
  • It needs to return the mean average
    • Total the numbers and divide by the amount of numbers in the array
// EXAMPLE INPUT -> [25, 50, 175, 50];
// EXPECTED OUTPUT -> 75

2. Practice Problem Solving

Spot the difference πŸ‘€

Write a function that:

  • Takes an array of words
  • Returns a new array of words
  • Figure out the difference with the examples below and use an array iterator method to implement it.
// EXAMPLE INPUT -> ["spray", "limit", "disco", "exuberant", "destruction", "present"];
// EXPECTED OUTPUT -> ["exuberant", "destruction", "present"]

Disco Shoes πŸ‘ž

Write a function that:

  • Takes an array of words
  • Returns a new array of words
  • Figure out the difference with the examples below and use an array iterator method to implement it.
// EXAMPLE INPUT -> ["please camel", "join casing", "these disco", "with shoes"];
// EXPECTED OUTPUT -> ["pleaseCamel", "joinCasing", "theseDisco", "withShoes"]

Scrabble πŸ₯š

Write a function that:

  • Takes an array of scrabble tile objects
  • Returns a score
  • Figure out the difference with the examples below and use an array iterator method to implement it.
// EXAMPLE INPUT -> [{ letter: "a", value: 1}, { letter: "k", value: 5}, { letter: "c", value: 3}];
// EXPECTED OUTPUT -> 9

3. Array's and Objects

Mi to Km 🌐

Write a function that:

  • Takes an array of mile values as a parameter.
  • It needs to convert them from miles to kilometers
  • It needs to total them
  • It needs to return an object with two keys
    • totalKM : The total of the mile values in km
    • convertedKM : The array of converted mile values
// EXAMPLE INPUT -> = [10, 5];
// EXPECTED OUTPUT -> { convertedKM:[16.0934, 8.04672], totalKM: 24.14012 }

Filter food πŸ”

Write a function that:

  • Takes an array of food objects and a food type either junk or healthy
  • It needs to return a new array of objects filtered on whether they are the given food type.
// EXAMPLE INPUT -> [
//  { img: "πŸ•", name: "pizza", foodType: "junk" },
//  { img: "πŸ”", name: "burger", foodType: "junk" },
//  { img: "🍟", name: "fries", foodType: "junk" },
//  { img: "🌭", name: "hot dog", foodType: "junk" },
//  { img: "πŸ₯—", name: "salad", foodType: "healthy" },
//  { img: "πŸ₯™", name: "pita", foodType: "healthy" },
//  { img: "πŸ₯ͺ", name: "sandwich", foodType: "healthy" },
//  { img: "🌯", name: "burrito", foodType: "healthy" },
//  { img: "πŸ₯£", name: "soup", foodType: "healthy" },
// ];

// EXPECTED OUTPUT -> JUNK -> [
//   { img: "πŸ•", name: "pizza", foodType: "junk" },
//   { img: "πŸ”", name: "burger", foodType: "junk" },
//   { img: "🍟", name: "fries", foodType: "junk" },
//   { img: "🌭", name: "hot dog", foodType: "junk" },
// ]

// EXPECTED OUTPUT -> HEALTHY -> [
//   { img: "πŸ₯—", name: "salad", foodType: "healthy" },
//   { img: "πŸ₯™", name: "pita", foodType: "healthy" },
//   { img: "πŸ₯ͺ", name: "sandwich", foodType: "healthy" },
//   { img: "🌯", name: "burrito", foodType: "healthy" },
//   { img: "πŸ₯£", name: "soup", foodType: "healthy" },
// ]

We can be Heros 🌎

Write a function that:

  • Takes an array of hero objects
  • It needs to return a new array of new hero objects where:
    • The name key is changed to hero
    • It should have a new key added called id, the value should be based on its index.
    • It should have a new key added called power, the value should be a random number between 1 - 10.
// EXAMPLE INPUT -> [
//  { name: "Spider-Man" },
//  { name: "Thor" },
//  { name: "Black Panther" },
//  { name: "Captain Marvel" },
//  { name: "Silver Surfer" },
// ];

// EXPECTED OUTPUT -> [
//   { id: 0, hero: "Spider-Man", power: 1 },
//   { id: 1, hero: "Thor", power: 9 },
//   { id: 2, hero: "Black Panther", power: 10 },
//   { id: 3, hero: "Captain Marvel", power: 8 },
//   { id: 4, hero: "Silver Surfer", power: 2 },
// ];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment