Visitar URL original
completed day six with readme · wesbos/JavaScript30@5b58032 · GitHub
Skip to content

Commit 5b58032

Browse files
committed
completed day six with readme
1 parent 9479a85 commit 5b58032

3 files changed

Lines changed: 110 additions & 1 deletion

File tree

‎05 - Flex Panel Gallery/index-WORKED.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135
console.log(e.propertyName);
136136
if(e.propertyName.includes('flex')) {
137137
this.classList.toggle('open-active')
138-
}
138+
}sd
139139
}
140140

141141
panels.forEach(panel => panel.addEventListener('click', toggleOpen));

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
22+
fetch(endpoint).then(blob => blob.json()).then(data => cities.push(...data));
23+
24+
function findMatches(wordToMatch, cities){
25+
return cities.filter(place => {
26+
const regex = new RegExp(wordToMatch, 'gi');
27+
return place.city.match(regex) || place.state.match(regex)
28+
});
29+
}
30+
31+
function numberWithCommas(x) {
32+
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
33+
}
34+
35+
function displayMatches() {
36+
const matchArray = findMatches(this.value, cities);
37+
const html = matchArray.map(place => {
38+
const regex = new RegExp(this.value, 'gi');
39+
const cityName = place.city.replace(regex, `<span class="hl">${this.value}</span>`)
40+
const stateName = place.state.replace(regex, `<span class="hl">${this.value}</span>`)
41+
return `
42+
<li>
43+
<span class="name">${cityName}, ${stateName}</span>
44+
<span class="population">${numberWithCommas(place.population)}</span>
45+
</li>
46+
`;
47+
}).join('');
48+
suggestions.innerHTML = html;
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+
57+
58+
</script>
59+
</body>
60+
</html>

‎06 - Type Ahead/readMe.md‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
Type Ahead:
2+
3+
Goal is to pull in a large block of JSON and make it so that as you type characters into the input box, it readjusts and displays a list of matches for cities and states plus the population.
4+
5+
```javascript
6+
const cities = [];
7+
8+
fetch(endpoint).then(blob => blob.json()).then(data => cities.push(...data));
9+
10+
```
11+
we use fetch() to make the AJAX request (similar to JQuery's JSON.get). Fetch returns a promise which requires us to make a .then call. the object return has a property inherent to it named 'json' which allows for us to call it (this also uses a promise) and return an actual array. we want to put this into const variable array so we use .push method and apply "..." which is called 'spread'. This keeps us from having nested array inside our declared array. Now each object is pushed onto cities.
12+
13+
```javascript
14+
function findMatches(wordToMatch, cities){
15+
return cities.filter(place => {
16+
const regex = new RegExp(wordToMatch, 'gi');
17+
return place.city.match(regex) || place.state.match(regex)
18+
});
19+
}
20+
```
21+
we create a function findMatches that takes to arguments: one is whatever we are filtering against the array and two is the array we are filtering (cities). We use the .filter method and we create a new RegExp where we pass it the wordToMatch argument and define the regex parametere 'g' for global and 'i' for insensitive(or case insensitive). We filter this regex against the city and state properties of each place in cities using the .match method.
22+
23+
```javascript
24+
const searchInput = document.querySelector('.search');
25+
const suggestions = document.querySelector('.suggestions');
26+
27+
searchInput.addEventListener('change', displayMatches);
28+
searchInput.addEventListener('keyup', displayMatches);
29+
```
30+
we use query selectors to watch the input box for changes and the ul with the suggestions class to populate the results. Our eventListener for change watches when we click off of the input box and keyup watches for each key we let up on so we can have a seemless update of matches.
31+
32+
```javascript
33+
function displayMatches() {
34+
const matchArray = findMatches(this.value, cities);
35+
const html = matchArray.map(place => {
36+
const regex = new RegExp(this.value, 'gi');
37+
const cityName = place.city.replace(regex, `<span class="hl">${this.value}</span>`)
38+
const stateName = place.state.replace(regex, `<span class="hl">${this.value}</span>`)
39+
return `
40+
<li>
41+
<span class="name">${cityName}, ${stateName}</span>
42+
<span class="population">${numberWithCommas(place.population)}</span>
43+
</li>
44+
`;
45+
}).join('');
46+
suggestions.innerHTML = html;
47+
}
48+
```
49+
displayMatches handles most of the functionality.

0 commit comments

Comments
 (0)