Skip to content

Commit 83f7b85

Browse files
committed
Complete Day 6 - Type Ahead
1 parent 8273d48 commit 83f7b85

4 files changed

Lines changed: 78 additions & 86 deletions

File tree

06 - Type Ahead/index-FINISHED.html

Lines changed: 0 additions & 62 deletions
This file was deleted.

06 - Type Ahead/index-START.html

Lines changed: 0 additions & 23 deletions
This file was deleted.

06 - Type Ahead/index.html

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
<link rel="icon" href="https://fav.farm/🔥" />
8+
</head>
9+
<body>
10+
11+
<form class="search-form">
12+
<input type="text" class="search" placeholder="Capital or Country">
13+
<ul class="suggestions">
14+
<li>Filter for a capital</li>
15+
<li>or a country</li>
16+
</ul>
17+
</form>
18+
<script>
19+
const endpoint = 'https://api.sampleapis.com/countries/countries';
20+
21+
const capitals = [];
22+
23+
fetch(endpoint)
24+
.then(blob => blob.json())
25+
.then(data => capitals.push(...data))
26+
27+
function findMatches(wordToMatch, countries) {
28+
return countries.filter(place => {
29+
const regex = new RegExp(wordToMatch, 'gi');
30+
return place.name.match(regex) || (place.capital && place.capital.match(regex));
31+
});
32+
}
33+
34+
function numberWithCommas(x) {
35+
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
36+
}
37+
38+
function displayMatches() {
39+
40+
const matchArray = findMatches(this.value, capitals);
41+
const html = matchArray.map(place => {
42+
const regex = new RegExp(this.value, 'gi');
43+
const capitalName = place.name.replace(regex, `<span class="hl">${this.value}</span>`);
44+
const countryName = (place.capital || '').replace(regex, `<span class="hl">${this.value}</span>`);
45+
return `
46+
<li>
47+
<span class="name">${capitalName}, ${countryName}</span>
48+
<span class="population">${place.population ? numberWithCommas(place.population) : 'N/A'}</span>
49+
</li>
50+
`;
51+
}).join('');
52+
suggestions.innerHTML = html;
53+
}
54+
55+
const searchInput = document.querySelector('.search');
56+
const suggestions = document.querySelector('.suggestions');
57+
58+
searchInput.addEventListener('change', displayMatches);
59+
searchInput.addEventListener('keyup', displayMatches);
60+
61+
62+
63+
</script>
64+
</body>
65+
</html>

06 - Type Ahead/style.css

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ input {
1616
}
1717

1818
.search-form {
19-
max-width: 400px;
19+
max-width: 700px;
2020
margin: 50px auto;
2121
}
2222

@@ -35,6 +35,18 @@ input.search {
3535
box-shadow: 0 0 5px rgba(0, 0, 0, 0.12), inset 0 0 2px rgba(0, 0, 0, 0.19);
3636
}
3737

38+
input.search:hover {
39+
border-color: hsl(47, 55%, 26%);
40+
font-size: 40px;
41+
box-shadow: 0 0 5px rgba(0, 0, 0, 0.12), inset 0 0 2px rgba(0, 0, 0, 0.19);
42+
}
43+
44+
input.search:focus {
45+
border-color: hsl(47, 55%, 26%);
46+
font-size: 40px;
47+
box-shadow: 0 0 5px rgba(0, 0, 0, 0.12), inset 0 0 2px rgba(0, 0, 0, 0.19);
48+
}
49+
3850
.suggestions {
3951
margin: 0;
4052
padding: 0;

0 commit comments

Comments
 (0)