Visitar URL original
finished the typeahead lesson. Good usage of array methods and REGEX. · wesbos/JavaScript30@a04e393 · GitHub
Skip to content

Commit a04e393

Browse files
author
Jonathan Alorda
committed
finished the typeahead lesson. Good usage of array methods and REGEX.
1 parent 826b9ca commit a04e393

1 file changed

Lines changed: 53 additions & 42 deletions

File tree

‎06 - Type Ahead/index-START.html‎

Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<!DOCTYPE html>
22
<html lang="en">
3+
34
<head>
45
<meta charset="UTF-8">
56
<title>Type Ahead 👀</title>
67
<link rel="stylesheet" href="style.css">
78
</head>
9+
810
<body>
911

1012
<form class="search-form">
@@ -14,45 +16,54 @@
1416
<li>or a state</li>
1517
</ul>
1618
</form>
17-
<script>
18-
const endpoint = 'https://gist.githubusercontent.com/Miserlou/c5cd8364bf9b2420bb29/raw/2bf258763cdddd704f8ffd3ea9a3e81d25e2c6f6/cities.json';
19-
20-
const cities = [];
21-
22-
fetch(endpoint)
23-
.then(blob => blob.json())
24-
.then(data => cities.push(...data))
25-
26-
27-
function findMatches(wordToMatch, cities) {
28-
return cities.filter(place => {
29-
// figure out if city/state matches user input
30-
const regex = new RegExp(wordToMatch, 'gi')
31-
return place.city.match(regex) || place.state.match(regex)
32-
33-
})
34-
}
35-
36-
function displayMatches() {
37-
const matchArray = findMatches(this.value, cities)
38-
const html = matchArray.map(place => {
39-
return `
40-
<li>
41-
<span class="name">${place.city}, ${place.state}</span>
42-
<span class="population">${place.population}</span>
43-
</li>
44-
`
45-
}).join('')
46-
suggestions.innterHTML = html
47-
console.log(matchArray)
48-
49-
}
50-
51-
const searchInput = document.querySelector('.search')
52-
const suggestions = document.querySelector('.suggestions')
53-
54-
searchInput.addEventListener('change', displayMatches)
55-
searchInput.addEventListener('keyup', displayMatches)
56-
</script>
57-
</body>
58-
</html>
19+
<script>
20+
const endpoint =
21+
'https://gist.githubusercontent.com/Miserlou/c5cd8364bf9b2420bb29/raw/2bf258763cdddd704f8ffd3ea9a3e81d25e2c6f6/cities.json';
22+
23+
const cities = [];
24+
25+
fetch(endpoint)
26+
.then(blob => blob.json())
27+
.then(data => cities.push(...data))
28+
29+
30+
function findMatches(wordToMatch, cities) {
31+
return cities.filter(place => {
32+
// figure out if city/state matches user input
33+
const regex = new RegExp(wordToMatch, 'gi')
34+
return place.city.match(regex) || place.state.match(regex)
35+
36+
})
37+
}
38+
39+
function numberWithCommas(x) {
40+
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')
41+
}
42+
43+
function displayMatches() {
44+
const matchArray = findMatches(this.value, cities)
45+
const html = matchArray.map(place => {
46+
const regex = new RegExp(this.value, 'gi')
47+
const cityName = place.city.replace(regex, `<span class="hl">${this.value}</span>`)
48+
const stateName = place.state.replace(regex, `<span class="hl">${this.value}</span>`)
49+
return `
50+
<li>
51+
<span class="name">${cityName}, ${stateName}</span>
52+
<span class="population">${numberWithCommas(place.population)}</span>
53+
</li>
54+
`
55+
}).join('')
56+
suggestions.innerHTML = html
57+
console.log(matchArray)
58+
59+
}
60+
61+
const searchInput = document.querySelector('.search')
62+
const suggestions = document.querySelector('.suggestions')
63+
64+
searchInput.addEventListener('change', displayMatches)
65+
searchInput.addEventListener('keyup', displayMatches)
66+
</script>
67+
</body>
68+
69+
</html>

0 commit comments

Comments
 (0)