Skip to content

Commit 48a7f00

Browse files
committed
wesbos#7 done
1 parent 49b2dda commit 48a7f00

3 files changed

Lines changed: 98 additions & 40 deletions

File tree

04 - Array Cardio Day 1/index-START.html

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
// ## Array Cardio Day 1
1212

1313
// Some data we can work with
14-
15-
const inventors = [
14+
let inventors = [
1615
{ first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 },
1716
{ first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 },
1817
{ first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 },
@@ -27,77 +26,80 @@
2726
{ first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909 }
2827
];
2928

30-
const people = ['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'];
3129

3230
// Array.prototype.filter()
3331
// 1. Filter the list of inventors for those who were born in the 1500's
34-
35-
const fifteens = inventors.filter(i => i.year > 1500 && i.year < 1600);
36-
//console.table(fifteens);
32+
const inventorsOfFifteens = inventors.filter(inventor =>
33+
inventor.year >= 1500 && inventor.year < 1600
34+
)
35+
// console.table(inventorsOfFifteens)
3736

3837

3938
// Array.prototype.map()
4039
// 2. Give us an array of the inventors' first and last names
40+
const inventorsNames = inventors.map(i => `${i.first} ${i.last}`)
41+
// console.table(inventorsNames)
4142

42-
const list = inventors.map(i => `${i.first} ${i.last}`);
43-
//console.log(list);
44-
4543

4644
// Array.prototype.sort()
4745
// 3. Sort the inventors by birthdate, oldest to youngest
48-
49-
const inventorsByYear = inventors.sort((a, b) => a.year > b.year ? 1 : -1 )
50-
//console.table(inventorsByYear);
46+
const inventorsSortedByBirthday = inventors.sort((a, b) => {
47+
const aBirthday = (a.year)
48+
const bBirthday = (b.year)
49+
return aBirthday > bBirthday ? 1 : -1
50+
}
51+
)
52+
// console.table(inventorsSortedByBirthday)
5153

5254

5355
// Array.prototype.reduce()
5456
// 4. How many years did all the inventors live?
55-
const totalYears = inventors.reduce(((total, i) => (i.passed - i.year) + total), 0);
56-
//console.log(totalYears);
57+
const inventorsTotalAge = inventors.reduce((total, i) => total + (i.passed - i.year), 0)
58+
// console.table(inventorsTotalAge)
5759

5860

5961
// 5. Sort the inventors by years lived
62+
const inventorsSortedByAge = inventors.sort((a, b) => {
63+
const aAge = (a.passed - a.year)
64+
const bAge = (b.passed - b.year)
65+
return aAge > bAge ? -1 : 1
66+
}
67+
).map(i => {
68+
const fullData = {...i}
69+
fullData.age = (i.passed - i.year)
70+
return fullData
71+
})
72+
// console.table(inventorsSortedByAge)
6073

61-
const inventorsByAge = inventors
62-
.sort((a, b) => (a.passed - a.year) > (b.passed - b.year) ? -1 : 1)
63-
.map(i => {
64-
const newObj = {
65-
...i,
66-
age: (i.passed - i.year)
67-
}
68-
return newObj
69-
});
70-
console.table(inventorsByAge);
7174

7275
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
7376
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
74-
77+
const blvdList = Array.from(document.querySelectorAll('.mw-category-group li a'))
78+
const deList = blvdList.map(b => b.innerText).filter(blvd => blvd.includes('de'))
79+
7580

7681
// 7. sort Exercise
7782
// Sort the people alphabetically by last name
78-
79-
const peopleByLast = people.sort((a, b) => {
80-
const [aLast, aFirst] = a.split(', ');
81-
const [bLast, bFirst] = b.split(', ');
83+
const people = ['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'];
84+
const peopleSorted = people.sort((a,b) => {
85+
const [aLast, aFirst] = a.split(', ')
86+
const [bLast, bFirst] = b.split(', ')
8287
return aLast > bLast ? 1 : -1
8388
})
84-
console.log(peopleByLast);
89+
// console.log(peopleSorted)
8590

8691

8792
// 8. Reduce Exercise
8893
// Sum up the instances of each of these
8994
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
90-
91-
const transportation = data.reduce(function(obj, item) {
92-
if (!obj[item]) {
93-
obj[item] = 0;
95+
const sum = data.reduce((obj, i) => {
96+
if(!obj[i]) {
97+
obj[i] = 0
9498
}
95-
obj[item]++;
96-
return obj;
99+
obj[i]++
100+
return obj
97101
}, {})
98-
99-
console.log(transportation);
100-
102+
console.log(sum)
101103
</script>
102104
</body>
103105
</html>

06 - Type Ahead/index-START.html

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,38 @@
1515
</ul>
1616
</form>
1717
<script>
18-
const endpoint = 'https://gist.githubusercontent.com/Miserlou/c5cd8364bf9b2420bb29/raw/2bf258763cdddd704f8ffd3ea9a3e81d25e2c6f6/cities.json';
18+
const endpoint = 'https://gist.githubusercontent.com/Miserlou/c5cd8364bf9b2420bb29/raw/2bf258763cdddd704f8ffd3ea9a3e81d25e2c6f6/cities.json'
19+
const cities = []
20+
const inputToMatch = document.querySelector('.search')
21+
const suggestions = document.querySelector('.suggestions')
22+
23+
fetch(endpoint)
24+
.then(data => data.json())
25+
.then(jsonData => cities.push(...jsonData))
26+
27+
function findMatches(textToMatch, cities) {
28+
regexp = new RegExp(textToMatch, 'gi')
29+
return cities.filter(item => item.city.match(regexp) || item.state.match(regexp))
30+
}
31+
32+
function showMatches() {
33+
const matchedArray = findMatches(this.value, cities)
34+
const suggestionList = matchedArray.map(suggestionItem => {
35+
const regexp = new RegExp(this.value, 'gi')
36+
const suggestionCity = suggestionItem.city.replace(regexp, `<span class="hl">${this.value}</span>`)
37+
const suggestionState = suggestionItem.state.replace(regexp, `<span class="hl">${this.value}</span>`)
38+
39+
return `
40+
<li>
41+
<span class="name">${suggestionCity}, ${suggestionState}</span>
42+
<span class="population">${suggestionItem.population}</span></li>`
43+
}).join('')
44+
suggestions.innerHTML = suggestionList
45+
46+
}
47+
48+
inputToMatch.addEventListener('keyup', showMatches)
49+
1950

2051
</script>
2152
</body>

07 - Array Cardio Day 2/index-START.html

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,41 @@
2626

2727
// Some and Every Checks
2828
// Array.prototype.some() // is at least one person 19 or older?
29+
const someAdult = people.some(person => {
30+
const currentYear = new Date().getFullYear()
31+
//console.log(currentYear - person.year)
32+
return currentYear - person.year >= 19
33+
})
34+
//console.log(someAdult)
35+
2936
// Array.prototype.every() // is everyone 19 or older?
37+
const allAdults = people.every(person => {
38+
const currentYear = new Date().getFullYear()
39+
//console.log(currentYear - person.year >= 19)
40+
return currentYear - person.year >= 19
41+
})
42+
//console.log(allAdults)
3043

3144
// Array.prototype.find()
3245
// Find is like filter, but instead returns just the one you are looking for
3346
// find the comment with the ID of 823423
47+
const comment = comments.find(c => c.id == 823423)
48+
//console.table(comment)
3449

3550
// Array.prototype.findIndex()
51+
const index = comments.findIndex(c => c.id == 823423)
52+
//console.log(index)
3653
// Find the comment with this ID
3754
// delete the comment with the ID of 823423
55+
// const newComments = comments.splice(index, 1)
56+
// console.table(newComments)
57+
// console.table(comments)
3858

59+
const newComm = [
60+
...comments.slice(0, index),
61+
...comments.slice(index+1)
62+
]
63+
console.table(newComm)
3964
</script>
4065
</body>
4166
</html>

0 commit comments

Comments
 (0)