Skip to content

Instantly share code, notes, and snippets.

@harrisonmalone
Last active May 21, 2019 02:23
Show Gist options
  • Save harrisonmalone/27670a6f0cc8df61489cee59a2ef52d7 to your computer and use it in GitHub Desktop.
Save harrisonmalone/27670a6f0cc8df61489cee59a2ef52d7 to your computer and use it in GitHub Desktop.
answers to javascript array method review questions 🎃
const inventors = [
{ first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 },
{ first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 },
{ first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 },
{ first: 'Marie', last: 'Curie', year: 1867, passed: 1934 },
{ first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630 },
{ first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543 },
{ first: 'Max', last: 'Planck', year: 1858, passed: 1947 },
{ first: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979 },
{ first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852 },
{ first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905 },
{ first: 'Lise', last: 'Meitner', year: 1878, passed: 1968 },
{ first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909 }
];
// Array.filter()
// 1. Filter the list of inventors for those who were born in the 1500's
const bornIn1500 = inventors.filter((inventor) => {
// greater than 1499 and less than or equal to 1599
return inventor.year > 1499 && inventor.year <= 1599
})
// console.log(bornIn1500)
// Array.map()
// 2. Give us an array of the inventors' first and last names
const firstAndLastNames = inventors.map((inventor) => {
return `${inventor.first} ${inventor.last}`
})
// console.log(firstAndLastNames)
// Array.sort()
// 3. Sort the inventors by birth date, oldest to youngest
const sortedByBirth = inventors.sort((inventorOne, inventorTwo) => {
return inventorOne.year - inventorTwo.year
})
// console.table(sortedByBirth)
// Array.sort()
// 4. Sort the inventors by years lived
const yearLivedValue = inventors.map((inventor) => {
const yearsLived = inventor.passed - inventor.year
inventor.yearsLived = yearsLived
return inventor
})
// console.table(yearLivedValue)
const sortedByYearsLived = yearLivedValue.sort((inventorOne, inventorTwo) => {
return inventorOne.yearsLived - inventorTwo.yearsLived
})
// console.table(sortedByYearsLived)
// Array.filter()
// 5. Create a list of Boulevards in Paris that contain 'de' anywhere in the name, you'll need to create your own array
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
const boulevardsInParis = [
"Boulevards of Paris",
"City walls of Paris",
"Thiers wall",
"Wall of Charles V",
"Wall of Philip II Augustus",
"City gates of Paris",
"Haussmann's renovation of Paris",
"Boulevards of the Marshals",
"Boulevard Auguste-Blanqui",
"Boulevard Barbès",
"Boulevard Beaumarchais",
"Boulevard de l'Amiral-Bruix",
"Boulevard Mortier",
"Boulevard Poniatowski",
"Boulevard des Capucines",
"Boulevard de la Chapelle",
"Boulevard de Clichy",
"Boulevard du Crime",
"Boulevard du Général-d'Armée-Jean-Simon",
"Boulevard Haussmann",
"Boulevard de l'Hôpital",
"Boulevard des Italiens",
"Boulevard Lefebvre",
"Boulevard de la Madeleine",
"Boulevard de Magenta",
"Boulevard Montmartre",
"Boulevard du Montparnasse",
"Boulevard Raspail",
"Boulevard Richard-Lenoir",
"Boulevard de Rochechouart",
"Boulevard Saint-Germain",
"Boulevard Saint-Michel",
"Boulevard de Sébastopol",
"Boulevard de Strasbourg",
"Boulevard du Temple",
"Boulevard Voltaire",
"Boulevard de la Zone",
]
const onlyDe = boulevardsInParis.filter((boulevard) => {
const boulevardArr = boulevard.split(" ")
return boulevardArr.includes("de")
})
// console.table(onlyDe)
const peopleOne = ['Beck, Glenn', 'Becker, Carl', 'Beckett, Samuel', 'Beddoes, Mick', 'Beecher, Henry', 'Beethoven, Ludwig', 'Begin, Menachem', 'Belloc, Hilaire', 'Bellow, Saul', 'Benchley, Robert', 'Benenson, Peter', 'Ben-Gurion, David', 'Benjamin, Walter', 'Benn, Tony', 'Bennington, Chester', 'Benson, Leana', 'Bent, Silas', 'Bentsen, Lloyd', 'Berger, Ric', 'Bergman, Ingmar', 'Berio, Luciano', 'Berle, Milton', 'Berlin, Irving', 'Berne, Eric', 'Bernhard, Sandra', 'Berra, Yogi', 'Berry, Halle', 'Berry, Wendell', 'Bethea, Erin', 'Bevan, Aneurin', 'Bevel, Ken', 'Biden, Joseph', 'Bierce, Ambrose', 'Biko, Steve', 'Billings, Josh', 'Biondo, Frank', 'Birrell, Augustine', 'Black, Elk', 'Blair, Robert', 'Blair, Tony', 'Blake, William'];
// Array.sort()
// 6. Sort the peopleOne alphabetically by first name
const peopleSplit = peopleOne.map((person) => {
return person.split(", ")
})
// console.log(peopleSplit)
const peopleSortedByFirstName = peopleSplit.sort((personArrOne, personArrTwo) => {
return personArrOne[1].localeCompare(personArrTwo[1])
})
// console.table(peopleSortedByFirstName)
const comments = [
{ text: 'Love this!', id: 523423 },
{ text: 'Super good', id: 823423 },
{ text: 'You are the best', id: 2039842 },
{ text: 'Ramen is my fav food ever', id: 123523 },
{ text: 'Nice Nice Nice!', id: 542328 }
];
// Array.find()
// Find is like filter, but instead returns just the one you are looking for
// 7. Find the comment with the ID of 823423
const foundComment = comments.find((comment) => {
return comment.id === 823423
})
// console.log(foundComment)
// Array.findIndex()
// 8. Find the comment with this ID
const index = comments.findIndex((comment) => {
return comment.id === 823423
})
// console.log(index)
// 9. Delete the comment with the ID of 823423
comments.splice(index, 1)
// console.log(comments)
const peopleTwo = [
{ name: 'Wes', year: 1988 },
{ name: 'Kait', year: 1986 },
{ name: 'Irv', year: 1970 },
{ name: 'Lux', year: 2015 }
];
// Array.some()
// 10. is at least one person 19 or older?
const olderthan19 = peopleTwo.some((person) => {
const age = 2019 - person.year
return age >= 19
})
// console.log(olderthan19)
// Array.every()
// 11. Is everyone 19 or older?
const everyOneOlderThan19 = peopleTwo.every((person) => {
const age = 2019 - person.year
return age >= 19
})
// console.log(everyOneOlderThan19)
// reduce is one of those functions that is very difficult to use at first
// give it a go and ask teachers if you need some help
// const numbers = [1,2,3,4,5,2,22]
// // ruby way of summing numbers
// // numbers.sum
// const result = numbers.reduce((acc, currentValue) => {
// console.log(`this is the accumulator: ${acc}, this is the currentValue: ${currentValue}`)
// return acc += currentValue
// }, initialValue)
// console.log(result)
// Array.reduce()
// 12. How many years did all the inventors live?
let initialValue = 0
const totalYearsLived = yearLivedValue.reduce((accumulator, currentValue) => {
console.log(`this is the accumulator: ${accumulator}, this is the currentValue: ${currentValue.yearsLived}`)
return accumulator + currentValue.yearsLived
}, initialValue)
console.log(totalYearsLived)
// Array.reduce()
// 13. Sum up the instances of each of these
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
// source: wes bos (edited by harrison and aaron)
// recursion example
function myFunc() {
// do something
// if i get so and so result
// call myFunc()
// else return this
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment