Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c6ca87d

Browse files
add task07 and update the answer of task 06
1 parent 92ff1ed commit c6ca87d

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

‎06 - Type Ahead/index-FINISHED.html

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Type Ahead 👀</title>
6+
<link rel="stylesheet" href="style.css">
7+
</head>
8+
<body>
9+
10+
<form class="search-form">
11+
<input type="text" class="search" placeholder="City or State">
12+
<ul class="suggestions">
13+
<li>Filter for a city</li>
14+
<li>or a state</li>
15+
</ul>
16+
</form>
17+
<script>
18+
const endpoint = 'https://gist.githubusercontent.com/Miserlou/c5cd8364bf9b2420bb29/raw/2bf258763cdddd704f8ffd3ea9a3e81d25e2c6f6/cities.json';
19+
20+
const cities = [];
21+
fetch(endpoint)
22+
.then(blob => blob.json())
23+
.then(data => cities.push(...data));
24+
25+
function findMatches(wordToMatch, cities) {
26+
return cities.filter(place => {
27+
// here we need to figure out if the city or state matches what was searched
28+
const regex = new RegExp(wordToMatch, 'gi');
29+
return place.city.match(regex) || place.state.match(regex)
30+
});
31+
}
32+
33+
function numberWithCommas(x) {
34+
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
35+
}
36+
37+
function displayMatches() {
38+
const matchArray = findMatches(this.value, cities);
39+
const html = matchArray.map(place => {
40+
const regex = new RegExp(this.value, 'gi');
41+
const cityName = place.city.replace(regex, `<span class="hl">${this.value}</span>`);
42+
const stateName = place.state.replace(regex, `<span class="hl">${this.value}</span>`);
43+
return `
44+
<li>
45+
<span class="name">${cityName}, ${stateName}</span>
46+
<span class="population">${numberWithCommas(place.population)}</span>
47+
</li>
48+
`;
49+
}).join('');
50+
suggestions.innerHTML = html;
51+
}
52+
53+
const searchInput = document.querySelector('.search');
54+
const suggestions = document.querySelector('.suggestions');
55+
56+
searchInput.addEventListener('change', displayMatches);
57+
searchInput.addEventListener('keyup', displayMatches);
58+
59+
</script>
60+
</body>
61+
</html>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Array Cardio 💪💪</title>
6+
</head>
7+
<body>
8+
<p><em>Psst: have a look at the JavaScript Console</em> 💁</p>
9+
<script>
10+
// ## Array Cardio Day 2
11+
12+
const people = [
13+
{ name: 'Wes', year: 1988 },
14+
{ name: 'Kait', year: 1986 },
15+
{ name: 'Irv', year: 1970 },
16+
{ name: 'Lux', year: 2015 }
17+
];
18+
19+
const comments = [
20+
{ text: 'Love this!', id: 523423 },
21+
{ text: 'Super good', id: 823423 },
22+
{ text: 'You are the best', id: 2039842 },
23+
{ text: 'Ramen is my fav food ever', id: 123523 },
24+
{ text: 'Nice Nice Nice!', id: 542328 }
25+
];
26+
27+
// Some and Every Checks
28+
// Array.prototype.some() // is at least one person 19 or older?
29+
// Array.prototype.every() // is everyone 19 or older?
30+
31+
// Array.prototype.find()
32+
// Find is like filter, but instead returns just the one you are looking for
33+
// find the comment with the ID of 823423
34+
35+
// Array.prototype.findIndex()
36+
// Find the comment with this ID
37+
// delete the comment with the ID of 823423
38+
39+
</script>
40+
</body>
41+
</html>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /